summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/tests/cts/checkout/src/unittests/async_expectations.spec.ts
blob: 2d62978b8fc3dda4bf728f58ff49af1d84b39009 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* eslint-disable @typescript-eslint/require-await */
export const description = `
Tests for eventualAsyncExpectation and immediateAsyncExpectation.
`;

import { makeTestGroup } from '../common/framework/test_group.js';
import { makeTestGroupForUnitTesting } from '../common/internal/test_group.js';
import { assert, objectEquals, rejectOnTimeout, resolveOnTimeout } from '../common/util/util.js';

import { TestGroupTest } from './test_group_test.js';
import { UnitTest } from './unit_test.js';

class FixtureToTest extends UnitTest {
  public override immediateAsyncExpectation<T>(fn: () => Promise<T>): Promise<T> {
    return super.immediateAsyncExpectation(fn);
  }
  public override eventualAsyncExpectation<T>(fn: (niceStack: Error) => Promise<T>): void {
    super.eventualAsyncExpectation(fn);
  }
}

export const g = makeTestGroup(TestGroupTest);

g.test('eventual').fn(async t0 => {
  const g = makeTestGroupForUnitTesting(FixtureToTest);

  const runState = [0, 0, 0, 0];
  let runStateIndex = 0;

  // Should pass in state 3
  g.test('noawait,resolve').fn(t => {
    const idx = runStateIndex++;

    runState[idx] = 1;
    t.eventualAsyncExpectation(async () => {
      runState[idx] = 2;
      await resolveOnTimeout(50);
      runState[idx] = 3;
    });
    runState[idx] = 4;
  });

  // Should fail in state 4
  g.test('noawait,reject').fn(t => {
    const idx = runStateIndex++;

    runState[idx] = 1;
    t.eventualAsyncExpectation(async () => {
      runState[idx] = 2;
      await rejectOnTimeout(50, 'rejected 1');
      runState[idx] = 3;
    });
    runState[idx] = 4;
  });

  // Should fail in state 3
  g.test('nested,2').fn(t => {
    const idx = runStateIndex++;

    runState[idx] = 1;
    t.eventualAsyncExpectation(async () => {
      runState[idx] = 2;
      await resolveOnTimeout(50); // Wait a bit before adding a new eventualAsyncExpectation
      t.eventualAsyncExpectation(() => rejectOnTimeout(100, 'inner rejected 1'));
      runState[idx] = 3;
    });
    runState[idx] = 4;
  });

  // Should fail in state 3
  g.test('nested,4').fn(t => {
    const idx = runStateIndex++;

    runState[idx] = 1;
    t.eventualAsyncExpectation(async () => {
      t.eventualAsyncExpectation(async () => {
        t.eventualAsyncExpectation(async () => {
          runState[idx] = 2;
          await resolveOnTimeout(50); // Wait a bit before adding a new eventualAsyncExpectation
          t.eventualAsyncExpectation(() => rejectOnTimeout(100, 'inner rejected 2'));
          runState[idx] = 3;
        });
      });
    });
    runState[idx] = 4;
  });

  const resultsPromise = t0.run(g);
  assert(objectEquals(runState, [0, 0, 0, 0]));

  const statuses = Array.from(await resultsPromise).map(([, v]) => v.status);
  assert(objectEquals(runState, [3, 4, 3, 3]), () => runState.toString());
  assert(objectEquals(statuses, ['pass', 'fail', 'fail', 'fail']), () => statuses.toString());
});

g.test('immediate').fn(async t0 => {
  const g = makeTestGroupForUnitTesting(FixtureToTest);

  const runState = [0, 0, 0, 0, 0];

  g.test('noawait,resolve').fn(t => {
    runState[0] = 1;
    void t.immediateAsyncExpectation(async () => {
      runState[0] = 2;
      await resolveOnTimeout(50);
      runState[0] = 3;
    });
    runState[0] = 4;
  });

  // (Can't g.test('noawait,reject') because it causes a top-level Promise
  // rejection which crashes Node.)

  g.test('await,resolve').fn(async t => {
    runState[1] = 1;
    await t.immediateAsyncExpectation(async () => {
      runState[1] = 2;
      await resolveOnTimeout(50);
      runState[1] = 3;
    });
  });

  g.test('await,reject').fn(async t => {
    runState[2] = 1;
    await t.immediateAsyncExpectation(async () => {
      runState[2] = 2;
      await rejectOnTimeout(50, 'rejected 3');
      runState[2] = 3;
    });
  });

  // (Similarly can't test 'nested,noawait'.)

  g.test('nested,await,2').fn(t => {
    runState[3] = 1;
    t.eventualAsyncExpectation(async () => {
      runState[3] = 2;
      await resolveOnTimeout(50); // Wait a bit before adding a new immediateAsyncExpectation
      runState[3] = 3;
      await t.immediateAsyncExpectation(() => rejectOnTimeout(100, 'inner rejected 3'));
      runState[3] = 5;
    });
    runState[3] = 4;
  });

  g.test('nested,await,4').fn(t => {
    runState[4] = 1;
    t.eventualAsyncExpectation(async () => {
      t.eventualAsyncExpectation(async () => {
        t.eventualAsyncExpectation(async () => {
          runState[4] = 2;
          await resolveOnTimeout(50); // Wait a bit before adding a new immediateAsyncExpectation
          runState[4] = 3;
          await t.immediateAsyncExpectation(() => rejectOnTimeout(100, 'inner rejected 3'));
          runState[4] = 5;
        });
      });
    });
    runState[4] = 4;
  });

  const resultsPromise = t0.run(g);
  assert(objectEquals(runState, [0, 0, 0, 0, 0]));

  const statuses = Array.from(await resultsPromise).map(([, v]) => v.status);
  assert(objectEquals(runState, [3, 3, 2, 3, 3]));
  assert(objectEquals(statuses, ['fail', 'pass', 'fail', 'fail', 'fail']));
});