Skip to content

Commit 07e5b78

Browse files
committed
Updated tests to use cancellation token
1 parent 9d80d1b commit 07e5b78

2 files changed

Lines changed: 40 additions & 9 deletions

File tree

src/EventLogExpert.UI.Tests/Services/DebugLogServiceTests.cs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,17 @@ public async Task ClearAsync_WhenCalled_ShouldClearLogFile()
2929
var fileLocationOptions = new FileLocationOptions(_testDirectory);
3030
var mockSettingsService = CreateMockSettingsService(LogLevel.Information);
3131

32-
await File.WriteAllTextAsync(_testLogPath, $"{Constants.DebugLogExistingContent}\n{Constants.DebugLogLine2}\n{Constants.DebugLogLine3}");
32+
await File.WriteAllTextAsync(_testLogPath,
33+
$"{Constants.DebugLogExistingContent}\n{Constants.DebugLogLine2}\n{Constants.DebugLogLine3}",
34+
TestContext.Current.CancellationToken);
3335

3436
using var debugLogService = new DebugLogService(fileLocationOptions, mockSettingsService);
3537

3638
// Act
3739
await debugLogService.ClearAsync();
3840

3941
// Assert
40-
var content = await File.ReadAllTextAsync(_testLogPath);
42+
var content = await File.ReadAllTextAsync(_testLogPath, TestContext.Current.CancellationToken);
4143
Assert.Empty(content);
4244
}
4345

@@ -55,7 +57,7 @@ public async Task ClearAsync_WhenLogFileDoesNotExist_ShouldCreateEmptyFile()
5557

5658
// Assert
5759
Assert.True(File.Exists(_testLogPath));
58-
var content = await File.ReadAllTextAsync(_testLogPath);
60+
var content = await File.ReadAllTextAsync(_testLogPath, TestContext.Current.CancellationToken);
5961
Assert.Empty(content);
6062
}
6163

@@ -190,7 +192,7 @@ public void Dispose_ShouldCleanupWithoutError()
190192
var debugLogService = new DebugLogService(fileLocationOptions, mockSettingsService);
191193

192194
// Act & Assert - Dispose should complete without throwing
193-
var exception = Record.Exception(() => debugLogService.Dispose());
195+
var exception = Record.Exception(debugLogService.Dispose);
194196
Assert.Null(exception);
195197
}
196198

@@ -202,12 +204,12 @@ public async Task LoadAsync_WhenLogFileHasContent_ShouldReturnAllLines()
202204
var mockSettingsService = CreateMockSettingsService(LogLevel.Information);
203205

204206
var expectedLines = new[] { Constants.DebugLogLine1, Constants.DebugLogLine2, Constants.DebugLogLine3 };
205-
await File.WriteAllLinesAsync(_testLogPath, expectedLines);
207+
await File.WriteAllLinesAsync(_testLogPath, expectedLines, TestContext.Current.CancellationToken);
206208

207209
using var debugLogService = new DebugLogService(fileLocationOptions, mockSettingsService);
208210

209211
// Act
210-
var lines = await debugLogService.LoadAsync().ToListAsync();
212+
var lines = await debugLogService.LoadAsync().ToListAsync(TestContext.Current.CancellationToken);
211213

212214
// Assert
213215
Assert.Equal(3, lines.Count);
@@ -223,12 +225,34 @@ public async Task LoadAsync_WhenLogFileIsEmpty_ShouldReturnNoLines()
223225
var fileLocationOptions = new FileLocationOptions(_testDirectory);
224226
var mockSettingsService = CreateMockSettingsService(LogLevel.Information);
225227

226-
await File.WriteAllTextAsync(_testLogPath, string.Empty);
228+
await File.WriteAllTextAsync(_testLogPath, string.Empty, TestContext.Current.CancellationToken);
227229

228230
using var debugLogService = new DebugLogService(fileLocationOptions, mockSettingsService);
229231

230232
// Act
231-
var lines = await debugLogService.LoadAsync().ToListAsync();
233+
var lines = await debugLogService.LoadAsync().ToListAsync(TestContext.Current.CancellationToken);
234+
235+
// Assert
236+
Assert.Empty(lines);
237+
}
238+
239+
[Fact]
240+
public async Task LoadAsync_WhenLogFileDoesNotExist_ShouldReturnNoLines()
241+
{
242+
// Arrange
243+
var fileLocationOptions = new FileLocationOptions(_testDirectory);
244+
var mockSettingsService = CreateMockSettingsService(LogLevel.Information);
245+
246+
// Ensure the log file does not exist (fresh install scenario)
247+
if (File.Exists(_testLogPath))
248+
{
249+
File.Delete(_testLogPath);
250+
}
251+
252+
using var debugLogService = new DebugLogService(fileLocationOptions, mockSettingsService);
253+
254+
// Act
255+
var lines = await debugLogService.LoadAsync().ToListAsync(TestContext.Current.CancellationToken);
232256

233257
// Assert
234258
Assert.Empty(lines);
@@ -244,7 +268,7 @@ public void LoadDebugLog_WhenNoSubscribers_ShouldNotThrow()
244268
using var debugLogService = new DebugLogService(fileLocationOptions, mockSettingsService);
245269

246270
// Act & Assert - no subscribers attached
247-
var exception = Record.Exception(() => debugLogService.LoadDebugLog());
271+
var exception = Record.Exception(debugLogService.LoadDebugLog);
248272
Assert.Null(exception);
249273
}
250274

src/EventLogExpert.UI/Services/DebugLogService.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ public void Dispose()
6464

6565
public async IAsyncEnumerable<string> LoadAsync()
6666
{
67+
// If the log file doesn't exist yet (fresh install, or before first write),
68+
// yield no lines instead of throwing FileNotFoundException.
69+
if (!File.Exists(_fileLocationOptions.LoggingPath))
70+
{
71+
yield break;
72+
}
73+
6774
// Read directly from the source file. Writers use File.AppendText which opens with
6875
// FileShare.Read, allowing concurrent readers. We open with FileShare.ReadWrite to
6976
// allow concurrent writers. This avoids the overhead of copying to a temp file and

0 commit comments

Comments
 (0)