File: blocking-queue.js

package info (click to toggle)
node-yarnpkg 1.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 92,980 kB
  • sloc: sh: 323; makefile: 19
file content (41 lines) | stat: -rw-r--r-- 736 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* @flow */

import BlockingQueue from '../../src/util/blocking-queue.js';

test('max concurrency', async function(): Promise<void> {
  jest.useFakeTimers();

  const queue = new BlockingQueue('test', 5);
  let i = 0;
  let running = 0;

  function create(): Promise<void> {
    return queue.push(++i + '', (): Promise<void> => {
      running++;
      jest.runAllTimers();

      if (running > 5) {
        return Promise.reject(new Error('Concurrency is broken'));
      }

      running--;

      return Promise.resolve();
    });
  }

  await Promise.all([
    create(),
    create(),
    create(),
    create(),
    create(),
    create(),
    create(),
    create(),
    create(),
    create(),
  ]);

  jest.useRealTimers();
});