Kaynağa Gözat

Update rate limiter tests.

Afshin T. Darian 6 yıl önce
ebeveyn
işleme
a184cbd450
1 değiştirilmiş dosya ile 86 ekleme ve 66 silme
  1. 86 66
      tests/test-coreutils/src/ratelimiter.spec.ts

+ 86 - 66
tests/test-coreutils/src/ratelimiter.spec.ts

@@ -15,44 +15,54 @@ describe('Debouncer', () => {
   });
 
   describe('#invoke()', () => {
-    it('should debounce a function', async () => {
+    it('should rate limit invocations', async () => {
+      const limit = 500;
+      const wanted = [1, 1, 1, 1, 1, 1];
       let called = 0;
+
+      debouncer = new Debouncer(async () => ++called, limit);
+
+      const one = debouncer.invoke();
+      const two = debouncer.invoke();
+      const three = debouncer.invoke();
+      const four = debouncer.invoke();
+      const five = debouncer.invoke();
+      const six = debouncer.invoke();
+
+      expect(await one).to.equal(wanted[0]);
+      expect(await two).to.equal(wanted[1]);
+      expect(await three).to.equal(wanted[2]);
+      expect(await four).to.equal(wanted[3]);
+      expect(await five).to.equal(wanted[4]);
+      expect(await six).to.equal(wanted[5]);
+    });
+
+    it('should debounce invocations within an interval', async () => {
       const limit = 500;
+      const sleeps = [200, 200, 200, 200, 600];
+      const wanted = [1, 1, 1, 1, 1, 2];
+      let called = 0;
 
       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);
+      const one = debouncer.invoke();
+      await sleep(sleeps[0]);
+      const two = debouncer.invoke();
+      await sleep(sleeps[1]);
+      const three = debouncer.invoke();
+      await sleep(sleeps[2]);
+      const four = debouncer.invoke();
+      await sleep(sleeps[3]);
+      const five = debouncer.invoke();
+      await sleep(sleeps[4]);
+      const six = debouncer.invoke();
+
+      expect(await one).to.equal(wanted[0]);
+      expect(await two).to.equal(wanted[1]);
+      expect(await three).to.equal(wanted[2]);
+      expect(await four).to.equal(wanted[3]);
+      expect(await five).to.equal(wanted[4]);
+      expect(await six).to.equal(wanted[5]);
     });
   });
 });
@@ -65,44 +75,54 @@ describe('Throttler', () => {
   });
 
   describe('#invoke()', () => {
-    it('should throttle a function', async () => {
+    it('should rate limit invocations', async () => {
+      const limit = 500;
+      const wanted = [1, 1, 1, 1, 1, 1];
       let called = 0;
+
+      throttler = new Debouncer(async () => ++called, limit);
+
+      const one = throttler.invoke();
+      const two = throttler.invoke();
+      const three = throttler.invoke();
+      const four = throttler.invoke();
+      const five = throttler.invoke();
+      const six = throttler.invoke();
+
+      expect(await one).to.equal(wanted[0]);
+      expect(await two).to.equal(wanted[1]);
+      expect(await three).to.equal(wanted[2]);
+      expect(await four).to.equal(wanted[3]);
+      expect(await five).to.equal(wanted[4]);
+      expect(await six).to.equal(wanted[5]);
+    });
+
+    it('should throttle invocations within an interval', async () => {
       const limit = 500;
+      const sleeps = [200, 200, 200, 200, 600];
+      const wanted = [1, 1, 1, 2, 2, 3];
+      let called = 0;
 
       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);
+      const one = throttler.invoke();
+      await sleep(sleeps[0]);
+      const two = throttler.invoke();
+      await sleep(sleeps[1]);
+      const three = throttler.invoke();
+      await sleep(sleeps[2]);
+      const four = throttler.invoke();
+      await sleep(sleeps[3]);
+      const five = throttler.invoke();
+      await sleep(sleeps[4]);
+      const six = throttler.invoke();
+
+      expect(await one).to.equal(wanted[0]);
+      expect(await two).to.equal(wanted[1]);
+      expect(await three).to.equal(wanted[2]);
+      expect(await four).to.equal(wanted[3]);
+      expect(await five).to.equal(wanted[4]);
+      expect(await six).to.equal(wanted[5]);
     });
   });
 });