Skip to content

Commit

Permalink
rpcsrv: fix TestSubClientWaitWithMissedEvent
Browse files Browse the repository at this point in the history
Add ensuring WaitGroup mechanism to prevent data race in the test.

Close #2958

Signed-off-by: Ekaterina Pavlova <[email protected]>
  • Loading branch information
AliceInHunterland committed Mar 27, 2024
1 parent 627a0ab commit e10c584
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions pkg/services/rpcsrv/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2050,10 +2050,18 @@ func testSubClientWaitWithMissedEvent(t *testing.T, local bool) {
b1 := blocks[0]
tx := b1.Transactions[0]

rcvr := make(chan *state.AppExecResult)
rcvr := make(chan *state.AppExecResult, 1)
errCh := make(chan error, 1) // Error channel for goroutine errors

var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
aer, err := act.Wait(tx.Hash(), tx.ValidUntilBlock, nil)
require.NoError(t, err)
if err != nil {
errCh <- err
return
}
rcvr <- aer
}()

Expand Down Expand Up @@ -2093,18 +2101,18 @@ func testSubClientWaitWithMissedEvent(t *testing.T, local bool) {
rpcSrv.subsLock.Unlock()

// Wait for the result.
waitloop:
for {
select {
case aer := <-rcvr:
require.Equal(t, tx.Hash(), aer.Container)
require.Equal(t, trigger.Application, aer.Trigger)
require.Equal(t, vmstate.Halt, aer.VMState)
break waitloop
case <-time.NewTimer(chain.GetConfig().TimePerBlock).C:
t.Fatal("transaction failed to be awaited")
}
select {
case aer := <-rcvr:
require.Equal(t, tx.Hash(), aer.Container)
require.Equal(t, trigger.Application, aer.Trigger)
require.Equal(t, vmstate.Halt, aer.VMState)
case err := <-errCh:
t.Fatalf("Error waiting for transaction: %v", err)
case <-time.After(chain.GetConfig().TimePerBlock):
t.Fatal("Timeout waiting for transaction to be awaited")
}

wg.Wait()
}

// TestWSClient_SubscriptionsCompat is aimed to test both deprecated and relevant
Expand Down

0 comments on commit e10c584

Please sign in to comment.