Skip to content

Commit

Permalink
tweak(test): fix reliability of jsonrpc/send_transaction_test (#1616)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanBelyakoff authored Jan 9, 2025
1 parent c8364c4 commit 63a2d8e
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions turbo/jsonrpc/send_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,31 +98,25 @@ func TestSendRawTransaction(t *testing.T) {
txsCh, id := ff.SubscribePendingTxs(1)
defer ff.UnsubscribePendingTxs(id)

txHash, err := api.SendRawTransaction(ctx, buf.Bytes())
require.NoError(err)
var txHash common.Hash
// When tx is send immediately, in rare cases it is not received by the channel,
// but it is hard to track down the issue, so we wait a bit.
time.AfterFunc(10*time.Millisecond, func() {
txHash, err = api.SendRawTransaction(ctx, buf.Bytes())
require.NoError(err)
})

done := make(chan struct{})
go func() {
select {
case got := <-txsCh:
require.Equal(expectedValue, got[0].GetValue().Uint64())
case <-time.After(10 * time.Second):
for i := 0; i < 20; i++ {
jsonTx, err := api.GetTransactionByHash(ctx, txHash, nil)
if err == nil {
jsonTxRPCTransaction, ok := jsonTx.(jsonrpc.RPCTransaction)
if ok && jsonTxRPCTransaction.Value.Uint64() == expectedValue {
close(done)
return
}
}
time.Sleep(time.Second)
}
t.Fatal("Timed out waiting for transaction")
}
close(done)
}()
<-done
select {
case got := <-txsCh:
require.Equal(expectedValue, got[0].GetValue().Uint64())
case <-time.After(20 * time.Second): // Sometimes the channel times out on github actions
t.Log("Timeout waiting for txn from channel")
jsonTx, err := api.GetTransactionByHash(ctx, txHash, nil)
require.NoError(err)
jsonTxRPCTransaction, ok := jsonTx.(jsonrpc.RPCTransaction)
require.True(ok)
require.Equal(expectedValue, jsonTxRPCTransaction.Value.Uint64())
}

//send same tx second time and expect error
_, err = api.SendRawTransaction(ctx, buf.Bytes())
Expand Down Expand Up @@ -166,8 +160,11 @@ func TestSendRawTransactionUnprotected(t *testing.T) {
txsCh, id := ff.SubscribePendingTxs(1)
defer ff.UnsubscribePendingTxs(id)

txHash, err := api.SendRawTransaction(ctx, buf.Bytes())
require.NoError(err)
var txHash common.Hash
time.AfterFunc(10*time.Millisecond, func() {
txHash, err = api.SendRawTransaction(ctx, buf.Bytes())
require.NoError(err)
})

select {
case got := <-txsCh:
Expand Down

0 comments on commit 63a2d8e

Please sign in to comment.