-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
pidfile.jl
367 lines (326 loc) · 10.3 KB
/
pidfile.jl
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
using FileWatching.Pidfile
using Test
using Base.Filesystem: File
using FileWatching.Pidfile: iswindows,
write_pidfile, parse_pidfile,
isvalidpid, stale_pidfile,
tryopen_exclusive, open_exclusive
# helper utilities
struct MemoryFile <: Base.AbstractPipe
io::IOBuffer
mtime::Float64
end
Base.pipe_reader(io::MemoryFile) = io.io
Base.Filesystem.mtime(io::MemoryFile) = io.mtime
# set the process umask so we can test the behavior of
# open mask without interference from parent's state
# and create a test environment temp directory
umask(new_mask) = ccall((@static iswindows() ? :_umask : :umask), Cint, (Cint,), new_mask)
# TODO: Use targeted @test_log tests instead of suppressing all logs to hide the expected warnings
Base.CoreLogging.with_logger(Base.CoreLogging.NullLogger()) do
@testset "Pidfile.jl" begin
old_umask = umask(0o002)
try
mktempdir() do dir
cd(dir) do
# now start tests definitions:
@testset "validpid" begin
mypid = getpid() % Cuint
@test isvalidpid(gethostname(), mypid)
@test isvalidpid("", mypid)
@test !isvalidpid("", 0 % Cuint)
@test isvalidpid("NOT" * gethostname(), mypid)
@test isvalidpid("NOT" * gethostname(), 0 % Cuint)
@test isvalidpid("NOT" * gethostname(), -1 % Cuint)
if !iswindows()
@test isvalidpid("", 1 % Cuint)
@test !isvalidpid("", -1 % Cuint)
@test !isvalidpid("", -mypid)
end
end
@testset "write_pidfile" begin
buf = IOBuffer()
pid, host, age = 0, "", 123
pid2, host2, age2 = parse_pidfile(MemoryFile(seekstart(buf), time() - age))
@test pid == pid2
@test host == host2
@test age ≈ age2 atol=5
host = " host\r\n"
write(buf, "-1 $host")
pid2, host2, age2 = parse_pidfile(MemoryFile(seekstart(buf), time() - age))
@test pid == pid2
@test host == host2
@test age ≈ age2 atol=5
truncate(seekstart(buf), 0)
pid, host = getpid(), gethostname()
write_pidfile(buf, pid)
@test read(seekstart(buf), String) == "$pid $host"
pid2, host2, age2 = parse_pidfile(MemoryFile(seekstart(buf), time() - age))
@test pid == pid2
@test host == host2
@test age ≈ age2 atol=5
truncate(seekstart(buf), 0)
@testset "parse_pidfile" begin
age = 0
@test parse_pidfile("nonexist") === (Cuint(0), "", 0.0)
open(io -> write_pidfile(io, pid), "pidfile", "w")
pid2, host2, age2 = parse_pidfile("pidfile")
@test pid == pid2
@test host == host2
@test age ≈ age2 atol=10
rm("pidfile")
end
end
@assert !ispath("pidfile")
@testset "open_exclusive" begin
f = open_exclusive("pidfile")::File
try
# check that f is open and read-writable
@test isfile("pidfile")
@test filemode("pidfile") & 0o777 == 0o444
@test filemode(f) & 0o777 == 0o444
@test filesize(f) == 0
@test write(f, "a") == 1
@test filesize(f) == 1
@test read(seekstart(f), String) == "a"
chmod("pidfile", 0o600)
@test filemode(f) & 0o777 == (iswindows() ? 0o666 : 0o600)
finally
close(f)
end
# release the pidfile after a short delay
deleted = false
rmtask = @async begin
sleep(3)
rm("pidfile")
deleted = true
end
isdefined(Base, :errormonitor) && Base.errormonitor(rmtask)
@test isfile("pidfile")
@test !deleted
# open the pidfile again (should wait for it to disappear first)
t = @elapsed f2 = open_exclusive(joinpath(dir, "pidfile"))::File
try
@test deleted
@test isfile("pidfile")
@test t > 2
if t > 6
println("INFO: watch_file optimization appears to have NOT succeeded")
end
@test filemode(f2) & 0o777 == 0o444
@test filesize(f2) == 0
@test write(f2, "bc") == 2
@test read(seekstart(f2), String) == "bc"
@test filesize(f2) == 2
finally
close(f2)
end
rm("pidfile")
wait(rmtask)
# now test with a long delay and other non-default options
f = open_exclusive("pidfile", mode = 0o000)::File
try
@test filemode(f) & 0o777 == (iswindows() ? 0o444 : 0o000)
finally
close(f)
end
deleted = false
rmtask = @async begin
sleep(8)
rm("pidfile")
deleted = true
end
isdefined(Base, :errormonitor) && Base.errormonitor(rmtask)
@test isfile("pidfile")
@test !deleted
# open the pidfile again (should wait for it to disappear first)
t = @elapsed f2 = open_exclusive("pidfile", mode = 0o777, poll_interval = 1.0)::File
try
@test deleted
@test isfile("pidfile")
@test filemode(f2) & 0o777 == (iswindows() ? 0o666 : 0o775)
@test write(f2, "def") == 3
@test read(seekstart(f2), String) == "def"
@test t > 7
finally
close(f2)
end
rm("pidfile")
wait(rmtask)
@testset "test for wait == false cases" begin
f = open_exclusive("pidfile", wait=false)
@test isfile("pidfile")
close(f)
rm("pidfile")
f = open_exclusive("pidfile")::File
deleted = false
rmtask = @async begin
sleep(2)
@test Pidfile.tryrmopenfile("pidfile")
deleted = true
end
isdefined(Base, :errormonitor) && Base.errormonitor(rmtask)
t1 = time()
@test_throws ErrorException open_exclusive("pidfile", wait=false)
@test time()-t1 ≈ 0 atol=1
sleep(1)
@test !deleted
t1 = time()
@test_throws ErrorException open_exclusive("pidfile", wait=false)
@test time()-t1 ≈ 0 atol=1
wait(rmtask)
@test deleted
t = @elapsed f2 = open_exclusive("pidfile", wait=false)::File
@test isfile("pidfile")
@test t ≈ 0 atol=1
close(f)
close(f2)
rm("pidfile")
end
end
@assert !ispath("pidfile")
@testset "open_exclusive: break lock" begin
# test for stale_age
t = @elapsed f = open_exclusive("pidfile", poll_interval=3, stale_age=10)::File
try
write_pidfile(f, getpid())
finally
close(f)
end
@test t < 2
t = @elapsed f = open_exclusive("pidfile", poll_interval=3, stale_age=1)::File
close(f)
@test 20 < t < 50
rm("pidfile")
t = @elapsed f = open_exclusive("pidfile", poll_interval=3, stale_age=10)::File
close(f)
@test t < 2
t = @elapsed f = open_exclusive("pidfile", poll_interval=3, stale_age=10)::File
close(f)
@test 8 < t < 20
rm("pidfile")
end
@testset "open_exclusive: other errors" begin
error = @test_throws(Base.IOError, open_exclusive("nonexist/folder"))
@test error.value.code == Base.UV_ENOENT
error = @test_throws(Base.IOError, open_exclusive(""))
@test error.value.code == Base.UV_ENOENT
end
@assert !ispath("pidfile")
@testset "mkpidlock" begin
lockf = mkpidlock("pidfile")
@test lockf.update === nothing
waittask = @async begin
sleep(3)
cd(homedir()) do
return close(lockf)
end
end
isdefined(Base, :errormonitor) && Base.errormonitor(waittask)
# mkpidlock with no waiting
t = @elapsed @test_throws ErrorException mkpidlock("pidfile", wait=false)
@test t ≈ 0 atol=1
t = @elapsed lockf1 = mkpidlock(joinpath(dir, "pidfile"))
@test t > 2
@test istaskdone(waittask) && fetch(waittask)
@test !close(lockf)
finalize(lockf1)
t = @elapsed lockf2 = mkpidlock("pidfile")
@test t < 2
@test !close(lockf1)
# test manual breakage of the lock
# is correctly handled
@test Pidfile.tryrmopenfile("pidfile")
t = @elapsed lockf3 = mkpidlock("pidfile")
@test t < 2
@test isopen(lockf2.fd)
@test !close(lockf2)
@test !isopen(lockf2.fd)
@test isfile("pidfile")
@test close(lockf3)
@test !isfile("pidfile")
# Just for coverage's sake, run a test with do-block syntax
lock_times = Float64[]
synchronizer = Base.Event()
t_loop = @async begin
wait(synchronizer)
for idx in 1:100
t = @elapsed mkpidlock("do_block_pidfile") do
# nothing
end
sleep(0.01)
push!(lock_times, t)
end
end
isdefined(Base, :errormonitor) && Base.errormonitor(t_loop)
mkpidlock("do_block_pidfile") do
notify(synchronizer)
sleep(3)
end
wait(t_loop)
@test maximum(lock_times) > 2
@test minimum(lock_times) < 1
end
@assert !ispath("pidfile")
@testset "mkpidlock update" begin
lockf = mkpidlock("pidfile")
@test lockf.update === nothing
new = mtime(lockf.fd)
@test new ≈ time() atol=1
sleep(1)
@test mtime(lockf.fd) == new
touch(lockf)
old, new = new, mtime(lockf.fd)
@test new != old
@test new ≈ time() atol=1
close(lockf)
lockf = mkpidlock("pidfile"; refresh=0.2)
new = mtime(lockf.fd)
@test new ≈ time() atol=1
for i = 1:10
sleep(0.5)
old, new = new, mtime(lockf.fd)
@test new != old
@test new ≈ time() atol=1
end
@test isopen(lockf.update::Timer)
close(lockf)
@test !isopen(lockf.update::Timer)
lockf = mkpidlock("pidfile"; stale_age=10)
@test lockf.update isa Timer
close(lockf.update) # simulate a finalizer running in an undefined order
close(lockf)
end
@assert !ispath("pidfile")
@testset "mkpidlock for child" begin
proc = open(`cat`, "w", devnull)
lock = mkpidlock("pidfile", proc)
@test isopen(lock.fd)
@test isfile("pidfile")
close(proc)
@test success(proc)
sleep(1) # give some time for the other task to finish releasing the lock resources
@test !isopen(lock.fd)
@test !isfile("pidfile")
error = @test_throws Base.IOError mkpidlock("pidfile", proc)
@test error.value.code == Base.UV_ESRCH
end
@assert !ispath("pidfile-2")
@testset "mkpidlock non-blocking stale lock break" begin
# mkpidlock with no waiting
lockf = mkpidlock("pidfile-2", wait=false)
@test lockf.update === nothing
sleep(1)
t = @elapsed @test_throws ErrorException mkpidlock("pidfile-2", wait=false, stale_age=1, poll_interval=1, refresh=0)
@test t ≈ 0 atol=1
sleep(5)
t = @elapsed (lockf2 = mkpidlock("pidfile-2", wait=false, stale_age=.1, poll_interval=1, refresh=0))
@test t ≈ 0 atol=1
close(lockf)
close(lockf2)
end
end; end # cd(tempdir)
finally
umask(old_umask)
end; end # testset
end # with_logger