-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
504742d
commit 91f6fcd
Showing
14 changed files
with
10,152 additions
and
431 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/.vs | ||
/src/RecurrentTasks/RecurrentTasks.xproj.user | ||
/artifacts | ||
*.user |
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,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion> | ||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> | ||
</PropertyGroup> | ||
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" /> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>650a5a85-5956-491e-9312-5e25a27d1108</ProjectGuid> | ||
<RootNamespace>RecurrentTasks.Tests</RootNamespace> | ||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath> | ||
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" /> | ||
</ItemGroup> | ||
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" /> | ||
</Project> |
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,35 @@ | ||
namespace RecurrentTasks | ||
{ | ||
using System; | ||
using System.Threading; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
public class SampleTask : TaskBase<TaskStatus> | ||
{ | ||
public readonly ManualResetEventSlim TaskRunCalled = new ManualResetEventSlim(false); | ||
|
||
public bool MustThrowError { get; set; } = false; | ||
|
||
public ManualResetEventSlim CanContinueRun = new ManualResetEventSlim(true); | ||
|
||
public SampleTask(ILoggerFactory loggerFactory, TimeSpan interval, IServiceScopeFactory serviceScopeFactory) | ||
: base(loggerFactory, interval, serviceScopeFactory) | ||
{ | ||
// Nothing | ||
} | ||
|
||
protected override void Run(IServiceProvider serviceProvider, TaskStatus state) | ||
{ | ||
TaskRunCalled.Set(); | ||
if (MustThrowError) | ||
{ | ||
throw new Exception("You asked - I throw"); | ||
} | ||
if (!CanContinueRun.Wait(TimeSpan.FromSeconds(10))) | ||
{ | ||
throw new Exception("CanContinueRun not set during 10 seconds. Something wrong with test..."); | ||
} | ||
} | ||
} | ||
} |
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,179 @@ | ||
namespace RecurrentTasks | ||
{ | ||
using System; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Xunit; | ||
|
||
public class TaskBaseTests : IDisposable | ||
{ | ||
private SampleTask sampleTask; | ||
|
||
public TaskBaseTests() | ||
{ | ||
var lf = new LoggerFactory(); | ||
lf.AddConsole(); | ||
|
||
var serviceProvider = new ServiceCollection().BuildServiceProvider(); | ||
sampleTask = new SampleTask( | ||
lf, | ||
TimeSpan.FromSeconds(5), | ||
serviceProvider.GetService<IServiceScopeFactory>() | ||
); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (sampleTask != null) | ||
{ | ||
if (sampleTask.IsStarted) | ||
{ | ||
sampleTask.Stop(); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public void Task_CanStart() | ||
{ | ||
// must start after 2 seconds | ||
sampleTask.Start(TimeSpan.FromSeconds(2)); | ||
|
||
// waiting 5 seconds max, then failing | ||
Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5))); | ||
} | ||
|
||
[Fact] | ||
public void Task_CanNotStartTwice() | ||
{ | ||
// must start after 1 seconds | ||
sampleTask.Start(TimeSpan.FromSeconds(1)); | ||
|
||
// waiting 5 seconds max (then failing) | ||
Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5))); | ||
|
||
// and real test - trying to start again | ||
var ex = Assert.Throws<InvalidOperationException>(() => sampleTask.Start()); | ||
} | ||
|
||
[Fact] | ||
public void Task_RunAgainAndAgain() | ||
{ | ||
// must start after 2 seconds | ||
sampleTask.Start(TimeSpan.FromSeconds(2)); | ||
|
||
// waiting 5 seconds max, then failing | ||
Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5))); | ||
|
||
// resetting event | ||
sampleTask.TaskRunCalled.Reset(); | ||
|
||
// waiting for next run - twice default interval | ||
Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(10))); | ||
} | ||
|
||
[Fact] | ||
public void Task_CanStop() | ||
{ | ||
// must start after 2 seconds | ||
sampleTask.Start(TimeSpan.FromSeconds(2)); | ||
|
||
// waiting 5 seconds max, then failing | ||
Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5))); | ||
|
||
sampleTask.TaskRunCalled.Reset(); | ||
sampleTask.Stop(); | ||
|
||
// should NOT run again - waiting twice default interval | ||
Assert.False(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(10))); | ||
} | ||
|
||
[Fact] | ||
public void Task_CanNotStopTwice() | ||
{ | ||
// must start after 2 seconds | ||
sampleTask.Start(TimeSpan.FromSeconds(2)); | ||
|
||
// waiting 5 seconds max, then failing | ||
Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5))); | ||
|
||
sampleTask.Stop(); | ||
|
||
System.Threading.Thread.Sleep(500); // wait for real stop | ||
|
||
// and real test - trying to stop again | ||
var ex = Assert.Throws<InvalidOperationException>(() => sampleTask.Stop()); | ||
} | ||
|
||
[Fact] | ||
public void Task_IsStarted_Works() | ||
{ | ||
Assert.False(sampleTask.IsStarted); | ||
|
||
sampleTask.Start(TimeSpan.FromSeconds(2)); | ||
|
||
Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5))); | ||
|
||
Assert.True(sampleTask.IsStarted); | ||
|
||
sampleTask.Stop(); | ||
|
||
System.Threading.Thread.Sleep(500); // wait for real stop | ||
|
||
Assert.False(sampleTask.IsStarted); | ||
} | ||
|
||
[Fact] | ||
public void Task_IsRunningRightNow_Works() | ||
{ | ||
Assert.False(sampleTask.IsRunningRightNow, "Already running... WFT???"); | ||
|
||
sampleTask.CanContinueRun.Reset(); // do not complete 'Run' without permission! | ||
|
||
sampleTask.Start(TimeSpan.FromSeconds(2)); | ||
Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5))); | ||
|
||
Assert.True(sampleTask.IsRunningRightNow, "Oops, IsRunningRightNow is not 'true'. Something is broken!!!"); | ||
|
||
sampleTask.CanContinueRun.Set(); | ||
|
||
sampleTask.Stop(); | ||
|
||
System.Threading.Thread.Sleep(500); // wait for real stop | ||
|
||
Assert.False(sampleTask.IsRunningRightNow, "Ooops, IsRunningRightNow is still 'true'.... WTF???"); | ||
} | ||
|
||
[Fact] | ||
public void Task_RunImmediately_Works() | ||
{ | ||
// must start after 2 seconds | ||
sampleTask.Start(TimeSpan.FromSeconds(2)); | ||
|
||
// waiting 5 seconds max, then failing | ||
Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5)), "Failed to start first time"); | ||
|
||
sampleTask.TaskRunCalled.Reset(); | ||
|
||
sampleTask.TryRunImmediately(); | ||
|
||
// waiting very little time, not 'full' 5 secs | ||
Assert.True(sampleTask.TaskRunCalled.Wait(1000), "Not run immediately :( "); | ||
} | ||
|
||
[Fact] | ||
public void Task_RunningAgainAfterException() | ||
{ | ||
sampleTask.MustThrowError = true; | ||
sampleTask.Start(TimeSpan.FromSeconds(2)); | ||
|
||
// waiting 5 seconds max, then failing | ||
Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5))); | ||
|
||
sampleTask.TaskRunCalled.Reset(); | ||
|
||
// should run again - waiting twice default interval | ||
Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(10))); | ||
} | ||
} | ||
} |
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,20 @@ | ||
{ | ||
"version": "1.0.0-*", | ||
|
||
"dependencies": { | ||
"RecurrentTasks": "", | ||
"Microsoft.Extensions.DependencyInjection": "1.0.0-rc1-final", | ||
"Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final", | ||
"xunit": "2.1.0", | ||
"xunit.runner.dnx": "2.1.0-rc1-*" | ||
}, | ||
|
||
"commands": { | ||
"test": "xunit.runner.dnx" | ||
}, | ||
|
||
"frameworks": { | ||
"dnx451": { }, | ||
"dnxcore50": { } | ||
} | ||
} |
Oops, something went wrong.