瀏覽代碼

Test debouncing and throttling.

Afshin T. Darian 6 年之前
父節點
當前提交
f3683a7984
共有 1 個文件被更改,包括 99 次插入26 次删除
  1. 99 26
      tests/test-coreutils/src/ratelimiter.spec.ts

+ 99 - 26
tests/test-coreutils/src/ratelimiter.spec.ts

@@ -3,33 +3,106 @@
 
 import { expect } from 'chai';
 
-import { Debouncer } from '@jupyterlab/coreutils';
+import { Debouncer, Throttler } from '@jupyterlab/coreutils';
+
+import { sleep } from '@jupyterlab/testutils';
 
 describe('Debouncer', () => {
-  it('should debounce a function within a limit interval', async () => {
-    let called = 0;
-    const limit = 500;
-    const debouncer = new Debouncer(async () => {
-      called += 1;
-    }, limit);
-    const one = debouncer.invoke();
-    const two = debouncer.invoke();
-    const three = debouncer.invoke();
-    const four = debouncer.invoke();
-    const five = debouncer.invoke();
-    await five;
-    expect(called).to.equal(1);
-    await four;
-    expect(called).to.equal(1);
-    await three;
-    expect(called).to.equal(1);
-    await two;
-    expect(called).to.equal(1);
-    await one;
-    expect(called).to.equal(1);
-    await debouncer.invoke();
-    expect(called).to.equal(2);
-    await debouncer.invoke();
-    expect(called).to.equal(3);
+  let debouncer: Debouncer;
+
+  afterEach(() => {
+    debouncer.dispose();
+  });
+
+  describe('#invoke()', () => {
+    it('should debounce a function', async () => {
+      let called = 0;
+      const limit = 500;
+
+      debouncer = new Debouncer(async () => ++called, limit);
+
+      let one = debouncer.invoke();
+      let two = debouncer.invoke();
+      let three = debouncer.invoke();
+      let four = debouncer.invoke();
+      let five = debouncer.invoke();
+      let six = debouncer.invoke();
+
+      expect(await one).to.equal(1);
+      expect(await two).to.equal(1);
+      expect(await three).to.equal(1);
+      expect(await four).to.equal(1);
+      expect(await five).to.equal(1);
+      expect(await six).to.equal(1);
+
+      one = debouncer.invoke();
+      await sleep(300);
+      two = debouncer.invoke();
+      await sleep(300);
+      three = debouncer.invoke();
+      await sleep(300);
+      four = debouncer.invoke();
+      await sleep(300);
+      five = debouncer.invoke();
+      await sleep(300);
+      six = debouncer.invoke();
+
+      expect(await one).to.equal(2);
+      expect(await two).to.equal(2);
+      expect(await three).to.equal(2);
+      expect(await four).to.equal(2);
+      expect(await five).to.equal(2);
+      expect(await six).to.equal(2);
+    });
+  });
+});
+
+describe('Throttler', () => {
+  let throttler: Throttler;
+
+  afterEach(() => {
+    throttler.dispose();
+  });
+
+  describe('#invoke()', () => {
+    it('should throttle a function', async () => {
+      let called = 0;
+      const limit = 500;
+
+      throttler = new Throttler(async () => ++called, limit);
+
+      let one = throttler.invoke();
+      let two = throttler.invoke();
+      let three = throttler.invoke();
+      let four = throttler.invoke();
+      let five = throttler.invoke();
+      let six = throttler.invoke();
+
+      expect(await one).to.equal(1);
+      expect(await two).to.equal(1);
+      expect(await three).to.equal(1);
+      expect(await four).to.equal(1);
+      expect(await five).to.equal(1);
+      expect(await six).to.equal(1);
+
+      one = throttler.invoke();
+      await sleep(300);
+      two = throttler.invoke();
+      await sleep(300);
+      three = throttler.invoke();
+      await sleep(300);
+      four = throttler.invoke();
+      await sleep(300);
+      five = throttler.invoke();
+      await sleep(300);
+      six = throttler.invoke();
+
+      expect(await one).to.equal(2);
+      expect(await two).to.equal(2);
+      expect(await three).to.equal(3);
+      expect(await four).to.equal(3);
+      expect(await five).to.equal(4);
+      expect(await six).to.equal(4);
+    });
   });
 });