Skip to content

Commit

Permalink
make race detector happy
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo committed Jan 16, 2022
1 parent 4951a5d commit 6b34597
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 28 deletions.
76 changes: 66 additions & 10 deletions itest/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -53,15 +53,71 @@ 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()

e.mx.Lock()
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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
32 changes: 14 additions & 18 deletions itest/rcmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()})
Expand All @@ -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++ {
Expand All @@ -133,7 +131,7 @@ func TestResourceManagerServiceInbound(t *testing.T) {
if err != nil {
t.Log(err)
once.Do(func() {
close(*ready)
close(ready)
})
}
}(i)
Expand All @@ -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()})
Expand All @@ -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)
Expand All @@ -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++ {
Expand All @@ -208,7 +204,7 @@ func TestResourceManagerServicePeerInbound(t *testing.T) {
if err != nil {
t.Log(err)
once.Do(func() {
close(*ready)
close(ready)
})
}
}()
Expand All @@ -223,25 +219,25 @@ 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")
}
}
}

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")
Expand Down

0 comments on commit 6b34597

Please sign in to comment.