-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
78 lines (66 loc) · 1.66 KB
/
test.ts
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
import {
assert,
assertEquals,
assertNotEquals,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import * as path from "https://deno.land/[email protected]/path/mod.ts";
import { isFreePort, getFreePort } from "./mod.ts";
const { test, run, execPath } = Deno;
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, ms);
});
}
const __filepath = path.normalize(
import.meta.url.replace("file://", ""),
).replace(/^\\/, "");
console.log(`test filename: ${JSON.stringify(__filepath)}`);
function testWithServer(port: number, fn: () => Promise<void>) {
test({
name: fn.name,
fn: async () => {
const ps = run({
stdout: "inherit",
stderr: "inherit",
cmd: [
execPath(),
"run",
"--allow-net",
path.join(path.dirname(__filepath), "testdata", "server.ts"),
port + "",
],
});
await sleep(5000);
let err!: Error;
try {
fn();
} catch (e) {
err = e;
} finally {
ps.close();
}
if (err) {
throw err;
}
},
});
}
test({
name: "GetFreePortIfPortIsFree",
fn: async () => {
const port = await getFreePort(10086);
assert(port);
assertEquals(typeof port, "number");
assertEquals(port, 10086);
assertEquals(await isFreePort(10086), true);
},
});
testWithServer(10086, async function testGetFreePortIfPortHasBeenToke() {
const port = await getFreePort(10086);
assert(port);
assertEquals(typeof port, "number");
assertNotEquals(port, 10086);
assertEquals(await isFreePort(10086), false);
});