Skip to content

Commit

Permalink
fix: TestPaychGetRestartAfterAddFundsMsg may stuck in forever waiting (
Browse files Browse the repository at this point in the history
…#141)

* fix TestPaychGetRestartAfterAddFundsMsg may stuck in forever waiting
  • Loading branch information
zl03jsj authored Jun 10, 2022
1 parent c117e1c commit b042e39
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
4 changes: 4 additions & 0 deletions paychmgr/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,16 @@ func NewManager(mctx metrics.MetricsCtx, repo repo.Repo, msgClient clients.IMixM

// newManager is used by the tests to supply mocks
func newManager(ctx context.Context, r repo.Repo, pchapi managerAPI) (*Manager, error) {
var shutdown context.CancelFunc
ctx, shutdown = context.WithCancel(ctx)
pm := &Manager{
sa: &stateAccessor{sm: pchapi},
channelInfoRepo: r.PaychChannelInfoRepo(),
msgInfoRepo: r.PaychMsgInfoRepo(),
channels: make(map[string]*channelAccessor),
pchapi: pchapi,
ctx: ctx,
shutdown: shutdown,
}
return pm, pm.Start(ctx)
}
Expand Down
10 changes: 7 additions & 3 deletions paychmgr/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func newMockPaychAPI() *mockPaychAPI {

func (pchapi *mockPaychAPI) WaitMsg(ctx context.Context, mcid cid.Cid, confidence uint64) (*types.MsgLookup, error) {
pchapi.lk.Lock()
response := make(chan types.MessageReceipt)
response := make(chan types.MessageReceipt, 1)

if response, ok := pchapi.waitingResponses[mcid]; ok {
defer pchapi.lk.Unlock()
Expand All @@ -154,8 +154,12 @@ func (pchapi *mockPaychAPI) WaitMsg(ctx context.Context, mcid cid.Cid, confidenc
pchapi.waitingCalls[mcid] = &waitingCall{response: response}
pchapi.lk.Unlock()

receipt := <-response
return &types.MsgLookup{Receipt: receipt}, nil
select {
case receipt := <-response:
return &types.MsgLookup{Receipt: receipt}, nil
case <-ctx.Done():
return nil, ctx.Err()
}
}

func (pchapi *mockPaychAPI) receiveMsgResponse(mcid cid.Cid, receipt types.MessageReceipt) {
Expand Down
2 changes: 2 additions & 0 deletions paychmgr/paychget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ func TestPaychGetRestartAfterAddFundsMsg(t *testing.T) {
require.NoError(t, err)

// Simulate shutting down system
mgr.Stop()
mock.close()

// Create a new manager with the same datastore
Expand All @@ -465,6 +466,7 @@ func TestPaychGetRestartAfterAddFundsMsg(t *testing.T) {

mgr2, err := newManager(ctx, repo, mock2)
require.NoError(t, err)
defer mgr2.Stop()

// Send success add funds response
mock2.receiveMsgResponse(mcid2, types.MessageReceipt{
Expand Down
7 changes: 6 additions & 1 deletion paychmgr/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ func (ca *channelAccessor) queueSize() int {
// msgWaitComplete is called when the message for a previous task is confirmed
// or there is an error.
func (ca *channelAccessor) msgWaitComplete(ctx context.Context, mcid cid.Cid, err error) {
// context related errors should be ignore
if err != nil && (errors.Is(err, ca.chctx.Err()) || errors.Is(err, ctx.Err())) {
return
}

ca.lk.Lock()
defer ca.lk.Unlock()

Expand Down Expand Up @@ -413,7 +418,7 @@ func (ca *channelAccessor) waitForPaychCreateMsg(ctx context.Context, channelID
func (ca *channelAccessor) waitPaychCreateMsg(ctx context.Context, channelID string, mcid cid.Cid) error {
mwait, err := ca.api.WaitMsg(ca.chctx, mcid, 1)
if err != nil {
log.Errorf("wait msg: %w", err)
log.Errorf("wait msg: %s", err)
return err
}
// If channel creation failed
Expand Down

0 comments on commit b042e39

Please sign in to comment.