2021-10-29 15:24:31 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-11-23 15:03:12 +00:00
|
|
|
"sync"
|
2021-10-29 15:24:31 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p"
|
2021-11-24 20:11:24 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2021-10-29 15:24:31 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peerstore"
|
2022-01-18 18:17:06 +00:00
|
|
|
"github.com/status-im/go-waku/tests"
|
2021-10-29 15:24:31 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestKeepAlive(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2022-03-22 13:12:58 +00:00
|
|
|
host1, err := libp2p.New(libp2p.DefaultTransports, libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0"))
|
2021-10-29 15:24:31 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-22 13:12:58 +00:00
|
|
|
host2, err := libp2p.New(libp2p.DefaultTransports, libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0"))
|
2021-10-29 15:24:31 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
host1.Peerstore().AddAddrs(host2.ID(), host2.Addrs(), peerstore.PermanentAddrTTL)
|
|
|
|
|
|
|
|
err = host1.Connect(ctx, host1.Peerstore().PeerInfo(host2.ID()))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Len(t, host1.Network().Peers(), 1)
|
|
|
|
|
|
|
|
ctx2, cancel2 := context.WithTimeout(ctx, 3*time.Second)
|
|
|
|
defer cancel2()
|
2021-11-23 15:03:12 +00:00
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
|
2021-11-24 20:11:24 +00:00
|
|
|
w := &WakuNode{
|
|
|
|
host: host1,
|
|
|
|
ctx: ctx2,
|
|
|
|
wg: wg,
|
2022-01-18 18:17:06 +00:00
|
|
|
log: tests.Logger(),
|
2021-11-24 20:11:24 +00:00
|
|
|
keepAliveMutex: sync.Mutex{},
|
|
|
|
keepAliveFails: make(map[peer.ID]int),
|
|
|
|
}
|
|
|
|
|
|
|
|
w.wg.Add(1)
|
|
|
|
w.pingPeer(host2.ID())
|
2021-10-29 15:24:31 +00:00
|
|
|
|
|
|
|
require.NoError(t, ctx.Err())
|
|
|
|
}
|