-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathGivenARecurringTask.cs
More file actions
95 lines (77 loc) · 3.9 KB
/
GivenARecurringTask.cs
File metadata and controls
95 lines (77 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.IO;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Gofer.NET.Utils;
using Xunit;
using FluentAssertions;
using System.Reflection;
namespace Gofer.NET.Tests
{
public class GivenARecurringTask
{
[Fact]
public void ItPersistsPropertiesWhenSerializedAndDeserialized()
{
var taskKey =
new TaskKey($"{nameof(ItPersistsPropertiesWhenSerializedAndDeserialized)}::{Guid.NewGuid().ToString()}");
var testTask = GetTestTask(() =>
Console.WriteLine(nameof(ItPersistsPropertiesWhenSerializedAndDeserialized)));
var recurringTasks = new [] {
new RecurringTask(testTask, TimeSpan.FromMinutes(5), taskKey),
new RecurringTask(testTask, "* * * * * *", taskKey)
};
foreach (var recurringTask in recurringTasks) {
var serializedRecurringTask = JsonTaskInfoSerializer.Serialize(recurringTask);
var deserializedRecurringTask = JsonTaskInfoSerializer.Deserialize<RecurringTask>(serializedRecurringTask);
deserializedRecurringTask.StartTime.Should().Be(recurringTask.StartTime);
deserializedRecurringTask.TaskKey.Should().Be(recurringTask.TaskKey);
deserializedRecurringTask.Interval.Should().Be(recurringTask.Interval);
deserializedRecurringTask.Crontab.Should().Be(recurringTask.Crontab);
deserializedRecurringTask.TaskInfo.IsEquivalent(recurringTask.TaskInfo).Should().BeTrue();
}
}
[Fact]
public void ItProperlyDeterminesEquivalence()
{
var taskInfo1 = GetTestTask(() => TestMethod1("hello world"));
var taskInfo2 = GetTestTask(() => TestMethod2());
var taskKey = new TaskKey($"{nameof(ItPersistsPropertiesWhenSerializedAndDeserialized)}::{Guid.NewGuid().ToString()}");
new RecurringTask(taskInfo1, TimeSpan.FromMinutes(5), taskKey)
.IsEquivalent(new RecurringTask(taskInfo1, TimeSpan.FromMinutes(5), taskKey))
.Should().BeTrue("TRUE: Equivalent Timespans");
new RecurringTask(taskInfo1, "* * * * * *", taskKey)
.IsEquivalent(new RecurringTask(taskInfo1, "* * * * * *", taskKey))
.Should().BeTrue("TRUE: Equivalent Crontabs");
new RecurringTask(taskInfo1, TimeSpan.FromMinutes(5), taskKey)
.IsEquivalent(new RecurringTask(taskInfo2, TimeSpan.FromMinutes(5), taskKey))
.Should().BeFalse("FALSE: Different TaskInfo");
new RecurringTask(taskInfo1, TimeSpan.FromMinutes(5), taskKey)
.IsEquivalent(new RecurringTask(taskInfo1, TimeSpan.FromMinutes(7), taskKey))
.Should().BeFalse("FALSE: Different Timespans");
new RecurringTask(taskInfo1, "* * * * * *", taskKey)
.IsEquivalent(new RecurringTask(taskInfo1, "1 * * * * *", taskKey))
.Should().BeFalse("FALSE: Different Crontabs");
new RecurringTask(taskInfo1, TimeSpan.FromMinutes(5), taskKey)
.IsEquivalent(new RecurringTask(taskInfo1, "* * * * * *", taskKey))
.Should().BeFalse("FALSE: Timespan & Crontab");
Action differentIdsThrow = () =>
new RecurringTask(taskInfo1, TimeSpan.FromMinutes(5), taskKey)
.IsEquivalent(new RecurringTask(taskInfo1, TimeSpan.FromMinutes(5), new TaskKey("anothertaskkey")));
differentIdsThrow.ShouldThrow<Exception>();
}
private TaskInfo GetTestTask(Expression<Action> action)
{
return action.ToTaskInfo();
}
private string TestMethod1(string arg)
{
return arg;
}
private void TestMethod2()
{
Console.WriteLine(nameof(TestMethod2));
}
}
}