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

Commit

Permalink
Merge pull request #22 from jchen86/check-on-startup
Browse files Browse the repository at this point in the history
Perform initial check on startup
  • Loading branch information
jimmygchen authored Jun 18, 2022
2 parents ae5ebd8 + 376dac3 commit 8e1ca8b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
},
"jest": {
"transform": {},
"testEnvironment": "jest-environment-node"
"testEnvironment": "jest-environment-node",
"restoreMocks": true,
"clearMocks": true,
"resetMocks": true
}
}
6 changes: 5 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SMSNotifier } from './notifiers/index.js';
import { config } from './config.js';
import { BeaconAPIClient } from './beacon-api-client.js';
import { validatorBalanceReducedAlert, validatorStatusChangedAlert } from './alerts/index.js';
import { logger } from './logger.js';

const smsNotifer = new SMSNotifier(config.sms)
const beaconApiClient = new BeaconAPIClient(config.beaconAPIs);
Expand All @@ -11,4 +12,7 @@ const validatorPollingService = new ValidatorPollingService(beaconApiClient);
validatorPollingService.addValidators(config.pubkeys);
validatorPollingService.addListener(validatorBalanceReducedAlert(smsNotifer, config.alerts.validatorBalanceReduced));
validatorPollingService.addListener(validatorStatusChangedAlert(smsNotifer));
validatorPollingService.start();
validatorPollingService.start()
.catch((err) => {
logger.error(`Error polling validators from Beacon API ${config.beaconAPIs}: `, err);
});
3 changes: 2 additions & 1 deletion src/validator-polling-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ class ValidatorPollingService {
}
}

start() {
async start() {
await this.#pollValidators();
setInterval(this.#pollValidators.bind(this), this.#pollingIntervalSeconds * 1000);
logger.info(`Started polling validators every ${this.#pollingIntervalSeconds} seconds.`)
}
Expand Down
12 changes: 9 additions & 3 deletions test/validator-polling-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,23 @@ describe('ValidatorPollingService', () => {
beaconApiClient = new BeaconAPIClient('http://localhost:5052');
svc = new ValidatorPollingService(beaconApiClient);
jest.spyOn(BeaconAPIClient.prototype, 'getGenesisTime').mockImplementation(() => Promise.resolve());
jest.clearAllTimers();
});

it('should allow adding listener functions', () => {
svc.addListener(() => 'I hear you!')
});

it('should poll beaconApiClient every epoch', async () => {
it('should poll beaconApiClient on startup', async () => {
const getValidatorsMock = jest.spyOn(BeaconAPIClient.prototype, 'getValidators').mockImplementation(() => Promise.resolve({ data: [] }));
await svc.start();
expect(getValidatorsMock).toHaveBeenCalledTimes(2);
});

svc.start();
expect(getValidatorsMock).not.toBeCalled();
it('should poll beaconApiClient every epoch', async () => {
const getValidatorsMock = jest.spyOn(BeaconAPIClient.prototype, 'getValidators').mockImplementation(() => Promise.resolve({ data: [] }));
await svc.start();
getValidatorsMock.mockClear();

jest.advanceTimersByTime(SECONDS_PER_EPOCH * 1000);
await flushPromises();
Expand Down

0 comments on commit 8e1ca8b

Please sign in to comment.