-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add base class for implementing IBackgroundServiceExceptionHandler
- Loading branch information
Putta Khunchalee
committed
Dec 2, 2019
1 parent
d9e5e87
commit 73f933b
Showing
9 changed files
with
159 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/Ztm.Hosting.Tests/BackgroundServiceExceptionHandlerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Moq; | ||
using Xunit; | ||
using Ztm.Testing; | ||
|
||
namespace Ztm.Hosting.Tests | ||
{ | ||
public sealed class BackgroundServiceExceptionHandlerTests | ||
{ | ||
readonly FakeBackgroundServiceExceptionHandler subject; | ||
|
||
public BackgroundServiceExceptionHandlerTests() | ||
{ | ||
this.subject = new FakeBackgroundServiceExceptionHandler(); | ||
} | ||
|
||
[Fact] | ||
public async Task RunAsync_WithNullService_ShouldThrow() | ||
{ | ||
var exception = new Exception(); | ||
|
||
await Assert.ThrowsAsync<ArgumentNullException>( | ||
"service", | ||
() => this.subject.InvokeRunAsync(null, exception, CancellationToken.None) | ||
); | ||
} | ||
|
||
[Fact] | ||
public async Task RunAsync_WithNullException_ShouldThrow() | ||
{ | ||
await Assert.ThrowsAsync<ArgumentNullException>( | ||
"exception", | ||
() => this.subject.InvokeRunAsync(typeof(FakeBackgroundService), null, CancellationToken.None) | ||
); | ||
} | ||
|
||
[Fact] | ||
public Task RunAsync_WithValidArgs_ShouldInvokeProtectedRunAsync() | ||
{ | ||
return AsynchronousTesting.WithCancellationTokenAsync(async cancellationToken => | ||
{ | ||
// Arrange. | ||
var exception = new Exception(); | ||
|
||
// Act. | ||
await this.subject.InvokeRunAsync(typeof(FakeBackgroundService), exception, cancellationToken); | ||
|
||
// Assert. | ||
this.subject.StubbedRunAsync.Verify( | ||
f => f(typeof(FakeBackgroundService), exception, cancellationToken), | ||
Times.Once() | ||
); | ||
}); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Ztm.Hosting.Tests/FakeBackgroundServiceExceptionHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Moq; | ||
|
||
namespace Ztm.Hosting.Tests | ||
{ | ||
sealed class FakeBackgroundServiceExceptionHandler : BackgroundServiceExceptionHandler | ||
{ | ||
public FakeBackgroundServiceExceptionHandler() | ||
{ | ||
StubbedRunAsync = new Mock<Func<Type, Exception, CancellationToken, Task>>(); | ||
} | ||
|
||
public Mock<Func<Type, Exception, CancellationToken, Task>> StubbedRunAsync { get; } | ||
|
||
protected override Task RunAsync(Type service, Exception exception, CancellationToken cancellationToken) | ||
{ | ||
return StubbedRunAsync.Object(service, exception, cancellationToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Ztm.Hosting | ||
{ | ||
public abstract class BackgroundServiceExceptionHandler : IBackgroundServiceExceptionHandler | ||
{ | ||
protected BackgroundServiceExceptionHandler() | ||
{ | ||
} | ||
|
||
public Task InvokeRunAsync(Type service, Exception exception, CancellationToken cancellationToken) | ||
{ | ||
return ((IBackgroundServiceExceptionHandler)this).RunAsync(service, exception, cancellationToken); | ||
} | ||
|
||
protected abstract Task RunAsync(Type service, Exception exception, CancellationToken cancellationToken); | ||
|
||
Task IBackgroundServiceExceptionHandler.RunAsync( | ||
Type service, | ||
Exception exception, | ||
CancellationToken cancellationToken) | ||
{ | ||
if (service == null) | ||
{ | ||
throw new ArgumentNullException(nameof(service)); | ||
} | ||
|
||
if (exception == null) | ||
{ | ||
throw new ArgumentNullException(nameof(exception)); | ||
} | ||
|
||
return RunAsync(service, exception, cancellationToken); | ||
} | ||
} | ||
} |