Skip to content
This repository was archived by the owner on Apr 2, 2025. It is now read-only.

Commit 07faf8e

Browse files
committed
test: add context timeout to tests in ExecutorSuite and gRPC client
Introduced a context with a timeout to ensure all tests in `ExecutorSuite` and the gRPC client-server tests properly respect execution time limits. Standardized the use of a `maxTestDuration` constant to improve maintainability and readability across the test suite.
1 parent 191d5ed commit 07faf8e

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

proxy/grpc/client_server_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ func TestClientServer(t *testing.T) {
6868
mockExec.On("InitChain", mock.Anything, expectedTime, initialHeight, chainID).
6969
Return(expectedStateRoot, expectedMaxBytes, nil).Once()
7070

71-
stateRoot, maxBytes, err := client.InitChain(context.TODO(), genesisTime, initialHeight, chainID)
71+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
72+
defer cancel()
73+
stateRoot, maxBytes, err := client.InitChain(ctx, genesisTime, initialHeight, chainID)
7274

7375
require.NoError(t, err)
7476
assert.Equal(t, expectedStateRoot, stateRoot)

test/suite.go

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ type ExecutorSuite struct {
1717
TxInjector TxInjector
1818
}
1919

20+
const maxTestDuration = 3 * time.Second
21+
2022
// TxInjector provides an interface for injecting transactions into a test suite.
2123
type TxInjector interface {
2224
InjectRandomTx() types.Tx
@@ -28,7 +30,10 @@ func (s *ExecutorSuite) TestInitChain() {
2830
initialHeight := uint64(1)
2931
chainID := "test-chain"
3032

31-
stateRoot, maxBytes, err := s.Exec.InitChain(context.TODO(), genesisTime, initialHeight, chainID)
33+
ctx, cancel := context.WithTimeout(context.Background(), maxTestDuration)
34+
defer cancel()
35+
36+
stateRoot, maxBytes, err := s.Exec.InitChain(ctx, genesisTime, initialHeight, chainID)
3237
s.Require().NoError(err)
3338
s.NotEqual(types.Hash{}, stateRoot)
3439
s.Greater(maxBytes, uint64(0))
@@ -38,9 +43,12 @@ func (s *ExecutorSuite) TestInitChain() {
3843
func (s *ExecutorSuite) TestGetTxs() {
3944
s.skipIfInjectorNotSet()
4045

46+
ctx, cancel := context.WithTimeout(context.Background(), maxTestDuration)
47+
defer cancel()
48+
4149
tx1 := s.TxInjector.InjectRandomTx()
4250
tx2 := s.TxInjector.InjectRandomTx()
43-
txs, err := s.Exec.GetTxs(context.TODO())
51+
txs, err := s.Exec.GetTxs(ctx)
4452
s.Require().NoError(err)
4553
s.Require().Len(txs, 2)
4654
s.Require().Contains(txs, tx1)
@@ -56,11 +64,16 @@ func (s *ExecutorSuite) skipIfInjectorNotSet() {
5664
// TestExecuteTxs tests ExecuteTxs method.
5765
func (s *ExecutorSuite) TestExecuteTxs() {
5866
s.skipIfInjectorNotSet()
67+
5968
txs := []types.Tx{s.TxInjector.InjectRandomTx(), s.TxInjector.InjectRandomTx()}
69+
initialHeight := uint64(1)
6070

61-
genesisTime, initialHeight, genesisStateRoot, _ := s.initChain(context.TODO())
71+
ctx, cancel := context.WithTimeout(context.Background(), maxTestDuration)
72+
defer cancel()
73+
74+
genesisTime, genesisStateRoot, _ := s.initChain(ctx, initialHeight)
6275

63-
stateRoot, maxBytes, err := s.Exec.ExecuteTxs(context.TODO(), txs, initialHeight+1, genesisTime.Add(time.Second), genesisStateRoot)
76+
stateRoot, maxBytes, err := s.Exec.ExecuteTxs(ctx, txs, initialHeight, genesisTime.Add(time.Second), genesisStateRoot)
6477
s.Require().NoError(err)
6578
s.Require().NotEmpty(stateRoot)
6679
s.Require().NotEqualValues(genesisStateRoot, stateRoot)
@@ -69,22 +82,27 @@ func (s *ExecutorSuite) TestExecuteTxs() {
6982

7083
// TestSetFinal tests SetFinal method.
7184
func (s *ExecutorSuite) TestSetFinal() {
85+
ctx, cancel := context.WithTimeout(context.Background(), maxTestDuration)
86+
defer cancel()
87+
7288
// finalizing invalid height must return error
73-
err := s.Exec.SetFinal(context.TODO(), 1)
89+
err := s.Exec.SetFinal(ctx, 7)
7490
s.Require().Error(err)
7591

76-
_, height, stateRoot, _ := s.initChain(context.TODO())
77-
_, _, err = s.Exec.ExecuteTxs(context.TODO(), nil, height+1, time.Now(), stateRoot)
92+
initialHeight := uint64(1)
93+
_, stateRoot, _ := s.initChain(ctx, initialHeight)
94+
_, _, err = s.Exec.ExecuteTxs(ctx, nil, initialHeight, time.Now(), stateRoot)
7895
s.Require().NoError(err)
79-
err = s.Exec.SetFinal(context.TODO(), 2)
96+
err = s.Exec.SetFinal(ctx, initialHeight)
8097
s.Require().NoError(err)
8198
}
8299

83100
// TestMultipleBlocks is a basic test ensuring that all API methods used together can be used to produce multiple blocks.
84101
func (s *ExecutorSuite) TestMultipleBlocks() {
85-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
102+
ctx, cancel := context.WithTimeout(context.Background(), maxTestDuration)
86103
defer cancel()
87-
genesisTime, initialHeight, stateRoot, maxBytes := s.initChain(ctx)
104+
initialHeight := uint64(1)
105+
genesisTime, stateRoot, maxBytes := s.initChain(ctx, initialHeight)
88106

89107
for i := initialHeight; i <= 10; i++ {
90108
txs, err := s.Exec.GetTxs(ctx)
@@ -100,13 +118,12 @@ func (s *ExecutorSuite) TestMultipleBlocks() {
100118
}
101119
}
102120

103-
func (s *ExecutorSuite) initChain(ctx context.Context) (time.Time, uint64, types.Hash, uint64) {
121+
func (s *ExecutorSuite) initChain(ctx context.Context, initialHeight uint64) (time.Time, types.Hash, uint64) {
104122
genesisTime := time.Now().UTC()
105-
initialHeight := uint64(1)
106123
chainID := "test-chain"
107124

108125
stateRoot, maxBytes, err := s.Exec.InitChain(ctx, genesisTime, initialHeight, chainID)
109126
s.Require().NoError(err)
110127
s.Require().NotEmpty(stateRoot)
111-
return genesisTime, initialHeight, stateRoot, maxBytes
128+
return genesisTime, stateRoot, maxBytes
112129
}

0 commit comments

Comments
 (0)