From 3ecfbc2a6042f7a1cae43ea006ee01a5262bf087 Mon Sep 17 00:00:00 2001 From: vyzo Date: Wed, 21 Feb 2018 15:52:37 +0200 Subject: [PATCH] better test for fanout maintenance --- gossipsub_test.go | 83 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 3 deletions(-) diff --git a/gossipsub_test.go b/gossipsub_test.go index 09bc8d0..4046d5f 100644 --- a/gossipsub_test.go +++ b/gossipsub_test.go @@ -145,9 +145,6 @@ func TestGossipsubFanout(t *testing.T) { } } - // wait for heartbeat to exercise fanout maintainance - time.Sleep(time.Second * 2) - // subscribe the owner subch, err := psubs[0].Subscribe("foobar") if err != nil { @@ -177,6 +174,86 @@ func TestGossipsubFanout(t *testing.T) { } } +func TestGossipsubFanoutMaintenance(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + hosts := getNetHosts(t, ctx, 20) + + psubs := getGossipsubs(ctx, hosts) + + var msgs []*Subscription + for _, ps := range psubs[1:] { + subch, err := ps.Subscribe("foobar") + if err != nil { + t.Fatal(err) + } + + msgs = append(msgs, subch) + } + + denseConnect(t, hosts) + + // wait for heartbeats to build mesh + time.Sleep(time.Second * 2) + + for i := 0; i < 100; i++ { + msg := []byte(fmt.Sprintf("%d it's not a floooooood %d", i, i)) + + owner := 0 + + psubs[owner].Publish("foobar", msg) + + for _, sub := range msgs { + got, err := sub.Next(ctx) + if err != nil { + t.Fatal(sub.err) + } + if !bytes.Equal(msg, got.Data) { + t.Fatal("got wrong message!") + } + } + } + + // unsubscribe all peers to exercise fanout maintenance + for _, sub := range msgs { + sub.Cancel() + } + msgs = nil + + // wait for heartbeats + time.Sleep(time.Second * 2) + + // resubscribe and repeat + for _, ps := range psubs[1:] { + subch, err := ps.Subscribe("foobar") + if err != nil { + t.Fatal(err) + } + + msgs = append(msgs, subch) + } + + time.Sleep(time.Second * 2) + + for i := 0; i < 100; i++ { + msg := []byte(fmt.Sprintf("%d it's not a floooooood %d", i, i)) + + owner := 0 + + psubs[owner].Publish("foobar", msg) + + for _, sub := range msgs { + got, err := sub.Next(ctx) + if err != nil { + t.Fatal(sub.err) + } + if !bytes.Equal(msg, got.Data) { + t.Fatal("got wrong message!") + } + } + } +} + func TestGossipsubGossip(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel()