ratelimiter.spec.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { expect } from 'chai';
  4. import { Debouncer, Throttler } from '@jupyterlab/coreutils';
  5. import { sleep } from '@jupyterlab/testutils';
  6. describe('Debouncer', () => {
  7. let debouncer: Debouncer;
  8. afterEach(() => {
  9. debouncer.dispose();
  10. });
  11. describe('#invoke()', () => {
  12. it('should debounce a function', async () => {
  13. let called = 0;
  14. const limit = 500;
  15. debouncer = new Debouncer(async () => ++called, limit);
  16. let one = debouncer.invoke();
  17. let two = debouncer.invoke();
  18. let three = debouncer.invoke();
  19. let four = debouncer.invoke();
  20. let five = debouncer.invoke();
  21. let six = debouncer.invoke();
  22. expect(await one).to.equal(1);
  23. expect(await two).to.equal(1);
  24. expect(await three).to.equal(1);
  25. expect(await four).to.equal(1);
  26. expect(await five).to.equal(1);
  27. expect(await six).to.equal(1);
  28. one = debouncer.invoke();
  29. await sleep(300);
  30. two = debouncer.invoke();
  31. await sleep(300);
  32. three = debouncer.invoke();
  33. await sleep(300);
  34. four = debouncer.invoke();
  35. await sleep(300);
  36. five = debouncer.invoke();
  37. await sleep(300);
  38. six = debouncer.invoke();
  39. expect(await one).to.equal(2);
  40. expect(await two).to.equal(2);
  41. expect(await three).to.equal(2);
  42. expect(await four).to.equal(2);
  43. expect(await five).to.equal(2);
  44. expect(await six).to.equal(2);
  45. });
  46. });
  47. });
  48. describe('Throttler', () => {
  49. let throttler: Throttler;
  50. afterEach(() => {
  51. throttler.dispose();
  52. });
  53. describe('#invoke()', () => {
  54. it('should throttle a function', async () => {
  55. let called = 0;
  56. const limit = 500;
  57. throttler = new Throttler(async () => ++called, limit);
  58. let one = throttler.invoke();
  59. let two = throttler.invoke();
  60. let three = throttler.invoke();
  61. let four = throttler.invoke();
  62. let five = throttler.invoke();
  63. let six = throttler.invoke();
  64. expect(await one).to.equal(1);
  65. expect(await two).to.equal(1);
  66. expect(await three).to.equal(1);
  67. expect(await four).to.equal(1);
  68. expect(await five).to.equal(1);
  69. expect(await six).to.equal(1);
  70. one = throttler.invoke();
  71. await sleep(300);
  72. two = throttler.invoke();
  73. await sleep(300);
  74. three = throttler.invoke();
  75. await sleep(300);
  76. four = throttler.invoke();
  77. await sleep(300);
  78. five = throttler.invoke();
  79. await sleep(300);
  80. six = throttler.invoke();
  81. expect(await one).to.equal(2);
  82. expect(await two).to.equal(2);
  83. expect(await three).to.equal(3);
  84. expect(await four).to.equal(3);
  85. expect(await five).to.equal(4);
  86. expect(await six).to.equal(4);
  87. });
  88. });
  89. });