This repository has been archived by the owner on Sep 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 654
/
LockFileTests.cs
244 lines (210 loc) · 8.91 KB
/
LockFileTests.cs
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
using System;
using System.IO;
using System.IO.Abstractions;
using System.Threading;
using System.Threading.Tasks;
using Kudu.Contracts.Infrastructure;
using Kudu.Contracts.SourceControl;
using Kudu.Contracts.Tracing;
using Kudu.Core.Infrastructure;
using Kudu.Core.SourceControl;
using Kudu.Core.Tracing;
using Moq;
using Xunit;
namespace Kudu.Core.Test
{
public class LockFileTests
{
[Fact]
public void LockFileBasicTest()
{
// Mock
var lockFile = MockLockFile(@"x:\path\file.lock");
var lockFile2 = MockLockFile(@"x:\path\file.lock");
for (int i = 0; i < 2; i++)
{
// Acquire
Assert.Equal(false, lockFile.IsHeld);
Assert.Equal(true, lockFile.Lock("operationName"));
// Test
Assert.Equal(true, lockFile.IsHeld);
Assert.Equal(false, lockFile.Lock("operationName"));
Assert.Equal(true, lockFile2.IsHeld);
Assert.Equal(false, lockFile2.Lock("operationName"));
// Release
lockFile.Release();
Assert.Equal(false, lockFile.IsHeld);
Assert.Equal(false, lockFile2.IsHeld);
}
}
[Fact]
public void LockFileConcurrentTest()
{
// Mock
var file = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
var lockFile = new LockFile(file, NullTracerFactory.Instance);
int count = 10;
int threads = 2;
int totals = 0;
// Test
var result = Parallel.For(0, threads, i =>
{
for (int j = 0; j < count; ++j)
{
lockFile.LockOperation(() =>
{
// simulate get/set
int val = totals;
Thread.Sleep(0);
totals = val + 1;
using (var reader = new StreamReader(FileSystemHelpers.OpenFile(file, FileMode.Open, FileAccess.Read, FileShare.Write)))
{
Assert.Contains(" Kudu.Core.Test.LockFileTests", reader.ReadToEnd());
}
}, "operationName", TimeSpan.FromSeconds(60));
}
});
// Assert
Assert.True(result.IsCompleted);
Assert.Equal(count * threads, totals);
FileSystemHelpers.DeleteFileSafe(file);
}
[Fact]
public void LockFileRepositoryClearLockTest()
{
// Mock
var path = @"x:\temp";
var lockFileName = Path.Combine(path, "deployment.lock");
var fileSystem = new Mock<IFileSystem>();
var file = new Mock<FileBase>();
var directory = new Mock<DirectoryBase>();
var repository = new Mock<IRepository>();
var repositoryFactory = new Mock<IRepositoryFactory>();
var lockFile = new DeploymentLockFile(lockFileName, Mock.Of<ITraceFactory>(f => f.GetTracer() == Mock.Of<ITracer>()));
// Setup
fileSystem.SetupGet(f => f.Directory)
.Returns(directory.Object);
directory.Setup(d => d.Exists(path))
.Returns(true);
fileSystem.SetupGet(f => f.File)
.Returns(file.Object);
file.Setup(f => f.Open(lockFileName, FileMode.Create, FileAccess.Write, FileShare.Read))
.Returns(Mock.Of<Stream>());
repositoryFactory.Setup(f => f.GetRepository())
.Returns(repository.Object);
FileSystemHelpers.Instance = fileSystem.Object;
// Test
lockFile.RepositoryFactory = repositoryFactory.Object;
var locked = lockFile.Lock("operationName");
// Assert
Assert.True(locked, "lock should be successful!");
repository.Verify(r => r.ClearLock(), Times.Once);
}
[Fact]
public void DeploymentLockSentinelTest()
{
// Mock
System.Environment.SetEnvironmentVariable("WEBSITE_INSTANCE_ID", "Instance1");
var path = @"x:\temp1";
var pathSentinel = "C:\\local\\ShutdownSentinel\\Sentinel.txt";
var lockFileName = Path.Combine(path, "deployment1.lock");
var fileSystem = new Mock<IFileSystem>();
var file = new Mock<FileBase>();
var fileInfoBase = new Mock<IFileInfoFactory>();
var directory = new Mock<DirectoryBase>();
var repository = new Mock<IRepository>();
var repositoryFactory = new Mock<IRepositoryFactory>();
var lockFile = new DeploymentLockFile(lockFileName, Mock.Of<ITraceFactory>(f => f.GetTracer() == Mock.Of<ITracer>()));
// Setup
fileSystem.SetupGet(f => f.Directory)
.Returns(directory.Object);
directory.Setup(d => d.Exists(path))
.Returns(true);
fileSystem.SetupGet(f => f.File)
.Returns(file.Object);
file.Setup(f => f.Open(lockFileName, FileMode.Create, FileAccess.Write, FileShare.Read))
.Returns(Mock.Of<Stream>());
file.Setup(f => f.Create(pathSentinel)).Returns(Mock.Of<Stream>());
fileInfoBase.Setup(f => f.FromFileName(pathSentinel)).Returns(Mock.Of<FileInfoBase>());
fileSystem.SetupGet(f => f.FileInfo).Returns(fileInfoBase.Object);
repositoryFactory.Setup(f => f.GetRepository())
.Returns(repository.Object);
FileSystemHelpers.Instance = fileSystem.Object;
lockFile.RepositoryFactory = repositoryFactory.Object;
// Test
var locked = lockFile.Lock("operationName");
file.Setup(f => f.Exists(pathSentinel)).Returns(true);
Assert.True(locked, "lock should be successful!");
Assert.True(FileSystemHelpers.FileExists(pathSentinel), "Sentinel file should be present");
repository.Verify(r => r.ClearLock(), Times.Once);
// Reset IsAzureEnvironment env variable
System.Environment.SetEnvironmentVariable("WEBSITE_INSTANCE_ID", null);
}
private LockFile MockLockFile(string path, ITraceFactory traceFactory = null, IFileSystem fileSystem = null)
{
traceFactory = traceFactory ?? NullTracerFactory.Instance;
if (fileSystem == null)
{
var locked = false;
var stream = new Mock<Stream>(MockBehavior.Strict);
var fs = new Mock<IFileSystem>(MockBehavior.Strict);
stream.Setup(s => s.Close())
.Callback(() =>
{
locked = false;
});
stream.Setup(s => s.Write(It.IsAny<byte[]>(), 0, It.IsAny<int>()));
stream.Setup(s => s.Flush());
fs.Setup(f => f.Directory.Exists(Path.GetDirectoryName(path)))
.Returns(true);
fs.Setup(f => f.File.Exists(path))
.Returns(() => true);
fs.Setup(f => f.File.Delete(path));
fs.Setup(f => f.File.Open(path, It.IsAny<FileMode>(), FileAccess.Write, FileShare.Read))
.Returns(() =>
{
lock (stream)
{
if (locked)
{
throw new IOException();
}
locked = true;
return stream.Object;
}
});
fileSystem = fs.Object;
}
FileSystemHelpers.Instance = fileSystem;
return new LockFile(path, traceFactory);
}
[Fact]
public void LockFailOnLockAcquiredTest()
{
// Mock
var file = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
var lockFile = new FailOnLockAcquiredLock(file);
// 1st attempt lock unsuccessful since OnLockAcquired failed
Assert.False(lockFile.Lock("operationName"));
// Next attempt successful
Assert.True(lockFile.Lock("operationName"));
lockFile.Release();
FileSystemHelpers.DeleteFileSafe(file);
}
class FailOnLockAcquiredLock : LockFile
{
private int _attempt = 0;
public FailOnLockAcquiredLock(string path)
: base(path)
{
}
protected override void OnLockAcquired()
{
if (_attempt++ == 0)
{
throw new InvalidOperationException("fail OnLockAcquired!");
}
}
}
}
}