Skip to content
This repository has been archived by the owner on Dec 7, 2019. It is now read-only.

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Aug 19, 2017
1 parent 0dae19f commit 8eb1851
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 33 deletions.
63 changes: 33 additions & 30 deletions muxtest/muxt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package muxtest

import (
"bytes"
"context"
crand "crypto/rand"
"fmt"
"io"
Expand All @@ -12,14 +13,17 @@ import (
"sync"
"testing"

peer "github.com/libp2p/go-libp2p-peer"
ps "github.com/libp2p/go-peerstream"

conn "github.com/libp2p/go-libp2p-conn"
smux "github.com/libp2p/go-stream-muxer"
tcpc "github.com/libp2p/go-tcp-transport"
testutil "github.com/libp2p/go-testutil"
ma "github.com/multiformats/go-multiaddr"
)

var zeroaddr = ma.StringCast("/ip4/0.0.0.0/tcp/0")
var localaddr = ma.StringCast("/ip4/127.0.0.1/tcp/0")

var randomness []byte
var nextPort = 20000
Expand Down Expand Up @@ -61,8 +65,9 @@ type echoSetup struct {
}

func singleConn(t *testing.T, tr smux.Transport) echoSetup {
listenerIdentity := testutil.RandIdentityOrFatal(t)
tcp := tcpc.NewTCPTransport()
swarm := ps.NewSwarm(tr)
swarm := ps.NewSwarm()
swarm.SetStreamHandler(func(s *ps.Stream) {
defer s.Close()
log("accepted stream")
Expand All @@ -71,16 +76,17 @@ func singleConn(t *testing.T, tr smux.Transport) echoSetup {
})

log("listening at %s", "localhost:0")
d, err := tcp.Dialer(zeroaddr)
l, err := tcp.Listen(localaddr)
checkErr(t, err)
l, err := tcp.Listen(ma.StringCast("/ip4/0.0.0.0/tcp/0"))
cl, err := conn.WrapTransportListener(context.Background(), l, listenerIdentity.ID(), tr, listenerIdentity.PrivateKey())
checkErr(t, err)

_, err = swarm.AddListener(l)
_, err = swarm.AddListener(cl)
checkErr(t, err)

log("dialing to %s", l.Multiaddr())
nc1, err := d.Dial(l.Multiaddr())
dialerIdentity := testutil.RandIdentityOrFatal(t)
dialer := conn.NewDialer(dialerIdentity.ID(), dialerIdentity.PrivateKey(), nil, tr)
nc1, err := dialer.Dial(context.Background(), l.Multiaddr(), listenerIdentity.ID())
checkErr(t, err)

c1, err := swarm.AddConn(nc1)
Expand All @@ -94,7 +100,7 @@ func singleConn(t *testing.T, tr smux.Transport) echoSetup {

func makeSwarm(t *testing.T, tr smux.Transport, nListeners int) *ps.Swarm {
tcp := tcpc.NewTCPTransport()
swarm := ps.NewSwarm(tr)
swarm := ps.NewSwarm()
swarm.SetStreamHandler(func(s *ps.Stream) {
defer s.Close()
log("accepted stream")
Expand All @@ -103,10 +109,13 @@ func makeSwarm(t *testing.T, tr smux.Transport, nListeners int) *ps.Swarm {
})

for i := 0; i < nListeners; i++ {
listenerIdentity := testutil.RandIdentityOrFatal(t)
log("%p listening at %s", swarm, "localhost:0")
l, err := tcp.Listen(ma.StringCast("/ip4/0.0.0.0/tcp/0"))
l, err := tcp.Listen(localaddr)
checkErr(t, err)
cl, err := conn.WrapTransportListener(context.Background(), l, listenerIdentity.ID(), tr, listenerIdentity.PrivateKey())
checkErr(t, err)
_, err = swarm.AddListener(l)
_, err = swarm.AddListener(cl)
checkErr(t, err)
}

Expand All @@ -122,12 +131,12 @@ func makeSwarms(t *testing.T, tr smux.Transport, nSwarms, nListeners int) []*ps.
}

func SubtestConstructSwarm(t *testing.T, tr smux.Transport) {
ps.NewSwarm(tr)
ps.NewSwarm()
}

func SubtestSimpleWrite(t *testing.T, tr smux.Transport) {
tcp := tcpc.NewTCPTransport()
swarm := ps.NewSwarm(tr)
swarm := ps.NewSwarm()
defer swarm.Close()

piper, pipew := io.Pipe()
Expand All @@ -140,19 +149,20 @@ func SubtestSimpleWrite(t *testing.T, tr smux.Transport) {
})

log("listening at %s", "localhost:0")
l, err := tcp.Listen(ma.StringCast("/ip4/0.0.0.0/tcp/0"))
l, err := tcp.Listen(localaddr)
checkErr(t, err)

_, err = swarm.AddListener(l)
listenerIdentity := testutil.RandIdentityOrFatal(t)
cl, err := conn.WrapTransportListener(context.Background(), l, listenerIdentity.ID(), tr, listenerIdentity.PrivateKey())
checkErr(t, err)

d, err := tcp.Dialer(zeroaddr)
_, err = swarm.AddListener(cl)
checkErr(t, err)

log("dialing to %s", l.Addr().String())
nc1, err := d.Dial(l.Multiaddr())
dialerIdentity := testutil.RandIdentityOrFatal(t)
dialer := conn.NewDialer(dialerIdentity.ID(), dialerIdentity.PrivateKey(), nil, tr)
nc1, err := dialer.Dial(context.Background(), l.Multiaddr(), listenerIdentity.ID())
checkErr(t, err)

c1, err := swarm.AddConn(nc1)
checkErr(t, err)
defer c1.Close()
Expand All @@ -172,15 +182,15 @@ func SubtestSimpleWrite(t *testing.T, tr smux.Transport) {
_, err = s1.Read(buf2)
checkErr(t, err)
if string(buf2) != string(buf1) {
t.Error("buf1 and buf2 not equal: %s != %s", string(buf1), string(buf2))
t.Errorf("buf1 and buf2 not equal: %s != %s", string(buf1), string(buf2))
}

buf3 := make([]byte, len(buf1))
log("reading %d bytes from pipe (tee)", len(buf3))
_, err = piper.Read(buf3)
checkErr(t, err)
if string(buf3) != string(buf1) {
t.Error("buf1 and buf3 not equal: %s != %s", string(buf1), string(buf3))
t.Errorf("buf1 and buf3 not equal: %s != %s", string(buf1), string(buf3))
}
}

Expand Down Expand Up @@ -311,19 +321,12 @@ func SubtestStressNSwarmNConnNStreamNMsg(t *testing.T, tr smux.Transport, nSwarm

ls := b.Listeners()
l := ls[mrand.Intn(len(ls))]
nl := l.NetListener()
nla := nl.Addr()

tcp := tcpc.NewTCPTransport()
d, err := tcp.Dialer(zeroaddr)
dialerIdentity := testutil.RandIdentityOrFatal(t)
dialer := conn.NewDialer(dialerIdentity.ID(), dialerIdentity.PrivateKey(), nil, tr)
nc, err := dialer.Dial(context.Background(), l.Multiaddr(), peer.ID(0))
checkErr(t, err)

nc, err := d.Dial(nl.Multiaddr())
if err != nil {
t.Fatal(fmt.Errorf("net.Dial(%s, %s): %s", nla.Network(), nla.String(), err))
return
}

c, err := a.AddConn(nc)
if err != nil {
t.Fatal(fmt.Errorf("a.AddConn(%s <--> %s): %s", nc.LocalAddr(), nc.RemoteAddr(), err))
Expand Down
23 changes: 20 additions & 3 deletions swarm_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
package peerstream

import (
"context"
"fmt"
"sync"
"testing"
"time"

tpt "github.com/libp2p/go-libp2p-transport"
iconn "github.com/libp2p/go-libp2p-interface-conn"
smux "github.com/libp2p/go-stream-muxer"
)

type fakeconn struct {
tpt.Conn
done <-chan struct{} // close to make AcceptStream return
iconn.Conn
}

func (f *fakeconn) IsClosed() bool {
return true
}

func (f *fakeconn) AcceptStream() (smux.Stream, error) {
<-f.done
return nil, nil
}

func (f *fakeconn) Close() error {
Expand Down Expand Up @@ -49,6 +61,9 @@ func (mn *myNotifee) OpenedStream(*Stream) {}
func (mn *myNotifee) ClosedStream(*Stream) {}

func TestNotificationOrdering(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

s := NewSwarm()
notifiee := &myNotifee{conns: make(map[*Conn]bool)}

Expand All @@ -59,8 +74,10 @@ func TestNotificationOrdering(t *testing.T) {
wg.Add(1)
go func() {
defer wg.Done()

for j := 0; j < 50; j++ {
nc := new(fakeconn)
conn := &fakeconn{done: ctx.Done()}
nc := conn
c, err := s.AddConn(nc)
if err != nil {
t.Error(err)
Expand Down

0 comments on commit 8eb1851

Please sign in to comment.