2021-11-09 23:34:04 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-10-19 19:39:32 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peerstore"
|
2023-02-16 16:17:52 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/protocol"
|
2021-11-09 23:34:04 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/tests"
|
2021-11-09 23:34:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSelectPeer(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
h1, err := tests.MakeHost(ctx, 0, rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer h1.Close()
|
|
|
|
|
|
|
|
h2, err := tests.MakeHost(ctx, 0, rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer h2.Close()
|
|
|
|
|
|
|
|
h3, err := tests.MakeHost(ctx, 0, rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer h3.Close()
|
|
|
|
|
2023-02-16 16:17:52 +00:00
|
|
|
proto := protocol.ID("test/protocol")
|
2021-11-09 23:34:04 +00:00
|
|
|
|
|
|
|
h1.Peerstore().AddAddrs(h2.ID(), h2.Network().ListenAddresses(), peerstore.PermanentAddrTTL)
|
|
|
|
h1.Peerstore().AddAddrs(h3.ID(), h2.Network().ListenAddresses(), peerstore.PermanentAddrTTL)
|
|
|
|
|
|
|
|
// No peers with selected protocol
|
2022-11-24 21:50:43 +00:00
|
|
|
_, err = SelectPeer(h1, proto, nil, Logger())
|
2021-11-09 23:34:04 +00:00
|
|
|
require.Error(t, ErrNoPeersAvailable, err)
|
|
|
|
|
|
|
|
// Peers with selected protocol
|
|
|
|
_ = h1.Peerstore().AddProtocols(h2.ID(), proto)
|
|
|
|
_ = h1.Peerstore().AddProtocols(h3.ID(), proto)
|
|
|
|
|
2022-11-24 21:50:43 +00:00
|
|
|
_, err = SelectPeerWithLowestRTT(ctx, h1, proto, nil, Logger())
|
2021-11-09 23:34:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSelectPeerWithLowestRTT(t *testing.T) {
|
|
|
|
// help-wanted: how to slowdown the ping response to properly test the rtt
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
h1, err := tests.MakeHost(ctx, 0, rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer h1.Close()
|
|
|
|
|
|
|
|
h2, err := tests.MakeHost(ctx, 0, rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer h2.Close()
|
|
|
|
|
|
|
|
h3, err := tests.MakeHost(ctx, 0, rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer h3.Close()
|
|
|
|
|
2023-02-16 16:17:52 +00:00
|
|
|
proto := protocol.ID("test/protocol")
|
2021-11-09 23:34:04 +00:00
|
|
|
|
|
|
|
h1.Peerstore().AddAddrs(h2.ID(), h2.Network().ListenAddresses(), peerstore.PermanentAddrTTL)
|
|
|
|
h1.Peerstore().AddAddrs(h3.ID(), h2.Network().ListenAddresses(), peerstore.PermanentAddrTTL)
|
|
|
|
|
|
|
|
// No peers with selected protocol
|
2022-11-24 21:50:43 +00:00
|
|
|
_, err = SelectPeerWithLowestRTT(ctx, h1, proto, nil, Logger())
|
2021-11-09 23:34:04 +00:00
|
|
|
require.Error(t, ErrNoPeersAvailable, err)
|
|
|
|
|
|
|
|
// Peers with selected protocol
|
|
|
|
_ = h1.Peerstore().AddProtocols(h2.ID(), proto)
|
|
|
|
_ = h1.Peerstore().AddProtocols(h3.ID(), proto)
|
|
|
|
|
2022-11-24 21:50:43 +00:00
|
|
|
_, err = SelectPeerWithLowestRTT(ctx, h1, proto, nil, Logger())
|
2021-11-09 23:34:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|