Merges changes from Mehdi Gholam (mgholam@hotmail.com)

This commit is contained in:
Kevin Colyar 2013-07-17 09:06:45 -07:00
parent 2613d6d29d
commit b38b7e8ba5
10 changed files with 234 additions and 132 deletions

4
CronNET.sln Normal file → Executable file
View File

@ -1,6 +1,6 @@
 
Microsoft Visual Studio Solution File, Format Version 9.00 Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2005 # Visual C# Express 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CronNET", "CronNET\CronNET.csproj", "{F31D7AF3-FDFA-44F1-9C63-305BAF11D002}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CronNET", "CronNET\CronNET.csproj", "{F31D7AF3-FDFA-44F1-9C63-305BAF11D002}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CronNETTests", "CronNETTests\CronNETTests.csproj", "{6FCFBDF4-ECB7-4FC2-A376-E962B11D487D}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CronNETTests", "CronNETTests\CronNETTests.csproj", "{6FCFBDF4-ECB7-4FC2-A376-E962B11D487D}"

22
CronNET/CronDaemon.cs Normal file → Executable file
View File

@ -1,30 +1,34 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Timers; using System.Timers;
using System.Threading;
namespace CronNET namespace CronNET
{ {
public class CronDaemon public class CronDaemon
{ {
private readonly Timer timer = new Timer(60000); private readonly System.Timers.Timer timer = new System.Timers.Timer(30000);
private readonly List<CronJob> cron_jobs = new List<CronJob>(); private readonly List<CronJob> cron_jobs = new List<CronJob>();
private DateTime _last= DateTime.Now;
public CronDaemon() public CronDaemon()
{ {
timer.AutoReset = true;
timer.Elapsed += timer_elapsed; timer.Elapsed += timer_elapsed;
} }
public void add_job(CronJob cron_job) public void AddJob(string schedule, ThreadStart action)
{ {
cron_jobs.Add(cron_job); var cj = new CronJob(schedule, action);
cron_jobs.Add(cj);
} }
public void start() public void Start()
{ {
timer.Start(); timer.Start();
} }
public void stop() public void Stop()
{ {
timer.Stop(); timer.Stop();
@ -34,8 +38,12 @@ namespace CronNET
private void timer_elapsed(object sender, ElapsedEventArgs e) private void timer_elapsed(object sender, ElapsedEventArgs e)
{ {
foreach (CronJob job in cron_jobs) if (DateTime.Now.Minute != _last.Minute)
job.execute(DateTime.Now); {
_last = DateTime.Now;
foreach (CronJob job in cron_jobs)
job.execute(DateTime.Now);
}
} }
} }
} }

16
CronNET/CronJob.cs Normal file → Executable file
View File

@ -16,16 +16,20 @@ namespace CronNET
_thread = new Thread(thread_start); _thread = new Thread(thread_start);
} }
private object _lock = new object();
public void execute(DateTime date_time) public void execute(DateTime date_time)
{ {
if (!_cron_schedule.is_time(date_time)) lock (_lock)
return; {
if (!_cron_schedule.isTime(date_time))
return;
if (_thread.ThreadState == ThreadState.Running) if (_thread.ThreadState == ThreadState.Running)
return; return;
_thread = new Thread(_thread_start); _thread = new Thread(_thread_start);
_thread.Start(); _thread.Start();
}
} }
public void abort() public void abort()

9
CronNET/CronNET.csproj Normal file → Executable file
View File

@ -1,4 +1,5 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
@ -9,6 +10,12 @@
<AppDesignerFolder>Properties</AppDesignerFolder> <AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Cron</RootNamespace> <RootNamespace>Cron</RootNamespace>
<AssemblyName>Cron</AssemblyName> <AssemblyName>Cron</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileUpgradeFlags>
</FileUpgradeFlags>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>2.0</OldToolsVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>

115
CronNET/CronSchedule.cs Normal file → Executable file
View File

