FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
xum/src/node/runtime/sshConnectionPool.test.ts at main · coder/xum · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
coder
/
xum
Public
Notifications
You must be signed in to change notification settings
Fork
135
Star
2k
Code
Issues
46
Pull requests
203
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
xum
/
src
/
node
/
runtime
/
sshConnectionPool.test.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
674 lines (569 loc) · 21.6 KB
Breadcrumbs
xum
/
src
/
node
/
runtime
/
sshConnectionPool.test.ts
Copy path
File metadata and controls
674 lines (569 loc) · 21.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
import
*
as
os
from
"os"
;
import
*
as
path
from
"path"
;
import
{
afterEach
,
describe
,
expect
,
test
,
spyOn
}
from
"bun:test"
;
import
{
appendOpenSSHHostKeyPolicyArgs
,
getControlPath
,
setOpenSSHHostKeyPolicyMode
,
SSHConnectionPool
,
type
SSHRuntimeConfig
,
}
from
"./sshConnectionPool"
;
describe
(
"sshConnectionPool"
,
(
)
=>
{
describe
(
"getControlPath"
,
(
)
=>
{
test
(
"identical configs produce same controlPath"
,
(
)
=>
{
const
config
:
SSHRuntimeConfig
=
{
host
:
"test.example.com"
,
srcBaseDir
:
"/work"
,
}
;
const
path1
=
getControlPath
(
config
)
;
const
path2
=
getControlPath
(
config
)
;
expect
(
path1
)
.
toBe
(
path2
)
;
}
)
;
test
(
"different hosts produce different controlPaths"
,
(
)
=>
{
const
config1
:
SSHRuntimeConfig
=
{
host
:
"host1.example.com"
,
srcBaseDir
:
"/work"
,
}
;
const
config2
:
SSHRuntimeConfig
=
{
host
:
"host2.example.com"
,
srcBaseDir
:
"/work"
,
}
;
const
path1
=
getControlPath
(
config1
)
;
const
path2
=
getControlPath
(
config2
)
;
expect
(
path1
)
.
not
.
toBe
(
path2
)
;
}
)
;
test
(
"different ports produce different controlPaths"
,
(
)
=>
{
const
config1
:
SSHRuntimeConfig
=
{
host
:
"test.com"
,
srcBaseDir
:
"/work"
,
port
:
22
,
}
;
const
config2
:
SSHRuntimeConfig
=
{
host
:
"test.com"
,
srcBaseDir
:
"/work"
,
port
:
2222
,
}
;
expect
(
getControlPath
(
config1
)
)
.
not
.
toBe
(
getControlPath
(
config2
)
)
;
}
)
;
test
(
"different identityFiles produce different controlPaths"
,
(
)
=>
{
const
config1
:
SSHRuntimeConfig
=
{
host
:
"test.com"
,
srcBaseDir
:
"/work"
,
identityFile
:
"/path/to/key1"
,
}
;
const
config2
:
SSHRuntimeConfig
=
{
host
:
"test.com"
,
srcBaseDir
:
"/work"
,
identityFile
:
"/path/to/key2"
,
}
;
expect
(
getControlPath
(
config1
)
)
.
not
.
toBe
(
getControlPath
(
config2
)
)
;
}
)
;
test
(
"different srcBaseDirs produce same controlPaths (connection shared)"
,
(
)
=>
{
// srcBaseDir is intentionally excluded from connection key -
// workspaces on the same host share health tracking and multiplexing
const
config1
:
SSHRuntimeConfig
=
{
host
:
"test.com"
,
srcBaseDir
:
"/work1"
,
}
;
const
config2
:
SSHRuntimeConfig
=
{
host
:
"test.com"
,
srcBaseDir
:
"/work2"
,
}
;
expect
(
getControlPath
(
config1
)
)
.
toBe
(
getControlPath
(
config2
)
)
;
}
)
;
test
(
"controlPath is in tmpdir with expected format"
,
(
)
=>
{
const
config
:
SSHRuntimeConfig
=
{
host
:
"test.com"
,
srcBaseDir
:
"/work"
,
}
;
const
controlPath
=
getControlPath
(
config
)
;
expect
(
controlPath
)
.
toContain
(
os
.
tmpdir
(
)
)
;
expect
(
controlPath
)
.
toMatch
(
/
m
u
x
-
s
s
h
-
[
a
-
f
0
-
9
]
{
12
}
$
/
)
;
}
)
;
test
(
"missing port defaults to 22 in hash calculation"
,
(
)
=>
{
const
config1
:
SSHRuntimeConfig
=
{
host
:
"test.com"
,
srcBaseDir
:
"/work"
,
port
:
22
,
}
;
const
config2
:
SSHRuntimeConfig
=
{
host
:
"test.com"
,
srcBaseDir
:
"/work"
,
// port omitted, should default to 22
}
;
expect
(
getControlPath
(
config1
)
)
.
toBe
(
getControlPath
(
config2
)
)
;
}
)
;
test
(
"missing identityFile defaults to 'default' in hash calculation"
,
(
)
=>
{
const
config1
:
SSHRuntimeConfig
=
{
host
:
"test.com"
,
srcBaseDir
:
"/work"
,
identityFile
:
undefined
,
}
;
const
config2
:
SSHRuntimeConfig
=
{
host
:
"test.com"
,
srcBaseDir
:
"/work"
,
// identityFile omitted
}
;
expect
(
getControlPath
(
config1
)
)
.
toBe
(
getControlPath
(
config2
)
)
;
}
)
;
}
)
;
}
)
;
describe
(
"appendOpenSSHHostKeyPolicyArgs"
,
(
)
=>
{
afterEach
(
(
)
=>
{
setOpenSSHHostKeyPolicyMode
(
"headless-fallback"
)
;
}
)
;
test
(
"appends fallback args in headless-fallback mode"
,
(
)
=>
{
const
args
:
string
[
]
=
[
"-T"
]
;
setOpenSSHHostKeyPolicyMode
(
"headless-fallback"
)
;
appendOpenSSHHostKeyPolicyArgs
(
args
)
;
expect
(
args
)
.
toEqual
(
[
"-T"
,
"-o"
,
"StrictHostKeyChecking=no"
,
"-o"
,
"UserKnownHostsFile=/dev/null"
,
]
)
;
}
)
;
test
(
"does not append fallback args in strict mode"
,
(
)
=>
{
const
args
:
string
[
]
=
[
"-T"
]
;
setOpenSSHHostKeyPolicyMode
(
"strict"
)
;
appendOpenSSHHostKeyPolicyArgs
(
args
)
;
expect
(
args
)
.
toEqual
(
[
"-T"
]
)
;
}
)
;
test
(
"defaults to headless-fallback"
,
(
)
=>
{
const
args
:
string
[
]
=
[
"-T"
]
;
appendOpenSSHHostKeyPolicyArgs
(
args
)
;
expect
(
args
)
.
toEqual
(
[
"-T"
,
"-o"
,
"StrictHostKeyChecking=no"
,
"-o"
,
"UserKnownHostsFile=/dev/null"
,
]
)
;
}
)
;
}
)
;
describe
(
"username isolation"
,
(
)
=>
{
test
(
"controlPath includes local username to prevent cross-user collisions"
,
(
)
=>
{
// This test verifies that os.userInfo().username is included in the hash
// On multi-user systems, different users connecting to the same remote
// would get different controlPaths, preventing permission errors
const
config
:
SSHRuntimeConfig
=
{
host
:
"test.com"
,
srcBaseDir
:
"/work"
,
}
;
const
controlPath
=
getControlPath
(
config
)
;
// The path should be deterministic for this user
expect
(
controlPath
)
.
toBe
(
getControlPath
(
config
)
)
;
const
expectedPrefix
=
path
.
join
(
os
.
tmpdir
(
)
,
"mux-ssh-"
)
;
expect
(
controlPath
.
startsWith
(
expectedPrefix
)
)
.
toBe
(
true
)
;
expect
(
controlPath
)
.
toMatch
(
/
m
u
x
-
s
s
h
-
[
a
-
f
0
-
9
]
{
12
}
$
/
)
;
}
)
;
}
)
;
describe
(
"SSHConnectionPool"
,
(
)
=>
{
describe
(
"health tracking"
,
(
)
=>
{
test
(
"getConnectionHealth returns undefined for unknown connection"
,
(
)
=>
{
const
pool
=
new
SSHConnectionPool
(
)
;
const
config
:
SSHRuntimeConfig
=
{
host
:
"unknown.example.com"
,
srcBaseDir
:
"/work"
,
}
;
expect
(
pool
.
getConnectionHealth
(
config
)
)
.
toBeUndefined
(
)
;
}
)
;
test
(
"markHealthy sets connection to healthy state"
,
(
)
=>
{
const
pool
=
new
SSHConnectionPool
(
)
;
const
config
:
SSHRuntimeConfig
=
{
host
:
"test.example.com"
,
srcBaseDir
:
"/work"
,
}
;
pool
.
markHealthy
(
config
)
;
const
health
=
pool
.
getConnectionHealth
(
config
)
;
expect
(
health
)
.
toBeDefined
(
)
;
expect
(
health
!
.
status
)
.
toBe
(
"healthy"
)
;
expect
(
health
!
.
consecutiveFailures
)
.
toBe
(
0
)
;
expect
(
health
!
.
lastSuccess
)
.
toBeInstanceOf
(
Date
)
;
}
)
;
test
(
"reportFailure puts connection into backoff"
,
(
)
=>
{
const
pool
=
new
SSHConnectionPool
(
)
;
const
config
:
SSHRuntimeConfig
=
{
host
:
"test.example.com"
,
srcBaseDir
:
"/work"
,
}
;
// Mark healthy first
pool
.
markHealthy
(
config
)
;
expect
(
pool
.
getConnectionHealth
(
config
)
?.
status
)
.
toBe
(
"healthy"
)
;
// Report a failure
pool
.
reportFailure
(
config
,
"Connection refused"
)
;
const
health
=
pool
.
getConnectionHealth
(
config
)
;
expect
(
health
?.
status
)
.
toBe
(
"unhealthy"
)
;
expect
(
health
?.
consecutiveFailures
)
.
toBe
(
1
)
;
expect
(
health
?.
lastError
)
.
toBe
(
"Connection refused"
)
;
expect
(
health
?.
backoffUntil
)
.
toBeDefined
(
)
;
}
)
;
test
(
"backoff caps at ~10s with jitter"
,
(
)
=>
{
const
pool
=
new
SSHConnectionPool
(
)
;
const
config
:
SSHRuntimeConfig
=
{
host
:
"test.example.com"
,
srcBaseDir
:
"/work"
,
}
;
// Report many failures to hit the cap
for
(
let
i
=
0
;
i
<
10
;
i
++
)
{
pool
.
reportFailure
(
config
,
"Connection refused"
)
;
}
const
health
=
pool
.
getConnectionHealth
(
config
)
!
;
const
backoffMs
=
health
.
backoffUntil
!
.
getTime
(
)
-
Date
.
now
(
)
;
// Max base is 10s, jitter adds ±20%, so max is ~12s (10 * 1.2)
expect
(
backoffMs
)
.
toBeGreaterThan
(
7_500
)
;
// 10 * 0.8 - some tolerance
expect
(
backoffMs
)
.
toBeLessThanOrEqual
(
12_500
)
;
// 10 * 1.2 + some tolerance
}
)
;
test
(
"resetBackoff clears backoff state after failed probe"
,
async
(
)
=>
{
const
pool
=
new
SSHConnectionPool
(
)
;
const
config
:
SSHRuntimeConfig
=
{
host
:
"nonexistent.invalid.host.test"
,
srcBaseDir
:
"/work"
,
}
;
// Trigger a failure via acquireConnection (will fail to connect)
// eslint-disable-next-line @typescript-eslint/await-thenable
await
expect
(
pool
.
acquireConnection
(
config
,
{
timeoutMs
:
1000
,
maxWaitMs
:
0
}
)
)
.
rejects
.
toThrow
(
)
;
// Verify we're now in backoff
const
healthBefore
=
pool
.
getConnectionHealth
(
config
)
;
expect
(
healthBefore
?.
status
)
.
toBe
(
"unhealthy"
)
;
expect
(
healthBefore
?.
backoffUntil
)
.
toBeDefined
(
)
;
// Reset backoff
pool
.
resetBackoff
(
config
)
;
const
healthAfter
=
pool
.
getConnectionHealth
(
config
)
;
expect
(
healthAfter
)
.
toBeDefined
(
)
;
expect
(
healthAfter
!
.
status
)
.
toBe
(
"unknown"
)
;
expect
(
healthAfter
!
.
consecutiveFailures
)
.
toBe
(
0
)
;
expect
(
healthAfter
!
.
backoffUntil
)
.
toBeUndefined
(
)
;
}
)
;
}
)
;
describe
(
"acquireConnection"
,
(
)
=>
{
test
(
"returns immediately for known healthy connection"
,
async
(
)
=>
{
const
pool
=
new
SSHConnectionPool
(
)
;
const
config
:
SSHRuntimeConfig
=
{
host
:
"test.example.com"
,
srcBaseDir
:
"/work"
,
}
;
// Mark as healthy first
pool
.
markHealthy
(
config
)
;
// Should return immediately without probing
const
start
=
Date
.
now
(
)
;
await
pool
.
acquireConnection
(
config
)
;
const
elapsed
=
Date
.
now
(
)
-
start
;
// Should be nearly instant (< 50ms)
expect
(
elapsed
)
.
toBeLessThan
(
50
)
;
}
)
;
test
(
"waits through backoff (bounded) instead of throwing"
,
async
(
)
=>
{
const
pool
=
new
SSHConnectionPool
(
)
;
const
config
:
SSHRuntimeConfig
=
{
host
:
"test.example.com"
,
srcBaseDir
:
"/work"
,
}
;
// Put host into backoff without doing a real probe.
pool
.
reportFailure
(
config
,
"Connection refused"
)
;
expect
(
pool
.
getConnectionHealth
(
config
)
?.
backoffUntil
)
.
toBeDefined
(
)
;
const
sleepCalls
:
number
[
]
=
[
]
;
const
onWaitCalls
:
number
[
]
=
[
]
;
await
pool
.
acquireConnection
(
config
,
{
onWait
:
(
ms
)
=>
{
onWaitCalls
.
push
(
ms
)
;
}
,
sleep
:
(
ms
)
=>
{
sleepCalls
.
push
(
ms
)
;
// Simulate time passing / recovery.
pool
.
markHealthy
(
config
)
;
return
Promise
.
resolve
(
)
;
}
,
}
)
;
expect
(
sleepCalls
.
length
)
.
toBe
(
1
)
;
expect
(
onWaitCalls
.
length
)
.
toBe
(
1
)
;
expect
(
sleepCalls
[
0
]
)
.
toBeGreaterThan
(
0
)
;
expect
(
onWaitCalls
[
0
]
)
.
toBe
(
sleepCalls
[
0
]
)
;
expect
(
pool
.
getConnectionHealth
(
config
)
?.
status
)
.
toBe
(
"healthy"
)
;
}
)
;
test
(
"throws immediately when in backoff"
,
async
(
)
=>
{
const
pool
=
new
SSHConnectionPool
(
)
;
const
config
:
SSHRuntimeConfig
=
{
host
:
"nonexistent.invalid.host.test"
,
srcBaseDir
:
"/work"
,
}
;
// Trigger a failure to put connection in backoff
// eslint-disable-next-line @typescript-eslint/await-thenable
await
expect
(
pool
.
acquireConnection
(
config
,
{
timeoutMs
:
1000
,
maxWaitMs
:
0
}
)
)
.
rejects
.
toThrow
(
)
;
// Second call should throw immediately with backoff message
// eslint-disable-next-line @typescript-eslint/await-thenable
await
expect
(
pool
.
acquireConnection
(
config
,
{
maxWaitMs
:
0
}
)
)
.
rejects
.
toThrow
(
/
i
n
b
a
c
k
o
f
f
/
)
;
}
)
;
test
(
"getControlPath returns deterministic path"
,
(
)
=>
{
const
pool
=
new
SSHConnectionPool
(
)
;
const
config
:
SSHRuntimeConfig
=
{
host
:
"test.example.com"
,
srcBaseDir
:
"/work"
,
}
;
const
path1
=
pool
.
getControlPath
(
config
)
;
const
path2
=
pool
.
getControlPath
(
config
)
;
expect
(
path1
)
.
toBe
(
path2
)
;
expect
(
path1
)
.
toBe
(
getControlPath
(
config
)
)
;
}
)
;
test
(
"does not record backoff when a probe is aborted"
,
async
(
)
=>
{
const
pool
=
new
SSHConnectionPool
(
)
;
const
config
:
SSHRuntimeConfig
=
{
host
:
"abort.example.com"
,
srcBaseDir
:
"/work"
,
}
;
const
abortController
=
new
AbortController
(
)
;
const
spy
=
spyOn
(
pool
as
unknown
as
{
probeConnection
:
(
)
=>
Promise
<
void
>
}
,
"probeConnection"
)
.
mockImplementationOnce
(
(
)
=>
{
abortController
.
abort
(
)
;
return
Promise
.
reject
(
new
Error
(
"Operation aborted"
)
)
;
}
)
;
// eslint-disable-next-line @typescript-eslint/await-thenable
await
expect
(
pool
.
acquireConnection
(
config
,
{
timeoutMs
:
1000
,
maxWaitMs
:
0
,
abortSignal
:
abortController
.
signal
,
}
)
)
.
rejects
.
toThrow
(
"Operation aborted"
)
;
expect
(
pool
.
getConnectionHealth
(
config
)
)
.
toBeUndefined
(
)
;
spy
.
mockRestore
(
)
;
}
)
;
test
(
"records backoff when probe rejects without recording it (safety net)"
,
async
(
)
=>
{
const
pool
=
new
SSHConnectionPool
(
)
;
const
config
:
SSHRuntimeConfig
=
{
host
:
"askpass-fail.example.com"
,
srcBaseDir
:
"/work"
,
}
;
// Simulate a probe that fails before reaching markFailedByKey
// (e.g., createAskpassSession throwing on fs.watch ENOSPC).
const
spy
=
spyOn
(
pool
as
unknown
as
{
probeConnection
:
(
)
=>
Promise
<
void
>
}
,
"probeConnection"
)
.
mockRejectedValueOnce
(
new
Error
(
"ENOSPC: no space left on device"
)
)
;
// eslint-disable-next-line @typescript-eslint/await-thenable
await
expect
(
pool
.
acquireConnection
(
config
,
{
timeoutMs
:
1000
,
maxWaitMs
:
0
}
)
)
.
rejects
.
toThrow
(
/
E
N
O
S
P
C
/
)
;
// Safety net should have recorded backoff despite probeConnection not doing so.
const
health
=
pool
.
getConnectionHealth
(
config
)
;
expect
(
health
?.
status
)
.
toBe
(
"unhealthy"
)
;
expect
(
health
?.
backoffUntil
)
.
toBeDefined
(
)
;
expect
(
health
?.
lastError
)
.
toContain
(
"ENOSPC"
)
;
spy
.
mockRestore
(
)
;
}
)
;
}
)
;
describe
(
"askpass prompt classification"
,
(
)
=>
{
// classifyAskpassPrompt() routes prompts containing "continue connecting"
// through host-key verification and treats other prompts as credentials.
const
HOST_KEY_PATTERN
=
/
c
o
n
t
i
n
u
e
c
o
n
n
e
c
t
i
n
g
/
i
;
test
(
"detects standard host-key confirmation prompt"
,
(
)
=>
{
expect
(
HOST_KEY_PATTERN
.
test
(
"Are you sure you want to continue connecting (yes/no/[fingerprint])? "
)
)
.
toBe
(
true
)
;
}
)
;
test
(
"detects host-key prompt case-insensitively"
,
(
)
=>
{
expect
(
HOST_KEY_PATTERN
.
test
(
"Are you sure you want to Continue Connecting (yes/no)?"
)
)
.
toBe
(
true
)
;
}
)
;
test
(
"rejects passphrase prompt"
,
(
)
=>
{
expect
(
HOST_KEY_PATTERN
.
test
(
"Enter passphrase for key '/home/user/.ssh/id_ed25519':"
)
)
.
toBe
(
false
)
;
}
)
;
test
(
"rejects password prompt"
,
(
)
=>
{
expect
(
HOST_KEY_PATTERN
.
test
(
"user@host's password:"
)
)
.
toBe
(
false
)
;
}
)
;
test
(
"rejects empty prompt"
,
(
)
=>
{
expect
(
HOST_KEY_PATTERN
.
test
(
""
)
)
.
toBe
(
false
)
;
}
)
;
}
)
;
describe
(
"singleflighting"
,
(
)
=>
{
test
(
"concurrent acquireConnection calls share same probe"
,
async
(
)
=>
{
const
pool
=
new
SSHConnectionPool
(
)
;
const
config
:
SSHRuntimeConfig
=
{
host
:
"nonexistent.invalid.host.test"
,
srcBaseDir
:
"/work"
,
}
;
// All concurrent calls should share the same probe and get same result
const
results
=
await
Promise
.
allSettled
(
[
pool
.
acquireConnection
(
config
,
{
timeoutMs
:
1000
,
maxWaitMs
:
0
}
)
,
pool
.
acquireConnection
(
config
,
{
timeoutMs
:
1000
,
maxWaitMs
:
0
}
)
,
pool
.
acquireConnection
(
config
,
{
timeoutMs
:
1000
,
maxWaitMs
:
0
}
)
,
]
)
;
// All should be rejected (connection fails)
expect
(
results
.
every
(
(
r
)
=>
r
.
status
===
"rejected"
)
)
.
toBe
(
true
)
;
// Only 1 failure should be recorded (not 3) - proves singleflighting worked
expect
(
pool
.
getConnectionHealth
(
config
)
?.
consecutiveFailures
)
.
toBe
(
1
)
;
}
)
;
test
(
"re-checks requested ControlPath after waiting for another probe"
,
async
(
)
=>
{
const
pool
=
new
SSHConnectionPool
(
)
;
const
config
:
SSHRuntimeConfig
=
{
host
:
"test.example.com"
,
srcBaseDir
:
"/work"
,
}
;
const
privatePool
=
pool
as
unknown
as
{
probeConnection
:
(
config
:
SSHRuntimeConfig
,
timeoutMs
:
number
,
key
:
string
,
controlPath
:
string
)
=>
Promise
<
void
>
;
markHealthyByKey
:
(
key
:
string
)
=>
void
;
markControlPathReady
:
(
key
:
string
,
controlPath
:
string
)
=>
void
;
}
;
const
probeCalls
:
string
[
]
=
[
]
;
let
releaseFirstProbe
!
:
(
)
=>
void
;
let
signalFirstProbeStarted
!
:
(
)
=>
void
;
const
firstProbeStarted
=
new
Promise
<
void
>
(
(
resolve
)
=>
{
signalFirstProbeStarted
=
resolve
;
}
)
;
const
probeSpy
=
spyOn
(
privatePool
,
"probeConnection"
)
.
mockImplementation
(
async
(
_config
,
_timeoutMs
,
key
,
controlPath
)
=>
{
probeCalls
.
push
(
controlPath
)
;
if
(
probeCalls
.
length
===
1
)
{
signalFirstProbeStarted
(
)
;
await
new
Promise
<
void
>
(
(
resolve
)
=>
{
releaseFirstProbe
=
(
)
=>
{
privatePool
.
markHealthyByKey
(
key
)
;
privatePool
.
markControlPathReady
(
key
,
controlPath
)
;
resolve
(
)
;
}
;
}
)
;
return
;
}
privatePool
.
markHealthyByKey
(
key
)
;
privatePool
.
markControlPathReady
(
key
,
controlPath
)
;
}
)
;
const
firstAcquire
=
pool
.
acquireConnection
(
config
,
{
controlPath
:
"/tmp/mux-control-a"
,
maxWaitMs
:
0
,
}
)
;
await
firstProbeStarted
;
const
secondAcquire
=
pool
.
acquireConnection
(
config
,
{
controlPath
:
"/tmp/mux-control-b"
,
maxWaitMs
:
0
,
}
)
;
releaseFirstProbe
(
)
;
await
Promise
.
all
(
[
firstAcquire
,
secondAcquire
]
)
;
expect
(
probeCalls
)
.
toEqual
(
[
"/tmp/mux-control-a"
,
"/tmp/mux-control-b"
]
)
;
probeSpy
.
mockRestore
(
)
;
}
)
;
test
(
"caps the follow-up probe timeout to the remaining wait budget"
,
async
(
)
=>
{
const
pool
=
new
SSHConnectionPool
(
)
;
const
config
:
SSHRuntimeConfig
=
{
host
:
"test.example.com"
,
srcBaseDir
:
"/work"
,
}
;
const
privatePool
=
pool
as
unknown
as
{
probeConnection
:
(
config
:
SSHRuntimeConfig
,
timeoutMs
:
number
,
key
:
string
,
controlPath
:
string
)
=>
Promise
<
void
>
;
markHealthyByKey
:
(
key
:
string
)
=>
void
;
markControlPathReady
:
(
key
:
string
,
controlPath
:
string
)
=>
void
;
}
;
const
probeCalls
:
Array
<
{
controlPath
:
string
;
timeoutMs
:
number
}
>
=
[
]
;
let
releaseFirstProbe
!
:
(
)
=>
void
;
let
signalFirstProbeStarted
!
:
(
)
=>
void
;
const
firstProbeStarted
=
new
Promise
<
void
>
(
(
resolve
)
=>
{
signalFirstProbeStarted
=
resolve
;
}
)
;
let
now
=
1_000
;
const
nowSpy
=
spyOn
(
Date
,
"now"
)
.
mockImplementation
(
(
)
=>
now
)
;
const
probeSpy
=
spyOn
(
privatePool
,
"probeConnection"
)
.
mockImplementation
(
async
(
_config
,
timeoutMs
,
key
,
controlPath
)
=>
{
probeCalls
.
push
(
{
controlPath
,
timeoutMs
}
)
;
if
(
probeCalls
.
length
===
1
)
{
signalFirstProbeStarted
(
)
;
await
new
Promise
<
void
>
(
(
resolve
)
=>
{
releaseFirstProbe
=
(
)
=>
{
now
+=
25
;
privatePool
.
markHealthyByKey
(
key
)
;
privatePool
.
markControlPathReady
(
key
,
controlPath
)
;
resolve
(
)
;
}
;
}
)
;
return
;
}
privatePool
.
markHealthyByKey
(
key
)
;
privatePool
.
markControlPathReady
(
key
,
controlPath
)
;
}
)
;
// try/finally: a leaked Date.now spy freezes time process-wide and breaks
// downstream suites (e.g. WorkflowService crash-recovery retries).
try
{
const
firstAcquire
=
pool
.
acquireConnection
(
config
,
{
controlPath
:
"/tmp/mux-control-a"
,
timeoutMs
:
30
,
maxWaitMs
:
30
,
}
)
;
await
firstProbeStarted
;
const
secondAcquire
=
pool
.
acquireConnection
(
config
,
{
controlPath
:
"/tmp/mux-control-b"
,
timeoutMs
:
30
,
maxWaitMs
:
30
,
}
)
;
releaseFirstProbe
(
)
;
await
Promise
.
all
(
[
firstAcquire
,
secondAcquire
]
)
;
expect
(
probeCalls
)
.
toEqual
(
[
{
controlPath
:
"/tmp/mux-control-a"
,
timeoutMs
:
30
}
,
{
controlPath
:
"/tmp/mux-control-b"
,
timeoutMs
:
5
}
,
]
)
;
}
finally
{
probeSpy
.
mockRestore
(
)
;
nowSpy
.
mockRestore
(
)
;
}
}
)
;
test
(
"callers waking from backoff share single probe (herd only released on success)"
,
async
(
)
=>
{
const
pool
=
new
SSHConnectionPool
(
)
;
const
config
:
SSHRuntimeConfig
=
{
host
:
"test.example.com"
,
srcBaseDir
:
"/work"
,
}
;
// Put connection in backoff
pool
.
reportFailure
(
config
,
"Initial failure"
)
;
expect
(
pool
.
getConnectionHealth
(
config
)
?.
consecutiveFailures
)
.
toBe
(
1
)
;
let
probeCount
=
0
;
const
sleepResolvers
:
Array
<
(
)
=>
void
>
=
[
]
;
// Start 3 waiters - they'll all sleep through backoff
const
waiters
=
[
1
,
2
,
3
]
.
map
(
(
)
=>
pool
.
acquireConnection
(
config
,
{
sleep
:
(
)
=>
new
Promise
<
void
>
(
(
resolve
)
=>
{
sleepResolvers
.
push
(
(
)
=>
{
// When sleep resolves, simulate recovery (mark healthy)
// This happens during the first probe - all waiters share it
if
(
probeCount
===
0
)
{
probeCount
++
;
pool
.
markHealthy
(
config
)
;
}
resolve
(
)
;
}
)
;
}
)
,
}
)
)
;
// Let all sleepers proceed
await
Promise
.
resolve
(
)
;
// Let all acquireConnection calls reach sleep
expect
(
sleepResolvers
.
length
)
.
toBe
(
3
)
;
// Wake them all up "simultaneously"
sleepResolvers
.
forEach
(
(
resolve
)
=>
resolve
(
)
)
;
// All should succeed
await
Promise
.
all
(
waiters
)
;
// Only one "probe" (markHealthy) should have happened
expect
(
probeCount
)
.
toBe
(
1
)
;
expect
(
pool
.
getConnectionHealth
(
config
)
?.
status
)
.
toBe
(
"healthy"
)
;
}
)
;
}
)
;
}
)
;
Back
|
FazBrowse Home
|
New Git URL