90 lines
2.5 KiB
C#
90 lines
2.5 KiB
C#
using System;
|
|
using Antlr4.Runtime;
|
|
using ChaosBot.Services;
|
|
using NUnit.Framework;
|
|
|
|
namespace ChaosBot.UnitTests
|
|
{
|
|
public class TimerTests
|
|
{
|
|
[SetUp]
|
|
public void Setup()
|
|
{
|
|
}
|
|
|
|
[Test]
|
|
public void RunAt_RunningTimerAtTimestamp_True()
|
|
{
|
|
DateTime current = DateTime.Now;
|
|
DateTime runAt = current + new TimeSpan(TimeSpan.TicksPerSecond * 10);
|
|
DateTime testAt = current + new TimeSpan(TimeSpan.TicksPerSecond * 2);
|
|
|
|
int totalRuns = 0;
|
|
|
|
void ToRun()
|
|
{
|
|
totalRuns++;
|
|
}
|
|
|
|
int timerId = Timer.RunAt(ToRun, runAt);
|
|
|
|
Assert.AreEqual(totalRuns, 0);
|
|
// Wait for the timer to finish
|
|
Timer.Join(timerId);
|
|
Assert.AreEqual(totalRuns, 1);
|
|
Assert.GreaterOrEqual(DateTime.Now, testAt);
|
|
}
|
|
|
|
[Test]
|
|
public void RunIn_RunningTimerInTimeSpan_True()
|
|
{
|
|
DateTime current = DateTime.Now;
|
|
TimeSpan runIn = new TimeSpan(TimeSpan.TicksPerSecond * 10);
|
|
DateTime testAt = current + new TimeSpan(TimeSpan.TicksPerSecond * 2);
|
|
|
|
int totalRuns = 0;
|
|
|
|
void ToRun()
|
|
{
|
|
totalRuns++;
|
|
}
|
|
|
|
int timerId = Timer.RunIn(ToRun, runIn);
|
|
|
|
Assert.AreEqual(totalRuns, 0);
|
|
// Wait for the timer to finish
|
|
Timer.Join(timerId);
|
|
Assert.AreEqual(totalRuns, 1);
|
|
Assert.GreaterOrEqual(DateTime.Now, testAt);
|
|
}
|
|
|
|
[Test]
|
|
public void RunTimer_RunningTimerCountingExecutions_True()
|
|
{
|
|
DateTime current = DateTime.Now;
|
|
TimeSpan interval = new TimeSpan(TimeSpan.TicksPerSecond * 2);
|
|
DateTime testAt = current + new TimeSpan(TimeSpan.TicksPerSecond * 11);
|
|
|
|
int totalRuns = 0;
|
|
|
|
void ToRun()
|
|
{
|
|
totalRuns++;
|
|
}
|
|
|
|
int timerId = Timer.RunTimer(ToRun, interval, offset: interval);
|
|
|
|
void CancelCallback()
|
|
{
|
|
Timer.CancelTimer(timerId);
|
|
}
|
|
|
|
int cancelTimerId = Timer.RunAt(CancelCallback, testAt);
|
|
|
|
Assert.AreEqual(totalRuns, 0);
|
|
// Wait for the cancel timer to finish
|
|
Timer.Join(cancelTimerId);
|
|
Assert.AreEqual(totalRuns, 5);
|
|
}
|
|
}
|
|
} |