coreapi pubsub: add tests
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Sep 11, 2018 at 05:43 UTC
d3f3afa5de36bf90e940ceb9aa832cf735097bf2
3 files changed
+100
-1
core/coreapi/interface/options/pubsub.go
+1
-1
@@ -41,7 +41,7 @@ func PubSubSubscribeOptions(opts ...PubSubSubscribeOption) (*PubSubSubscribeSett
41
42
type pubsubOpts struct{}
43
44
-var PubBub nameOpts
44
+var PubSub pubsubOpts
45
46
func (pubsubOpts) Topic(topic string) PubSubPeersOption {
47
return func(settings *PubSubPeersSettings) error {
core/coreapi/pubsub_test.go
new
+96
@@ -0,0 +1,96 @@
1
+package coreapi_test
2
+
3
+import (
4
+ "context"
5
+ "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
6
+ "testing"
7
+ "time"
8
+)
9
+
10
+func TestBasicPubSub(t *testing.T) {
11
+ ctx, cancel := context.WithCancel(context.Background())
12
+ defer cancel()
13
+
14
+ nds, apis, err := makeAPISwarm(ctx, true, 2)
15
+ if err != nil {
16
+ t.Fatal(err)
17
+ }
18
+
19
+ sub, err := apis[0].PubSub().Subscribe(ctx, "testch")
20
+ if err != nil {
21
+ t.Fatal(err)
22
+ }
23
+
24
+ go func() {
25
+ tick := time.Tick(100 * time.Millisecond)
26
+
27
+ for {
28
+ err = apis[1].PubSub().Publish(ctx, "testch", []byte("hello world"))
29
+ if err != nil {
30
+ t.Fatal(err)
31
+ }
32
+ select {
33
+ case <-tick:
34
+ case <-ctx.Done():
35
+ return
36
+ }
37
+ }
38
+ }()
39
+
40
+ m, err := sub.Next(ctx)
41
+ if err != nil {
42
+ t.Fatal(err)
43
+ }
44
+
45
+ if string(m.Data()) != "hello world" {
46
+ t.Errorf("got invalid data: %s", string(m.Data()))
47
+ }
48
+
49
+ if m.From() != nds[1].Identity {
50
+ t.Errorf("m.From didn't match")
51
+ }
52
+
53
+ peers, err := apis[1].PubSub().Peers(ctx, options.PubSub.Topic("testch"))
54
+ if err != nil {
55
+ t.Fatal(err)
56
+ }
57
+
58
+ if len(peers) != 1 {
59
+ t.Fatalf("got incorrect number of peers: %d", len(peers))
60
+ }
61
+
62
+ if peers[0] != nds[0].Identity {
63
+ t.Errorf("peer didn't match")
64
+ }
65
+
66
+ peers, err = apis[1].PubSub().Peers(ctx, options.PubSub.Topic("nottestch"))
67
+ if err != nil {
68
+ t.Fatal(err)
69
+ }
70
+
71
+ if len(peers) != 0 {
72
+ t.Fatalf("got incorrect number of peers: %d", len(peers))
73
+ }
74
+
75
+ topics, err := apis[0].PubSub().Ls(ctx)
76
+ if err != nil {
77
+ t.Fatal(err)
78
+ }
79
+
80
+ if len(topics) != 1 {
81
+ t.Fatalf("got incorrect number of topics: %d", len(peers))
82
+ }
83
+
84
+ if topics[0] != "testch" {
85
+ t.Errorf("topic didn't match")
86
+ }
87
+
88
+ topics, err = apis[1].PubSub().Ls(ctx)
89
+ if err != nil {
90
+ t.Fatal(err)
91
+ }
92
+
93
+ if len(topics) != 0 {
94
+ t.Fatalf("got incorrect number of topics: %d", len(peers))
95
+ }
96
+}
core/coreapi/unixfs_test.go
+3
@@ -94,6 +94,9 @@ func makeAPISwarm(ctx context.Context, fullIdentity bool, n int) ([]*core.IpfsNo
94
Repo: r,
95
Host: mock.MockHostOption(mn),
96
Online: fullIdentity,
97
+ ExtraOpts: map[string]bool{
98
+ "pubsub": true,
99
+ },
100
})
101
if err != nil {
102
return nil, nil, err