From 6b34597401c4a36bf304b50dd5bfd51960ae14e2 Mon Sep 17 00:00:00 2001 From: vyzo Date: Mon, 17 Jan 2022 01:38:08 +0200 Subject: [PATCH] make race detector happy --- itest/echo.go | 76 +++++++++++++++++++++++++++++++++++++++------ itest/rcmgr_test.go | 32 +++++++++---------- 2 files changed, 80 insertions(+), 28 deletions(-) diff --git a/itest/echo.go b/itest/echo.go index 6ea0b883c4..96c5efa13f 100644 --- a/itest/echo.go +++ b/itest/echo.go @@ -26,10 +26,10 @@ var ( type Echo struct { Host host.Host - BeforeReserve, BeforeRead, BeforeWrite, BeforeDone func() error - mx sync.Mutex status EchoStatus + + beforeReserve, beforeRead, beforeWrite, beforeDone func() error } type EchoStatus struct { @@ -53,6 +53,62 @@ func (e *Echo) Status() EchoStatus { return e.status } +func (e *Echo) BeforeReserve(f func() error) { + e.mx.Lock() + defer e.mx.Unlock() + + e.beforeReserve = f +} + +func (e *Echo) BeforeRead(f func() error) { + e.mx.Lock() + defer e.mx.Unlock() + + e.beforeRead = f +} + +func (e *Echo) BeforeWrite(f func() error) { + e.mx.Lock() + defer e.mx.Unlock() + + e.beforeWrite = f +} + +func (e *Echo) BeforeDone(f func() error) { + e.mx.Lock() + defer e.mx.Unlock() + + e.beforeDone = f +} + +func (e *Echo) getBeforeReserve() func() error { + e.mx.Lock() + defer e.mx.Unlock() + + return e.beforeReserve +} + +func (e *Echo) getBeforeRead() func() error { + e.mx.Lock() + defer e.mx.Unlock() + + return e.beforeRead +} + +func (e *Echo) getBeforeWrite() func() error { + e.mx.Lock() + defer e.mx.Unlock() + + return e.beforeWrite +} + +func (e *Echo) getBeforeDone() func() error { + e.mx.Lock() + defer e.mx.Unlock() + + return e.beforeDone +} + func (e *Echo) handleStream(s network.Stream) { defer s.Close() @@ -60,8 +116,8 @@ func (e *Echo) handleStream(s network.Stream) { e.status.StreamsIn++ e.mx.Unlock() - if e.BeforeReserve != nil { - if err := e.BeforeReserve(); err != nil { + if beforeReserve := e.getBeforeReserve(); beforeReserve != nil { + if err := beforeReserve(); err != nil { echoLog.Debugf("error syncing before reserve: %s", err) s.Reset() @@ -91,8 +147,8 @@ func (e *Echo) handleStream(s network.Stream) { return } - if e.BeforeRead != nil { - if err := e.BeforeRead(); err != nil { + if beforeRead := e.getBeforeRead(); beforeRead != nil { + if err := beforeRead(); err != nil { echoLog.Debugf("error syncing before read: %s", err) s.Reset() @@ -125,8 +181,8 @@ func (e *Echo) handleStream(s network.Stream) { e.status.EchosIn++ e.mx.Unlock() - if e.BeforeWrite != nil { - if err := e.BeforeWrite(); err != nil { + if beforeWrite := e.getBeforeWrite(); beforeWrite != nil { + if err := beforeWrite(); err != nil { echoLog.Debugf("error syncing before write: %s", err) s.Reset() @@ -153,8 +209,8 @@ func (e *Echo) handleStream(s network.Stream) { s.CloseWrite() - if e.BeforeDone != nil { - if err := e.BeforeDone(); err != nil { + if beforeDone := e.getBeforeDone(); beforeDone != nil { + if err := beforeDone(); err != nil { echoLog.Debugf("error syncing before done: %s", err) s.Reset() diff --git a/itest/rcmgr_test.go b/itest/rcmgr_test.go index 987ed0358c..9523b1a889 100644 --- a/itest/rcmgr_test.go +++ b/itest/rcmgr_test.go @@ -109,8 +109,8 @@ func TestResourceManagerServiceInbound(t *testing.T) { defer closeEchos(echos) defer closeRcmgrs(echos) - ready := new(chan struct{}) - echos[0].BeforeDone = waitForChannel(ready, time.Minute) + ready := make(chan struct{}) + echos[0].BeforeDone(waitForChannel(ready, time.Minute)) for i := 1; i < 5; i++ { err := echos[i].Host.Connect(context.Background(), peer.AddrInfo{ID: echos[0].Host.ID()}) @@ -120,8 +120,6 @@ func TestResourceManagerServiceInbound(t *testing.T) { time.Sleep(10 * time.Millisecond) } - *ready = make(chan struct{}) - var once sync.Once var wg sync.WaitGroup for i := 1; i < 5; i++ { @@ -133,7 +131,7 @@ func TestResourceManagerServiceInbound(t *testing.T) { if err != nil { t.Log(err) once.Do(func() { - close(*ready) + close(ready) }) } }(i) @@ -160,8 +158,9 @@ func TestResourceManagerServicePeerInbound(t *testing.T) { defer closeRcmgrs(echos) count := new(int32) - ready := new(chan struct{}) - echos[0].BeforeDone = waitForBarrier(count, ready, time.Minute) + ready := make(chan struct{}) + *count = 4 + echos[0].BeforeDone(waitForBarrier(count, ready, time.Minute)) for i := 1; i < 5; i++ { err := echos[i].Host.Connect(context.Background(), peer.AddrInfo{ID: echos[0].Host.ID()}) @@ -171,9 +170,6 @@ func TestResourceManagerServicePeerInbound(t *testing.T) { time.Sleep(10 * time.Millisecond) } - *count = 4 - *ready = make(chan struct{}) - var wg sync.WaitGroup for i := 1; i < 5; i++ { wg.Add(1) @@ -195,8 +191,8 @@ func TestResourceManagerServicePeerInbound(t *testing.T) { ResourceServiceErrors: 0, }) - *ready = make(chan struct{}) - echos[0].BeforeDone = waitForChannel(ready, time.Minute) + ready = make(chan struct{}) + echos[0].BeforeDone(waitForChannel(ready, time.Minute)) var once sync.Once for i := 0; i < 3; i++ { @@ -208,7 +204,7 @@ func TestResourceManagerServicePeerInbound(t *testing.T) { if err != nil { t.Log(err) once.Do(func() { - close(*ready) + close(ready) }) } }() @@ -223,14 +219,14 @@ func TestResourceManagerServicePeerInbound(t *testing.T) { }) } -func waitForBarrier(count *int32, ready *chan struct{}, timeout time.Duration) func() error { +func waitForBarrier(count *int32, ready chan struct{}, timeout time.Duration) func() error { return func() error { if atomic.AddInt32(count, -1) == 0 { - close(*ready) + close(ready) } select { - case <-*ready: + case <-ready: return nil case <-time.After(timeout): return fmt.Errorf("timeout") @@ -238,10 +234,10 @@ func waitForBarrier(count *int32, ready *chan struct{}, timeout time.Duration) f } } -func waitForChannel(ready *chan struct{}, timeout time.Duration) func() error { +func waitForChannel(ready chan struct{}, timeout time.Duration) func() error { return func() error { select { - case <-*ready: + case <-ready: return nil case <-time.After(timeout): return fmt.Errorf("timeout")