Skip to content

Commit

Permalink
Merge pull request #133 from libp2p/fix-racy-test-active-relay
Browse files Browse the repository at this point in the history
fix race condition in TestActiveRelay
  • Loading branch information
marten-seemann committed Jul 10, 2021
2 parents f572d19 + d5fa138 commit 0234901
Showing 1 changed file with 13 additions and 23 deletions.
36 changes: 13 additions & 23 deletions p2p/protocol/internal/circuitv1-deprecated/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
swarm "github.com/libp2p/go-libp2p-swarm"
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
)

/* TODO: add tests
Expand Down Expand Up @@ -395,43 +396,27 @@ func TestActiveRelay(t *testing.T) {
time.Sleep(10 * time.Millisecond)

r1 := newTestRelay(t, ctx, hosts[0])

newTestRelay(t, ctx, hosts[1], OptHop, OptActive)

r3 := newTestRelay(t, ctx, hosts[2])

var (
conn1, conn2 net.Conn
done = make(chan struct{})
)

defer func() {
<-done
if conn1 != nil {
conn1.Close()
}
if conn2 != nil {
conn2.Close()
}
}()
connChan := make(chan manet.Conn)

msg := []byte("relay works!")
go func() {
defer close(done)
defer close(connChan)
list := r3.Listener()

var err error
conn2, err = list.Accept()
conn1, err := list.Accept()
if err != nil {
t.Error(err)
return
}

_, err = conn2.Write(msg)
if err != nil {
if _, err := conn1.Write(msg); err != nil {
t.Error(err)
return
}
connChan <- conn1
}()

rinfo := hosts[1].Peerstore().PeerInfo(hosts[1].ID())
Expand All @@ -440,11 +425,11 @@ func TestActiveRelay(t *testing.T) {
rctx, rcancel := context.WithTimeout(ctx, time.Second)
defer rcancel()

var err error
conn2, err = r1.DialPeer(rctx, rinfo, dinfo)
conn2, err := r1.DialPeer(rctx, rinfo, dinfo)
if err != nil {
t.Fatal(err)
}
defer conn2.Close()

data := make([]byte, len(msg))
_, err = io.ReadFull(conn2, data)
Expand All @@ -455,6 +440,11 @@ func TestActiveRelay(t *testing.T) {
if !bytes.Equal(data, msg) {
t.Fatal("message was incorrect:", string(data))
}
conn1, ok := <-connChan
if !ok {
t.Fatal("listener didn't accept a connection")
}
conn1.Close()
}

func TestRelayCanHop(t *testing.T) {
Expand Down

0 comments on commit 0234901

Please sign in to comment.