public async Task BlobGetsProcessedOnlyOnce_MultipleHosts() { await _testContainer .GetBlockBlobClient(TestBlobName) .UploadTextAsync("10"); var prog = new BlobGetsProcessedOnlyOnce_SingleHost_Program(); string hostId = Guid.NewGuid().ToString("N"); var host1 = NewBuilder(prog, builder => builder.UseHostId(hostId)) .Build(); var host2 = NewBuilder(prog, builder => builder.UseHostId(hostId)) .Build(); using (prog._completedEvent = new ManualResetEvent(initialState: false)) using (host1) using (host2) { host1.Start(); host2.Start(); Assert.True(prog._completedEvent.WaitOne(TimeSpan.FromSeconds(60))); } Assert.AreEqual(1, prog._timesProcessed); }
public async Task BlobGetsProcessedOnlyOnce_SingleHost() { TextWriter hold = Console.Out; StringWriter consoleOutput = new StringWriter(); Console.SetOut(consoleOutput); CloudBlockBlob blob = _testContainer.GetBlockBlobReference(TestBlobName); await blob.UploadTextAsync("0"); int timeToProcess; var prog = new BlobGetsProcessedOnlyOnce_SingleHost_Program(); var config = NewConfig(prog); // Process the blob first using (prog._completedEvent = new ManualResetEvent(initialState: false)) using (JobHost host = new JobHost(config)) { DateTime startTime = DateTime.Now; host.Start(); Assert.True(prog._completedEvent.WaitOne(TimeSpan.FromSeconds(60))); timeToProcess = (int)(DateTime.Now - startTime).TotalMilliseconds; Console.SetOut(hold); Assert.Equal(1, prog._timesProcessed); string[] consoleOutputLines = consoleOutput.ToString().Trim().Split(new string[] { Environment.NewLine }, StringSplitOptions.None); var executions = consoleOutputLines.Where(p => p.Contains("Executing")); Assert.Equal(1, executions.Count()); Assert.StartsWith(string.Format("Executing 'BlobGetsProcessedOnlyOnce_SingleHost_Program.SingleBlobTrigger' (Reason='New blob detected: {0}/{1}', Id=", blob.Container.Name, blob.Name), executions.Single()); } // Then start again and make sure the blob doesn't get reprocessed // wait twice the amount of time required to process first before // deciding that it doesn't get reprocessed using (prog._completedEvent = new ManualResetEvent(initialState: false)) using (JobHost host = new JobHost(config)) { host.Start(); bool blobReprocessed = prog._completedEvent.WaitOne(2 * timeToProcess); Assert.False(blobReprocessed); } Assert.Equal(1, prog._timesProcessed); }
public async Task BlobGetsProcessedOnlyOnce_SingleHost() { var blob = _testContainer.GetBlockBlobClient(TestBlobName); await blob.UploadTextAsync("0"); int timeToProcess; var prog = new BlobGetsProcessedOnlyOnce_SingleHost_Program(); // make sure they both have the same id var host = NewBuilder(prog, builder => builder.UseHostId(Guid.NewGuid().ToString("N"))) .Build(); // Process the blob first using (prog._completedEvent = new ManualResetEvent(initialState: false)) using (host) { DateTime startTime = DateTime.Now; host.Start(); Assert.True(prog._completedEvent.WaitOne(TimeSpan.FromSeconds(60))); timeToProcess = (int)(DateTime.Now - startTime).TotalMilliseconds; Assert.AreEqual(1, prog._timesProcessed); string[] loggerOutputLines = host.GetTestLoggerProvider().GetAllLogMessages() .Where(p => p.FormattedMessage != null) .SelectMany(p => p.FormattedMessage.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.None)) .ToArray(); var executions = loggerOutputLines.Where(p => p.Contains("Executing")); Assert.AreEqual(1, executions.Count()); StringAssert.StartsWith(string.Format("Executing 'BlobGetsProcessedOnlyOnce_SingleHost_Program.SingleBlobTrigger' (Reason='New blob detected: {0}/{1}', Id=", blob.BlobContainerName, blob.Name), executions.Single()); await host.StopAsync(); // Can't restart Assert.Throws <InvalidOperationException>(() => host.Start()); } Assert.AreEqual(1, prog._timesProcessed); } // host
public async Task BlobGetsProcessedOnlyOnce_MultipleHosts() { await _testContainer .GetBlockBlobReference(TestBlobName) .UploadTextAsync("10"); var prog = new BlobGetsProcessedOnlyOnce_SingleHost_Program(); var config = NewConfig(prog); using (prog._completedEvent = new ManualResetEvent(initialState: false)) using (JobHost host1 = new JobHost(config)) using (JobHost host2 = new JobHost(config)) { host1.Start(); host2.Start(); Assert.True(prog._completedEvent.WaitOne(TimeSpan.FromSeconds(60))); } Assert.Equal(1, prog._timesProcessed); }