Async function execution support was added

This commit is contained in:
kamilozturk 2019-06-20 19:04:06 +03:00
parent d2b6759998
commit 223b856c1f
11 changed files with 372 additions and 304 deletions

2
.gitignore vendored
View File

@ -22,6 +22,8 @@ local.properties
.settings/
.loadpath
.vs/
# External tool builders
.externalToolBuilders/

View File

@ -1,19 +1,12 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.21005.1
# Visual Studio 15
VisualStudioVersion = 15.0.28307.705
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CronNET", "CronNET\CronNET.csproj", "{F31D7AF3-FDFA-44F1-9C63-305BAF11D002}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CronNETTests", "CronNETTests\CronNETTests.csproj", "{6FCFBDF4-ECB7-4FC2-A376-E962B11D487D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{EA4722C9-7FD3-4D8C-BA24-AAAFC5E52EAC}"
ProjectSection(SolutionItems) = preProject
.nuget\NuGet.Config = .nuget\NuGet.Config
.nuget\NuGet.exe = .nuget\NuGet.exe
.nuget\NuGet.targets = .nuget\NuGet.targets
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -32,4 +25,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {48EEC37A-1E37-4172-81FC-A957D3351B5C}
EndGlobalSection
EndGlobal

View File

@ -1,56 +1,88 @@
using CronNET.Interfaces;
using System;
using System.Collections.Generic;
using System.Timers;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
namespace CronNET
{
public interface ICronDaemon
{
void AddJob(string schedule, ThreadStart action);
void Start();
void Stop();
}
public class CronDaemon : ICronDaemon
{
private readonly System.Timers.Timer timer = new System.Timers.Timer(30000);
private readonly List<ICronJob> cron_jobs = new List<ICronJob>();
private DateTime _last= DateTime.Now;
private readonly System.Timers.Timer _timer;
private readonly List<ICronJob> _cronJobs;
private CancellationToken _cancellationToken;
public event EventHandler<string> JobExecuting;
public event EventHandler<string> JobExecuted;
public CronDaemon()
{
timer.AutoReset = true;
timer.Elapsed += timer_elapsed;
_cronJobs = new List<ICronJob>();
_timer = new System.Timers.Timer(1000 * 60);
_timer.Elapsed += TimerElapsed;
_timer.Enabled = true;
}
public void AddJob(string schedule, ThreadStart action)
public void Add(CronJob job)
{
var cj = new CronJob(schedule, action);
cron_jobs.Add(cj);
job.JobExecuted += Job_JobExecuted;
job.JobExecuting += Job_JobExecuting;
_cronJobs.Add(job);
}
public void Start()
private void Job_JobExecuting(object sender, string name)
{
timer.Start();
JobExecuting?.Invoke(sender, name);
}
private void Job_JobExecuted(object sender, string name)
{
JobExecuted?.Invoke(sender, name);
}
public void Start(CancellationToken cancellationToken)
{
_cancellationToken = cancellationToken;
_cancellationToken.Register(Stop);
_timer.Start();
}
public void Stop()
{
timer.Stop();
foreach (CronJob job in cron_jobs)
job.abort();
_timer.Stop();
}
private void timer_elapsed(object sender, ElapsedEventArgs e)
private void TimerElapsed(object sender, ElapsedEventArgs e)
{
if (DateTime.Now.Minute != _last.Minute)
{
_last = DateTime.Now;
foreach (ICronJob job in cron_jobs)
job.execute(DateTime.Now);
}
Parallel.ForEach(_cronJobs, job => job.ExecuteAsync(DateTime.Now, _cancellationToken));
}
public void Remove(string name)
{
var job = _cronJobs.First(x => x.Name == name);
if (job != null)
_cronJobs.Remove(job);
}
public void Remove(CronJob job)
{
_cronJobs.Remove(job);
}
public void Clear()
{
_cronJobs.Clear();
}
public Task RunAsync(Func<Task> func, CancellationToken cancellationToken, string name)
{
JobExecuting?.Invoke(this, name);
return Task.Run(func, cancellationToken).ContinueWith(x => JobExecuted?.Invoke(this, name));
}
}
}

View File

@ -1,47 +1,46 @@
using CronNET.Interfaces;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace CronNET
{
public interface ICronJob
{
void execute(DateTime date_time);
void abort();
}
public class CronJob : ICronJob
{
private readonly ICronSchedule _cron_schedule = new CronSchedule();
private readonly ThreadStart _thread_start;
private Thread _thread;
private ICollection<ICronSchedule> _cronSchedules;
private Func<Task> _func;
public CronJob(string schedule, ThreadStart thread_start)
{
_cron_schedule = new CronSchedule(schedule);
_thread_start = thread_start;
_thread = new Thread(thread_start);
}
internal event EventHandler<string> JobExecuted;
internal event EventHandler<string> JobExecuting;
private object _lock = new object();
public void execute(DateTime date_time)
public string Name { get; private set; }
public CronJob(Func<Task> func, string name, params string[] cronPatterns)
{
lock (_lock)
Name = name;
_func = func;
_cronSchedules = new List<ICronSchedule>();
foreach (var item in cronPatterns)
{
if (!_cron_schedule.isTime(date_time))
return;
if (_thread.ThreadState == ThreadState.Running)
return;
_thread = new Thread(_thread_start);
_thread.Start();
_cronSchedules.Add(new CronSchedule(item));
}
}
public void abort()
public Task ExecuteAsync(DateTime dateTime, CancellationToken cancellationToken)
{
_thread.Abort();
}
foreach (var cronSchedule in _cronSchedules)
{
if (!cronSchedule.IsTime(dateTime))
continue;
JobExecuting?.Invoke(this, Name);
return Task.Run(_func, cancellationToken).ContinueWith(x => JobExecuted?.Invoke(this, Name));
}
return Task.CompletedTask;
}
}
}
}

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="12.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
@ -8,14 +8,15 @@
<ProjectGuid>{F31D7AF3-FDFA-44F1-9C63-305BAF11D002}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Cron</RootNamespace>
<AssemblyName>Cron</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<RootNamespace>CronNET</RootNamespace>
<AssemblyName>CronNET</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileUpgradeFlags>
</FileUpgradeFlags>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>2.0</OldToolsVersion>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@ -25,6 +26,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
@ -33,6 +35,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
@ -43,6 +46,9 @@
<Compile Include="CronDaemon.cs" />
<Compile Include="CronJob.cs" />
<Compile Include="CronSchedule.cs" />
<Compile Include="Interfaces\ICronSchedule.cs" />
<Compile Include="Interfaces\ICronDaemon.cs" />
<Compile Include="Interfaces\ICronJob.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

