| 1 | package tests |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | iface "github.com/ipfs/kubo/core/coreiface" |
| 9 | "github.com/ipfs/kubo/core/coreiface/options" |
| 10 | ) |
| 11 | |
| 12 | func (tp *TestSuite) TestPubSub(t *testing.T) { |
| 13 | tp.hasApi(t, func(api iface.CoreAPI) error { |
| 14 | if api.PubSub() == nil { |
| 15 | return errAPINotImplemented |
| 16 | } |
| 17 | return nil |
| 18 | }) |
| 19 | |
| 20 | t.Run("TestBasicPubSub", tp.TestBasicPubSub) |
| 21 | } |
| 22 | |
| 23 | func (tp *TestSuite) TestBasicPubSub(t *testing.T) { |
| 24 | ctx, cancel := context.WithCancel(context.Background()) |
| 25 | defer cancel() |
| 26 | |
| 27 | apis, err := tp.MakeAPISwarm(t, ctx, 2) |
| 28 | if err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | |
| 32 | sub, err := apis[0].PubSub().Subscribe(ctx, "testch") |
| 33 | if err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | |
| 37 | done := make(chan struct{}) |
| 38 | go func() { |
| 39 | defer close(done) |
| 40 | |
| 41 | ticker := time.NewTicker(100 * time.Millisecond) |
| 42 | defer ticker.Stop() |
| 43 | |
| 44 | for { |
| 45 | err := apis[1].PubSub().Publish(ctx, "testch", []byte("hello world")) |
| 46 | switch err { |
| 47 | case nil: |
| 48 | case context.Canceled: |
| 49 | return |
| 50 | default: |
| 51 | t.Error(err) |
| 52 | cancel() |
| 53 | return |
| 54 | } |
| 55 | select { |
| 56 | case <-ticker.C: |
| 57 | case <-ctx.Done(): |
| 58 | return |
| 59 | } |
| 60 | } |
| 61 | }() |
| 62 | |
| 63 | // Wait for the sender to finish before we return. |
| 64 | // Otherwise, we can get random errors as publish fails. |
| 65 | defer func() { |
| 66 | cancel() |
| 67 | <-done |
| 68 | }() |
| 69 | |
| 70 | m, err := sub.Next(ctx) |
| 71 | if err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | |
| 75 | if string(m.Data()) != "hello world" { |
| 76 | t.Errorf("got invalid data: %s", string(m.Data())) |
| 77 | } |
| 78 | |
| 79 | self1, err := apis[1].Key().Self(ctx) |
| 80 | if err != nil { |
| 81 | t.Fatal(err) |
| 82 | } |
| 83 | |
| 84 | if m.From() != self1.ID() { |
| 85 | t.Errorf("m.From didn't match") |
| 86 | } |
| 87 | |
| 88 | peers, err := apis[1].PubSub().Peers(ctx, options.PubSub.Topic("testch")) |
| 89 | if err != nil { |
| 90 | t.Fatal(err) |
| 91 | } |
| 92 | |
| 93 | if len(peers) != 1 { |
| 94 | t.Fatalf("got incorrect number of peers: %d", len(peers)) |
| 95 | } |
| 96 | |
| 97 | self0, err := apis[0].Key().Self(ctx) |
| 98 | if err != nil { |
| 99 | t.Fatal(err) |
| 100 | } |
| 101 | |
| 102 | if peers[0] != self0.ID() { |
| 103 | t.Errorf("peer didn't match") |
| 104 | } |
| 105 | |
| 106 | peers, err = apis[1].PubSub().Peers(ctx, options.PubSub.Topic("nottestch")) |
| 107 | if err != nil { |
| 108 | t.Fatal(err) |
| 109 | } |
| 110 | |
| 111 | if len(peers) != 0 { |
| 112 | t.Fatalf("got incorrect number of peers: %d", len(peers)) |
| 113 | } |
| 114 | |
| 115 | topics, err := apis[0].PubSub().Ls(ctx) |
| 116 | if err != nil { |
| 117 | t.Fatal(err) |
| 118 | } |
| 119 | |
| 120 | if len(topics) != 1 { |
| 121 | t.Fatalf("got incorrect number of topics: %d", len(peers)) |
| 122 | } |
| 123 | |
| 124 | if topics[0] != "testch" { |
| 125 | t.Errorf("topic didn't match") |
| 126 | } |
| 127 | |
| 128 | topics, err = apis[1].PubSub().Ls(ctx) |
| 129 | if err != nil { |
| 130 | t.Fatal(err) |
| 131 | } |
| 132 | |
| 133 | if len(topics) != 0 { |
| 134 | t.Fatalf("got incorrect number of topics: %d", len(peers)) |
| 135 | } |
| 136 | } |