Skip to content

Commit

Permalink
fix #672: wait until done; fix ponger response.
Browse files Browse the repository at this point in the history
  • Loading branch information
raulk committed Jul 10, 2019
1 parent 4a8d355 commit 8e4bc2e
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions p2p/net/mock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func randPeer(t *testing.T) peer.ID {
}

func TestNetworkSetup(t *testing.T) {

ctx := context.Background()
sk1, _, err := test.RandTestKeyPair(ci.RSA, 512)
if err != nil {
Expand Down Expand Up @@ -319,28 +318,35 @@ func TestStreams(t *testing.T) {

}

func makePinger(st string, n int) func(network.Stream) {
return func(s network.Stream) {
func makePinger(t *testing.T, st string, n int) func(network.Stream) chan struct{} {
t.Helper()

return func(s network.Stream) chan struct{} {
done := make(chan struct{}, 1)
go func() {
defer func() { done <- struct{}{} }()
defer helpers.FullClose(s)

for i := 0; i < n; i++ {
b := make([]byte, 4+len(st))
if _, err := s.Write([]byte("ping" + st)); err != nil {
panic(err)
t.Fatal(err)
}
if _, err := io.ReadFull(s, b); err != nil {
panic(err)
t.Fatal(err)
}
if !bytes.Equal(b, []byte("pong"+st)) {
panic("bytes mismatch")
t.Fatal("bytes mismatch")
}
}
}()
return done
}
}

func makePonger(st string) func(network.Stream) {
func makePonger(t *testing.T, st string) func(network.Stream) {
t.Helper()

return func(s network.Stream) {
go func() {
defer helpers.FullClose(s)
Expand All @@ -351,13 +357,13 @@ func makePonger(st string) func(network.Stream) {
if err == io.EOF {
return
}
panic(err)
t.Fatal(err)
}
if !bytes.Equal(b, []byte("ping"+st)) {
panic("bytes mismatch")
t.Fatal("bytes mismatch")
}
if _, err := s.Write([]byte("pong" + st)); err != nil {
panic(err)
t.Fatal(err)
}
}
}()
Expand All @@ -378,7 +384,7 @@ func TestStreamsStress(t *testing.T) {

hosts := mn.Hosts()
for _, h := range hosts {
ponger := makePonger(string(protocol.TestingID))
ponger := makePonger(t, "pingpong")
h.SetStreamHandler(protocol.TestingID, ponger)
}

Expand All @@ -399,7 +405,7 @@ func TestStreamsStress(t *testing.T) {
}

log.Infof("%d start pinging", i)
makePinger("pingpong", rand.Intn(100))(s)
<-makePinger(t, "pingpong", rand.Intn(100))(s)
log.Infof("%d done pinging", i)
}(i)
}
Expand All @@ -408,7 +414,6 @@ func TestStreamsStress(t *testing.T) {
}

func TestAdding(t *testing.T) {

mn := New(context.Background())

peers := []peer.ID{}
Expand Down

0 comments on commit 8e4bc2e

Please sign in to comment.