2021-11-02 00:04:54 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
2021-11-04 18:33:55 +00:00
|
|
|
"time"
|
2021-11-02 00:04:54 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/persistence"
|
|
|
|
"github.com/waku-org/go-waku/waku/persistence/sqlite"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/utils"
|
2021-11-02 00:04:54 +00:00
|
|
|
)
|
|
|
|
|
2022-06-10 12:15:00 +00:00
|
|
|
func goCheckConnectedness(t *testing.T, wg *sync.WaitGroup, connStatusChan chan ConnStatus, clientNode *WakuNode, node *WakuNode, nodeShouldBeConnected bool, shouldBeOnline bool, shouldHaveHistory bool, expectedPeers int) {
|
2021-11-04 18:33:55 +00:00
|
|
|
wg.Add(1)
|
2022-06-10 12:15:00 +00:00
|
|
|
go checkConnectedness(t, wg, connStatusChan, clientNode, node, nodeShouldBeConnected, shouldBeOnline, shouldHaveHistory, expectedPeers)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkConnectedness(t *testing.T, wg *sync.WaitGroup, connStatusChan chan ConnStatus, clientNode *WakuNode, node *WakuNode, nodeShouldBeConnected bool, shouldBeOnline bool, shouldHaveHistory bool, expectedPeers int) {
|
2021-11-04 18:33:55 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
timeout := time.After(5 * time.Second)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case connStatus := <-connStatusChan:
|
|
|
|
_, ok := connStatus.Peers[node.Host().ID()]
|
|
|
|
if (nodeShouldBeConnected && ok) || (!nodeShouldBeConnected && !ok) {
|
|
|
|
// Only execute the test when the node is connected or disconnected and it does not appear in the map returned by the connection status channel
|
|
|
|
require.True(t, connStatus.IsOnline == shouldBeOnline)
|
|
|
|
require.True(t, connStatus.HasHistory == shouldHaveHistory)
|
|
|
|
require.Len(t, clientNode.Host().Network().Peers(), expectedPeers)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-timeout:
|
|
|
|
require.Fail(t, "node should have connected")
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-02 00:04:54 +00:00
|
|
|
func TestConnectionStatusChanges(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2021-11-04 18:33:55 +00:00
|
|
|
connStatusChan := make(chan ConnStatus, 100)
|
2021-11-02 00:04:54 +00:00
|
|
|
|
|
|
|
// Node1: Only Relay
|
|
|
|
hostAddr1, err := net.ResolveTCPAddr("tcp", "0.0.0.0:0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
node1, err := New(ctx,
|
2021-11-17 16:19:42 +00:00
|
|
|
WithHostAddress(hostAddr1),
|
2021-11-02 00:04:54 +00:00
|
|
|
WithWakuRelay(),
|
2021-11-05 20:09:48 +00:00
|
|
|
WithConnectionStatusChannel(connStatusChan),
|
2021-11-02 00:04:54 +00:00
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = node1.Start()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Node2: Relay
|
|
|
|
hostAddr2, err := net.ResolveTCPAddr("tcp", "0.0.0.0:0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
node2, err := New(ctx,
|
2021-11-17 16:19:42 +00:00
|
|
|
WithHostAddress(hostAddr2),
|
2021-11-02 00:04:54 +00:00
|
|
|
WithWakuRelay(),
|
|
|
|
)
|
2021-11-02 00:12:18 +00:00
|
|
|
require.NoError(t, err)
|
2021-11-02 00:04:54 +00:00
|
|
|
err = node2.Start()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-30 18:48:22 +00:00
|
|
|
db, err := sqlite.NewDB(":memory:")
|
|
|
|
require.NoError(t, err)
|
|
|
|
dbStore, err := persistence.NewDBStore(utils.Logger(), persistence.WithDB(db))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-11-02 00:04:54 +00:00
|
|
|
// Node3: Relay + Store
|
|
|
|
hostAddr3, err := net.ResolveTCPAddr("tcp", "0.0.0.0:0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
node3, err := New(ctx,
|
2021-11-17 16:19:42 +00:00
|
|
|
WithHostAddress(hostAddr3),
|
2021-11-02 00:04:54 +00:00
|
|
|
WithWakuRelay(),
|
2022-11-25 20:54:11 +00:00
|
|
|
WithWakuStore(false, nil),
|
2022-05-30 18:48:22 +00:00
|
|
|
WithMessageProvider(dbStore),
|
2021-11-02 00:04:54 +00:00
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = node3.Start()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2022-06-10 12:15:00 +00:00
|
|
|
goCheckConnectedness(t, &wg, connStatusChan, node1, node2, true, true, false, 1)
|
2021-11-02 00:04:54 +00:00
|
|
|
|
2021-11-04 18:33:55 +00:00
|
|
|
err = node1.DialPeer(ctx, node2.ListenAddresses()[0].String())
|
|
|
|
require.NoError(t, err)
|
2021-11-02 00:04:54 +00:00
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
2022-06-10 12:15:00 +00:00
|
|
|
goCheckConnectedness(t, &wg, connStatusChan, node1, node3, true, true, true, 2)
|
2021-11-02 00:04:54 +00:00
|
|
|
|
2021-11-04 18:33:55 +00:00
|
|
|
err = node1.DialPeer(ctx, node3.ListenAddresses()[0].String())
|
|
|
|
require.NoError(t, err)
|
2021-11-02 00:12:18 +00:00
|
|
|
|
2022-06-10 12:15:00 +00:00
|
|
|
goCheckConnectedness(t, &wg, connStatusChan, node1, node3, false, true, false, 1)
|
2021-11-04 18:33:55 +00:00
|
|
|
|
|
|
|
node3.Stop()
|
2021-11-02 00:04:54 +00:00
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
2022-06-10 12:15:00 +00:00
|
|
|
goCheckConnectedness(t, &wg, connStatusChan, node1, node2, false, false, false, 0)
|
2021-11-02 00:04:54 +00:00
|
|
|
|
2021-11-04 18:33:55 +00:00
|
|
|
err = node1.ClosePeerById(node2.Host().ID())
|
|
|
|
require.NoError(t, err)
|
|
|
|
wg.Wait()
|
2021-11-02 00:12:18 +00:00
|
|
|
|
2022-06-10 12:15:00 +00:00
|
|
|
goCheckConnectedness(t, &wg, connStatusChan, node1, node2, true, true, false, 1)
|
2021-11-02 00:04:54 +00:00
|
|
|
|
2021-11-04 18:33:55 +00:00
|
|
|
err = node1.DialPeerByID(ctx, node2.Host().ID())
|
|
|
|
require.NoError(t, err)
|
2021-11-02 00:04:54 +00:00
|
|
|
wg.Wait()
|
|
|
|
}
|