mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-21 18:37:45 +08:00
coreapi pubsub: add tests
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
parent
51bb9d6843
commit
d3f3afa5de
@ -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 {
|
||||
|
||||
96
core/coreapi/pubsub_test.go
Normal file
96
core/coreapi/pubsub_test.go
Normal file
@ -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))
|
||||
}
|
||||
}
|
||||
@ -94,6 +94,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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user