Skip to content

Commit 14c3ed2

Browse files
committed
Readme, tests, CustomClock
1 parent 908e49e commit 14c3ed2

File tree

4 files changed

+171
-57
lines changed

4 files changed

+171
-57
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Example usage when running unit tests
4242
```csharp
4343
//Arrange
4444
DateTime startTime = new DateTime(1970, 1, 1);
45-
ISystemClock systemClock = new ConstantClock(startTime);
45+
CustomClock systemClock = new CustomClock(startTime);
4646
MyService myService = new MyService(systemClock);
4747

4848
// Act
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
namespace HeboTech.Quartz.Tests
2+
{
3+
public class CustomClockTests
4+
{
5+
[Fact]
6+
public void Default_constructor_initialize_with_DateTimeOffset_min_value()
7+
{
8+
ISystemClock customClock = new CustomClock();
9+
10+
Assert.Equal(DateTimeOffset.MinValue, customClock.UtcNow);
11+
}
12+
13+
[Fact]
14+
public void Setting_custom_DateTimeOffset_through_constructor_returns_the_custom_time()
15+
{
16+
DateTimeOffset time = new(2022, 1, 1, 0, 0, 0, TimeSpan.Zero);
17+
ISystemClock customClock = new CustomClock(time);
18+
19+
Assert.Equal(time, customClock.UtcNow);
20+
}
21+
22+
[Fact]
23+
public void Setting_custom_DateTimeOffset_func_through_constructor_returns_the_custom_time()
24+
{
25+
DateTimeOffset time = new(2022, 1, 1, 0, 0, 0, TimeSpan.Zero);
26+
ISystemClock customClock = new CustomClock(() => time);
27+
28+
Assert.Equal(time, customClock.UtcNow);
29+
}
30+
31+
[Fact]
32+
public void Setting_custom_DateTime_through_constructor_returns_the_custom_time()
33+
{
34+
DateTime time = new(2022, 1, 1, 0, 0, 0, DateTimeKind.Utc);
35+
ISystemClock customClock = new CustomClock(time);
36+
37+
Assert.Equal(time, customClock.UtcNow);
38+
}
39+
40+
[Fact]
41+
public void Setting_custom__DateTime_func_through_constructor_returns_the_custom_time()
42+
{
43+
DateTime time = new(2022, 1, 1, 0, 0, 0, DateTimeKind.Utc);
44+
ISystemClock customClock = new CustomClock(() => time);
45+
46+
Assert.Equal(time, customClock.UtcNow);
47+
}
48+
49+
[Fact]
50+
public void Set_DateTimeOffset_returns_the_custom_time()
51+
{
52+
DateTimeOffset time = new(2022, 1, 1, 0, 0, 0, TimeSpan.Zero);
53+
CustomClock customClock = new CustomClock();
54+
customClock.Set(time);
55+
56+
Assert.Equal(time, customClock.UtcNow);
57+
}
58+
59+
[Fact]
60+
public void Set_DateTimeOffset_func_returns_the_custom_time()
61+
{
62+
DateTimeOffset time = new(2022, 1, 1, 0, 0, 0, TimeSpan.Zero);
63+
CustomClock customClock = new CustomClock();
64+
customClock.Set(() => time);
65+
66+
Assert.Equal(time, customClock.UtcNow);
67+
}
68+
69+
[Fact]
70+
public void Set_DateTime_returns_the_custom_time()
71+
{
72+
DateTime time = new(2022, 1, 1, 0, 0, 0, DateTimeKind.Utc);
73+
CustomClock customClock = new CustomClock();
74+
customClock.Set(time);
75+
76+
Assert.Equal(time, customClock.UtcNow);
77+
}
78+
79+
[Fact]
80+
public void Set_DateTime_func_returns_the_custom_time()
81+
{
82+
DateTime time = new(2022, 1, 1, 0, 0, 0, DateTimeKind.Utc);
83+
CustomClock customClock = new CustomClock();
84+
customClock.Set(() => time);
85+
86+
Assert.Equal(time, customClock.UtcNow);
87+
}
88+
}
89+
}

src/HeboTech.Quartz/ConstantClock.cs

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/HeboTech.Quartz/CustomClock.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
3+
namespace HeboTech.Quartz
4+
{
5+
public class CustomClock : ISystemClock
6+
{
7+
private Func<DateTimeOffset> timeFunc;
8+
9+
public CustomClock()
10+
{
11+
timeFunc = () => DateTimeOffset.MinValue;
12+
}
13+
14+
public CustomClock(DateTimeOffset time)
15+
{
16+
timeFunc = () => time;
17+
}
18+
19+
public CustomClock(Func<DateTimeOffset> timeFunc)
20+
{
21+
if (timeFunc == null)
22+
throw new ArgumentNullException(nameof(timeFunc));
23+
24+
this.timeFunc = timeFunc;
25+
}
26+
27+
public CustomClock(DateTime time)
28+
{
29+
timeFunc = () => time;
30+
}
31+
32+
public CustomClock(Func<DateTime> timeFunc)
33+
{
34+
if (timeFunc == null)
35+
throw new ArgumentNullException(nameof(timeFunc));
36+
37+
this.timeFunc = () => timeFunc();
38+
}
39+
40+
public DateTimeOffset UtcNow => timeFunc();
41+
42+
/// <summary>
43+
/// Sets the current time. The func can either return a constant time, or a dynamic one (like DateTimeOffset.UtcNow).
44+
/// </summary>
45+
/// <param name="timeFunc">Func that returns the current time</param>
46+
public void Set(Func<DateTimeOffset> timeFunc)
47+
{
48+
this.timeFunc = timeFunc ?? throw new ArgumentNullException(nameof(timeFunc));
49+
}
50+
51+
/// <summary>
52+
/// Sets the current (constant) time.
53+
/// </summary>
54+
/// <param name="time">The current (constant) time</param>
55+
public void Set(DateTimeOffset time)
56+
{
57+
timeFunc = () => time;
58+
}
59+
60+
/// <summary>
61+
/// Sets the current time. The func can either return a constant time, or a dynamic one (like DateTime.UtcNow).
62+
/// </summary>
63+
/// <param name="timeFunc">Func that returns the current time</param>
64+
public void Set(Func<DateTime> timeFunc)
65+
{
66+
if (timeFunc == null)
67+
throw new ArgumentNullException(nameof(timeFunc));
68+
69+
this.timeFunc = () => timeFunc();
70+
}
71+
72+
/// <summary>
73+
/// Sets the current (constant) time.
74+
/// </summary>
75+
/// <param name="time">The current (constant) time</param>
76+
public void Set(DateTime time)
77+
{
78+
timeFunc = () => time;
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)