|
1 | 1 | import { ControllerMessenger } from '@metamask/base-controller';
|
| 2 | +import EventEmitter from 'events'; |
2 | 3 | import { useFakeTimers } from 'sinon';
|
3 | 4 |
|
4 | 5 | import { advanceTime } from '../../../tests/helpers';
|
@@ -277,30 +278,30 @@ describe('PollingController', () => {
|
277 | 278 | controller.startPollingByNetworkClientId('mainnet');
|
278 | 279 | await advanceTime({ clock, duration: 0 });
|
279 | 280 |
|
280 |
| - controller.startPollingByNetworkClientId('rinkeby'); |
| 281 | + controller.startPollingByNetworkClientId('goerli'); |
281 | 282 | await advanceTime({ clock, duration: 0 });
|
282 | 283 |
|
283 | 284 | expect(controller._executePoll.mock.calls).toMatchObject([
|
284 | 285 | ['mainnet', {}],
|
285 |
| - ['rinkeby', {}], |
| 286 | + ['goerli', {}], |
286 | 287 | ]);
|
287 | 288 | await advanceTime({ clock, duration: TICK_TIME });
|
288 | 289 |
|
289 | 290 | expect(controller._executePoll.mock.calls).toMatchObject([
|
290 | 291 | ['mainnet', {}],
|
291 |
| - ['rinkeby', {}], |
| 292 | + ['goerli', {}], |
292 | 293 | ['mainnet', {}],
|
293 |
| - ['rinkeby', {}], |
| 294 | + ['goerli', {}], |
294 | 295 | ]);
|
295 | 296 | await advanceTime({ clock, duration: TICK_TIME });
|
296 | 297 |
|
297 | 298 | expect(controller._executePoll.mock.calls).toMatchObject([
|
298 | 299 | ['mainnet', {}],
|
299 |
| - ['rinkeby', {}], |
| 300 | + ['goerli', {}], |
300 | 301 | ['mainnet', {}],
|
301 |
| - ['rinkeby', {}], |
| 302 | + ['goerli', {}], |
302 | 303 | ['mainnet', {}],
|
303 |
| - ['rinkeby', {}], |
| 304 | + ['goerli', {}], |
304 | 305 | ]);
|
305 | 306 | controller.stopAllPolling();
|
306 | 307 | });
|
@@ -383,4 +384,145 @@ describe('PollingController', () => {
|
383 | 384 | expect(c.stopPollingByPollingToken).toBeDefined();
|
384 | 385 | });
|
385 | 386 | });
|
| 387 | + describe('startPollingByNetworkClientId after setPollOnNewBlocks', () => { |
| 388 | + class TestBlockTracker extends EventEmitter { |
| 389 | + private latestBlockNumber: number; |
| 390 | + |
| 391 | + constructor({ interval } = { interval: 1000 }) { |
| 392 | + super(); |
| 393 | + this.latestBlockNumber = 0; |
| 394 | + this.start(interval); |
| 395 | + } |
| 396 | + |
| 397 | + private start(interval: number) { |
| 398 | + setInterval(() => { |
| 399 | + this.latestBlockNumber += 1; |
| 400 | + this.emit('latest', this.latestBlockNumber); |
| 401 | + }, interval); |
| 402 | + } |
| 403 | + } |
| 404 | + |
| 405 | + it('should start polling for the specified networkClientId', async () => { |
| 406 | + class MyGasFeeController extends PollingController<any, any, any> { |
| 407 | + _executePoll = createExecutePollMock(); |
| 408 | + } |
| 409 | + const mockMessenger = new ControllerMessenger<any, any>(); |
| 410 | + |
| 411 | + const controller = new MyGasFeeController({ |
| 412 | + messenger: mockMessenger, |
| 413 | + metadata: {}, |
| 414 | + name: 'PollingController', |
| 415 | + state: { foo: 'bar' }, |
| 416 | + }); |
| 417 | + |
| 418 | + const getNetworkClientById = jest.fn().mockReturnValue({ |
| 419 | + blockTracker: new TestBlockTracker({ interval: 5 }), |
| 420 | + }); |
| 421 | + |
| 422 | + controller.setPollOnNewBlocks(getNetworkClientById); |
| 423 | + |
| 424 | + controller.startPollingByNetworkClientId('mainnet'); |
| 425 | + |
| 426 | + expect(getNetworkClientById).toHaveBeenCalledWith('mainnet'); |
| 427 | + |
| 428 | + await advanceTime({ clock, duration: 5 }); |
| 429 | + |
| 430 | + expect(controller._executePoll).toHaveBeenCalledTimes(1); |
| 431 | + |
| 432 | + await advanceTime({ clock, duration: 1 }); |
| 433 | + |
| 434 | + expect(controller._executePoll).toHaveBeenCalledTimes(1); |
| 435 | + |
| 436 | + await advanceTime({ clock, duration: 4 }); |
| 437 | + |
| 438 | + expect(controller._executePoll.mock.calls).toMatchObject([ |
| 439 | + expect.arrayContaining(['mainnet', {}]), |
| 440 | + expect.arrayContaining(['mainnet', {}]), |
| 441 | + ]); |
| 442 | + |
| 443 | + // Stop all polling |
| 444 | + controller.stopAllPolling(); |
| 445 | + }); |
| 446 | + |
| 447 | + it('should start polling with different intervals for each networkClientId', async () => { |
| 448 | + class MyGasFeeController extends PollingController<any, any, any> { |
| 449 | + _executePoll = createExecutePollMock(); |
| 450 | + } |
| 451 | + const mockMessenger = new ControllerMessenger<any, any>(); |
| 452 | + |
| 453 | + const controller = new MyGasFeeController({ |
| 454 | + messenger: mockMessenger, |
| 455 | + metadata: {}, |
| 456 | + name: 'PollingController', |
| 457 | + state: { foo: 'bar' }, |
| 458 | + }); |
| 459 | + |
| 460 | + const getNetworkClientById = jest |
| 461 | + .fn() |
| 462 | + .mockImplementation((networkClientId) => { |
| 463 | + switch (networkClientId) { |
| 464 | + case 'mainnet': |
| 465 | + return { |
| 466 | + blockTracker: new TestBlockTracker({ interval: 5 }), |
| 467 | + }; |
| 468 | + case 'goerli': |
| 469 | + return { |
| 470 | + blockTracker: new TestBlockTracker({ interval: 10 }), |
| 471 | + }; |
| 472 | + case 'sepolia': |
| 473 | + return { |
| 474 | + blockTracker: new TestBlockTracker({ interval: 15 }), |
| 475 | + }; |
| 476 | + default: |
| 477 | + throw new Error(`Unknown networkClientId: ${networkClientId}`); |
| 478 | + } |
| 479 | + }); |
| 480 | + |
| 481 | + controller.setPollOnNewBlocks(getNetworkClientById); |
| 482 | + |
| 483 | + controller.startPollingByNetworkClientId('mainnet'); |
| 484 | + await advanceTime({ clock, duration: 5 }); |
| 485 | + |
| 486 | + expect(controller._executePoll).toHaveBeenCalledTimes(1); |
| 487 | + expect(controller._executePoll).toHaveBeenCalledWith('mainnet', {}, 1); |
| 488 | + |
| 489 | + // Start polling for goerli, 10ms interval |
| 490 | + controller.startPollingByNetworkClientId('goerli'); |
| 491 | + await advanceTime({ clock, duration: 5 }); |
| 492 | + |
| 493 | + expect(controller._executePoll.mock.calls).toMatchObject([ |
| 494 | + ['mainnet', {}, 1], |
| 495 | + ['mainnet', {}, 2], |
| 496 | + ]); |
| 497 | + |
| 498 | + await advanceTime({ clock, duration: 5 }); |
| 499 | + |
| 500 | + expect(controller._executePoll.mock.calls).toMatchObject([ |
| 501 | + ['mainnet', {}, 1], |
| 502 | + ['mainnet', {}, 2], |
| 503 | + ['mainnet', {}, 3], |
| 504 | + ['goerli', {}, 1], |
| 505 | + ]); |
| 506 | + |
| 507 | + // Start polling for sepolia, 15ms interval |
| 508 | + controller.startPollingByNetworkClientId('sepolia'); |
| 509 | + |
| 510 | + await advanceTime({ clock, duration: 15 }); |
| 511 | + |
| 512 | + expect(controller._executePoll.mock.calls).toMatchObject([ |
| 513 | + ['mainnet', {}, 1], |
| 514 | + ['mainnet', {}, 2], |
| 515 | + ['mainnet', {}, 3], |
| 516 | + ['goerli', {}, 1], |
| 517 | + ['mainnet', {}, 4], |
| 518 | + ['mainnet', {}, 5], |
| 519 | + ['goerli', {}, 2], |
| 520 | + ['mainnet', {}, 6], |
| 521 | + ['sepolia', {}, 1], |
| 522 | + ]); |
| 523 | + |
| 524 | + // Stop all polling |
| 525 | + controller.stopAllPolling(); |
| 526 | + }); |
| 527 | + }); |
386 | 528 | });
|
0 commit comments