Skip to content

Commit

Permalink
Only test for own unobserved exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ShortDevelopment committed Jan 4, 2025
1 parent 1a171e7 commit 900fa96
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions tests/ShortDev.Microsoft.ConnectedDevices.Test/UtilsTest.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using System.Diagnostics;
using Xunit.Abstractions;

namespace ShortDev.Microsoft.ConnectedDevices.Test;
public sealed class UtilsTest
public sealed class UtilsTest(ITestOutputHelper output)
{
[Theory]
[InlineData(200, 1)]
[InlineData(200, 10)]
[InlineData(200, 100)]
public async Task WithTimeout_ShouldObserveException_WhenSlowerTimeout(int delayMs, int timeoutMs)
{
using UnobservedTaskExceptionObserver exceptionObserver = new();
using UnobservedTaskExceptionObserver exceptionObserver = new(output);

await Assert.ThrowsAsync<TaskCanceledException>(async () =>
{
Expand All @@ -28,9 +28,9 @@ await LongRunningOperationWithThrow(delayMs)
[InlineData(100, 200)]
public async Task WithTimeout_ShouldObserveException_WhenFasterAsTimeout(int delayMs, int timeoutMs)
{
using UnobservedTaskExceptionObserver exceptionObserver = new();
using UnobservedTaskExceptionObserver exceptionObserver = new(output);

await Assert.ThrowsAsync<NotImplementedException>(async () =>
await Assert.ThrowsAsync<ObservableException>(async () =>
{
// Await long-running task with longer timeout
await LongRunningOperationWithThrow(delayMs)
Expand All @@ -44,19 +44,31 @@ await LongRunningOperationWithThrow(delayMs)
static async Task<object> LongRunningOperationWithThrow(int delayMs)
{
await Task.Delay(delayMs);
throw new NotImplementedException();
throw new ObservableException();
}

sealed class UnobservedTaskExceptionObserver : IDisposable
{
public UnobservedTaskExceptionObserver()
=> TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;
readonly ITestOutputHelper _output;
public UnobservedTaskExceptionObserver(ITestOutputHelper output)
{
_output = output;

TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;
}

bool _hadUnobservedTaskException;
int _unobservedExceptionCounter;
void OnUnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
=> _hadUnobservedTaskException = true;
{
if (!e.Exception.InnerExceptions.All(x => x is ObservableException))
{
_output.WriteLine($"UnobservedTaskExceptions: {string.Join(',', e.Exception.InnerExceptions.Select(x => x.Message))}");
return;
}

Interlocked.Increment(ref _unobservedExceptionCounter);
}

[StackTraceHidden]
public void Dispose()
{
// Force GC to cleanup long-running task
Expand All @@ -69,6 +81,8 @@ public void Dispose()
}

void CheckForUnobservedTaskExceptions()
=> Assert.False(_hadUnobservedTaskException);
=> Assert.Equal(0, _unobservedExceptionCounter);
}

sealed class ObservableException : Exception;
}

0 comments on commit 900fa96

Please sign in to comment.