2023-08-30 11:57:22 +00:00
|
|
|
package peermanager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
|
|
|
libp2pProtocol "github.com/libp2p/go-libp2p/core/protocol"
|
|
|
|
"github.com/stretchr/testify/require"
|
2024-01-26 08:45:15 +00:00
|
|
|
"golang.org/x/exp/maps"
|
2023-08-30 11:57:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestServiceSlot(t *testing.T) {
|
|
|
|
slots := NewServiceSlot()
|
|
|
|
|
2024-03-18 12:12:36 +00:00
|
|
|
protocol := libp2pProtocol.ID("/vac/waku/test/2.0.0")
|
2023-08-30 11:57:22 +00:00
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
peerID := peer.ID("peerId")
|
2023-08-30 11:57:22 +00:00
|
|
|
|
|
|
|
//
|
2023-09-11 14:24:05 +00:00
|
|
|
slots.getPeers(protocol).add(peerID)
|
2023-08-30 11:57:22 +00:00
|
|
|
//
|
2024-01-26 08:45:15 +00:00
|
|
|
fetchedPeers, err := slots.getPeers(protocol).getRandom(1)
|
2023-08-30 11:57:22 +00:00
|
|
|
require.NoError(t, err)
|
2024-01-26 08:45:15 +00:00
|
|
|
require.Equal(t, peerID, maps.Keys(fetchedPeers)[0])
|
2023-08-30 11:57:22 +00:00
|
|
|
//
|
2023-09-11 14:24:05 +00:00
|
|
|
slots.getPeers(protocol).remove(peerID)
|
2023-08-30 11:57:22 +00:00
|
|
|
//
|
2024-01-26 08:45:15 +00:00
|
|
|
_, err = slots.getPeers(protocol).getRandom(1)
|
2023-10-16 16:42:01 +00:00
|
|
|
require.Equal(t, err, ErrNoPeersAvailable)
|
2024-03-18 08:22:20 +00:00
|
|
|
|
|
|
|
// Test with more peers
|
|
|
|
peerID2 := peer.ID("peerId2")
|
|
|
|
peerID3 := peer.ID("peerId3")
|
|
|
|
|
|
|
|
//
|
|
|
|
slots.getPeers(protocol).add(peerID2)
|
|
|
|
slots.getPeers(protocol).add(peerID3)
|
|
|
|
//
|
|
|
|
|
|
|
|
fetchedPeers, err = slots.getPeers(protocol).getRandom(2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 2, len(maps.Keys(fetchedPeers)))
|
|
|
|
|
|
|
|
// Check for uniqueness
|
|
|
|
require.NotEqual(t, maps.Keys(fetchedPeers)[0], maps.Keys(fetchedPeers)[1])
|
|
|
|
|
|
|
|
slots.getPeers(protocol).remove(peerID2)
|
|
|
|
|
|
|
|
fetchedPeers, err = slots.getPeers(protocol).getRandom(10)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, peerID3, maps.Keys(fetchedPeers)[0])
|
|
|
|
|
2023-08-30 11:57:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestServiceSlotRemovePeerFromAll(t *testing.T) {
|
|
|
|
slots := NewServiceSlot()
|
|
|
|
|
2024-03-18 12:12:36 +00:00
|
|
|
protocol := libp2pProtocol.ID("/vac/waku/test/2.0.0")
|
|
|
|
protocol1 := libp2pProtocol.ID("/vac/waku/test/2.0.2")
|
2023-08-30 11:57:22 +00:00
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
peerID := peer.ID("peerId")
|
2023-08-30 11:57:22 +00:00
|
|
|
|
|
|
|
//
|
2023-09-11 14:24:05 +00:00
|
|
|
slots.getPeers(protocol).add(peerID)
|
|
|
|
slots.getPeers(protocol1).add(peerID)
|
2023-08-30 11:57:22 +00:00
|
|
|
//
|
2024-01-26 08:45:15 +00:00
|
|
|
fetchedPeers, err := slots.getPeers(protocol1).getRandom(1)
|
2023-08-30 11:57:22 +00:00
|
|
|
require.NoError(t, err)
|
2024-01-26 08:45:15 +00:00
|
|
|
require.Equal(t, peerID, maps.Keys(fetchedPeers)[0])
|
2023-08-30 11:57:22 +00:00
|
|
|
|
|
|
|
//
|
2023-09-11 14:24:05 +00:00
|
|
|
slots.removePeer(peerID)
|
2023-08-30 11:57:22 +00:00
|
|
|
//
|
2024-01-26 08:45:15 +00:00
|
|
|
_, err = slots.getPeers(protocol).getRandom(1)
|
2023-10-16 16:42:01 +00:00
|
|
|
require.Equal(t, err, ErrNoPeersAvailable)
|
2024-01-26 08:45:15 +00:00
|
|
|
_, err = slots.getPeers(protocol1).getRandom(1)
|
2023-10-16 16:42:01 +00:00
|
|
|
require.Equal(t, err, ErrNoPeersAvailable)
|
2023-08-30 11:57:22 +00:00
|
|
|
}
|