-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathutils_test.ts
300 lines (259 loc) · 7.76 KB
/
utils_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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import { assertEquals, assertThrows } from "./test_deps.ts";
import { parseConnectionUri, type Uri } from "../utils/utils.ts";
import { DeferredAccessStack, DeferredStack } from "../utils/deferred.ts";
class LazilyInitializedObject {
#initialized = false;
// Simulate async check
get initialized() {
return new Promise<boolean>((r) => r(this.#initialized));
}
async initialize(): Promise<void> {
// Fake delay
await new Promise<void>((resolve) => {
setTimeout(() => {
resolve();
}, 10);
});
this.#initialized = true;
}
}
const dns_examples: Partial<Uri>[] = [
{ driver: "postgresql", host: "localhost" },
{ driver: "postgresql", host: "localhost", port: "5433" },
{ driver: "postgresql", host: "localhost", port: "5433", path: "mydb" },
{ driver: "postgresql", host: "localhost", path: "mydb" },
{ driver: "postgresql", host: "localhost", user: "user" },
{ driver: "postgresql", host: "localhost", password: "secret" },
{ driver: "postgresql", host: "localhost", user: "user", password: "secret" },
{
driver: "postgresql",
host: "localhost",
user: "user",
password: "secret",
params: { "param_1": "a" },
},
{
driver: "postgresql",
host: "localhost",
user: "user",
password: "secret",
path: "otherdb",
params: { "param_1": "a" },
},
{
driver: "postgresql",
path: "otherdb",
params: { "param_1": "a" },
},
{
driver: "postgresql",
host: "[2001:db8::1234]",
},
{
driver: "postgresql",
host: "[2001:db8::1234]",
port: "1500",
},
{
driver: "postgresql",
host: "[2001:db8::1234]",
port: "1500",
params: { "param_1": "a" },
},
];
Deno.test("Parses connection string into config", async function (context) {
for (
const {
driver,
user = "",
host = "",
params = {},
password = "",
path = "",
port = "",
} of dns_examples
) {
const url_params = new URLSearchParams();
for (const key in params) {
url_params.set(key, params[key]);
}
const dirty_dns =
`${driver}://${user}:${password}@${host}:${port}/${path}?${url_params.toString()}`;
await context.step(dirty_dns, () => {
const parsed_dirty_dsn = parseConnectionUri(dirty_dns);
assertEquals(parsed_dirty_dsn.driver, driver);
assertEquals(parsed_dirty_dsn.host, host);
assertEquals(parsed_dirty_dsn.params, params);
assertEquals(parsed_dirty_dsn.password, password);
assertEquals(parsed_dirty_dsn.path, path);
assertEquals(parsed_dirty_dsn.port, port);
assertEquals(parsed_dirty_dsn.user, user);
});
// Build the URL without leaving placeholders
let clean_dns_string = `${driver}://`;
if (user || password) {
clean_dns_string += `${user ?? ""}${password ? `:${password}` : ""}@`;
}
if (host || port) {
clean_dns_string += `${host ?? ""}${port ? `:${port}` : ""}`;
}
if (path) {
clean_dns_string += `/${path}`;
}
if (Object.keys(params).length > 0) {
clean_dns_string += `?${url_params.toString()}`;
}
await context.step(clean_dns_string, () => {
const parsed_clean_dsn = parseConnectionUri(clean_dns_string);
assertEquals(parsed_clean_dsn.driver, driver);
assertEquals(parsed_clean_dsn.host, host);
assertEquals(parsed_clean_dsn.params, params);
assertEquals(parsed_clean_dsn.password, password);
assertEquals(parsed_clean_dsn.path, path);
assertEquals(parsed_clean_dsn.port, port);
assertEquals(parsed_clean_dsn.user, user);
});
}
});
Deno.test("Throws on invalid parameters", () => {
assertThrows(
() => parseConnectionUri("postgres://some_host:invalid"),
Error,
`The provided port "invalid" is not a valid number`,
);
});
Deno.test("Parses connection string params into param object", function () {
const params = {
param_1: "asd",
param_2: "xyz",
param_3: "3541",
};
const base_url = new URL("postgres://fizz:[email protected]:8000/test_database");
for (const [key, value] of Object.entries(params)) {
base_url.searchParams.set(key, value);
}
const parsed_dsn = parseConnectionUri(base_url.toString());
assertEquals(parsed_dsn.params, params);
});
const encoded_hosts = ["/var/user/postgres", "./some_other_route"];
const encoded_passwords = ["Mtx=", "pássword!=?with_symbols"];
Deno.test("Decodes connection string values correctly", async (context) => {
await context.step("Host", () => {
for (const host of encoded_hosts) {
assertEquals(
parseConnectionUri(
`postgres://${encodeURIComponent(host)}:9999/txdb`,
).host,
host,
);
}
});
await context.step("Password", () => {
for (const pwd of encoded_passwords) {
assertEquals(
parseConnectionUri(
`postgres://root:${encodeURIComponent(pwd)}@localhost:9999/txdb`,
).password,
pwd,
);
}
});
});
const invalid_hosts = ["Mtx%3", "%E0%A4%A.socket"];
const invalid_passwords = ["Mtx%3", "%E0%A4%A"];
Deno.test("Defaults to connection string literal if decoding fails", async (context) => {
await context.step("Host", () => {
for (const host of invalid_hosts) {
assertEquals(
parseConnectionUri(
`postgres://${host}`,
).host,
host,
);
}
});
await context.step("Password", () => {
for (const pwd of invalid_passwords) {
assertEquals(
parseConnectionUri(
`postgres://root:${pwd}@localhost:9999/txdb`,
).password,
pwd,
);
}
});
});
Deno.test("DeferredStack", async () => {
const stack = new DeferredStack<undefined>(
10,
[],
() => new Promise((r) => r(undefined)),
);
assertEquals(stack.size, 0);
assertEquals(stack.available, 0);
const item = await stack.pop();
assertEquals(stack.size, 1);
assertEquals(stack.available, 0);
stack.push(item);
assertEquals(stack.size, 1);
assertEquals(stack.available, 1);
});
Deno.test("An empty DeferredStack awaits until an object is back in the stack", async () => {
const stack = new DeferredStack<undefined>(
1,
[],
() => new Promise((r) => r(undefined)),
);
const a = await stack.pop();
let fulfilled = false;
const b = stack.pop()
.then((e) => {
fulfilled = true;
return e;
});
await new Promise((r) => setTimeout(r, 100));
assertEquals(fulfilled, false);
stack.push(a);
assertEquals(a, await b);
assertEquals(fulfilled, true);
});
Deno.test("DeferredAccessStack", async () => {
const stack_size = 10;
const stack = new DeferredAccessStack(
Array.from({ length: stack_size }, () => new LazilyInitializedObject()),
(e) => e.initialize(),
(e) => e.initialized,
);
assertEquals(stack.size, stack_size);
assertEquals(stack.available, stack_size);
assertEquals(await stack.initialized(), 0);
const a = await stack.pop();
assertEquals(await a.initialized, true);
assertEquals(stack.size, stack_size);
assertEquals(stack.available, stack_size - 1);
assertEquals(await stack.initialized(), 0);
stack.push(a);
assertEquals(stack.size, stack_size);
assertEquals(stack.available, stack_size);
assertEquals(await stack.initialized(), 1);
});
Deno.test("An empty DeferredAccessStack awaits until an object is back in the stack", async () => {
const stack_size = 1;
const stack = new DeferredAccessStack(
Array.from({ length: stack_size }, () => new LazilyInitializedObject()),
(e) => e.initialize(),
(e) => e.initialized,
);
const a = await stack.pop();
let fulfilled = false;
const b = stack.pop()
.then((e) => {
fulfilled = true;
return e;
});
await new Promise((r) => setTimeout(r, 100));
assertEquals(fulfilled, false);
stack.push(a);
assertEquals(a, await b);
assertEquals(fulfilled, true);
});