FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScriptSolidServer/test/withlock.test.js at gh-pages · JavaScriptSolidServer/JavaScriptSolidServer · GitHub
JavaScriptSolidServer
JavaScriptSolidServer
Repository navigation
Code
Issues
164
(164)
Pull requests
14
(14)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScriptSolidServer
/
test
/
withlock.test.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
81 lines (73 loc) · 3.34 KB
Breadcrumbs
JavaScriptSolidServer
/
test
/
withlock.test.js
Copy path
File metadata and controls
81 lines (73 loc) · 3.34 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
/**
* storage.withLock: a slow holder keeps its lock, a dead holder loses it (#600)
*/
import
{
describe
,
it
,
before
,
after
}
from
'node:test'
;
import
assert
from
'node:assert'
;
import
fs
from
'fs-extra'
;
import
os
from
'os'
;
import
path
from
'path'
;
import
{
withLock
,
lockPathFor
}
from
'../src/storage/filesystem.js'
;
const
sleep
=
(
ms
)
=>
new
Promise
(
(
r
)
=>
setTimeout
(
r
,
ms
)
)
;
let
ROOT
,
originalDataRoot
;
describe
(
'withLock'
,
(
)
=>
{
before
(
async
(
)
=>
{
originalDataRoot
=
process
.
env
.
DATA_ROOT
;
ROOT
=
await
fs
.
mkdtemp
(
path
.
join
(
os
.
tmpdir
(
)
,
'jss-lock-'
)
)
;
process
.
env
.
DATA_ROOT
=
ROOT
;
await
fs
.
ensureDir
(
path
.
join
(
ROOT
,
'pod'
)
)
;
}
)
;
after
(
async
(
)
=>
{
if
(
originalDataRoot
===
undefined
)
delete
process
.
env
.
DATA_ROOT
;
else
process
.
env
.
DATA_ROOT
=
originalDataRoot
;
await
fs
.
remove
(
ROOT
)
;
}
)
;
it
(
'a holder slower than the stale timeout keeps the lock: the second writer waits for it'
,
async
(
)
=>
{
const
log
=
[
]
;
const
opts
=
{
staleMs
:
100
,
waitMs
:
5000
}
;
const
a
=
withLock
(
'/pod/slow.json'
,
async
(
)
=>
{
await
sleep
(
400
)
;
log
.
push
(
'A done'
)
;
}
,
opts
)
;
await
sleep
(
20
)
;
const
b
=
withLock
(
'/pod/slow.json'
,
async
(
)
=>
{
log
.
push
(
'B start'
)
;
}
,
opts
)
;
await
Promise
.
all
(
[
a
,
b
]
)
;
assert
.
deepStrictEqual
(
log
,
[
'A done'
,
'B start'
]
)
;
}
)
;
it
(
'a lock left by a dead holder is broken after the stale timeout'
,
async
(
)
=>
{
const
lockPath
=
lockPathFor
(
'/pod/dead.json'
)
;
await
fs
.
writeFile
(
lockPath
,
''
)
;
const
old
=
new
Date
(
Date
.
now
(
)
-
60_000
)
;
await
fs
.
utimes
(
lockPath
,
old
,
old
)
;
const
started
=
Date
.
now
(
)
;
let
ran
=
false
;
await
withLock
(
'/pod/dead.json'
,
async
(
)
=>
{
ran
=
true
;
}
,
{
staleMs
:
100
,
waitMs
:
5000
}
)
;
assert
.
ok
(
ran
,
'fn ran'
)
;
assert
.
ok
(
Date
.
now
(
)
-
started
<
1000
,
'acquired without waiting out the timeout'
)
;
assert
.
ok
(
!
await
fs
.
pathExists
(
lockPath
)
,
'lock removed afterwards'
)
;
}
)
;
it
(
'many contenders on a dead lock: all run, never two at once, no lock left behind'
,
async
(
)
=>
{
const
lockPath
=
lockPathFor
(
'/pod/crowd.json'
)
;
await
fs
.
writeFile
(
lockPath
,
''
)
;
const
old
=
new
Date
(
Date
.
now
(
)
-
60_000
)
;
await
fs
.
utimes
(
lockPath
,
old
,
old
)
;
let
inside
=
0
,
peak
=
0
,
ran
=
0
;
const
opts
=
{
staleMs
:
100
,
waitMs
:
5000
}
;
await
Promise
.
all
(
Array
.
from
(
{
length
:
8
}
,
(
)
=>
withLock
(
'/pod/crowd.json'
,
async
(
)
=>
{
inside
++
;
peak
=
Math
.
max
(
peak
,
inside
)
;
ran
++
;
await
sleep
(
30
)
;
inside
--
;
}
,
opts
)
)
)
;
assert
.
strictEqual
(
ran
,
8
,
'every contender ran'
)
;
assert
.
strictEqual
(
peak
,
1
,
'never two holders at once'
)
;
assert
.
ok
(
!
await
fs
.
pathExists
(
lockPath
)
,
'lock removed afterwards'
)
;
const
leftovers
=
(
await
fs
.
readdir
(
path
.
join
(
ROOT
,
'pod'
)
)
)
.
filter
(
(
n
)
=>
n
.
startsWith
(
'.crowd.json.lock'
)
)
;
assert
.
deepStrictEqual
(
leftovers
,
[
]
,
'no moved-aside lock files left'
)
;
}
)
;
it
(
'a live lock that is never released ends in ELOCKED after waitMs'
,
async
(
)
=>
{
const
lockPath
=
lockPathFor
(
'/pod/held.json'
)
;
await
fs
.
writeFile
(
lockPath
,
''
)
;
const
keep
=
setInterval
(
(
)
=>
{
const
n
=
new
Date
(
)
;
fs
.
utimes
(
lockPath
,
n
,
n
)
.
catch
(
(
)
=>
{
}
)
;
}
,
20
)
;
try
{
await
assert
.
rejects
(
withLock
(
'/pod/held.json'
,
async
(
)
=>
{
}
,
{
staleMs
:
100
,
waitMs
:
300
}
)
,
(
e
)
=>
e
.
code
===
'ELOCKED'
)
;
}
finally
{
clearInterval
(
keep
)
;
await
fs
.
remove
(
lockPath
)
;
}
}
)
;
}
)
;
Back
|
FazBrowse Home
|
New Git URL