View File

@ -1,35 +1,30 @@
using CronNET.Interfaces;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace CronNET
{
public interface ICronSchedule
{
bool isValid(string expression);
bool isTime(DateTime date_time);
}
public class CronSchedule : ICronSchedule
{
#region Readonly Class Members
readonly static Regex divided_regex = new Regex(@"(\*/\d+)");
readonly static Regex range_regex = new Regex(@"(\d+\-\d+)\/?(\d+)?");
readonly static Regex wild_regex = new Regex(@"(\*)");
readonly static Regex list_regex = new Regex(@"(((\d+,)*\d+)+)");
readonly static Regex validation_regex = new Regex(divided_regex + "|" + range_regex + "|" + wild_regex + "|" + list_regex);
static readonly Regex DividedRegex = new Regex(@"(\*/\d+)");
static readonly Regex RangeRegex = new Regex(@"(\d+\-\d+)\/?(\d+)?");
static readonly Regex WildRegex = new Regex(@"(\*)");
static readonly Regex ListRegex = new Regex(@"(((\d+,)*\d+)+)");
static readonly Regex ValidationRegex = new Regex(DividedRegex + "|" + RangeRegex + "|" + WildRegex + "|" + ListRegex);
#endregion
#region Private Instance Members
private readonly string _expression;
public List<int> minutes;
public List<int> hours;
public List<int> days_of_month;
public List<int> months;
public List<int> days_of_week;
private List<int> _minutes;
private List<int> _hours;
private List<int> _daysOfMonth;
private List<int> _months;
private List<int> _daysOfWeek;
#endregion
@ -41,101 +36,99 @@ namespace CronNET
public CronSchedule(string expressions)
{
this._expression = expressions;
generate();
_expression = expressions;
Generate();
}
public List<int> Minutes => _minutes;
public List<int> Hours => _hours;
public List<int> DaysOfMonth => _daysOfMonth;
public List<int> Months => _months;
public List<int> DaysOfWeek => _daysOfWeek;
#endregion
#region Public Methods
private bool isValid()
private bool IsValid()
{
return isValid(this._expression);
return IsValid(_expression);
}
public bool isValid(string expression)
public bool IsValid(string expression)
{
MatchCollection matches = validation_regex.Matches(expression);
MatchCollection matches = ValidationRegex.Matches(expression);
return matches.Count > 0;//== 5;
}
public bool isTime(DateTime date_time)
public bool IsTime(DateTime dateTime)
{
return minutes.Contains(date_time.Minute) &&
hours.Contains(date_time.Hour) &&
days_of_month.Contains(date_time.Day) &&
months.Contains(date_time.Month) &&
days_of_week.Contains((int)date_time.DayOfWeek);
return _minutes.Contains(dateTime.Minute) &&
_hours.Contains(dateTime.Hour) &&
_daysOfMonth.Contains(dateTime.Day) &&
_months.Contains(dateTime.Month) &&
_daysOfWeek.Contains((int)dateTime.DayOfWeek);
}
private void generate()
private void Generate()
{
if (!isValid()) return;
if (!IsValid()) return;
MatchCollection matches = validation_regex.Matches(this._expression);
MatchCollection matches = ValidationRegex.Matches(_expression);
generate_minutes(matches[0].ToString());
if (matches.Count > 1)
generate_hours(matches[1].ToString());
else
generate_hours("*");
if (matches.Count > 2)
generate_days_of_month(matches[2].ToString());
else
generate_days_of_month("*");
if (matches.Count > 3)
generate_months(matches[3].ToString());
else
generate_months("*");
if (matches.Count > 4)
generate_days_of_weeks(matches[4].ToString());
else
generate_days_of_weeks("*");
generate_hours(matches.Count > 1 ? matches[1].ToString() : "*");
generate_days_of_month(matches.Count > 2 ? matches[2].ToString() : "*");
generate_months(matches.Count > 3 ? matches[3].ToString() : "*");
generate_days_of_weeks(matches.Count > 4 ? matches[4].ToString() : "*");
}
private void generate_minutes(string match)
{
this.minutes = generate_values(match, 0, 60);
_minutes = generate_values(match, 0, 60);
}
private void generate_hours(string match)
{
this.hours = generate_values(match, 0, 24);
_hours = generate_values(match, 0, 24);
}
private void generate_days_of_month(string match)
{
this.days_of_month = generate_values(match, 1, 32);
_daysOfMonth = generate_values(match, 1, 32);
}
private void generate_months(string match)
{
this.months = generate_values(match, 1, 13);
_months = generate_values(match, 1, 13);
}
private void generate_days_of_weeks(string match)
{
this.days_of_week = generate_values(match, 0, 7);
_daysOfWeek = generate_values(match, 0, 7);
}
private List<int> generate_values(string configuration, int start, int max)
{
if (divided_regex.IsMatch(configuration)) return divided_array(configuration, start, max);
if (range_regex.IsMatch(configuration)) return range_array(configuration);
if (wild_regex.IsMatch(configuration)) return wild_array(configuration, start, max);
if (list_regex.IsMatch(configuration)) return list_array(configuration);
if (DividedRegex.IsMatch(configuration)) return divided_array(configuration, start, max);
if (RangeRegex.IsMatch(configuration)) return range_array(configuration);
if (WildRegex.IsMatch(configuration)) return wild_array(configuration, start, max);
if (ListRegex.IsMatch(configuration)) return list_array(configuration);
return new List<int>();
}
private List<int> divided_array(string configuration, int start, int max)
{
if (!divided_regex.IsMatch(configuration))
if (!DividedRegex.IsMatch(configuration))
return new List<int>();
List<int> ret = new List<int>();
@ -151,13 +144,13 @@ namespace CronNET
private List<int> range_array(string configuration)
{
if (!range_regex.IsMatch(configuration))
if (!RangeRegex.IsMatch(configuration))
return new List<int>();
List<int> ret = new List<int>();
string[] split = configuration.Split("-".ToCharArray());
int start = int.Parse(split[0]);
int end = 0;
int end;
if (split[1].Contains("/"))
{
split = split[1].Split("/".ToCharArray());
@ -169,8 +162,7 @@ namespace CronNET
ret.Add(i);
return ret;
}
else
end = int.Parse(split[1]);
end = int.Parse(split[1]);
for (int i = start; i <= end; ++i)
ret.Add(i);
@ -180,7 +172,7 @@ namespace CronNET
private List<int> wild_array(string configuration, int start, int max)
{
if (!wild_regex.IsMatch(configuration))
if (!WildRegex.IsMatch(configuration))
return new List<int>();
List<int> ret = new List<int>();
@ -193,7 +185,7 @@ namespace CronNET
private List<int> list_array(string configuration)
{
if (!list_regex.IsMatch(configuration))
if (!ListRegex.IsMatch(configuration))
return new List<int>();
List<int> ret = new List<int>();

View File

@ -0,0 +1,20 @@
using System;
using System.Threading;
using System.Threading.Tasks;
namespace CronNET.Interfaces
{
public interface ICronDaemon
{
void Add(CronJob job);
void Remove(CronJob job);
void Remove(string name);
void Clear();
void Start(CancellationToken cancellationToken);
Task RunAsync(Func<Task> func, CancellationToken cancellationToken, string name);
void Stop();
event EventHandler<string> JobExecuting;
event EventHandler<string> JobExecuted;
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Threading;
using System.Threading.Tasks;
namespace CronNET.Interfaces
{
public interface ICronJob
{
string Name { get; }
Task ExecuteAsync(DateTime dateTime, CancellationToken cancellationToken);
}
}

View File

@ -0,0 +1,10 @@
using System;
namespace CronNET.Interfaces
{
public interface ICronSchedule
{
bool IsValid(string expression);
bool IsTime(DateTime dateTime);
}
}

View File

@ -1,5 +1,4 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following

View File

@ -1,192 +1,192 @@
using System;
using System.Collections.Generic;
using CronNET;
using NUnit.Framework;
using System.Threading;
//using System;
//using System.Collections.Generic;
//using CronNET;
//using NUnit.Framework;
//using System.Threading;
namespace CronTests
{
[TestFixture]
public class CronScheduleTests
{
//namespace CronTests
//{
// [TestFixture]
// public class CronScheduleTests
// {
[Test]
public void is_valid_test()
{
var cron_schedule = new CronSchedule();
Assert.IsTrue(cron_schedule.isValid("*/2"));
Assert.IsTrue(cron_schedule.isValid("* * * * *"));
Assert.IsTrue(cron_schedule.isValid("0 * * * *"));
Assert.IsTrue(cron_schedule.isValid("0,1,2 * * * *"));
Assert.IsTrue(cron_schedule.isValid("*/2 * * * *"));
Assert.IsTrue(cron_schedule.isValid("1-4 * * * *"));
Assert.IsTrue(cron_schedule.isValid("1-55/3 * * * *"));
Assert.IsTrue(cron_schedule.isValid("1,10,20 * * * *"));
Assert.IsTrue(cron_schedule.isValid("* 1,10,20 * * *"));
}
// [Test]
// public void is_valid_test()
// {
// var cron_schedule = new CronSchedule();
// Assert.IsTrue(cron_schedule.isValid("*/2"));
// Assert.IsTrue(cron_schedule.isValid("* * * * *"));
// Assert.IsTrue(cron_schedule.isValid("0 * * * *"));
// Assert.IsTrue(cron_schedule.isValid("0,1,2 * * * *"));
// Assert.IsTrue(cron_schedule.isValid("*/2 * * * *"));
// Assert.IsTrue(cron_schedule.isValid("1-4 * * * *"));
// Assert.IsTrue(cron_schedule.isValid("1-55/3 * * * *"));
// Assert.IsTrue(cron_schedule.isValid("1,10,20 * * * *"));
// Assert.IsTrue(cron_schedule.isValid("* 1,10,20 * * *"));
// }
[Test]
public static void divided_array_test()
{
var cron_schedule = new CronSchedule("*/2");
List<int> results = cron_schedule.minutes.GetRange(0,5);//("*/2", 0, 10);
Assert.AreEqual(results.ToArray(), new int[] { 0, 2, 4, 6, 8 });
}
// [Test]
// public static void divided_array_test()
// {
// var cron_schedule = new CronSchedule("*/2");
// List<int> results = cron_schedule.minutes.GetRange(0,5);//("*/2", 0, 10);
// Assert.AreEqual(results.ToArray(), new int[] { 0, 2, 4, 6, 8 });
// }
[Test]
public static void range_array_test()
{
var cron_schedule = new CronSchedule("1-10");
List<int> results = cron_schedule.minutes.GetRange(0,10);//();
Assert.AreEqual(results.ToArray(), new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
var cs = new CronSchedule("1-10/3 20-45/4 * * *");
results = cs.minutes;
Assert.AreEqual(results.ToArray(), new int[] { 3, 6, 9 });
}
// [Test]
// public static void range_array_test()
// {
// var cron_schedule = new CronSchedule("1-10");
// List<int> results = cron_schedule.minutes.GetRange(0,10);//();
// Assert.AreEqual(results.ToArray(), new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
// var cs = new CronSchedule("1-10/3 20-45/4 * * *");
// results = cs.minutes;
// Assert.AreEqual(results.ToArray(), new int[] { 3, 6, 9 });
// }
[Test]
public void wild_array_test()
{
var cron_schedule = new CronSchedule("*");
List<int> results = cron_schedule.minutes.GetRange(0,10);//("*", 0, 10);
Assert.AreEqual(results.ToArray(), new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
}
// [Test]
// public void wild_array_test()
// {
// var cron_schedule = new CronSchedule("*");
// List<int> results = cron_schedule.minutes.GetRange(0,10);//("*", 0, 10);
// Assert.AreEqual(results.ToArray(), new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
// }
[Test]
public void list_array_test()
{
var cron_schedule = new CronSchedule("1,2,3,4,5,6,7,8,9,10");
List<int> results = cron_schedule.minutes;
Assert.AreEqual(results.ToArray(), new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
}
// [Test]
// public void list_array_test()
// {
// var cron_schedule = new CronSchedule("1,2,3,4,5,6,7,8,9,10");
// List<int> results = cron_schedule.minutes;
// Assert.AreEqual(results.ToArray(), new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
// }
[Test]
public void generate_values_divided_test()
{
var cron_schedule = new CronSchedule("*/2");
List<int> results = cron_schedule.minutes.GetRange(0,5);//(, 0, 10);
Assert.AreEqual(results.ToArray(), new int[] { 0, 2, 4, 6, 8 });
}
// [Test]
// public void generate_values_divided_test()
// {
// var cron_schedule = new CronSchedule("*/2");
// List<int> results = cron_schedule.minutes.GetRange(0,5);//(, 0, 10);
// Assert.AreEqual(results.ToArray(), new int[] { 0, 2, 4, 6, 8 });
// }
[Test]
public void generate_values_range_test()
{
var cron_schedule = new CronSchedule("1-10");
List<int> results = cron_schedule.minutes.GetRange(0,10);//(, 0, 10);
Assert.AreEqual(results.ToArray(), new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
}
// [Test]
// public void generate_values_range_test()
// {
// var cron_schedule = new CronSchedule("1-10");
// List<int> results = cron_schedule.minutes.GetRange(0,10);//(, 0, 10);
// Assert.AreEqual(results.ToArray(), new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
// }
[Test]
public void generate_minutes_test()
{
var cron_schedule = new CronSchedule("1,2,3 * * * *");
Assert.AreEqual(cron_schedule.minutes.ToArray(), new int[] { 1, 2, 3 });
}
// [Test]
// public void generate_minutes_test()
// {
// var cron_schedule = new CronSchedule("1,2,3 * * * *");
// Assert.AreEqual(cron_schedule.minutes.ToArray(), new int[] { 1, 2, 3 });
// }
[Test]
public void generate_hours_test()
{
var cron_schedule = new CronSchedule("* 1,2,3 * * *");
Assert.AreEqual(cron_schedule.hours.ToArray(), new int[] { 1, 2, 3 });
}
// [Test]
// public void generate_hours_test()
// {
// var cron_schedule = new CronSchedule("* 1,2,3 * * *");
// Assert.AreEqual(cron_schedule.hours.ToArray(), new int[] { 1, 2, 3 });
// }
[Test]
public void generate_days_of_month_test()
{
var cron_schedule = new CronSchedule("* * 1,2,3 * *");
Assert.AreEqual(cron_schedule.days_of_month.ToArray(), new int[] { 1, 2, 3 });
}
// [Test]
// public void generate_days_of_month_test()
// {
// var cron_schedule = new CronSchedule("* * 1,2,3 * *");
// Assert.AreEqual(cron_schedule.days_of_month.ToArray(), new int[] { 1, 2, 3 });
// }
[Test]
public void generate_months_test()
{
var cron_schedule = new CronSchedule("* * * 1,2,3 *");
Assert.AreEqual(cron_schedule.months.ToArray(), new int[] { 1, 2, 3 });
}
// [Test]
// public void generate_months_test()
// {
// var cron_schedule = new CronSchedule("* * * 1,2,3 *");
// Assert.AreEqual(cron_schedule.months.ToArray(), new int[] { 1, 2, 3 });
// }
[Test]
public void generate_days_of_weeks()
{
var cron_schedule = new CronSchedule("* * * * 1,2,3 ");
Assert.AreEqual(cron_schedule.days_of_week.ToArray(), new int[] { 1, 2, 3 });
}
// [Test]
// public void generate_days_of_weeks()
// {
// var cron_schedule = new CronSchedule("* * * * 1,2,3 ");
// Assert.AreEqual(cron_schedule.days_of_week.ToArray(), new int[] { 1, 2, 3 });
// }
[Test]
public void is_time_minute_test()
{
var cron_schedule = new CronSchedule("0 * * * *");
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("8:00 am")));
Assert.IsFalse(cron_schedule.isTime(DateTime.Parse("8:01 am")));
// [Test]
// public void is_time_minute_test()
// {
// var cron_schedule = new CronSchedule("0 * * * *");
// Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("8:00 am")));
// Assert.IsFalse(cron_schedule.isTime(DateTime.Parse("8:01 am")));
cron_schedule = new CronSchedule("0-10 * * * *");
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("8:00 am")));
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("8:03 am")));
// cron_schedule = new CronSchedule("0-10 * * * *");
// Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("8:00 am")));
// Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("8:03 am")));
cron_schedule = new CronSchedule("*/2 * * * *");
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("8:00 am")));
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("8:02 am")));
Assert.IsFalse(cron_schedule.isTime(DateTime.Parse("8:03 am")));
}
// cron_schedule = new CronSchedule("*/2 * * * *");
// Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("8:00 am")));
// Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("8:02 am")));
// Assert.IsFalse(cron_schedule.isTime(DateTime.Parse("8:03 am")));
// }
[Test]
public void is_time_hour_test()
{
var cron_schedule = new CronSchedule("* 0 * * *");
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("12:00 am")));
// [Test]
// public void is_time_hour_test()
// {
// var cron_schedule = new CronSchedule("* 0 * * *");
// Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("12:00 am")));
cron_schedule = new CronSchedule("* 0,12 * * *");
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("12:00 am")));
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("12:00 pm")));
}
// cron_schedule = new CronSchedule("* 0,12 * * *");
// Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("12:00 am")));
// Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("12:00 pm")));
// }
[Test]
public void is_time_day_of_month_test()
{
var cron_schedule = new CronSchedule("* * 1 * *");
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("2010/08/01")));
}
// [Test]
// public void is_time_day_of_month_test()
// {
// var cron_schedule = new CronSchedule("* * 1 * *");
// Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("2010/08/01")));
// }
[Test]
public void is_time_month_test()
{
var cron_schedule = new CronSchedule("* * * 1 *");
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("1/1/2008")));
// [Test]
// public void is_time_month_test()
// {
// var cron_schedule = new CronSchedule("* * * 1 *");
// Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("1/1/2008")));
cron_schedule = new CronSchedule("* * * 12 *");
Assert.IsFalse(cron_schedule.isTime(DateTime.Parse("1/1/2008")));
// cron_schedule = new CronSchedule("* * * 12 *");
// Assert.IsFalse(cron_schedule.isTime(DateTime.Parse("1/1/2008")));
cron_schedule = new CronSchedule("* * * */3 *");
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("3/1/2008")));
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("6/1/2008")));
}
// cron_schedule = new CronSchedule("* * * */3 *");
// Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("3/1/2008")));
// Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("6/1/2008")));
// }
[Test]
public void is_time_day_of_week_test()
{
var cron_schedule = new CronSchedule("* * * * 0");
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("10/12/2008")));
Assert.IsFalse(cron_schedule.isTime(DateTime.Parse("10/13/2008")));
// [Test]
// public void is_time_day_of_week_test()
// {
// var cron_schedule = new CronSchedule("* * * * 0");
// Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("10/12/2008")));
// Assert.IsFalse(cron_schedule.isTime(DateTime.Parse("10/13/2008")));
cron_schedule = new CronSchedule("* * * * */2");
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("10/14/2008")));
}
// cron_schedule = new CronSchedule("* * * * */2");
// Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("10/14/2008")));
// }
[Test]
public void is_time_test()
{
var cron_schedule = new CronSchedule("0 0 12 10 *");
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("12:00:00 am 10/12/2008")));
Assert.IsFalse(cron_schedule.isTime(DateTime.Parse("12:01:00 am 10/12/2008")));
}
// [Test]
// public void is_time_test()
// {
// var cron_schedule = new CronSchedule("0 0 12 10 *");
// Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("12:00:00 am 10/12/2008")));
// Assert.IsFalse(cron_schedule.isTime(DateTime.Parse("12:01:00 am 10/12/2008")));
// }
[Test]
public static void ppp()
{
var d = new CronDaemon();
d.AddJob("*/1 * * * *", () => { Console.WriteLine(DateTime.Now.ToString()); });
d.Start();
//Thread.Sleep(60 * 1000);
}
}
}
// [Test]
// public static void ppp()
// {
// var d = new CronDaemon();
// d.AddJob("*/1 * * * *", () => { Console.WriteLine(DateTime.Now.ToString()); });
// d.Start();
// //Thread.Sleep(60 * 1000);
// }
// }
//}