From 3bedefb8ccc4dded7452cc6b3cdafccc304941ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Tue, 11 Sep 2018 05:43:54 +0200 Subject: [PATCH] coreapi pubsub: add tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Ɓukasz Magiera --- core/coreapi/interface/options/pubsub.go | 2 +- core/coreapi/pubsub_test.go | 96 ++++++++++++++++++++++++ core/coreapi/unixfs_test.go | 3 + 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 core/coreapi/pubsub_test.go diff --git a/core/coreapi/interface/options/pubsub.go b/core/coreapi/interface/options/pubsub.go index f0a614d5802..c387d613db4 100644 --- a/core/coreapi/interface/options/pubsub.go +++ b/core/coreapi/interface/options/pubsub.go @@ -41,7 +41,7 @@ func PubSubSubscribeOptions(opts ...PubSubSubscribeOption) (*PubSubSubscribeSett type pubsubOpts struct{} -var PubBub nameOpts +var PubSub pubsubOpts func (pubsubOpts) Topic(topic string) PubSubPeersOption { return func(settings *PubSubPeersSettings) error { diff --git a/core/coreapi/pubsub_test.go b/core/coreapi/pubsub_test.go new file mode 100644 index 00000000000..11a8c68a5af --- /dev/null +++ b/core/coreapi/pubsub_test.go @@ -0,0 +1,96 @@ +package coreapi_test + +import ( + "context" + "github.com/ipfs/go-ipfs/core/coreapi/interface/options" + "testing" + "time" +) + +func TestBasicPubSub(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + nds, apis, err := makeAPISwarm(ctx, true, 2) + if err != nil { + t.Fatal(err) + } + + sub, err := apis[0].PubSub().Subscribe(ctx, "testch") + if err != nil { + t.Fatal(err) + } + + go func() { + tick := time.Tick(100 * time.Millisecond) + + for { + err = apis[1].PubSub().Publish(ctx, "testch", []byte("hello world")) + if err != nil { + t.Fatal(err) + } + select { + case <-tick: + case <-ctx.Done(): + return + } + } + }() + + m, err := sub.Next(ctx) + if err != nil { + t.Fatal(err) + } + + if string(m.Data()) != "hello world" { + t.Errorf("got invalid data: %s", string(m.Data())) + } + + if m.From() != nds[1].Identity { + t.Errorf("m.From didn't match") + } + + peers, err := apis[1].PubSub().Peers(ctx, options.PubSub.Topic("testch")) + if err != nil { + t.Fatal(err) + } + + if len(peers) != 1 { + t.Fatalf("got incorrect number of peers: %d", len(peers)) + } + + if peers[0] != nds[0].Identity { + t.Errorf("peer didn't match") + } + + peers, err = apis[1].PubSub().Peers(ctx, options.PubSub.Topic("nottestch")) + if err != nil { + t.Fatal(err) + } + + if len(peers) != 0 { + t.Fatalf("got incorrect number of peers: %d", len(peers)) + } + + topics, err := apis[0].PubSub().Ls(ctx) + if err != nil { + t.Fatal(err) + } + + if len(topics) != 1 { + t.Fatalf("got incorrect number of topics: %d", len(peers)) + } + + if topics[0] != "testch" { + t.Errorf("topic didn't match") + } + + topics, err = apis[1].PubSub().Ls(ctx) + if err != nil { + t.Fatal(err) + } + + if len(topics) != 0 { + t.Fatalf("got incorrect number of topics: %d", len(peers)) + } +} diff --git a/core/coreapi/unixfs_test.go b/core/coreapi/unixfs_test.go index bc78448f551..30a673f5037 100644 --- a/core/coreapi/unixfs_test.go +++ b/core/coreapi/unixfs_test.go @@ -88,6 +88,9 @@ func makeAPISwarm(ctx context.Context, fullIdentity bool, n int) ([]*core.IpfsNo Repo: r, Host: mock.MockHostOption(mn), Online: fullIdentity, + ExtraOpts: map[string]bool{ + "pubsub": true, + }, }) if err != nil { return nil, nil, err