2016-09-09 20:13:50 -07:00
|
|
|
package floodsub
|
2016-09-09 17:15:39 -07:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"math/rand"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
2016-09-10 08:28:29 -07:00
|
|
|
host "github.com/libp2p/go-libp2p/p2p/host"
|
|
|
|
|
netutil "github.com/libp2p/go-libp2p/p2p/test/util"
|
2016-09-09 17:15:39 -07:00
|
|
|
)
|
|
|
|
|
|
2016-09-10 16:03:53 -07:00
|
|
|
func getNetHosts(t *testing.T, ctx context.Context, n int) []host.Host {
|
2016-09-09 17:15:39 -07:00
|
|
|
var out []host.Host
|
|
|
|
|
|
|
|
|
|
for i := 0; i < n; i++ {
|
2016-09-10 16:03:53 -07:00
|
|
|
h := netutil.GenHostSwarm(t, ctx)
|
2016-09-09 17:15:39 -07:00
|
|
|
out = append(out, h)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func connect(t *testing.T, a, b host.Host) {
|
|
|
|
|
pinfo := a.Peerstore().PeerInfo(a.ID())
|
|
|
|
|
err := b.Connect(context.Background(), pinfo)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-10 16:03:53 -07:00
|
|
|
func sparseConnect(t *testing.T, hosts []host.Host) {
|
|
|
|
|
for i, a := range hosts {
|
|
|
|
|
for j := 0; j < 3; j++ {
|
|
|
|
|
n := rand.Intn(len(hosts))
|
|
|
|
|
if n == i {
|
|
|
|
|
j--
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b := hosts[n]
|
|
|
|
|
|
|
|
|
|
connect(t, a, b)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-09 17:15:39 -07:00
|
|
|
func connectAll(t *testing.T, hosts []host.Host) {
|
|
|
|
|
for i, a := range hosts {
|
|
|
|
|
for j, b := range hosts {
|
|
|
|
|
if i == j {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connect(t, a, b)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-10 16:03:53 -07:00
|
|
|
func getPubsubs(ctx context.Context, hs []host.Host) []*PubSub {
|
2016-09-09 17:15:39 -07:00
|
|
|
var psubs []*PubSub
|
2016-09-10 16:03:53 -07:00
|
|
|
for _, h := range hs {
|
|
|
|
|
psubs = append(psubs, NewFloodSub(ctx, h))
|
2016-09-09 17:15:39 -07:00
|
|
|
}
|
2016-09-10 16:03:53 -07:00
|
|
|
return psubs
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestBasicFloodsub(t *testing.T) {
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
defer cancel()
|
|
|
|
|
hosts := getNetHosts(t, ctx, 20)
|
|
|
|
|
|
|
|
|
|
psubs := getPubsubs(ctx, hosts)
|
2016-09-09 17:15:39 -07:00
|
|
|
|
|
|
|
|
var msgs []<-chan *Message
|
|
|
|
|
for _, ps := range psubs {
|
|
|
|
|
subch, err := ps.Subscribe("foobar")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
msgs = append(msgs, subch)
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-10 16:03:53 -07:00
|
|
|
//connectAll(t, hosts)
|
|
|
|
|
sparseConnect(t, hosts)
|
2016-09-09 17:15:39 -07:00
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
|
msg := []byte(fmt.Sprintf("%d the flooooooood %d", i, i))
|
|
|
|
|
|
|
|
|
|
owner := rand.Intn(len(psubs))
|
|
|
|
|
|
|
|
|
|
psubs[owner].Publish("foobar", msg)
|
|
|
|
|
|
|
|
|
|
for _, resp := range msgs {
|
|
|
|
|
got := <-resp
|
|
|
|
|
if !bytes.Equal(msg, got.Data) {
|
|
|
|
|
t.Fatal("got wrong message!")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-10 16:03:53 -07:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestMultihops(t *testing.T) {
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
hosts := getNetHosts(t, ctx, 6)
|
|
|
|
|
|
|
|
|
|
psubs := getPubsubs(ctx, hosts)
|
|
|
|
|
|
|
|
|
|
connect(t, hosts[0], hosts[1])
|
|
|
|
|
connect(t, hosts[1], hosts[2])
|
|
|
|
|
connect(t, hosts[2], hosts[3])
|
|
|
|
|
connect(t, hosts[3], hosts[4])
|
|
|
|
|
connect(t, hosts[4], hosts[5])
|
|
|
|
|
|
|
|
|
|
var msgChs []<-chan *Message
|
|
|
|
|
for i := 1; i < 6; i++ {
|
|
|
|
|
ch, err := psubs[i].Subscribe("foobar")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
msgChs = append(msgChs, ch)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
|
|
|
|
|
|
msg := []byte("i like cats")
|
|
|
|
|
err := psubs[0].Publish("foobar", msg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// last node in the chain should get the message
|
|
|
|
|
select {
|
|
|
|
|
case out := <-msgChs[4]:
|
|
|
|
|
if !bytes.Equal(out.GetData(), msg) {
|
|
|
|
|
t.Fatal("got wrong data")
|
|
|
|
|
}
|
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
|
t.Fatal("timed out waiting for message")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestReconnects(t *testing.T) {
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
hosts := getNetHosts(t, ctx, 10)
|
|
|
|
|
|
|
|
|
|
psubs := getPubsubs(ctx, hosts)
|
|
|
|
|
|
|
|
|
|
connect(t, hosts[0], hosts[1])
|
|
|
|
|
connect(t, hosts[0], hosts[2])
|
|
|
|
|
|
|
|
|
|
A, err := psubs[1].Subscribe("cats")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
B, err := psubs[2].Subscribe("cats")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
|
|
|
|
|
|
msg := []byte("apples and oranges")
|
|
|
|
|
err = psubs[0].Publish("cats", msg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assertReceive(t, A, msg)
|
|
|
|
|
assertReceive(t, B, msg)
|
|
|
|
|
|
|
|
|
|
hosts[2].Close()
|
|
|
|
|
|
|
|
|
|
msg2 := []byte("potato")
|
|
|
|
|
err = psubs[0].Publish("cats", msg2)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assertReceive(t, A, msg2)
|
|
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 50)
|
|
|
|
|
_, ok := psubs[0].peers[hosts[2].ID()]
|
|
|
|
|
if ok {
|
|
|
|
|
t.Fatal("shouldnt have this peer anymore")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func assertReceive(t *testing.T, ch <-chan *Message, exp []byte) {
|
|
|
|
|
select {
|
|
|
|
|
case msg := <-ch:
|
|
|
|
|
if !bytes.Equal(msg.GetData(), exp) {
|
|
|
|
|
t.Fatalf("got wrong message, expected %s but got %s", string(exp), string(msg.GetData()))
|
|
|
|
|
}
|
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
|
t.Fatal("timed out waiting for message of: ", exp)
|
|
|
|
|
}
|
2016-09-09 17:15:39 -07:00
|
|
|
}
|
2016-09-11 13:56:07 -07:00
|
|
|
|
|
|
|
|
func TestNoConnection(t *testing.T) {
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
hosts := getNetHosts(t, ctx, 10)
|
|
|
|
|
|
|
|
|
|
psubs := getPubsubs(ctx, hosts)
|
|
|
|
|
|
|
|
|
|
ch, err := psubs[5].Subscribe("foobar")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = psubs[0].Publish("foobar", []byte("TESTING"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
case <-ch:
|
|
|
|
|
t.Fatal("shouldnt have gotten a message")
|
|
|
|
|
case <-time.After(time.Millisecond * 200):
|
|
|
|
|
}
|
|
|
|
|
}
|