@ -9,7 +9,7 @@ namespace CronNET
#region Readonly Class Members #region Readonly Class Members
readonly static Regex divided_regex = new Regex(@"(\*/\d+)"); readonly static Regex divided_regex = new Regex(@"(\*/\d+)");
readonly static Regex range_regex = new Regex(@"(\d+\-\d+)"); readonly static Regex range_regex = new Regex(@"(\d+\-\d+)\/?(\d+)?");
readonly static Regex wild_regex = new Regex(@"(\*)"); readonly static Regex wild_regex = new Regex(@"(\*)");
readonly static Regex list_regex = new Regex(@"(((\d+,)*\d+)+)"); readonly static Regex list_regex = new Regex(@"(((\d+,)*\d+)+)");
readonly static Regex validation_regex = new Regex(divided_regex + "|" + range_regex + "|" + wild_regex + "|" + list_regex); readonly static Regex validation_regex = new Regex(divided_regex + "|" + range_regex + "|" + wild_regex + "|" + list_regex);
@ -17,9 +17,9 @@ namespace CronNET
#endregion #endregion
#region Private Instance Members #region Private Instance Members
private readonly string _expression; private readonly string _expression;
public List<int> minutes; public List<int> minutes;
public List<int> hours; public List<int> hours;
public List<int> days_of_month; public List<int> days_of_month;
public List<int> months; public List<int> months;
@ -43,65 +43,81 @@ namespace CronNET
#region Public Methods #region Public Methods
public bool is_valid() private bool isValid()
{ {
return is_valid(this._expression); return isValid(this._expression);
} }
public bool is_valid(string expression) public bool isValid(string expression)
{ {
MatchCollection matches = validation_regex.Matches(expression); MatchCollection matches = validation_regex.Matches(expression);
return matches.Count == 5; return matches.Count > 0;//== 5;
} }
public bool is_time(DateTime date_time) public bool isTime(DateTime date_time)
{ {
return minutes.Contains(date_time.Minute) && return minutes.Contains(date_time.Minute) &&
hours.Contains(date_time.Hour) && hours.Contains(date_time.Hour) &&
days_of_month.Contains(date_time.Day) && days_of_month.Contains(date_time.Day) &&
months.Contains(date_time.Month) && months.Contains(date_time.Month) &&
days_of_week.Contains((int)date_time.DayOfWeek); days_of_week.Contains((int)date_time.DayOfWeek);
} }
public void generate() private void generate()
{ {
if(!is_valid()) return; if (!isValid()) return;
MatchCollection matches = validation_regex.Matches(this._expression); MatchCollection matches = validation_regex.Matches(this._expression);
generate_minutes(matches[0]); generate_minutes(matches[0].ToString());
generate_hours(matches[1]);
generate_days_of_month(matches[2]); if (matches.Count > 1)
generate_months(matches[3]); generate_hours(matches[1].ToString());
generate_days_of_weeks(matches[4]); 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("*");
} }
public void generate_minutes(Match match) private void generate_minutes(string match)
{ {
this.minutes = generate_values(match.ToString(), 0, 60); this.minutes = generate_values(match, 0, 60);
} }
public void generate_hours(Match match) private void generate_hours(string match)
{ {
this.hours = generate_values(match.ToString(), 0, 24); this.hours = generate_values(match, 0, 24);
} }
public void generate_days_of_month(Match match) private void generate_days_of_month(string match)
{ {
this.days_of_month = generate_values(match.ToString(), 1, 32); this.days_of_month = generate_values(match, 1, 32);
} }
public void generate_months(Match match) private void generate_months(string match)
{ {
this.months = generate_values(match.ToString(), 1, 13); this.months = generate_values(match, 1, 13);
} }
public void generate_days_of_weeks(Match match) private void generate_days_of_weeks(string match)
{ {
this.days_of_week = generate_values(match.ToString(), 0, 7); this.days_of_week = generate_values(match, 0, 7);
} }
public List<int> generate_values(string configuration, int start, int max) private List<int> generate_values(string configuration, int start, int max)
{ {
if (divided_regex.IsMatch(configuration)) return divided_array(configuration, start, max); if (divided_regex.IsMatch(configuration)) return divided_array(configuration, start, max);
if (range_regex.IsMatch(configuration)) return range_array(configuration); if (range_regex.IsMatch(configuration)) return range_array(configuration);
@ -111,31 +127,44 @@ namespace CronNET
return new List<int>(); return new List<int>();
} }
public List<int> divided_array(string configuration, int start, int max) private List<int> divided_array(string configuration, int start, int max)
{ {
if(!divided_regex.IsMatch(configuration)) if (!divided_regex.IsMatch(configuration))
return new List<int>(); return new List<int>();
List<int> ret = new List<int>(); List<int> ret = new List<int>();
string[] split = configuration.Split("/".ToCharArray()); string[] split = configuration.Split("/".ToCharArray());
int divisor = int.Parse(split[1]); int divisor = int.Parse(split[1]);
for(int i=start; i < max; ++i) for (int i = start; i < max; ++i)
if(i % divisor == 0) if (i % divisor == 0)
ret.Add(i); ret.Add(i);
return ret; return ret;
} }
public List<int> range_array(string configuration) private List<int> range_array(string configuration)
{ {
if(!range_regex.IsMatch(configuration)) if (!range_regex.IsMatch(configuration))
return new List<int>(); return new List<int>();
List<int> ret = new List<int>(); List<int> ret = new List<int>();
string[] split = configuration.Split("-".ToCharArray()); string[] split = configuration.Split("-".ToCharArray());
int start = int.Parse(split[0]); int start = int.Parse(split[0]);
int end = int.Parse(split[1]); int end = 0;
if (split[1].Contains("/"))
{
split = split[1].Split("/".ToCharArray());
end = int.Parse(split[0]);
int divisor = int.Parse(split[1]);
for (int i = start; i < end; ++i)
if (i % divisor == 0)
ret.Add(i);
return ret;
}
else
end = int.Parse(split[1]);
for (int i = start; i <= end; ++i) for (int i = start; i <= end; ++i)
ret.Add(i); ret.Add(i);
@ -143,9 +172,9 @@ namespace CronNET
return ret; return ret;
} }
public List<int> wild_array(string configuration, int start, int max) private List<int> wild_array(string configuration, int start, int max)
{ {
if(!wild_regex.IsMatch(configuration)) if (!wild_regex.IsMatch(configuration))
return new List<int>(); return new List<int>();
List<int> ret = new List<int>(); List<int> ret = new List<int>();
@ -156,16 +185,16 @@ namespace CronNET
return ret; return ret;
} }
public List<int> list_array(string configuration) private List<int> list_array(string configuration)
{ {
if(!list_regex.IsMatch(configuration)) if (!list_regex.IsMatch(configuration))
return new List<int>(); return new List<int>();
List<int> ret = new List<int>(); List<int> ret = new List<int>();
string[] split = configuration.Split(",".ToCharArray()); string[] split = configuration.Split(",".ToCharArray());
foreach(string s in split) foreach (string s in split)
ret.Add(int.Parse(s)); ret.Add(int.Parse(s));
return ret; return ret;

0
CronNET/Properties/AssemblyInfo.cs Normal file → Executable file
View File

51
CronNETTests/CronNETTests.csproj Normal file → Executable file
View File

@ -1,4 +1,5 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
@ -9,6 +10,27 @@
<AppDesignerFolder>Properties</AppDesignerFolder> <AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CronTests</RootNamespace> <RootNamespace>CronTests</RootNamespace>
<AssemblyName>CronTests</AssemblyName> <AssemblyName>CronTests</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileUpgradeFlags>
</FileUpgradeFlags>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>2.0</OldToolsVersion>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
@ -28,7 +50,10 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="MbUnit.Framework, Version=2.4.2.130, Culture=neutral, PublicKeyToken=5e72ecd30bc408d5" /> <Reference Include="nunit.framework, Version=2.5.10.11092, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\NUnit 2.5.10\bin\net-2.0\framework\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
@ -43,6 +68,28 @@
<Name>CronNET</Name> <Name>CronNET</Name>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.0">
<Visible>False</Visible>
<ProductName>Microsoft .NET Framework 4 %28x86 and x64%29</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName>
<Install>false</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Windows.Installer.3.1">
<Visible>False</Visible>
<ProductName>Windows Installer 3.1</ProductName>
<Install>true</Install>
</BootstrapperPackage>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.

143
CronNETTests/CronScheduleTests.cs Normal file → Executable file
View File

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

0
CronNETTests/Properties/AssemblyInfo.cs Normal file → Executable file
View File

6
CronTests.nunit Executable file
View File

@ -0,0 +1,6 @@
<NUnitProject>
<Settings activeconfig="Default" />
<Config name="Default" binpathtype="Auto">
<assembly path="CronNETTests\bin\Debug\CronTests.dll" />
</Config>
</NUnitProject>