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

56 lines
1.2 KiB
Go
Raw Normal View History

2021-10-22 14:19:38 +00:00
package relay
import (
"context"
"crypto/rand"
"testing"
"github.com/stretchr/testify/require"
"github.com/waku-org/go-waku/tests"
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
2022-12-09 03:08:04 +00:00
"github.com/waku-org/go-waku/waku/v2/timesource"
"github.com/waku-org/go-waku/waku/v2/utils"
2021-10-22 14:19:38 +00:00
)
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)
2023-01-06 22:37:57 +00:00
relay := NewWakuRelay(host, nil, 0, timesource.NewDefaultClock(), utils.Logger())
err = relay.Start(context.Background())
2021-10-22 14:19:38 +00:00
require.NoError(t, err)
2023-01-06 22:37:57 +00:00
defer relay.Stop()
2021-10-22 14:19:38 +00:00
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()
}