-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_util.ts
45 lines (37 loc) · 1.18 KB
/
test_util.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
// deno-lint-ignore-file
import { delay } from "./test_deps.ts";
import { compose, type Context } from "./mod.ts";
export const connInfo = {
localAddr: { transport: "tcp" as const, hostname: "127.0.0.1", port: 8080 },
remoteAddr: { transport: "tcp" as const, hostname: "127.0.0.1", port: 48951 },
};
function createMiddleware(f: (...args: any[]) => any | Promise<any>) {
return async <C extends Context>(ctx: C) => {
ctx.state.result = await f(ctx.state.result);
return ctx;
};
}
export function add10(n: number) {
return n + 10;
}
export function multiply10(n: number) {
return n * 10;
}
export async function subtract5Delayed(n: number) {
await delay(10);
return n - 10;
}
export async function divide5Delayed(n: number) {
await delay(10);
return n / 5;
}
export const add10Middleware = createMiddleware(add10);
export const multiplyMiddleware = createMiddleware(multiply10);
export const subtract5DelayedMiddleware = createMiddleware(subtract5Delayed);
export const divide5DelayedMiddleware = createMiddleware(divide5Delayed);
export const tryMiddleware = compose(
add10Middleware,
divide5DelayedMiddleware,
subtract5DelayedMiddleware,
multiplyMiddleware,
) as any;