-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracted CrawlConnectionEventHandlers + added unit tests. Refactored
Lag detection.
- Loading branch information
1 parent
826101f
commit 8c8497c
Showing
19 changed files
with
572 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/crawl-connection-event-handlers/__tests__/on-connected-handler.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { PeerNodeCollection } from '../../peer-node-collection'; | ||
import { mock } from 'jest-mock-extended'; | ||
import { ConnectedPayload, ConnectionManager } from '../../connection-manager'; | ||
import { CrawlQueueManager } from '../../crawl-queue-manager'; | ||
import { DisconnectTimeout } from '../../disconnect-timeout'; | ||
import { OnConnectedHandler } from '../on-connected-handler'; | ||
|
||
describe('OnConnectedHandler', () => { | ||
const connectionManager = mock<ConnectionManager>(); | ||
const crawlQueueManager = mock<CrawlQueueManager>(); | ||
const disconnectTimeout = mock<DisconnectTimeout>(); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should handle a successful connection', () => { | ||
const onConnectedHandler = new OnConnectedHandler( | ||
connectionManager, | ||
crawlQueueManager, | ||
disconnectTimeout | ||
); | ||
const data: ConnectedPayload = { | ||
ip: 'localhost', | ||
port: 11625, | ||
publicKey: 'publicKey', | ||
nodeInfo: { | ||
overlayVersion: 3, | ||
overlayMinVersion: 1, | ||
networkId: 'networkId', | ||
ledgerVersion: 2, | ||
versionString: 'versionString' | ||
} | ||
}; | ||
const peerNodes = mock<PeerNodeCollection>(); | ||
const topTierNodes: Set<string> = new Set(); | ||
const listenTimeouts = new Map(); | ||
const localTime = new Date(); | ||
onConnectedHandler.onConnected( | ||
data, | ||
peerNodes, | ||
topTierNodes, | ||
listenTimeouts, | ||
localTime | ||
); | ||
|
||
expect(peerNodes.addSuccessfullyConnected).toHaveBeenCalledWith( | ||
data.publicKey, | ||
data.ip, | ||
data.port, | ||
data.nodeInfo, | ||
localTime | ||
); | ||
expect(disconnectTimeout.start).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should handle a peer node error', () => { | ||
const onConnectedHandler = new OnConnectedHandler( | ||
connectionManager, | ||
crawlQueueManager, | ||
disconnectTimeout | ||
); | ||
const data: ConnectedPayload = { | ||
ip: 'localhost', | ||
port: 11625, | ||
publicKey: 'publicKey', | ||
nodeInfo: { | ||
overlayVersion: 3, | ||
overlayMinVersion: 1, | ||
networkId: 'networkId', | ||
ledgerVersion: 2, | ||
versionString: 'versionString' | ||
} | ||
}; | ||
const peerNodes = mock<PeerNodeCollection>(); | ||
const topTierNodes: Set<string> = new Set(); | ||
const listenTimeouts = new Map(); | ||
const localTime = new Date(); | ||
const error = new Error('error'); | ||
peerNodes.addSuccessfullyConnected.mockReturnValue(error); | ||
onConnectedHandler.onConnected( | ||
data, | ||
peerNodes, | ||
topTierNodes, | ||
listenTimeouts, | ||
localTime | ||
); | ||
|
||
expect(peerNodes.addSuccessfullyConnected).toHaveBeenCalledWith( | ||
data.publicKey, | ||
data.ip, | ||
data.port, | ||
data.nodeInfo, | ||
localTime | ||
); | ||
expect(connectionManager.disconnectByAddress).toHaveBeenCalledWith( | ||
`${data.ip}:${data.port}`, | ||
error | ||
); | ||
}); | ||
}); |
84 changes: 84 additions & 0 deletions
84
src/crawl-connection-event-handlers/__tests__/on-connection-close-handler.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { mock } from 'jest-mock-extended'; | ||
import { ConnectionManager } from '../../connection-manager'; | ||
import { CrawlQueueManager } from '../../crawl-queue-manager'; | ||
import { DisconnectTimeout } from '../../disconnect-timeout'; | ||
import { OnConnectionCloseHandler } from '../on-connection-close-handler'; | ||
import { CrawlState } from '../../crawl-state'; | ||
import { QuorumSetManager } from '../../quorum-set-manager'; | ||
import { PeerNodeCollection } from '../../peer-node-collection'; | ||
|
||
describe('OnConnectionCloseHandler', () => { | ||
const queueManager = mock<CrawlQueueManager>(); | ||
const quorumSetManager = mock<QuorumSetManager>(); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should cleanup a closed connection', () => { | ||
const onConnectionCloseHandler = new OnConnectionCloseHandler( | ||
quorumSetManager, | ||
queueManager | ||
); | ||
const address = 'localhost:11625'; | ||
const publicKey: string = 'publicKey'; | ||
const crawlState = mock<CrawlState>(); | ||
crawlState.listenTimeouts = new Map(); | ||
const spy = jest.spyOn(crawlState.listenTimeouts, 'delete'); | ||
crawlState.peerNodes = new PeerNodeCollection(); | ||
const peer = crawlState.peerNodes.addSuccessfullyConnected( | ||
publicKey, | ||
'localhost', | ||
11625, | ||
{ | ||
overlayVersion: 3, | ||
overlayMinVersion: 1, | ||
networkId: 'networkId', | ||
ledgerVersion: 2, | ||
versionString: 'versionString' | ||
}, | ||
new Date() | ||
); | ||
if (peer instanceof Error) { | ||
throw peer; | ||
} | ||
const localTime = new Date(); | ||
|
||
onConnectionCloseHandler.onConnectionClose( | ||
address, | ||
publicKey, | ||
crawlState, | ||
localTime | ||
); | ||
|
||
expect(quorumSetManager.onNodeDisconnected).toHaveBeenCalledWith( | ||
publicKey, | ||
crawlState | ||
); | ||
expect(queueManager.completeCrawlQueueTask).toHaveBeenCalledWith( | ||
crawlState.crawlQueueTaskDoneCallbacks, | ||
address | ||
); | ||
expect(spy).toHaveBeenCalledWith(publicKey); | ||
|
||
expect(peer.disconnected).toBe(true); | ||
expect(peer.disconnectionTime).toBe(localTime); | ||
}); | ||
|
||
it('should update failed connections', () => { | ||
const onConnectionCloseHandler = new OnConnectionCloseHandler( | ||
quorumSetManager, | ||
queueManager | ||
); | ||
const address = 'localhost:11625'; | ||
const crawlState = mock<CrawlState>(); | ||
crawlState.failedConnections = []; | ||
onConnectionCloseHandler.onConnectionClose( | ||
address, | ||
undefined, | ||
crawlState, | ||
new Date() | ||
); | ||
expect(crawlState.failedConnections).toEqual([address]); | ||
}); | ||
}); |
79 changes: 79 additions & 0 deletions
79
src/crawl-connection-event-handlers/__tests__/on-data-handler.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { OnDataHandler } from '../on-data-handler'; | ||
import { ConnectionManager, DataPayload } from '../../connection-manager'; | ||
import { StellarMessageHandler } from '../../stellar-message-handlers/stellar-message-handler'; | ||
import { mock } from 'jest-mock-extended'; | ||
import { P } from 'pino'; | ||
import { CrawlState } from '../../crawl-state'; | ||
import { createDummyExternalizeMessage } from '../../__fixtures__/createDummyExternalizeMessage'; | ||
import { err, ok } from 'neverthrow'; | ||
|
||
describe('OnDataHandler', () => { | ||
const connectionManager = mock<ConnectionManager>(); | ||
const stellarMessageHandler = mock<StellarMessageHandler>(); | ||
const logger = mock<P.Logger>(); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should handle data', () => { | ||
const onDataHandler = new OnDataHandler( | ||
connectionManager, | ||
stellarMessageHandler, | ||
logger | ||
); | ||
const data: DataPayload = { | ||
publicKey: 'publicKey', | ||
stellarMessageWork: { | ||
stellarMessage: createDummyExternalizeMessage(), | ||
done: jest.fn() | ||
}, | ||
address: 'address' | ||
}; | ||
const crawlState = mock<CrawlState>(); | ||
|
||
stellarMessageHandler.handleStellarMessage.mockReturnValue(ok(undefined)); | ||
onDataHandler.onData(data, crawlState); | ||
|
||
expect(stellarMessageHandler.handleStellarMessage).toHaveBeenCalledWith( | ||
data.publicKey, | ||
data.stellarMessageWork.stellarMessage, | ||
crawlState | ||
); | ||
expect(data.stellarMessageWork.done).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should handle data error', () => { | ||
const onDataHandler = new OnDataHandler( | ||
connectionManager, | ||
stellarMessageHandler, | ||
logger | ||
); | ||
const data: DataPayload = { | ||
publicKey: 'publicKey', | ||
stellarMessageWork: { | ||
stellarMessage: createDummyExternalizeMessage(), | ||
done: jest.fn() | ||
}, | ||
address: 'address' | ||
}; | ||
const crawlState = mock<CrawlState>(); | ||
|
||
stellarMessageHandler.handleStellarMessage.mockReturnValue( | ||
err(new Error('error')) | ||
); | ||
onDataHandler.onData(data, crawlState); | ||
|
||
expect(stellarMessageHandler.handleStellarMessage).toHaveBeenCalledWith( | ||
data.publicKey, | ||
data.stellarMessageWork.stellarMessage, | ||
crawlState | ||
); | ||
expect(data.stellarMessageWork.done).toHaveBeenCalled(); | ||
expect(logger.info).toHaveBeenCalledWith({ peer: data.publicKey }, 'error'); | ||
expect(connectionManager.disconnectByAddress).toHaveBeenCalledWith( | ||
data.address, | ||
new Error('error') | ||
); | ||
}); | ||
}); |
Oops, something went wrong.