Skip to content

Commit

Permalink
feat(p2p): Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gfanton committed Jan 25, 2019
1 parent 481d00c commit c392170
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 8 deletions.
50 changes: 43 additions & 7 deletions core/network/p2p/protocol/provider/pubsub/pubsub_test.go
Expand Up @@ -11,6 +11,7 @@ import (
libp2p "github.com/libp2p/go-libp2p"
host "github.com/libp2p/go-libp2p-host"
pstore "github.com/libp2p/go-libp2p-peerstore"
mh "github.com/multiformats/go-multihash"
. "github.com/smartystreets/goconvey/convey"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand All @@ -33,6 +34,15 @@ func getBoostrap(d *ProviderTest) []string {
return bootstrap
}

func createCid(id string) (cid.Cid, error) {
h, err := mh.Sum([]byte(id), mh.SHA2_256, -1)
if err != nil {
return cid.Cid{}, err
}

return cid.NewCidV0(h), nil
}

func (pt *ProviderTest) handler(id string, pi pstore.PeerInfo) {
logger().Debug("Handler", zap.String("name", pt.name), zap.String("id", id), zap.String("pi", pi.ID.Pretty()))
}
Expand Down Expand Up @@ -111,14 +121,14 @@ func setupTestLogging() {

func TestP2PNetwork(t *testing.T) {
var (
homer, lisa, roger, bart *ProviderTest
err error
homer, lisa, roger *ProviderTest
err error
)

// setupTestLogging()
// log.SetDebugLogging()

hs := []*ProviderTest{homer, lisa, roger, bart}
hs := []*ProviderTest{homer, lisa, roger}
defer func() {
for _, h := range hs {
if h != nil {
Expand All @@ -144,13 +154,39 @@ func TestP2PNetwork(t *testing.T) {
roger, err = setupProviderTest("roger", b...)
So(err, ShouldBeNil)

bart, err = setupProviderTest("bart", b...)
So(err, ShouldBeNil)

peers := homer.host.Peerstore().Peers()
So(len(peers), ShouldEqual, len(hs))
})

Convey("Roger lookup for homer", FailureHalts, func(c C) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*4)
defer cancel()

topic, err := createCid("homer")
So(err, ShouldBeNil)

cps := make(chan []pstore.PeerInfo, 1)
roger.provider.RegisterHandler(func(id cid.Cid, ps ...pstore.PeerInfo) {
cps <- ps
})

err = homer.provider.Provide(ctx, topic)
So(err, ShouldBeNil)

err = roger.provider.FindProviders(ctx, topic)
So(err, ShouldBeNil)

var ps []pstore.PeerInfo
select {
case <-ctx.Done():
err = ctx.Err()
case ps = <-cps:
}

So(err, ShouldBeNil)
So(len(ps), ShouldEqual, 1)
})

Convey("Roger lookup for Lisa via homer", FailureHalts, func(c C) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*4)
defer cancel()
Expand All @@ -162,7 +198,7 @@ func TestP2PNetwork(t *testing.T) {
cps <- ps
})

err := lisa.provider.Subscribe(ctx, topic)
err = lisa.provider.Subscribe(ctx, topic)
So(err, ShouldBeNil)

err = roger.provider.Announce(topic)
Expand Down
31 changes: 30 additions & 1 deletion core/network/p2p/test/p2p_test.go
Expand Up @@ -75,7 +75,9 @@ func TestP2PNetwork(t *testing.T) {
homer, lisa, bart *p2p.Driver
err error
)
setupTestLogging()
// setupTestLogging()
// log.SetDebugLogging()

// logging.SetDebugLogging()

dht.PoolSize = 3
Expand Down Expand Up @@ -111,6 +113,33 @@ func TestP2PNetwork(t *testing.T) {
So(err, ShouldBeNil)
})

Convey("Bart send an event to Homer", FailureHalts, func(c C) {
tctx, cancel := context.WithTimeout(ctx, time.Second*4)
defer cancel()

e := &api.Envelope{
ChannelID: "Homer",
}

homerQueue := make(chan *api.Envelope, 1)
homer.OnEnvelopeHandler(func(ctx context.Context, envelope *api.Envelope) (*api.Void, error) {
if envelope == nil {
homerQueue <- nil
return nil, fmt.Errorf("empty envelope")
}
homerQueue <- envelope
return &api.Void{}, nil
})

logger().Debug("Homer joing himself")
err = homer.Join(ctx, "Homer")

err := bart.Emit(tctx, e)
So(err, ShouldBeNil)
So(<-homerQueue, ShouldNotBeNil)
// So(len(homerQueue), ShouldEqual, 1)
})

Convey("Roger send an event to Lisa", FailureHalts, func(c C) {
tctx, cancel := context.WithTimeout(ctx, time.Second*4)
defer cancel()
Expand Down

0 comments on commit c392170

Please sign in to comment.