Skip to content

Commit 91f6fcd

Browse files
committed
Ver 1.0.0
1 parent 504742d commit 91f6fcd

File tree

14 files changed

+10152
-431
lines changed

14 files changed

+10152
-431
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/.vs
2-
/src/RecurrentTasks/RecurrentTasks.xproj.user
2+
/artifacts
3+
*.user

RecurrentTasks.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1212
EndProject
1313
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "RecurrentTasks", "src\RecurrentTasks\RecurrentTasks.xproj", "{0B62072E-451A-46B0-B0BA-3FB2CB25501A}"
1414
EndProject
15+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "RecurrentTasks.Tests", "src\RecurrentTasks.Tests\RecurrentTasks.Tests.xproj", "{650A5A85-5956-491E-9312-5E25A27D1108}"
16+
EndProject
1517
Global
1618
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1719
Debug|Any CPU = Debug|Any CPU
@@ -22,11 +24,16 @@ Global
2224
{0B62072E-451A-46B0-B0BA-3FB2CB25501A}.Debug|Any CPU.Build.0 = Debug|Any CPU
2325
{0B62072E-451A-46B0-B0BA-3FB2CB25501A}.Release|Any CPU.ActiveCfg = Release|Any CPU
2426
{0B62072E-451A-46B0-B0BA-3FB2CB25501A}.Release|Any CPU.Build.0 = Release|Any CPU
27+
{650A5A85-5956-491E-9312-5E25A27D1108}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
28+
{650A5A85-5956-491E-9312-5E25A27D1108}.Debug|Any CPU.Build.0 = Debug|Any CPU
29+
{650A5A85-5956-491E-9312-5E25A27D1108}.Release|Any CPU.ActiveCfg = Release|Any CPU
30+
{650A5A85-5956-491E-9312-5E25A27D1108}.Release|Any CPU.Build.0 = Release|Any CPU
2531
EndGlobalSection
2632
GlobalSection(SolutionProperties) = preSolution
2733
HideSolutionNode = FALSE
2834
EndGlobalSection
2935
GlobalSection(NestedProjects) = preSolution
3036
{0B62072E-451A-46B0-B0BA-3FB2CB25501A} = {79344640-D2E5-4A31-880B-C6A9B0CC5F78}
37+
{650A5A85-5956-491E-9312-5E25A27D1108} = {79344640-D2E5-4A31-880B-C6A9B0CC5F78}
3138
EndGlobalSection
3239
EndGlobal
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
8+
<PropertyGroup Label="Globals">
9+
<ProjectGuid>650a5a85-5956-491e-9312-5e25a27d1108</ProjectGuid>
10+
<RootNamespace>RecurrentTasks.Tests</RootNamespace>
11+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
12+
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
13+
</PropertyGroup>
14+
<PropertyGroup>
15+
<SchemaVersion>2.0</SchemaVersion>
16+
</PropertyGroup>
17+
<ItemGroup>
18+
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
19+
</ItemGroup>
20+
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
21+
</Project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
namespace RecurrentTasks
2+
{
3+
using System;
4+
using System.Threading;
5+
using Microsoft.Extensions.Logging;
6+
using Microsoft.Extensions.DependencyInjection;
7+
8+
public class SampleTask : TaskBase<TaskStatus>
9+
{
10+
public readonly ManualResetEventSlim TaskRunCalled = new ManualResetEventSlim(false);
11+
12+
public bool MustThrowError { get; set; } = false;
13+
14+
public ManualResetEventSlim CanContinueRun = new ManualResetEventSlim(true);
15+
16+
public SampleTask(ILoggerFactory loggerFactory, TimeSpan interval, IServiceScopeFactory serviceScopeFactory)
17+
: base(loggerFactory, interval, serviceScopeFactory)
18+
{
19+
// Nothing
20+
}
21+
22+
protected override void Run(IServiceProvider serviceProvider, TaskStatus state)
23+
{
24+
TaskRunCalled.Set();
25+
if (MustThrowError)
26+
{
27+
throw new Exception("You asked - I throw");
28+
}
29+
if (!CanContinueRun.Wait(TimeSpan.FromSeconds(10)))
30+
{
31+
throw new Exception("CanContinueRun not set during 10 seconds. Something wrong with test...");
32+
}
33+
}
34+
}
35+
}
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
namespace RecurrentTasks
2+
{
3+
using System;
4+
using Microsoft.Extensions.Logging;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Xunit;
7+
8+
public class TaskBaseTests : IDisposable
9+
{
10+
private SampleTask sampleTask;
11+
12+
public TaskBaseTests()
13+
{
14+
var lf = new LoggerFactory();
15+
lf.AddConsole();
16+
17+
var serviceProvider = new ServiceCollection().BuildServiceProvider();
18+
sampleTask = new SampleTask(
19+
lf,
20+
TimeSpan.FromSeconds(5),
21+
serviceProvider.GetService<IServiceScopeFactory>()
22+
);
23+
}
24+
25+
public void Dispose()
26+
{
27+
if (sampleTask != null)
28+
{
29+
if (sampleTask.IsStarted)
30+
{
31+
sampleTask.Stop();
32+
}
33+
}
34+
}
35+
36+
[Fact]
37+
public void Task_CanStart()
38+
{
39+
// must start after 2 seconds
40+
sampleTask.Start(TimeSpan.FromSeconds(2));
41+
42+
// waiting 5 seconds max, then failing
43+
Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5)));
44+
}
45+
46+
[Fact]
47+
public void Task_CanNotStartTwice()
48+
{
49+
// must start after 1 seconds
50+
sampleTask.Start(TimeSpan.FromSeconds(1));
51+
52+
// waiting 5 seconds max (then failing)
53+
Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5)));
54+
55+
// and real test - trying to start again
56+
var ex = Assert.Throws<InvalidOperationException>(() => sampleTask.Start());
57+
}
58+
59+
[Fact]
60+
public void Task_RunAgainAndAgain()
61+
{
62+
// must start after 2 seconds
63+
sampleTask.Start(TimeSpan.FromSeconds(2));
64+
65+
// waiting 5 seconds max, then failing
66+
Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5)));
67+
68+
// resetting event
69+
sampleTask.TaskRunCalled.Reset();
70+
71+
// waiting for next run - twice default interval
72+
Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(10)));
73+
}
74+
75+
[Fact]
76+
public void Task_CanStop()
77+
{
78+
// must start after 2 seconds
79+
sampleTask.Start(TimeSpan.FromSeconds(2));
80+
81+
// waiting 5 seconds max, then failing
82+
Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5)));
83+
84+
sampleTask.TaskRunCalled.Reset();
85+
sampleTask.Stop();
86+
87+
// should NOT run again - waiting twice default interval
88+
Assert.False(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(10)));
89+
}
90+
91+
[Fact]
92+
public void Task_CanNotStopTwice()
93+
{
94+
// must start after 2 seconds
95+
sampleTask.Start(TimeSpan.FromSeconds(2));
96+
97+
// waiting 5 seconds max, then failing
98+
Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5)));
99+
100+
sampleTask.Stop();
101+
102+
System.Threading.Thread.Sleep(500); // wait for real stop
103+
104+
// and real test - trying to stop again
105+
var ex = Assert.Throws<InvalidOperationException>(() => sampleTask.Stop());
106+
}
107+
108+
[Fact]
109+
public void Task_IsStarted_Works()
110+
{
111+
Assert.False(sampleTask.IsStarted);
112+
113+
sampleTask.Start(TimeSpan.FromSeconds(2));
114+
115+
Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5)));
116+
117+
Assert.True(sampleTask.IsStarted);
118+
119+
sampleTask.Stop();
120+
121+
System.Threading.Thread.Sleep(500); // wait for real stop
122+
123+
Assert.False(sampleTask.IsStarted);
124+
}
125+
126+
[Fact]
127+
public void Task_IsRunningRightNow_Works()
128+
{
129+
Assert.False(sampleTask.IsRunningRightNow, "Already running... WFT???");
130+
131+
sampleTask.CanContinueRun.Reset(); // do not complete 'Run' without permission!
132+
133+
sampleTask.Start(TimeSpan.FromSeconds(2));
134+
Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5)));
135+
136+
Assert.True(sampleTask.IsRunningRightNow, "Oops, IsRunningRightNow is not 'true'. Something is broken!!!");
137+
138+
sampleTask.CanContinueRun.Set();
139+
140+
sampleTask.Stop();
141+
142+
System.Threading.Thread.Sleep(500); // wait for real stop
143+
144+
Assert.False(sampleTask.IsRunningRightNow, "Ooops, IsRunningRightNow is still 'true'.... WTF???");
145+
}
146+
147+
[Fact]
148+
public void Task_RunImmediately_Works()
149+
{
150+
// must start after 2 seconds
151+
sampleTask.Start(TimeSpan.FromSeconds(2));
152+
153+
// waiting 5 seconds max, then failing
154+
Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5)), "Failed to start first time");
155+
156+
sampleTask.TaskRunCalled.Reset();
157+
158+
sampleTask.TryRunImmediately();
159+
160+
// waiting very little time, not 'full' 5 secs
161+
Assert.True(sampleTask.TaskRunCalled.Wait(1000), "Not run immediately :( ");
162+
}
163+
164+
[Fact]
165+
public void Task_RunningAgainAfterException()
166+
{
167+
sampleTask.MustThrowError = true;
168+
sampleTask.Start(TimeSpan.FromSeconds(2));
169+
170+
// waiting 5 seconds max, then failing
171+
Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5)));
172+
173+
sampleTask.TaskRunCalled.Reset();
174+
175+
// should run again - waiting twice default interval
176+
Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(10)));
177+
}
178+
}
179+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"version": "1.0.0-*",
3+
4+
"dependencies": {
5+
"RecurrentTasks": "",
6+
"Microsoft.Extensions.DependencyInjection": "1.0.0-rc1-final",
7+
"Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",
8+
"xunit": "2.1.0",
9+
"xunit.runner.dnx": "2.1.0-rc1-*"
10+
},
11+
12+
"commands": {
13+
"test": "xunit.runner.dnx"
14+
},
15+
16+
"frameworks": {
17+
"dnx451": { },
18+
"dnxcore50": { }
19+
}
20+
}

0 commit comments

Comments
 (0)