2021-10-22 14:19:38 +00:00
|
|
|
package relay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-11-09 19:53:01 +00:00
|
|
|
"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"
|
2022-11-09 19:53:01 +00:00
|
|
|
"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
|
|
|
|
2021-10-28 12:41:17 +00:00
|
|
|
port, err := tests.FindFreePort(t, "", 5)
|
2021-10-22 14:19:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-10-28 12:41:17 +00:00
|
|
|
host, err := tests.MakeHost(context.Background(), port, rand.Reader)
|
2021-10-22 14:19:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-12-09 03:08:04 +00:00
|
|
|
relay, err := NewWakuRelay(context.Background(), host, nil, 0, timesource.NewDefaultClock(), utils.Logger())
|
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()
|
|
|
|
}
|