go-waku/waku/v2/protocol/relay/waku_relay_test.go

53 lines
1.1 KiB
Go
Raw Normal View History

2021-10-22 14:19:38 +00:00
package relay
import (
"context"
"crypto/rand"
"testing"
"github.com/status-im/go-waku/tests"
2021-10-22 14:19:38 +00:00
"github.com/status-im/go-waku/waku/v2/protocol/pb"
"github.com/stretchr/testify/require"
)
func TestWakuRelay(t *testing.T) {
2021-11-19 16:19:48 +00:00
testTopic := "/waku/2/go/relay/test"
2021-10-22 14:19:38 +00:00
port, err := tests.FindFreePort(t, "", 5)
2021-10-22 14:19:38 +00:00
require.NoError(t, err)
host, err := tests.MakeHost(context.Background(), port, rand.Reader)
2021-10-22 14:19:38 +00:00
require.NoError(t, err)
2021-11-01 14:42:55 +00:00
relay, err := NewWakuRelay(context.Background(), host, nil)
2021-10-22 14:19:38 +00:00
defer relay.Stop()
require.NoError(t, err)
2021-11-01 14:42:55 +00:00
sub, err := relay.subscribe(testTopic)
2021-10-22 14:19:38 +00:00
defer sub.Cancel()
require.NoError(t, err)
topics := relay.Topics()
require.Equal(t, 1, len(topics))
require.Equal(t, testTopic, topics[0])
ctx, cancel := context.WithCancel(context.Background())
go func() {
defer cancel()
_, err := sub.Next(ctx)
require.NoError(t, err)
}()
msg := &pb.WakuMessage{
Payload: []byte{1},
Version: 0,
ContentTopic: "test",
Timestamp: 0,
}
2021-11-20 00:03:05 +00:00
_, err = relay.PublishToTopic(context.Background(), msg, testTopic)
2021-10-22 14:19:38 +00:00
require.NoError(t, err)
<-ctx.Done()
}