Skip to content

Commit

Permalink
test: add context timeout to tests in ExecutorSuite and gRPC client
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tzdybal committed Feb 20, 2025
1 parent afecab3 commit 4c257e1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
4 changes: 3 additions & 1 deletion proxy/grpc/client_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ func TestClientServer(t *testing.T) {
mockExec.On("InitChain", mock.Anything, expectedTime, initialHeight, chainID).
Return(expectedStateRoot, expectedMaxBytes, nil).Once()

stateRoot, maxBytes, err := client.InitChain(context.TODO(), genesisTime, initialHeight, chainID)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
stateRoot, maxBytes, err := client.InitChain(ctx, genesisTime, initialHeight, chainID)

require.NoError(t, err)
assert.Equal(t, expectedStateRoot, stateRoot)
Expand Down
43 changes: 30 additions & 13 deletions test/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type ExecutorSuite struct {
TxInjector TxInjector
}

const maxTestDuration = 3 * time.Second

// TxInjector provides an interface for injecting transactions into a test suite.
type TxInjector interface {
InjectRandomTx() types.Tx
Expand All @@ -28,7 +30,10 @@ func (s *ExecutorSuite) TestInitChain() {
initialHeight := uint64(1)
chainID := "test-chain"

stateRoot, maxBytes, err := s.Exec.InitChain(context.TODO(), genesisTime, initialHeight, chainID)
ctx, cancel := context.WithTimeout(context.Background(), maxTestDuration)
defer cancel()

stateRoot, maxBytes, err := s.Exec.InitChain(ctx, genesisTime, initialHeight, chainID)
s.Require().NoError(err)
s.NotEqual(types.Hash{}, stateRoot)
s.Greater(maxBytes, uint64(0))
Expand All @@ -38,9 +43,12 @@ func (s *ExecutorSuite) TestInitChain() {
func (s *ExecutorSuite) TestGetTxs() {
s.skipIfInjectorNotSet()

ctx, cancel := context.WithTimeout(context.Background(), maxTestDuration)
defer cancel()

tx1 := s.TxInjector.InjectRandomTx()
tx2 := s.TxInjector.InjectRandomTx()
txs, err := s.Exec.GetTxs(context.TODO())
txs, err := s.Exec.GetTxs(ctx)
s.Require().NoError(err)
s.Require().Len(txs, 2)
s.Require().Contains(txs, tx1)
Expand All @@ -56,11 +64,16 @@ func (s *ExecutorSuite) skipIfInjectorNotSet() {
// TestExecuteTxs tests ExecuteTxs method.
func (s *ExecutorSuite) TestExecuteTxs() {
s.skipIfInjectorNotSet()

txs := []types.Tx{s.TxInjector.InjectRandomTx(), s.TxInjector.InjectRandomTx()}
initialHeight := uint64(1)

genesisTime, initialHeight, genesisStateRoot, _ := s.initChain(context.TODO())
ctx, cancel := context.WithTimeout(context.Background(), maxTestDuration)
defer cancel()

genesisTime, genesisStateRoot, _ := s.initChain(ctx, initialHeight)

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

// TestSetFinal tests SetFinal method.
func (s *ExecutorSuite) TestSetFinal() {
ctx, cancel := context.WithTimeout(context.Background(), maxTestDuration)
defer cancel()

// finalizing invalid height must return error
err := s.Exec.SetFinal(context.TODO(), 1)
err := s.Exec.SetFinal(ctx, 7)
s.Require().Error(err)

_, height, stateRoot, _ := s.initChain(context.TODO())
_, _, err = s.Exec.ExecuteTxs(context.TODO(), nil, height+1, time.Now(), stateRoot)
initialHeight := uint64(1)
_, stateRoot, _ := s.initChain(ctx, initialHeight)
_, _, err = s.Exec.ExecuteTxs(ctx, nil, initialHeight, time.Now(), stateRoot)
s.Require().NoError(err)
err = s.Exec.SetFinal(context.TODO(), 2)
err = s.Exec.SetFinal(ctx, initialHeight)
s.Require().NoError(err)
}

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

Check failure on line 105 in test/suite.go

View workflow job for this annotation

GitHub Actions / lint / golangci-lint

ineffectual assignment to maxBytes (ineffassign)

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

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

stateRoot, maxBytes, err := s.Exec.InitChain(ctx, genesisTime, initialHeight, chainID)
s.Require().NoError(err)
s.Require().NotEmpty(stateRoot)
return genesisTime, initialHeight, stateRoot, maxBytes
return genesisTime, stateRoot, maxBytes
}

0 comments on commit 4c257e1

Please sign in to comment.