Skip to content

Commit

Permalink
fix: make test logger actually threadsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
tzdybal committed Apr 7, 2022
1 parent 1902905 commit 8a758cb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
6 changes: 3 additions & 3 deletions da/test/da_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestLifecycle(t *testing.T) {
func doTestLifecycle(t *testing.T, dalc da.DataAvailabilityLayerClient) {
require := require.New(t)

err := dalc.Init([]byte{}, nil, &test.TestLogger{T: t})
err := dalc.Init([]byte{}, nil, test.NewTestLogger(t))
require.NoError(err)

err = dalc.Start()
Expand Down Expand Up @@ -65,7 +65,7 @@ func doTestDALC(t *testing.T, dalc da.DataAvailabilityLayerClient) {
if _, ok := dalc.(*mock.MockDataAvailabilityLayerClient); ok {
conf = []byte(mockDaBlockTime.String())
}
err := dalc.Init(conf, store.NewDefaultInMemoryKVStore(), &test.TestLogger{T: t})
err := dalc.Init(conf, store.NewDefaultInMemoryKVStore(), test.NewTestLogger(t))
require.NoError(err)

err = dalc.Start()
Expand Down Expand Up @@ -139,7 +139,7 @@ func doTestRetrieve(t *testing.T, dalc da.DataAvailabilityLayerClient) {
if _, ok := dalc.(*mock.MockDataAvailabilityLayerClient); ok {
conf = []byte(mockDaBlockTime.String())
}
err := dalc.Init(conf, store.NewDefaultInMemoryKVStore(), &test.TestLogger{T: t})
err := dalc.Init(conf, store.NewDefaultInMemoryKVStore(), test.NewTestLogger(t))
require.NoError(err)

err = dalc.Start()
Expand Down
9 changes: 8 additions & 1 deletion log/test/loggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ import (

// TODO(tzdybal): move to some common place
type TestLogger struct {
mtx sync.Mutex
mtx *sync.Mutex
T *testing.T
}

func NewTestLogger(t *testing.T) *TestLogger {
return &TestLogger{
mtx: new(sync.Mutex),
T: t,
}
}

func (t *TestLogger) Debug(msg string, keyvals ...interface{}) {
t.T.Helper()
t.mtx.Lock()
Expand Down
8 changes: 4 additions & 4 deletions p2p/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

func TestClientStartup(t *testing.T) {
privKey, _, _ := crypto.GenerateEd25519Key(rand.Reader)
client, err := NewClient(config.P2PConfig{}, privKey, "TestChain", &test.TestLogger{T: t})
client, err := NewClient(config.P2PConfig{}, privKey, "TestChain", test.NewTestLogger(t))
assert := assert.New(t)
assert.NoError(err)
assert.NotNil(client)
Expand All @@ -35,7 +35,7 @@ func TestBootstrapping(t *testing.T) {
//log.SetDebugLogging()

assert := assert.New(t)
logger := &test.TestLogger{T: t}
logger := test.NewTestLogger(t)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand All @@ -55,7 +55,7 @@ func TestBootstrapping(t *testing.T) {

func TestDiscovery(t *testing.T) {
assert := assert.New(t)
logger := &test.TestLogger{T: t}
logger := test.NewTestLogger(t)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand All @@ -75,7 +75,7 @@ func TestDiscovery(t *testing.T) {

func TestGossiping(t *testing.T) {
assert := assert.New(t)
logger := &test.TestLogger{T: t}
logger := test.NewTestLogger(t)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down

0 comments on commit 8a758cb

Please sign in to comment.