go-waku/waku/v2/rpc/relay_test.go

130 lines
2.6 KiB
Go
Raw Normal View History

package rpc
import (
"context"
2021-11-18 14:20:58 +00:00
"fmt"
"testing"
2021-11-18 14:20:58 +00:00
"time"
2021-11-18 14:20:58 +00:00
"github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/require"
"github.com/waku-org/go-waku/waku/v2/node"
"github.com/waku-org/go-waku/waku/v2/utils"
)
func makeRelayService(t *testing.T) *RelayService {
options := node.WithWakuRelayAndMinPeers(0)
n, err := node.New(context.Background(), options)
require.NoError(t, err)
err = n.Start()
require.NoError(t, err)
2022-07-07 21:58:07 +00:00
return NewRelayService(n, 30, utils.Logger())
}
func TestPostV1Message(t *testing.T) {
var reply SuccessReply
d := makeRelayService(t)
err := d.PostV1Message(
makeRequest(t),
&RelayMessageArgs{},
&reply,
)
require.NoError(t, err)
2022-06-14 15:36:34 +00:00
require.True(t, reply)
}
2021-11-17 16:04:54 +00:00
func TestRelaySubscription(t *testing.T) {
var reply SuccessReply
d := makeRelayService(t)
args := &TopicsArgs{Topics: []string{"test"}}
err := d.PostV1Subscription(
makeRequest(t),
args,
&reply,
)
require.NoError(t, err)
2022-06-14 15:36:34 +00:00
require.True(t, reply)
err = d.DeleteV1Subscription(
makeRequest(t),
args,
&reply,
)
require.NoError(t, err)
2022-06-14 15:36:34 +00:00
require.True(t, reply)
}
2021-11-18 14:20:58 +00:00
func TestRelayGetV1Messages(t *testing.T) {
serviceA := makeRelayService(t)
var reply SuccessReply
serviceB := makeRelayService(t)
go serviceB.Start()
defer serviceB.Stop()
hostInfo, err := multiaddr.NewMultiaddr(fmt.Sprintf("/p2p/%s", serviceB.node.Host().ID().Pretty()))
require.NoError(t, err)
var addr multiaddr.Multiaddr
for _, a := range serviceB.node.Host().Addrs() {
addr = a.Encapsulate(hostInfo)
break
}
err = serviceA.node.DialPeerWithMultiAddress(context.Background(), addr)
require.NoError(t, err)
// Wait for the dial to complete
time.Sleep(1 * time.Second)
args := &TopicsArgs{Topics: []string{"test"}}
err = serviceB.PostV1Subscription(
makeRequest(t),
args,
&reply,
)
require.NoError(t, err)
2022-06-14 15:36:34 +00:00
require.True(t, reply)
2021-11-18 14:20:58 +00:00
// Wait for the subscription to be started
time.Sleep(1 * time.Second)
err = serviceA.PostV1Message(
makeRequest(t),
&RelayMessageArgs{
Topic: "test",
2022-06-13 18:30:35 +00:00
Message: RPCWakuRelayMessage{
2021-11-18 14:20:58 +00:00
Payload: []byte("test"),
},
},
&reply,
)
require.NoError(t, err)
2022-06-14 15:36:34 +00:00
require.True(t, reply)
2021-11-18 14:20:58 +00:00
// Wait for the message to be received
time.Sleep(1 * time.Second)
2022-06-13 18:30:35 +00:00
var messagesReply1 RelayMessagesReply
2021-11-18 14:20:58 +00:00
err = serviceB.GetV1Messages(
makeRequest(t),
&TopicArgs{"test"},
2022-06-13 18:30:35 +00:00
&messagesReply1,
2021-11-18 14:20:58 +00:00
)
require.NoError(t, err)
2022-06-13 18:30:35 +00:00
require.Len(t, messagesReply1, 1)
2021-11-18 14:20:58 +00:00
2022-06-13 18:30:35 +00:00
var messagesReply2 RelayMessagesReply
2021-11-18 14:20:58 +00:00
err = serviceB.GetV1Messages(
makeRequest(t),
&TopicArgs{"test"},
2022-06-13 18:30:35 +00:00
&messagesReply2,
2021-11-18 14:20:58 +00:00
)
require.NoError(t, err)
2022-06-13 18:30:35 +00:00
require.Len(t, messagesReply2, 0)
2021-11-18 14:20:58 +00:00
}