Skip to content

Commit cd61451

Browse files
committed
fix tests
1 parent efe8feb commit cd61451

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

test/file.jl

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -157,19 +157,24 @@ end
157157
import Base.Filesystem: TEMP_CLEANUP_MIN, TEMP_CLEANUP_MAX, TEMP_CLEANUP
158158

159159
function with_temp_cleanup(f::Function, n::Int)
160+
local SAVE_TEMP_CLEANUP
161+
@lock TEMP_CLEANUP begin
162+
SAVE_TEMP_CLEANUP = copy(TEMP_CLEANUP[])
163+
empty!(TEMP_CLEANUP[])
164+
end
160165
SAVE_TEMP_CLEANUP_MIN = TEMP_CLEANUP_MIN[]
161166
SAVE_TEMP_CLEANUP_MAX = TEMP_CLEANUP_MAX[]
162-
SAVE_TEMP_CLEANUP = copy(TEMP_CLEANUP)
163-
empty!(TEMP_CLEANUP)
164167
TEMP_CLEANUP_MIN[] = n
165168
TEMP_CLEANUP_MAX[] = n
166169
try f()
167170
finally
168171
Sys.iswindows() && GC.gc(true)
169-
for t in keys(TEMP_CLEANUP)
170-
rm(t, recursive=true, force=true)
172+
@lock TEMP_CLEANUP begin
173+
for t in keys(TEMP_CLEANUP[])
174+
rm(t, recursive=true, force=true)
175+
end
176+
copy!(TEMP_CLEANUP[], SAVE_TEMP_CLEANUP)
171177
end
172-
copy!(TEMP_CLEANUP, SAVE_TEMP_CLEANUP)
173178
TEMP_CLEANUP_MAX[] = SAVE_TEMP_CLEANUP_MAX
174179
TEMP_CLEANUP_MIN[] = SAVE_TEMP_CLEANUP_MIN
175180
end
@@ -185,23 +190,23 @@ end
185190
n = 12 # cleanup min & max
186191
@assert n % 2 == n % 3 == 0 # otherwise tests won't work
187192
with_temp_cleanup(n) do
188-
@test length(TEMP_CLEANUP) == 0
193+
@test lock(length, TEMP_CLEANUP) == 0
189194
@test TEMP_CLEANUP_MAX[] == n
190195
# for n mktemps, no purging is triggered
191196
temps = String[]
192197
for i = 1:n
193198
t = i % 2 == 0 ? mktempfile() : mktempdir()
194199
push!(temps, t)
195200
@test ispath(t)
196-
@test length(TEMP_CLEANUP) == i
201+
@test lock(length, TEMP_CLEANUP) == i
197202
@test TEMP_CLEANUP_MAX[] == n
198203
# delete 1/3 of the temp paths
199204
i % 3 == 0 && rm(t, recursive=true, force=true)
200205
end
201206
# without cleanup no purge is triggered
202207
t = mktempdir(cleanup=false)
203208
@test isdir(t)
204-
@test length(TEMP_CLEANUP) == n
209+
@test lock(length, TEMP_CLEANUP) == n
205210
@test TEMP_CLEANUP_MAX[] == n
206211
rm(t, recursive=true, force=true)
207212
# purge triggered by next mktemp with cleanup
@@ -210,7 +215,7 @@ end
210215
n′ = 2n÷3 + 1
211216
@test 2n′ > n
212217
@test isfile(t)
213-
@test length(TEMP_CLEANUP) == n′
218+
@test lock(length, TEMP_CLEANUP) == n′
214219
@test TEMP_CLEANUP_MAX[] == 2n′
215220
# remove all temp files
216221
for t in temps
@@ -221,15 +226,15 @@ end
221226
t = i % 2 == 0 ? mktempfile() : mktempdir()
222227
push!(temps, t)
223228
@test ispath(t)
224-
@test length(TEMP_CLEANUP) == n′ + i
229+
@test lock(length, TEMP_CLEANUP) == n′ + i
225230
@test TEMP_CLEANUP_MAX[] == 2n′
226231
# delete 2/3 of the temp paths
227232
i % 3 != 0 && rm(t, recursive=true, force=true)
228233
end
229234
# without cleanup no purge is triggered
230235
t = mktempfile(cleanup=false)
231236
@test isfile(t)
232-
@test length(TEMP_CLEANUP) == 2n′
237+
@test lock(length, TEMP_CLEANUP) == 2n′
233238
@test TEMP_CLEANUP_MAX[] == 2n′
234239
rm(t, force=true)
235240
# purge triggered by next mktemp
@@ -238,7 +243,7 @@ end
238243
n′′ = n′÷3 + 1
239244
@test 2n′′ < n
240245
@test isdir(t)
241-
@test length(TEMP_CLEANUP) == n′′
246+
@test lock(length, TEMP_CLEANUP) == n′′
242247
@test TEMP_CLEANUP_MAX[] == n
243248
end
244249
end
@@ -249,7 +254,7 @@ no_error_logging(f::Function) =
249254
@testset "hof mktemp/dir when cleanup is prevented" begin
250255
d = mktempdir()
251256
with_temp_cleanup(3) do
252-
@test length(TEMP_CLEANUP) == 0
257+
@test lock(length, TEMP_CLEANUP) == 0
253258
@test TEMP_CLEANUP_MAX[] == 3
254259
local t, f
255260
temps = String[]
@@ -259,7 +264,7 @@ no_error_logging(f::Function) =
259264
t = path
260265
end
261266
@test !ispath(t)
262-
@test length(TEMP_CLEANUP) == 0
267+
@test lock(length, TEMP_CLEANUP) == 0
263268
@test TEMP_CLEANUP_MAX[] == 3
264269
# mktemp when cleanup is prevented
265270
no_error_logging() do
@@ -273,7 +278,7 @@ no_error_logging(f::Function) =
273278
chmod(d, 0o700)
274279
close(f)
275280
@test isfile(t)
276-
@test length(TEMP_CLEANUP) == 1
281+
@test lock(length, TEMP_CLEANUP) == 1
277282
@test TEMP_CLEANUP_MAX[] == 3
278283
push!(temps, t)
279284
# mktempdir is normally cleaned up on completion
@@ -282,7 +287,7 @@ no_error_logging(f::Function) =
282287
t = path
283288
end
284289
@test !ispath(t)
285-
@test length(TEMP_CLEANUP) == 1
290+
@test lock(length, TEMP_CLEANUP) == 1
286291
@test TEMP_CLEANUP_MAX[] == 3
287292
# mktempdir when cleanup is prevented
288293
no_error_logging() do
@@ -297,13 +302,13 @@ no_error_logging(f::Function) =
297302
chmod(d, 0o700)
298303
close(f)
299304
@test isdir(t)
300-
@test length(TEMP_CLEANUP) == 2
305+
@test lock(length, TEMP_CLEANUP) == 2
301306
@test TEMP_CLEANUP_MAX[] == 3
302307
push!(temps, t)
303308
# make one more temp file
304309
t = mktemp()[1]
305310
@test isfile(t)
306-
@test length(TEMP_CLEANUP) == 3
311+
@test lock(length, TEMP_CLEANUP) == 3
307312
@test TEMP_CLEANUP_MAX[] == 3
308313
# nothing has been deleted yet
309314
for t in temps
@@ -312,7 +317,7 @@ no_error_logging(f::Function) =
312317
# another temp file triggers purge
313318
t = mktempdir()
314319
@test isdir(t)
315-
@test length(TEMP_CLEANUP) == 2
320+
@test lock(length, TEMP_CLEANUP) == 2
316321
@test TEMP_CLEANUP_MAX[] == 4
317322
# now all the temps are gone
318323
for t in temps

0 commit comments

Comments
 (0)