summaryrefslogtreecommitdiff
path: root/std/util/async_test.ts
blob: f3f17312aaccaa528f078ee83d28e5f2c0f9320c (plain)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
const { test } = Deno;
import { assert, assertEquals, assertStrictEq } from "../testing/asserts.ts";
import { collectUint8Arrays, deferred, MuxAsyncIterator } from "./async.ts";

test("asyncDeferred", function (): Promise<void> {
  const d = deferred<number>();
  d.resolve(12);
  return Promise.resolve();
});

// eslint-disable-next-line require-await
async function* gen123(): AsyncIterableIterator<number> {
  yield 1;
  yield 2;
  yield 3;
}

// eslint-disable-next-line require-await
async function* gen456(): AsyncIterableIterator<number> {
  yield 4;
  yield 5;
  yield 6;
}

test("asyncMuxAsyncIterator", async function (): Promise<void> {
  const mux = new MuxAsyncIterator<number>();
  mux.add(gen123());
  mux.add(gen456());
  const results = new Set();
  for await (const value of mux) {
    results.add(value);
  }
  assertEquals(results.size, 6);
});

test("collectUint8Arrays0", async function (): Promise<void> {
  async function* gen(): AsyncIterableIterator<Uint8Array> {}
  const result = await collectUint8Arrays(gen());
  assert(result instanceof Uint8Array);
  assertEquals(result.length, 0);
});

test("collectUint8Arrays0", async function (): Promise<void> {
  async function* gen(): AsyncIterableIterator<Uint8Array> {}
  const result = await collectUint8Arrays(gen());
  assert(result instanceof Uint8Array);
  assertStrictEq(result.length, 0);
});

test("collectUint8Arrays1", async function (): Promise<void> {
  const buf = new Uint8Array([1, 2, 3]);
  // eslint-disable-next-line require-await
  async function* gen(): AsyncIterableIterator<Uint8Array> {
    yield buf;
  }
  const result = await collectUint8Arrays(gen());
  assertStrictEq(result, buf);
  assertStrictEq(result.length, 3);
});

test("collectUint8Arrays4", async function (): Promise<void> {
  // eslint-disable-next-line require-await
  async function* gen(): AsyncIterableIterator<Uint8Array> {
    yield new Uint8Array([1, 2, 3]);
    yield new Uint8Array([]);
    yield new Uint8Array([4, 5]);
    yield new Uint8Array([6]);
  }
  const result = await collectUint8Arrays(gen());
  assert(result instanceof Uint8Array);
  assertStrictEq(result.length, 6);
  for (let i = 0; i < 6; i++) {
    assertStrictEq(result[i], i + 1);
  }
});