-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from Tolyandre/hosted-service
Access MongoDB lasily. Wait jobs to stop gracefully.
- Loading branch information
Showing
13 changed files
with
315 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Horarium.Interfaces; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace Horarium.AspNetCore | ||
{ | ||
public class HorariumServerHostedService : IHostedService | ||
{ | ||
private readonly HorariumServer _horariumServer; | ||
|
||
public HorariumServerHostedService(IHorarium horarium) | ||
{ | ||
_horariumServer = (HorariumServer) horarium; | ||
} | ||
|
||
public Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
_horariumServer.Start(); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
return _horariumServer.Stop(); | ||
} | ||
} | ||
} |
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
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,82 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Horarium.Handlers; | ||
using Xunit; | ||
|
||
namespace Horarium.Test | ||
{ | ||
public class UncompletedTaskListTests | ||
{ | ||
private readonly UncompletedTaskList _uncompletedTaskList = new UncompletedTaskList(); | ||
|
||
[Fact] | ||
public async Task Add_TaskWithAnyResult_KeepsTaskUntilCompleted() | ||
{ | ||
var tcs1 = new TaskCompletionSource<bool>(); | ||
var tcs2 = new TaskCompletionSource<bool>(); | ||
var tcs3 = new TaskCompletionSource<bool>(); | ||
|
||
_uncompletedTaskList.Add(tcs1.Task); | ||
_uncompletedTaskList.Add(tcs2.Task); | ||
_uncompletedTaskList.Add(tcs3.Task); | ||
|
||
Assert.Equal(3, _uncompletedTaskList.Count); | ||
|
||
tcs1.SetResult(false); | ||
await Task.Delay(TimeSpan.FromSeconds(1)); // give a chance to finish continuations | ||
Assert.Equal(2, _uncompletedTaskList.Count); | ||
|
||
tcs2.SetException(new ApplicationException()); | ||
await Task.Delay(TimeSpan.FromSeconds(1)); | ||
Assert.Equal(1, _uncompletedTaskList.Count); | ||
|
||
tcs3.SetCanceled(); | ||
await Task.Delay(TimeSpan.FromSeconds(1)); | ||
Assert.Equal(0, _uncompletedTaskList.Count); | ||
} | ||
|
||
[Fact] | ||
public async Task WhenAllCompleted_NoTasks_ReturnsCompletedTask() | ||
{ | ||
// Act | ||
var whenAll = _uncompletedTaskList.WhenAllCompleted(); | ||
|
||
// Assert | ||
Assert.True(whenAll.IsCompletedSuccessfully); | ||
await whenAll; | ||
} | ||
|
||
[Fact] | ||
public async Task WhenAllCompleted_TaskNotCompleted_AwaitsUntilTaskCompleted() | ||
{ | ||
// Arrange | ||
var tcs = new TaskCompletionSource<bool>(); | ||
_uncompletedTaskList.Add(tcs.Task); | ||
|
||
// Act | ||
var whenAll = _uncompletedTaskList.WhenAllCompleted(); | ||
|
||
// Assert | ||
await Task.Delay(TimeSpan.FromSeconds(1)); // give a chance to finish any running tasks | ||
Assert.False(whenAll.IsCompleted); | ||
|
||
tcs.SetResult(false); | ||
await Task.Delay(TimeSpan.FromSeconds(1)); | ||
Assert.True(whenAll.IsCompletedSuccessfully); | ||
|
||
await whenAll; | ||
} | ||
|
||
[Fact] | ||
public async Task WhenAllCompleted_TaskFaulted_DoesNotThrow() | ||
{ | ||
// Arrange | ||
_uncompletedTaskList.Add(Task.FromException(new ApplicationException())); | ||
|
||
// Act | ||
var whenAll = _uncompletedTaskList.WhenAllCompleted(); | ||
|
||
await whenAll; | ||
} | ||
} | ||
} |
Oops, something went wrong.