From 4336d5ae71cee7982fd51e915e6f5852fdb6e66a Mon Sep 17 00:00:00 2001 From: Adin Schmahmann Date: Sun, 6 Dec 2020 14:14:52 -0500 Subject: [PATCH] test: add test for if nodes are connected before pubsub is started --- floodsub_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/floodsub_test.go b/floodsub_test.go index 952855f..0fc334b 100644 --- a/floodsub_test.go +++ b/floodsub_test.go @@ -1137,3 +1137,66 @@ func TestWithInvalidMessageAuthor(t *testing.T) { t.Fatal("expected error") } } + +func TestPreconnectedNodes(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + // If this test fails it may hang so set a timeout + ctx, cancel = context.WithTimeout(ctx, time.Second*10) + defer cancel() + + // Create hosts + h1 := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx)) + h2 := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx)) + + opts := []Option{WithDiscovery(&dummyDiscovery{})} + // Setup first PubSub + p1, err := NewFloodSub(ctx, h1, opts...) + if err != nil { + t.Fatal(err) + } + + // Connect the two hosts together + connect(t, h2, h1) + + // Setup the second DHT + p2, err := NewFloodSub(ctx, h2, opts...) + if err != nil { + t.Fatal(err) + } + + // See if it works + p2Topic, err := p2.Join("test") + if err != nil { + t.Fatal(err) + } + + p1Topic, err := p1.Join("test") + if err != nil { + t.Fatal(err) + } + + testPublish := func(publisher, receiver *Topic, msg []byte) { + receiverSub, err := receiver.Subscribe() + if err != nil { + t.Fatal(err) + } + + if err := publisher.Publish(ctx, msg, WithReadiness(MinTopicSize(1))); err != nil { + t.Fatal(err) + } + + m, err := receiverSub.Next(ctx) + if err != nil { + t.Fatal(err) + } + + if receivedData := m.GetData(); !bytes.Equal(receivedData, msg) { + t.Fatalf("expected message %v, got %v", msg, receivedData) + } + } + + // Test both directions since PubSub uses one directional streams + testPublish(p1Topic, p2Topic, []byte("test1-to-2")) + testPublish(p1Topic, p2Topic, []byte("test2-to-1")) +}