|
6 | 6 | using System.Diagnostics;
|
7 | 7 | using System.IO;
|
8 | 8 | using System.Linq;
|
| 9 | +using System.Text.RegularExpressions; |
9 | 10 | using System.Threading;
|
10 | 11 | using System.Threading.Tasks;
|
11 | 12 | using Microsoft.Azure.WebJobs.Host;
|
@@ -235,53 +236,20 @@ public async Task TraceOutputsMessagesWhenLeaseIsAcquired()
|
235 | 236 |
|
236 | 237 | var blobMock = new Mock<ICloudBlob>();
|
237 | 238 | blobMock.Setup(b => b.AcquireLeaseAsync(It.IsAny<TimeSpan>(), It.IsAny<string>()))
|
238 |
| - .Returns(() => Task.FromResult(hostId)); |
239 |
| - |
240 |
| - blobMock.Setup(b => b.RenewLeaseAsync(It.IsAny<AccessCondition>())) |
241 |
| - .Returns(() => Task.Delay(10).ContinueWith(t => renewResetEvent.Set())); |
| 239 | + .Returns(() => Task.FromResult(hostId)); |
242 | 240 |
|
243 | 241 | using (var manager = new BlobLeaseManager(blobMock.Object, TimeSpan.FromSeconds(5), hostId, instanceId, traceWriter))
|
244 | 242 | {
|
245 | 243 | renewResetEvent.Wait(TimeSpan.FromSeconds(10));
|
246 | 244 |
|
247 | 245 | // Make sure we have enough time to trace the renewal
|
248 |
| - await TestHelpers.Await(() => traceWriter.Traces.Count == 2, 5000, 500); |
| 246 | + await TestHelpers.Await(() => traceWriter.Traces.Count == 1, 5000, 500); |
249 | 247 | }
|
250 | 248 |
|
251 | 249 | TraceEvent acquisitionEvent = traceWriter.Traces.First();
|
252 | 250 | Assert.Contains($"Host lock lease acquired by instance ID '{instanceId}'.", acquisitionEvent.Message);
|
253 | 251 | Assert.Equal(TraceLevel.Info, acquisitionEvent.Level);
|
254 |
| - |
255 |
| - TraceEvent renewalEvent = traceWriter.Traces.Skip(1).First(); |
256 |
| - Assert.Contains("Host lock lease renewed.", renewalEvent.Message); |
257 |
| - Assert.Equal(TraceLevel.Verbose, renewalEvent.Level); |
258 |
| - } |
259 |
| - |
260 |
| - [Fact] |
261 |
| - public async Task TraceOutputsMessagesWhenLeaseAcquisitionFails() |
262 |
| - { |
263 |
| - string hostId = Guid.NewGuid().ToString(); |
264 |
| - string instanceId = Guid.NewGuid().ToString(); |
265 |
| - var traceWriter = new TestTraceWriter(TraceLevel.Verbose); |
266 |
| - var acquisitionResetEvent = new ManualResetEventSlim(); |
267 |
| - |
268 |
| - var blobMock = new Mock<ICloudBlob>(); |
269 |
| - blobMock.Setup(b => b.AcquireLeaseAsync(It.IsAny<TimeSpan>(), It.IsAny<string>())) |
270 |
| - .Returns(() => Task.FromException<string>(new StorageException(new RequestResult { HttpStatusCode = 409 }, "test", null))) |
271 |
| - .Callback(() => acquisitionResetEvent.Set()); |
272 |
| - |
273 |
| - using (var manager = new BlobLeaseManager(blobMock.Object, TimeSpan.FromSeconds(10), hostId, instanceId, traceWriter)) |
274 |
| - { |
275 |
| - acquisitionResetEvent.Wait(TimeSpan.FromSeconds(5)); |
276 |
| - } |
277 |
| - |
278 |
| - // Make sure we have enough time to trace the renewal |
279 |
| - await TestHelpers.Await(() => traceWriter.Traces.Count == 1, 5000, 500); |
280 |
| - |
281 |
| - TraceEvent acquisitionEvent = traceWriter.Traces.First(); |
282 |
| - Assert.Contains($"Host instance '{instanceId}' failed to acquire host lock lease: Another host has an active lease.", acquisitionEvent.Message); |
283 |
| - Assert.Equal(TraceLevel.Verbose, acquisitionEvent.Level); |
284 |
| - } |
| 252 | + } |
285 | 253 |
|
286 | 254 | [Fact]
|
287 | 255 | public async Task TraceOutputsMessagesWhenLeaseRenewalFails()
|
@@ -310,8 +278,9 @@ public async Task TraceOutputsMessagesWhenLeaseRenewalFails()
|
310 | 278 | Assert.Contains($"Host lock lease acquired by instance ID '{instanceId}'.", acquisitionEvent.Message);
|
311 | 279 | Assert.Equal(TraceLevel.Info, acquisitionEvent.Level);
|
312 | 280 |
|
313 |
| - TraceEvent renewalEvent = traceWriter.Traces.Skip(1).First(); |
314 |
| - Assert.Contains("Failed to renew host lock lease", renewalEvent.Message); |
| 281 | + TraceEvent renewalEvent = traceWriter.Traces.Skip(1).First(); |
| 282 | + string pattern = @"Failed to renew host lock lease: Another host has acquired the lease. The last successful renewal completed at (.+) \([0-9]+ milliseconds ago\) with a duration of [0-9]+ milliseconds."; |
| 283 | + Assert.True(Regex.IsMatch(renewalEvent.Message, pattern), $"Expected trace event {pattern} not found."); |
315 | 284 | Assert.Equal(TraceLevel.Info, renewalEvent.Level);
|
316 | 285 | }
|
317 | 286 |
|
|
0 commit comments