-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathDeleteOnClose.cs
More file actions
81 lines (71 loc) · 3.05 KB
/
DeleteOnClose.cs
File metadata and controls
81 lines (71 loc) · 3.05 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace System.IO.Tests
{
public class FileStream_DeleteOnClose : FileSystemTest
{
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsFileLockingEnabled), nameof(PlatformDetection.IsThreadingSupported))]
public async Task OpenOrCreate_DeleteOnClose_UsableAsMutex()
{
var cts = new CancellationTokenSource();
int enterCount = 0;
int locksRemaining = int.MaxValue;
bool exclusive = true;
string path = GetTestFilePath();
Assert.False(File.Exists(path));
Func<Task> lockFile = async () =>
{
while (!cts.IsCancellationRequested)
{
try
{
using (var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, 4096, FileOptions.DeleteOnClose))
{
int counter = Interlocked.Increment(ref enterCount);
if (counter != 1)
{
// Test failed.
exclusive = false;
cts.Cancel(); // Stop other Tasks.
return;
}
// Hold the lock for a little bit.
await Task.Delay(TimeSpan.FromMilliseconds(5));
Interlocked.Decrement(ref enterCount);
if (Interlocked.Decrement(ref locksRemaining) <= 0)
{
return;
}
}
await Task.Delay(TimeSpan.FromMilliseconds(1));
}
catch (UnauthorizedAccessException)
{
// This can occur when the file is being deleted on Windows.
await Task.Delay(TimeSpan.FromMilliseconds(1));
}
catch (IOException)
{
// The file is opened by another Task.
await Task.Delay(TimeSpan.FromMilliseconds(1));
}
}
};
var tasks = new Task[50];
for (int i = 0; i < tasks.Length; i++)
{
tasks[i] = Task.Run(lockFile);
}
// Wait for 1000 locks.
cts.CancelAfter(TimeSpan.FromSeconds(60)); // Test timeout.
Volatile.Write(ref locksRemaining, 500);
await Task.WhenAll(tasks);
Assert.True(exclusive, "Exclusive");
Assert.False(cts.IsCancellationRequested, "Test cancelled");
Assert.False(File.Exists(path), "File exists");
}
}
}