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

add a small test for scheduler #178

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Cognite.Simulator.Tests/UtilsTests/SimulationSchedulerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Threading.Tasks;
using Xunit;
using CogniteSdk.Alpha;
using NCrontab;

namespace Cognite.Simulator.Tests.UtilsTests
{
Expand All @@ -37,9 +38,27 @@ public Task Delay(TimeSpan delay, CancellationToken token)
}
}


[Collection(nameof(SequentialTestCollection))]
public class SimulationSchedulerTest
{
[Fact(DisplayName = "Test Basic Working of Scheduler")]
public void TestBasicWorkingofExternalLibrary()
{
var job = new ScheduledJob<SimulatorRoutineRevision>() { };
job.SetSchedule("*/5 * * * *");
var nextOccurence = job.Schedule.GetNextOccurrence(DateTime.Now);
Assert.True(nextOccurence > DateTime.Now);


Assert.Throws<CrontabException>(() =>
{
job = new ScheduledJob<SimulatorRoutineRevision>() { };
job.SetSchedule("0 0 0 0 0");
});

}

[Fact]
public async Task TestSimulationSchedulerBase()
{
Expand Down
16 changes: 14 additions & 2 deletions Cognite.Simulator.Utils/SimulationScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ public class ScheduledJob<V> where V : SimulatorRoutineRevision
/// Routine revision.
/// </summary>
public V RoutineRevision { get; set; }

/// <summary>
/// Set schedule
/// </summary>
/// <param name="schedule">Crontab schedule</param>
/// <returns>Self</returns>
/// <exception cref="CrontabException">If the schedule is invalid</exception>
///
public ScheduledJob<V> SetSchedule(string CronExpression)
{
Schedule = CrontabSchedule.Parse(CronExpression);
return this;
}
Comment on lines +84 to +88
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not having this in the constructor?

}
/// <summary>
/// This class implements a basic simulation scheduler. It runs a loop on a configurable interval.
Expand Down Expand Up @@ -171,14 +184,13 @@ await Task.Run(async () =>
{
continue;
}
var schedule = CrontabSchedule.Parse(routineRev.Configuration.Schedule.CronExpression);
var newJob = new ScheduledJob<V>
{
Schedule = schedule,
TokenSource = new CancellationTokenSource(),
CreatedTime = routineRev.CreatedTime,
RoutineRevision = routineRev,
};
newJob.SetSchedule(routineRev.Configuration.Schedule.CronExpression);
_logger.LogDebug("Created new job for schedule: {0} with id {1}", routineRev.Configuration.Schedule.CronExpression, routineRev.ExternalId);
scheduledJobs.Add(routineRev.RoutineExternalId, newJob);
}
Expand Down
Loading