|
3 | 3 |
|
4 | 4 | using System; |
5 | 5 | using System.Collections.Generic; |
| 6 | +using System.Diagnostics; |
6 | 7 | using System.IO; |
7 | 8 | using System.Linq; |
8 | 9 | using System.Reactive.Linq; |
9 | | -using System.Text; |
10 | 10 | using System.Threading.Tasks; |
11 | 11 | using Microsoft.Azure.WebJobs.Script.Eventing; |
12 | 12 | using Microsoft.Azure.WebJobs.Script.Eventing.File; |
@@ -66,5 +66,29 @@ public async Task DisposedSource_DoesNotPublishEvents() |
66 | 66 | Assert.Equal(2, events.Count); |
67 | 67 | } |
68 | 68 | } |
| 69 | + |
| 70 | + [Fact] |
| 71 | + public async Task FileChangedHandlerExceptions_LogError_AndDoNotThrow() |
| 72 | + { |
| 73 | + // Note: The FileSystemWatcher handler is called on a background thread. This means that when the handler |
| 74 | + // throws an exception, it crashes the process. The Visual Studio test runner handles that scenario and |
| 75 | + // does not crash. Before this handler was fixed, this test would only fail in the console runner. |
| 76 | + |
| 77 | + TestTraceWriter traceWriter = new TestTraceWriter(TraceLevel.Verbose); |
| 78 | + using (var directory = new TempDirectory()) |
| 79 | + using (var eventManager = new ScriptEventManager()) |
| 80 | + using (var eventSource = new FileWatcherEventSource(eventManager, "TestSource", directory.Path, traceWriter: traceWriter)) |
| 81 | + { |
| 82 | + var expectedException = new InvalidOperationException("This should not crash the process!"); |
| 83 | + eventManager.Subscribe(p => throw expectedException); |
| 84 | + |
| 85 | + string fullPath = Path.Combine(directory.Path, "test.txt"); |
| 86 | + File.WriteAllText(fullPath, "Test"); |
| 87 | + |
| 88 | + await TestHelpers.Await( |
| 89 | + () => traceWriter.GetTraces().Any(p => p.Level == TraceLevel.Error && p.Message.Contains(fullPath) && p.Exception == expectedException), |
| 90 | + timeout: 2000, pollingInterval: 250); |
| 91 | + } |
| 92 | + } |
69 | 93 | } |
70 | 94 | } |
0 commit comments