Skip to content

Commit

Permalink
Merge pull request #2355 from OffchainLabs/minor-fixes
Browse files Browse the repository at this point in the history
Add tests to check errors are logged when context is cancelled [NIT-2547]
  • Loading branch information
anodar committed Jun 3, 2024
2 parents 64ae80c + 9050e2d commit 2423bd8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
6 changes: 3 additions & 3 deletions validator/valnode/redis/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (s *ValidationServer) Start(ctx_in context.Context) {
}
select {
case <-ctx.Done():
log.Info("Context done", "error", ctx.Err().Error())
log.Info("Context done while checking redis stream existance", "error", ctx.Err().Error())
return
case <-time.After(time.Millisecond * 100):
}
Expand All @@ -79,7 +79,7 @@ func (s *ValidationServer) Start(ctx_in context.Context) {
s.StopWaiter.LaunchThread(func(ctx context.Context) {
select {
case <-ctx.Done():
log.Info("Context done", "error", ctx.Err().Error())
log.Info("Context done while waiting a redis stream to be ready", "error", ctx.Err().Error())
return
case <-ready: // Wait until the stream exists and start consuming iteratively.
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func (s *ValidationServer) Start(ctx_in context.Context) {
case <-time.After(s.streamTimeout):
log.Error("Waiting for redis streams timed out")
case <-ctx.Done():
log.Info(("Context expired, failed to start"))
log.Info("Context done while waiting redis streams to be ready, failed to start")
return
}
}
Expand Down
30 changes: 30 additions & 0 deletions validator/valnode/redis/consumer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package redis

import (
"context"
"testing"
"time"

"github.com/ethereum/go-ethereum/log"
"github.com/offchainlabs/nitro/util/redisutil"
"github.com/offchainlabs/nitro/util/testhelpers"
)

func TestTimeout(t *testing.T) {
handler := testhelpers.InitTestLog(t, log.LevelInfo)
ctx, cancel := context.WithCancel(context.Background())
redisURL := redisutil.CreateTestRedis(ctx, t)
TestValidationServerConfig.RedisURL = redisURL
TestValidationServerConfig.ModuleRoots = []string{"0x123"}
TestValidationServerConfig.StreamTimeout = 100 * time.Millisecond
vs, err := NewValidationServer(&TestValidationServerConfig, nil)
if err != nil {
t.Fatalf("NewValidationSever() unexpected error: %v", err)
}
vs.Start(ctx)
time.Sleep(time.Second)
if !handler.WasLogged("Waiting for redis streams timed out") {
t.Error("Expected message about stream time-outs was not logged")
}
cancel()
}

0 comments on commit 2423bd8

Please sign in to comment.