Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-protocol/src/util/timeout.spec.ts
2500 views
1
/**
2
* Copyright (c) 2020 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import * as chai from "chai";
8
const expect = chai.expect;
9
import { suite, test } from "@testdeck/mocha";
10
import { Timeout } from "./timeout";
11
12
@suite()
13
export class TimeoutSpec {
14
@test
15
async testSimpleRun() {
16
const timeout = new Timeout(1);
17
timeout.start();
18
await timeout.await();
19
expect(timeout.signal?.aborted).to.be.true;
20
}
21
22
@test
23
async testSimpleRunNotStarted() {
24
const timeout = new Timeout(1);
25
await timeout.await();
26
expect(timeout.signal).to.be.undefined;
27
}
28
29
@test
30
async testRestart() {
31
const timeout = new Timeout(20);
32
timeout.start();
33
await timeout.await();
34
expect(timeout.signal?.aborted).to.be.true;
35
36
timeout.restart();
37
expect(timeout.signal).to.not.be.undefined;
38
expect(timeout.signal?.aborted).to.be.false;
39
await timeout.await();
40
expect(timeout.signal?.aborted).to.be.true;
41
}
42
43
@test
44
async testClear() {
45
const timeout = new Timeout(1000);
46
timeout.restart();
47
timeout.clear();
48
expect(timeout.signal).to.be.undefined;
49
}
50
51
@test
52
async testAbortCondition() {
53
const timeout = new Timeout(1, () => false); // will never trigger abort
54
timeout.start();
55
await new Promise((resolve) => setTimeout(resolve, 50));
56
expect(timeout.signal).to.not.be.undefined;
57
expect(timeout.signal?.aborted).to.be.false;
58
}
59
}
60
61