Skip to content

Commit

Permalink
Ver 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
justdmitry committed Feb 4, 2016
1 parent 504742d commit 91f6fcd
Show file tree
Hide file tree
Showing 14 changed files with 10,152 additions and 431 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.vs
/src/RecurrentTasks/RecurrentTasks.xproj.user
/artifacts
*.user
7 changes: 7 additions & 0 deletions RecurrentTasks.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "RecurrentTasks", "src\RecurrentTasks\RecurrentTasks.xproj", "{0B62072E-451A-46B0-B0BA-3FB2CB25501A}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "RecurrentTasks.Tests", "src\RecurrentTasks.Tests\RecurrentTasks.Tests.xproj", "{650A5A85-5956-491E-9312-5E25A27D1108}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -22,11 +24,16 @@ Global
{0B62072E-451A-46B0-B0BA-3FB2CB25501A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0B62072E-451A-46B0-B0BA-3FB2CB25501A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0B62072E-451A-46B0-B0BA-3FB2CB25501A}.Release|Any CPU.Build.0 = Release|Any CPU
{650A5A85-5956-491E-9312-5E25A27D1108}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{650A5A85-5956-491E-9312-5E25A27D1108}.Debug|Any CPU.Build.0 = Debug|Any CPU
{650A5A85-5956-491E-9312-5E25A27D1108}.Release|Any CPU.ActiveCfg = Release|Any CPU
{650A5A85-5956-491E-9312-5E25A27D1108}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{0B62072E-451A-46B0-B0BA-3FB2CB25501A} = {79344640-D2E5-4A31-880B-C6A9B0CC5F78}
{650A5A85-5956-491E-9312-5E25A27D1108} = {79344640-D2E5-4A31-880B-C6A9B0CC5F78}
EndGlobalSection
EndGlobal
21 changes: 21 additions & 0 deletions src/RecurrentTasks.Tests/RecurrentTasks.Tests.xproj
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>
35 changes: 35 additions & 0 deletions src/RecurrentTasks.Tests/SampleTask.cs
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...");
}
}
}
}
179 changes: 179 additions & 0 deletions src/RecurrentTasks.Tests/TaskBaseTests.cs
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)));
}
}
}
20 changes: 20 additions & 0 deletions src/RecurrentTasks.Tests/project.json
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": { }
}
}
Loading

0 comments on commit 91f6fcd

Please sign in to comment.