Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testcase for #1441 #1490

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
104 changes: 104 additions & 0 deletions src/Quartz.Tests.Unit/SystemTimeChange.cs
@@ -0,0 +1,104 @@
using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using NUnit.Framework;
using Quartz.Impl;

namespace Quartz.Tests.Unit
{
internal class SystemTimeChange
{
public static Boolean Ran;

[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME
{
public Int16 wYear;
public Int16 wMonth;
public Int16 wDayOfWeek;
public Int16 wDay;
public Int16 wHour;
public Int16 wMinute;
public Int16 wSecond;
public Int16 wMilliseconds;
}

[TestFixture]
public class Testcase : IJob
{
public Task Execute(IJobExecutionContext context)
{
Ran = true;
return Task.CompletedTask;
}
public void Dispose()
{
SystemTime.Now = () => DateTime.Now;
}

[DllImport("kernel32.dll", SetLastError = true)]
public static extern Boolean SetSystemTime(ref SYSTEMTIME st);

private SYSTEMTIME GetSYSTEMTIME(DateTime dt)
{
return new SYSTEMTIME()
{
wYear = (Int16)dt.Year,
wMonth = (Int16)dt.Month,
wDay = (Int16)dt.Day,
wDayOfWeek = (Int16)dt.DayOfWeek,
wHour = (Int16)dt.Hour,
wMinute = (Int16)dt.Minute,
wSecond = (Int16)dt.Second,
wMilliseconds = (Int16)dt.Millisecond,
};
}

public async Task<Boolean> ChangingTime()
{
const Int32 interval = 2;
const Int32 multiplier = 3;

IScheduler scheduler = await new StdSchedulerFactory().GetScheduler();
IJobDetail job = JobBuilder.Create<Testcase>().Build();
ITrigger trigger = TriggerBuilder.Create()
.WithSimpleSchedule(
s => s.WithIntervalInSeconds(interval)
.RepeatForever()
.WithMisfireHandlingInstructionFireNow()
)
.Build();

await scheduler.ScheduleJob(job, trigger);
await scheduler.Start();

DateTime yesterDateTime = DateTime.Now.AddDays(-1);
SYSTEMTIME yesterDay = GetSYSTEMTIME(yesterDateTime);
SetSystemTime(ref yesterDay);

if (DateTime.Now.Day != yesterDateTime.Day)
{
Console.WriteLine("Could not set the system date/time.");
return false;
}

Ran = false;
await Task.Delay(interval * multiplier * 1000);

DateTime todayDateTime = DateTime.Now.AddDays(1);
SYSTEMTIME today = GetSYSTEMTIME(todayDateTime);

SetSystemTime(ref today);
await Task.Delay(interval * multiplier * 1000);

return Ran;
}

[Test]
public async Task Main()
{
Assert.IsTrue(await ChangingTime());
}
}
}
}