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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestServiceSlot(t *testing.T) {
|
|
|
|
slots := NewServiceSlot()
|
|
|
|
|
|
|
|
protocol := libp2pProtocol.ID("test/protocol")
|
|
|
|
|
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
|
|
|
//
|
|
|
|
fetchedPeer, err := slots.getPeers(protocol).getRandom()
|
|
|
|
require.NoError(t, err)
|
2023-09-11 14:24:05 +00:00
|
|
|
require.Equal(t, peerID, fetchedPeer)
|
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
|
|
|
//
|
|
|
|
_, err = slots.getPeers(protocol).getRandom()
|
2023-10-16 16:42:01 +00:00
|
|
|
require.Equal(t, err, ErrNoPeersAvailable)
|
2023-08-30 11:57:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestServiceSlotRemovePeerFromAll(t *testing.T) {
|
|
|
|
slots := NewServiceSlot()
|
|
|
|
|
|
|
|
protocol := libp2pProtocol.ID("test/protocol")
|
|
|
|
protocol1 := libp2pProtocol.ID("test/protocol1")
|
|
|
|
|
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
|
|
|
//
|
|
|
|
fetchedPeer, err := slots.getPeers(protocol1).getRandom()
|
|
|
|
require.NoError(t, err)
|
2023-09-11 14:24:05 +00:00
|
|
|
require.Equal(t, peerID, fetchedPeer)
|
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
|
|
|
//
|
|
|
|
_, err = slots.getPeers(protocol).getRandom()
|
2023-10-16 16:42:01 +00:00
|
|
|
require.Equal(t, err, ErrNoPeersAvailable)
|
2023-08-30 11:57:22 +00:00
|
|
|
_, err = slots.getPeers(protocol1).getRandom()
|
2023-10-16 16:42:01 +00:00
|
|
|
require.Equal(t, err, ErrNoPeersAvailable)
|
2023-08-30 11:57:22 +00:00
|
|
|
}
|