Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
✅ Add unit tests for peer selection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nazarhussain committed Apr 16, 2021
1 parent e468fb6 commit 585bac1
Showing 1 changed file with 163 additions and 3 deletions.
166 changes: 163 additions & 3 deletions elements/lisk-p2p/test/unit/utils/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
selectPeersForSend,
} from '../../../src/utils/select';
import { P2PNodeInfo, P2PPeerInfo } from '../../../src/types';
import { DEFAULT_SEND_PEER_LIMIT } from '../../../src/constants';
import { ConnectionKind, PeerKind, DEFAULT_SEND_PEER_LIMIT } from '../../../src/constants';

describe('peer selector', () => {
const nodeInfo: P2PNodeInfo = {
Expand Down Expand Up @@ -137,9 +137,8 @@ describe('peer selector', () => {
});

describe('#selectPeersForSend', () => {
const peerList = initPeerInfoListWithSuffix('111.112.113', 120);

it('should return an array containing an even number of inbound and outbound peers', () => {
const peerList = initPeerInfoListWithSuffix('111.112.113', 120);
const selectedPeers = selectPeersForSend({
peers: peerList,
nodeInfo,
Expand All @@ -164,6 +163,167 @@ describe('peer selector', () => {
expect(peerKindCounts.inbound).toEqual(peerKindCounts.outbound);
expect(peerKindCounts.inbound).toBe(DEFAULT_SEND_PEER_LIMIT / 2);
});

it('should return an array containing more inbound peers and if outbound peers are less than half', () => {
const inboundPeers = initPeerInfoListWithSuffix(
'111.112.113',
DEFAULT_SEND_PEER_LIMIT / 2 - 2,
).map(
p =>
({
...p,
internalState: { ...p.internalState, connectionKind: ConnectionKind.INBOUND },
} as P2PPeerInfo),
);

const outboundPeers = initPeerInfoListWithSuffix(
'111.112.114',
DEFAULT_SEND_PEER_LIMIT / 2 + 2,
).map(
p =>
({
...p,
internalState: { ...p.internalState, connectionKind: ConnectionKind.OUTBOUND },
} as P2PPeerInfo),
);

const selectedPeers = selectPeersForSend({
peers: [...inboundPeers, ...outboundPeers],
nodeInfo,
peerLimit: DEFAULT_SEND_PEER_LIMIT,
messagePacket: { event: 'foo', data: {} },
});
const selectedInboundPeers = selectedPeers.filter(
p => p.internalState?.connectionKind === ConnectionKind.INBOUND,
);
const selectedOutboundPeers = selectedPeers.filter(
p => p.internalState?.connectionKind === ConnectionKind.OUTBOUND,
);

// Assert
expect(selectedPeers).toHaveLength(DEFAULT_SEND_PEER_LIMIT);
expect(selectedInboundPeers).toHaveLength(DEFAULT_SEND_PEER_LIMIT / 2 - 2);
expect(selectedOutboundPeers).toHaveLength(DEFAULT_SEND_PEER_LIMIT / 2 + 2);
});

it('should return an array containing more outbound peers and if inbound peers are less than half', () => {
const inboundPeers = initPeerInfoListWithSuffix(
'111.112.113',
DEFAULT_SEND_PEER_LIMIT / 2 + 2,
).map(
p =>
({
...p,
internalState: { ...p.internalState, connectionKind: ConnectionKind.INBOUND },
} as P2PPeerInfo),
);

const outboundPeers = initPeerInfoListWithSuffix(
'111.112.114',
DEFAULT_SEND_PEER_LIMIT / 2 - 2,
).map(
p =>
({
...p,
internalState: { ...p.internalState, connectionKind: ConnectionKind.OUTBOUND },
} as P2PPeerInfo),
);

const selectedPeers = selectPeersForSend({
peers: [...inboundPeers, ...outboundPeers],
nodeInfo,
peerLimit: DEFAULT_SEND_PEER_LIMIT,
messagePacket: { event: 'foo', data: {} },
});
const selectedInboundPeers = selectedPeers.filter(
p => p.internalState?.connectionKind === ConnectionKind.INBOUND,
);
const selectedOutboundPeers = selectedPeers.filter(
p => p.internalState?.connectionKind === ConnectionKind.OUTBOUND,
);

// Assert
expect(selectedPeers).toHaveLength(DEFAULT_SEND_PEER_LIMIT);
expect(selectedInboundPeers).toHaveLength(DEFAULT_SEND_PEER_LIMIT / 2 + 2);
expect(selectedOutboundPeers).toHaveLength(DEFAULT_SEND_PEER_LIMIT / 2 - 2);
});

it('should return an array must containing fixed peers when original list is more than limit', () => {
const peerList = initPeerInfoListWithSuffix('111.112.113', 120);
const fixedPeers = initPeerInfoListWithSuffix('111.112.114', 5).map(
p =>
({
...p,
internalState: { ...p.internalState, peerKind: PeerKind.FIXED_PEER },
} as P2PPeerInfo),
);

const selectedPeers = selectPeersForSend({
peers: [...peerList, ...fixedPeers],
nodeInfo,
peerLimit: DEFAULT_SEND_PEER_LIMIT,
messagePacket: { event: 'foo', data: {} },
});

const selectedFixedPeers = selectedPeers.filter(
p => p.internalState?.peerKind === PeerKind.FIXED_PEER,
);

// Assert
expect(selectedPeers.length).toBeGreaterThanOrEqual(DEFAULT_SEND_PEER_LIMIT);
expect(selectedFixedPeers).toHaveLength(5);
});

it('should return an array must containing fixed peers when original list is less than limit', () => {
const peerList = initPeerInfoListWithSuffix('111.112.113', 4);
const fixedPeers = initPeerInfoListWithSuffix('111.112.114', 5).map(
p =>
({
...p,
internalState: { ...p.internalState, peerKind: PeerKind.FIXED_PEER },
} as P2PPeerInfo),
);

const selectedPeers = selectPeersForSend({
peers: [...peerList, ...fixedPeers],
nodeInfo,
peerLimit: DEFAULT_SEND_PEER_LIMIT,
messagePacket: { event: 'foo', data: {} },
});

const selectedFixedPeers = selectedPeers.filter(
p => p.internalState?.peerKind === PeerKind.FIXED_PEER,
);

// Assert
expect(selectedPeers.length).toBeLessThanOrEqual(9);
expect(selectedFixedPeers).toHaveLength(5);
});

it('should return an unique array if fixed peers are also randomly selected as inbound or outbound', () => {
const peerList = [...initPeerInfoListWithSuffix('111.112.113', 20)];
for (let i = 0; i < 10; i += 1) {
peerList[i] = {
...peerList[i],
internalState: { ...peerList[i].internalState, peerKind: PeerKind.FIXED_PEER },
} as any;
}

const selectedPeers = selectPeersForSend({
peers: peerList,
nodeInfo,
peerLimit: DEFAULT_SEND_PEER_LIMIT,
messagePacket: { event: 'foo', data: {} },
});

const selectedFixedPeers = selectedPeers.filter(
p => p.internalState?.peerKind === PeerKind.FIXED_PEER,
);

// Assert
expect(selectedPeers.length).toBeGreaterThanOrEqual(DEFAULT_SEND_PEER_LIMIT);
expect(selectedFixedPeers).toHaveLength(10);
});
});

describe('#selectPeersForConnection', () => {
Expand Down

0 comments on commit 585bac1

Please sign in to comment.