Skip to content

Commit

Permalink
another test + cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
adonesky1 committed Dec 6, 2023
1 parent 1be1f9a commit 5ac05de
Showing 1 changed file with 13 additions and 141 deletions.
154 changes: 13 additions & 141 deletions packages/polling-controller/src/PollingController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,23 @@ class MyGasFeeController extends PollingController<any, any, any> {

describe('PollingController', () => {
let clock: sinon.SinonFakeTimers;
let mockMessenger: any;
let controller: any;
beforeEach(() => {
mockMessenger = new ControllerMessenger<any, any>();
controller = new MyGasFeeController({
messenger: mockMessenger,
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
});
clock = useFakeTimers();
});
afterEach(() => {
clock.restore();
});
describe('start', () => {
it('should start polling if not polling', async () => {
const mockMessenger = new ControllerMessenger<any, any>();

const controller = new MyGasFeeController({
messenger: mockMessenger,
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
});
controller.startPollingByNetworkClientId('mainnet');
await advanceTime({ clock, duration: 0 });
expect(controller._executePoll).toHaveBeenCalledTimes(1);
Expand All @@ -46,14 +47,6 @@ describe('PollingController', () => {
});
describe('stop', () => {
it('should stop polling when called with a valid polling that was the only active pollingToken for a given networkClient', async () => {
const mockMessenger = new ControllerMessenger<any, any>();

const controller = new MyGasFeeController({
messenger: mockMessenger,
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
});
const pollingToken = controller.startPollingByNetworkClientId('mainnet');
await advanceTime({ clock, duration: 0 });
expect(controller._executePoll).toHaveBeenCalledTimes(1);
Expand All @@ -64,14 +57,6 @@ describe('PollingController', () => {
controller.stopAllPolling();
});
it('should not stop polling if called with one of multiple active polling tokens for a given networkClient', async () => {
const mockMessenger = new ControllerMessenger<any, any>();

const controller = new MyGasFeeController({
messenger: mockMessenger,
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
});
const pollingToken1 = controller.startPollingByNetworkClientId('mainnet');
await advanceTime({ clock, duration: 0 });

Expand All @@ -84,29 +69,13 @@ describe('PollingController', () => {
controller.stopAllPolling();
});
it('should error if no pollingToken is passed', () => {
const mockMessenger = new ControllerMessenger<any, any>();

const controller = new MyGasFeeController({
messenger: mockMessenger,
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
});
controller.startPollingByNetworkClientId('mainnet');
expect(() => {
controller.stopPollingByPollingToken(undefined as unknown as any);
}).toThrow('pollingToken required');
controller.stopAllPolling();
});
it('should error if no matching pollingToken is found', () => {
const mockMessenger = new ControllerMessenger<any, any>();

const controller = new MyGasFeeController({
messenger: mockMessenger,
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
});
controller.startPollingByNetworkClientId('mainnet');
expect(() => {
controller.stopPollingByPollingToken('potato');
Expand All @@ -116,29 +85,13 @@ describe('PollingController', () => {
});
describe('startPollingByNetworkClientId', () => {
it('should call _executePoll immediately and on interval if polling', async () => {
const mockMessenger = new ControllerMessenger<any, any>();

const controller = new MyGasFeeController({
messenger: mockMessenger,
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
});
controller.startPollingByNetworkClientId('mainnet');
await advanceTime({ clock, duration: 0 });
expect(controller._executePoll).toHaveBeenCalledTimes(1);
await advanceTime({ clock, duration: TICK_TIME * 2 });
expect(controller._executePoll).toHaveBeenCalledTimes(3);
});
it('should call _executePoll immediately once and continue calling _executePoll on interval when start is called again with the same networkClientId', async () => {
const mockMessenger = new ControllerMessenger<any, any>();

const controller = new MyGasFeeController({
messenger: mockMessenger,
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
});
controller.startPollingByNetworkClientId('mainnet');
await advanceTime({ clock, duration: 0 });

Expand All @@ -153,31 +106,12 @@ describe('PollingController', () => {
});
it('should publish "pollingComplete" when stop is called', async () => {
const pollingComplete: any = jest.fn();

const name = 'PollingController';

const mockMessenger = new ControllerMessenger<any, any>();

const controller = new MyGasFeeController({
messenger: mockMessenger,
metadata: {},
name,
state: { foo: 'bar' },
});
controller.onPollingCompleteByNetworkClientId('mainnet', pollingComplete);
const pollingToken = controller.startPollingByNetworkClientId('mainnet');
controller.stopPollingByPollingToken(pollingToken);
expect(pollingComplete).toHaveBeenCalledTimes(1);
});
it('should poll at the interval length when set via setIntervalLength', async () => {
const mockMessenger = new ControllerMessenger<any, any>();

const controller = new MyGasFeeController({
messenger: mockMessenger,
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
});
controller.setIntervalLength(TICK_TIME);
controller.startPollingByNetworkClientId('mainnet');
await advanceTime({ clock, duration: 0 });
Expand All @@ -190,14 +124,6 @@ describe('PollingController', () => {
expect(controller._executePoll).toHaveBeenCalledTimes(2);
});
it('should start and stop polling sessions for different networkClientIds with the same options', async () => {
const mockMessenger = new ControllerMessenger<any, any>();

const controller = new MyGasFeeController({
messenger: mockMessenger,
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
});
const pollToken1 = controller.startPollingByNetworkClientId('mainnet', {
address: '0x1',
});
Expand Down Expand Up @@ -239,14 +165,6 @@ describe('PollingController', () => {
});
describe('multiple networkClientIds', () => {
it('should poll for each networkClientId', async () => {
const mockMessenger = new ControllerMessenger<any, any>();

const controller = new MyGasFeeController({
messenger: mockMessenger,
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
});
controller.startPollingByNetworkClientId('mainnet');
await advanceTime({ clock, duration: 0 });

Expand Down Expand Up @@ -279,14 +197,6 @@ describe('PollingController', () => {
});

it('should poll multiple networkClientIds when setting interval length', async () => {
const mockMessenger = new ControllerMessenger<any, any>();

const controller = new MyGasFeeController({
messenger: mockMessenger,
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
});
controller.setIntervalLength(TICK_TIME * 2);
controller.startPollingByNetworkClientId('mainnet');
await advanceTime({ clock, duration: 0 });
Expand Down Expand Up @@ -404,19 +314,6 @@ describe('PollingController', () => {
});

it('should start polling for the specified networkClientId', async () => {
const mockMessenger = new ControllerMessenger<any, any>();

const controller = new MyGasFeeController({
messenger: mockMessenger,
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
});

// const getNetworkClientById = jest.fn().mockReturnValue({
// blockTracker: new TestBlockTracker({ interval: 5 }),
// });

controller.setPollOnNewBlocks(getNetworkClientById);

controller.startPollingByNetworkClientId('mainnet');
Expand All @@ -438,20 +335,10 @@ describe('PollingController', () => {
expect.arrayContaining(['mainnet', {}]),
]);

// Stop all polling
controller.stopAllPolling();
});

it('should poll on new block intervals for each networkClientId', async () => {
const mockMessenger = new ControllerMessenger<any, any>();

const controller = new MyGasFeeController({
messenger: mockMessenger,
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
});

controller.setPollOnNewBlocks(getNetworkClientById);

controller.startPollingByNetworkClientId('mainnet');
Expand Down Expand Up @@ -500,20 +387,10 @@ describe('PollingController', () => {
['sepolia', {}, 2],
]);

// Stop all polling
controller.stopAllPolling();
});

it('should should stop polling when all polling tokens for a networkClientId are deleted', async () => {
const mockMessenger = new ControllerMessenger<any, any>();

const controller = new MyGasFeeController({
messenger: mockMessenger,
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
});

controller.setPollOnNewBlocks(getNetworkClientById);

const pollingToken1 = controller.startPollingByNetworkClientId('mainnet');
Expand Down Expand Up @@ -551,18 +428,11 @@ describe('PollingController', () => {
['mainnet', {}, 2],
['mainnet', {}, 3],
]);

controller.stopAllPolling();
});

it('should should stop polling when all polling tokens for a networkClientId are deleted, even if other networkClientIds are still polling', async () => {
const mockMessenger = new ControllerMessenger<any, any>();

const controller = new MyGasFeeController({
messenger: mockMessenger,
metadata: {},
name: 'PollingController',
state: { foo: 'bar' },
});

controller.setPollOnNewBlocks(getNetworkClientById);

const pollingToken1 = controller.startPollingByNetworkClientId('mainnet');
Expand Down Expand Up @@ -618,6 +488,8 @@ describe('PollingController', () => {
['goerli', {}, 3],
['goerli', {}, 4],
]);

controller.stopAllPolling();
});
});
});

0 comments on commit 5ac05de

Please sign in to comment.