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"
|
2022-10-19 19:39:32 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
|
|
|
"github.com/libp2p/go-libp2p/core/peerstore"
|
2021-10-29 15:24:31 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/utils"
|
2021-10-29 15:24:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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,
|
|
|
|
wg: wg,
|
2022-05-06 19:29:31 +00:00
|
|
|
log: utils.Logger(),
|
2021-11-24 20:11:24 +00:00
|
|
|
keepAliveMutex: sync.Mutex{},
|
|
|
|
keepAliveFails: make(map[peer.ID]int),
|
|
|
|
}
|
|
|
|
|
|
|
|
w.wg.Add(1)
|
2023-01-26 20:08:27 +00:00
|
|
|
w.pingPeer(ctx2, w.wg, host2.ID())
|
2021-10-29 15:24:31 +00:00
|
|
|
|
|
|
|
require.NoError(t, ctx.Err())
|
|
|
|
}
|