2021-11-09 14:15:45 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-11-18 14:20:58 +00:00
|
|
|
"fmt"
|
2021-11-09 14:15:45 +00:00
|
|
|
"testing"
|
2021-11-18 14:20:58 +00:00
|
|
|
"time"
|
2021-11-09 14:15:45 +00:00
|
|
|
|
2021-11-18 14:20:58 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
2021-11-09 14:15:45 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/node"
|
2021-11-18 14:20:58 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/pb"
|
2021-11-09 14:15:45 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func makeRelayService(t *testing.T) *RelayService {
|
2021-12-06 08:43:00 +00:00
|
|
|
options := node.WithWakuRelayAndMinPeers(0)
|
2021-11-09 14:15:45 +00:00
|
|
|
n, err := node.New(context.Background(), options)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = n.Start()
|
|
|
|
require.NoError(t, err)
|
2021-11-18 14:20:58 +00:00
|
|
|
return NewRelayService(n)
|
2021-11-09 14:15:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostV1Message(t *testing.T) {
|
|
|
|
var reply SuccessReply
|
|
|
|
|
|
|
|
d := makeRelayService(t)
|
|
|
|
|
|
|
|
err := d.PostV1Message(
|
|
|
|
makeRequest(t),
|
|
|
|
&RelayMessageArgs{},
|
|
|
|
&reply,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, reply.Success)
|
|
|
|
}
|
|
|
|
|
2021-11-17 16:04:54 +00:00
|
|
|
func TestRelaySubscription(t *testing.T) {
|
2021-11-09 14:15:45 +00:00
|
|
|
var reply SuccessReply
|
|
|
|
|
|
|
|
d := makeRelayService(t)
|
|
|
|
args := &TopicsArgs{Topics: []string{"test"}}
|
|
|
|
|
|
|
|
err := d.PostV1Subscription(
|
|
|
|
makeRequest(t),
|
|
|
|
args,
|
|
|
|
&reply,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, reply.Success)
|
|
|
|
|
|
|
|
err = d.DeleteV1Subscription(
|
|
|
|
makeRequest(t),
|
|
|
|
args,
|
|
|
|
&reply,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, reply.Success)
|
|
|
|
}
|
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)
|
|
|
|
require.True(t, reply.Success)
|
|
|
|
|
|
|
|
// Wait for the subscription to be started
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
err = serviceA.PostV1Message(
|
|
|
|
makeRequest(t),
|
|
|
|
&RelayMessageArgs{
|
|
|
|
Topic: "test",
|
|
|
|
Message: pb.WakuMessage{
|
|
|
|
Payload: []byte("test"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&reply,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, reply.Success)
|
|
|
|
|
|
|
|
// Wait for the message to be received
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
var messagesReply MessagesReply
|
|
|
|
err = serviceB.GetV1Messages(
|
|
|
|
makeRequest(t),
|
|
|
|
&TopicArgs{"test"},
|
|
|
|
&messagesReply,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, messagesReply.Messages, 1)
|
|
|
|
|
|
|
|
err = serviceB.GetV1Messages(
|
|
|
|
makeRequest(t),
|
|
|
|
&TopicArgs{"test"},
|
|
|
|
&messagesReply,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, messagesReply.Messages, 0)
|
|
|
|
}
|