-
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 Timers from StragglerTimer. Added unit tests. Made
StraggleTimeoutMS configurable
- Loading branch information
1 parent
74f019e
commit cd1acfa
Showing
9 changed files
with
181 additions
and
23 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { mock } from 'jest-mock-extended'; | ||
import { P } from 'pino'; | ||
import { ConnectionManager } from '../connection-manager'; | ||
import { StragglerTimer } from '../straggler-timer'; | ||
import { Timers } from '../../utilities/timers'; | ||
|
||
describe('StragglerTimer', () => { | ||
const logger = mock<P.Logger>(); | ||
const connectionManager = mock<ConnectionManager>(); | ||
const timers = mock<Timers>(); | ||
const stragglerHandler = new StragglerTimer( | ||
connectionManager, | ||
timers, | ||
1000, | ||
logger | ||
); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should start straggler timeout', () => { | ||
const addresses = ['address1', 'address2']; | ||
stragglerHandler.startStragglerTimeout(addresses); | ||
const callback1 = timers.startTimer.mock.calls[0][1]; | ||
expect(timers.startTimer).toHaveBeenCalledTimes(1); | ||
callback1(); | ||
|
||
expect(connectionManager.disconnectByAddress).toHaveBeenCalledWith( | ||
'address1' | ||
); | ||
expect(connectionManager.disconnectByAddress).toHaveBeenCalledWith( | ||
'address2' | ||
); | ||
}); | ||
|
||
it('should not start timeout if addresses is empty', () => { | ||
stragglerHandler.startStragglerTimeout([]); | ||
expect(timers.startTimer).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should start straggler timeout for active non top tier peers', () => { | ||
connectionManager.getActiveConnectionAddresses.mockReturnValue([ | ||
'peerAddress', | ||
'topTierAddress' | ||
]); | ||
stragglerHandler.startStragglerTimeoutForActivePeers( | ||
false, | ||
new Set(['topTierAddress']) | ||
); | ||
const callback1 = timers.startTimer.mock.calls[0][1]; | ||
expect(timers.startTimer).toHaveBeenCalledTimes(1); | ||
callback1(); | ||
|
||
expect(connectionManager.disconnectByAddress).toHaveBeenCalledWith( | ||
'peerAddress' | ||
); | ||
expect(connectionManager.disconnectByAddress).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should start straggler timeout for active top tier peers', () => { | ||
connectionManager.getActiveConnectionAddresses.mockReturnValue([ | ||
'peerAddress', | ||
'topTierAddress' | ||
]); | ||
stragglerHandler.startStragglerTimeoutForActivePeers( | ||
true, | ||
new Set('topTierAddress') | ||
); | ||
const callback1 = timers.startTimer.mock.calls[0][1]; | ||
expect(timers.startTimer).toHaveBeenCalledTimes(1); | ||
callback1(); | ||
|
||
expect(connectionManager.disconnectByAddress).toHaveBeenCalledTimes(2); | ||
expect(connectionManager.disconnectByAddress).toHaveBeenCalledWith( | ||
'peerAddress' | ||
); | ||
expect(connectionManager.disconnectByAddress).toHaveBeenCalledWith( | ||
'topTierAddress' | ||
); | ||
}); | ||
}); |
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
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,41 @@ | ||
import { TimerFactory } from '../timer-factory'; | ||
import { Timers } from '../timers'; | ||
import { mock } from 'jest-mock-extended'; | ||
import { Timer } from '../timer'; | ||
|
||
describe('timers', () => { | ||
const timerFactory = mock<TimerFactory>(); | ||
const timers = new Timers(timerFactory); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should start timer', () => { | ||
const callback = jest.fn(); | ||
const timer = mock<Timer>(); | ||
timerFactory.createTimer.mockReturnValue(timer); | ||
timers.startTimer(1000, callback); | ||
|
||
const calledTime = timer.startTimer.mock.calls[0][0]; | ||
const timerCallback = timer.startTimer.mock.calls[0][1]; | ||
|
||
timerCallback(); | ||
|
||
expect(calledTime).toBe(1000); | ||
expect(timerFactory.createTimer).toHaveBeenCalled(); | ||
expect(timer.startTimer).toHaveBeenCalled(); | ||
expect(timers.hasActiveTimers()).toBeFalsy(); | ||
}); | ||
|
||
it('should stop timers', () => { | ||
const timer = mock<Timer>(); | ||
timerFactory.createTimer.mockReturnValue(timer); | ||
const callback = jest.fn(); | ||
timers.startTimer(1000, callback); | ||
timers.stopTimers(); | ||
expect(timer.stopTimer).toHaveBeenCalled(); | ||
expect(timers.hasActiveTimers()).toBeFalsy(); | ||
expect(callback).not.toHaveBeenCalled(); | ||
}); | ||
}); |
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,7 @@ | ||
import { Timer } from './timer'; | ||
|
||
export class TimerFactory { | ||
createTimer() { | ||
return new Timer(); | ||
} | ||
} |
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,27 @@ | ||
import { Timer } from './timer'; | ||
import { TimerFactory } from './timer-factory'; | ||
|
||
export class Timers { | ||
private timers: Set<Timer> = new Set(); | ||
|
||
constructor(private timerFactory: TimerFactory) {} | ||
|
||
public startTimer(time: number, callback: () => void) { | ||
const timer = this.timerFactory.createTimer(); | ||
const myCallback = () => { | ||
this.timers.delete(timer); | ||
callback(); | ||
}; | ||
timer.startTimer(time, myCallback); | ||
this.timers.add(timer); | ||
} | ||
|
||
public stopTimers() { | ||
this.timers.forEach((timer) => timer.stopTimer()); | ||
this.timers = new Set(); | ||
} | ||
|
||
public hasActiveTimers() { | ||
return this.timers.size > 0; | ||
} | ||
} |