Skip to content

Commit

Permalink
fix: Exclude empty strings when listing failure message
Browse files Browse the repository at this point in the history
  • Loading branch information
simlecode committed Mar 21, 2023
1 parent 10c4188 commit e4a2b9d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
8 changes: 7 additions & 1 deletion integration_test/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ func testListMessageByAddress(ctx context.Context, t *testing.T, api, apiSign me

func testListFailedMessage(ctx context.Context, t *testing.T, api, apiSign messager.IMessager, addrs []address.Address, blockDelay time.Duration) {
msgs := genMessageWithAddress(addrs)
for _, msg := range msgs {
for i, msg := range msgs {
msg.Message.GasLimit = -1
id, err := api.PushMessageWithId(ctx, msg.ID, &msg.Message, nil)
assert.NoError(t, err)
Expand All @@ -431,6 +431,12 @@ func testListFailedMessage(ctx context.Context, t *testing.T, api, apiSign messa
res, err := api.GetMessageByUid(ctx, id)
assert.NoError(t, err)
checkUnsignedMsg(t, &msg.Message, &res.Message)

if i%3 == 0 {
failedMsg, err := api.ListFailedMessage(ctx)
assert.NoError(t, err)
assert.Len(t, failedMsg, 0)
}
}

time.Sleep(blockDelay * 2)
Expand Down
2 changes: 1 addition & 1 deletion models/mysql/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ func (m *mysqlMessageRepo) ListFailedMessage(p *repo.MsgQueryParams) ([]*types.M
var sqlMsgs []*mysqlMessage
p.State = []types.MessageState{types.UnFillMsg}
paramsMap := p.ToMap()
err := m.DB.Order("created_at").Where(paramsMap).Find(&sqlMsgs, "error_msg is not null").Error
err := m.DB.Order("created_at").Where(paramsMap).Find(&sqlMsgs, "error_msg != ?", "").Error
if err != nil {
return nil, err
}
Expand Down
12 changes: 6 additions & 6 deletions models/mysql/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,17 +399,17 @@ func TestListFailedMessage(t *testing.T) {
ids := []string{"msg1", "msg2"}

t.Run("no param", func(t *testing.T) {
mock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `messages` WHERE `state` = ? AND (error_msg is not null) ORDER BY created_at")).
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(ids[0]).AddRow(ids[1]))
mock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `messages` WHERE `state` = ? AND (error_msg != ?) ORDER BY created_at")).
WithArgs(types.UnFillMsg, "").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(ids[0]).AddRow(ids[1]))

res, err := r.MessageRepo().ListFailedMessage(&repo.MsgQueryParams{})
assert.NoError(t, err)
checkMsgWithIDs(t, res, ids)
})

t.Run("state cover", func(t *testing.T) {
mock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `messages` WHERE `state` = ? AND (error_msg is not null) ORDER BY created_at")).
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(ids[0]).AddRow(ids[1]))
mock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `messages` WHERE `state` = ? AND (error_msg != ?) ORDER BY created_at")).
WithArgs(types.UnFillMsg, "").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(ids[0]).AddRow(ids[1]))

res, err := r.MessageRepo().ListFailedMessage(&repo.MsgQueryParams{State: []types.MessageState{types.OnChainMsg}})
assert.NoError(t, err)
Expand All @@ -418,8 +418,8 @@ func TestListFailedMessage(t *testing.T) {

t.Run("indicate from", func(t *testing.T) {
addr := testutil.AddressProvider()(t)
mock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `messages` WHERE `from_addr` = ? AND `state` = ? AND (error_msg is not null) ORDER BY created_at")).
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(ids[0]).AddRow(ids[1]))
mock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `messages` WHERE `from_addr` = ? AND `state` = ? AND (error_msg != ?) ORDER BY created_at")).
WithArgs(addr.String(), types.UnFillMsg, "").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(ids[0]).AddRow(ids[1]))

res, err := r.MessageRepo().ListFailedMessage(&repo.MsgQueryParams{From: []address.Address{addr}})
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion models/sqlite/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ func (m *sqliteMessageRepo) ListFailedMessage(p *repo.MsgQueryParams) ([]*types.
var sqlMsgs []*sqliteMessage
p.State = []types.MessageState{types.UnFillMsg}
paramsMap := p.ToMap()
err := m.DB.Order("created_at").Where(paramsMap).Find(&sqlMsgs, "error_msg is not null").Error
err := m.DB.Order("created_at").Where(paramsMap).Find(&sqlMsgs, "error_msg != ?", "").Error
if err != nil {
return nil, err
}
Expand Down
18 changes: 14 additions & 4 deletions models/sqlite/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,18 @@ func TestListFailedMessage(t *testing.T) {
msgCount := 100
failedMsgCount := 0
msgs := testhelper.NewMessages(msgCount)
for _, msg := range msgs {
msg.State = types.MessageState(rand.Intn(7))
addrs := make([]address.Address, 0)
for i, msg := range msgs {
msg.State = types.MessageState(rand.Intn(6))
if msg.State == types.UnFillMsg {
msg.ErrorMsg = "gas over limit"
failedMsgCount++
if i%2 == 0 {
msg.ErrorMsg = "gas over limit"
failedMsgCount++

addrs = append(addrs, msg.From)
}
}

assert.NoError(t, messageRepo.CreateMessage(msg))
}

Expand All @@ -408,6 +414,10 @@ func TestListFailedMessage(t *testing.T) {
return msgList[i].CreatedAt.Before(msgList[j].CreatedAt)
})
assert.True(t, sorted)

msgList, err = messageRepo.ListFailedMessage(&repo.MsgQueryParams{From: addrs})
assert.NoError(t, err)
assert.Equal(t, failedMsgCount, len(msgList))
}

func TestListBlockedMessage(t *testing.T) {
Expand Down

0 comments on commit e4a2b9d

Please sign in to comment.