|
1 | 1 | using System;
|
2 | 2 | using Xunit;
|
3 | 3 |
|
4 |
| -namespace Server.Tests |
| 4 | +namespace Server.Tests; |
| 5 | + |
| 6 | +[Collection("Sequential Tests")] |
| 7 | +public class TimerTests : IClassFixture<ServerFixture> |
5 | 8 | {
|
6 |
| - [Collection("Sequential Tests")] |
7 |
| - public class TimerTests : IClassFixture<ServerFixture> |
| 9 | + [Theory] |
| 10 | + [InlineData(0L, 8L)] |
| 11 | + [InlineData(80L, 80L)] |
| 12 | + [InlineData(65L, 72L)] |
| 13 | + [InlineData(32767L, 32768L)] |
| 14 | + [InlineData(32768L, 32768L)] |
| 15 | + [InlineData(32833L, 32840L)] |
| 16 | + [InlineData(134217729L, 134217736L)] |
| 17 | + public void TestVariousTimes(long ticks, long expectedTicks) |
8 | 18 | {
|
9 |
| - [Theory] |
10 |
| - [InlineData(0L, 8L)] |
11 |
| - [InlineData(80L, 80L)] |
12 |
| - [InlineData(65L, 72L)] |
13 |
| - [InlineData(32767L, 32768L)] |
14 |
| - [InlineData(32768L, 32768L)] |
15 |
| - [InlineData(32833L, 32840L)] |
16 |
| - [InlineData(134217729L, 134217736L)] |
17 |
| - public void TestVariousTimes(long ticks, long expectedTicks) |
18 |
| - { |
19 |
| - var timerTicks = new TimerTicks(); |
20 |
| - void action() |
21 |
| - { |
22 |
| - Assert.Equal(expectedTicks, timerTicks.Ticks); |
23 |
| - timerTicks.ExecutedCount++; |
24 |
| - } |
| 19 | + var timerTicks = new TimerTicks(); |
| 20 | + Timer.Init(timerTicks.Ticks); |
25 | 21 |
|
26 |
| - Timer.Init(timerTicks.Ticks); |
| 22 | + Timer.StartTimer(TimeSpan.FromMilliseconds(ticks), action); |
27 | 23 |
|
28 |
| - Timer.StartTimer(TimeSpan.FromMilliseconds(ticks), action); |
| 24 | + var tickCount = expectedTicks / 8; |
29 | 25 |
|
30 |
| - var tickCount = expectedTicks / 8; |
| 26 | + for (int i = 1; i <= tickCount; i++) |
| 27 | + { |
| 28 | + timerTicks.Ticks = i * 8; |
31 | 29 |
|
32 |
| - for (int i = 1; i <= tickCount; i++) |
33 |
| - { |
34 |
| - timerTicks.Ticks = i * 8; |
| 30 | + Timer.Slice(timerTicks.Ticks); |
| 31 | + } |
35 | 32 |
|
36 |
| - Timer.Slice(timerTicks.Ticks); |
37 |
| - } |
| 33 | + Assert.Equal(1, timerTicks.ExecutedCount); |
| 34 | + return; |
38 | 35 |
|
39 |
| - Assert.Equal(1, timerTicks.ExecutedCount); |
| 36 | + void action() |
| 37 | + { |
| 38 | + Assert.Equal(expectedTicks, timerTicks.Ticks); |
| 39 | + timerTicks.ExecutedCount++; |
40 | 40 | }
|
| 41 | + } |
41 | 42 |
|
42 |
| - [Theory] |
43 |
| - [InlineData(1000L, 1000L, 30000L, 30000L, 2)] |
44 |
| - public void TestIntervals(long delay, long expectedDelayTicks, long interval, long expectedIntervalTicks, int count) |
45 |
| - { |
46 |
| - var timerTicks = new TimerTicks(); |
| 43 | + [Theory] |
| 44 | + [InlineData(1000L, 1000L, 30000L, 30000L, 2)] |
| 45 | + public void TestIntervals(long delay, long expectedDelayTicks, long interval, long expectedIntervalTicks, int count) |
| 46 | + { |
| 47 | + var timerTicks = new TimerTicks(); |
47 | 48 |
|
48 |
| - Timer.Init(timerTicks.Ticks); |
| 49 | + Timer.Init(timerTicks.Ticks); |
49 | 50 |
|
50 |
| - Timer.StartTimer(TimeSpan.FromMilliseconds(delay), TimeSpan.FromMilliseconds(interval), count, action); |
| 51 | + Timer.StartTimer(TimeSpan.FromMilliseconds(delay), TimeSpan.FromMilliseconds(interval), count, action); |
51 | 52 |
|
52 |
| - var tickCount = (expectedDelayTicks + (expectedIntervalTicks * count - 1)) / 8; |
| 53 | + var tickCount = (expectedDelayTicks + (expectedIntervalTicks * count - 1)) / 8; |
53 | 54 |
|
54 |
| - for (int i = 1; i <= tickCount; i++) |
55 |
| - { |
56 |
| - timerTicks.Ticks = i * 8; |
| 55 | + for (int i = 1; i <= tickCount; i++) |
| 56 | + { |
| 57 | + timerTicks.Ticks = i * 8; |
57 | 58 |
|
58 |
| - Timer.Slice(timerTicks.Ticks); |
59 |
| - } |
| 59 | + Timer.Slice(timerTicks.Ticks); |
| 60 | + } |
60 | 61 |
|
61 |
| - Assert.Equal(count, timerTicks.ExecutedCount); |
62 |
| - return; |
| 62 | + Assert.Equal(count, timerTicks.ExecutedCount); |
| 63 | + return; |
63 | 64 |
|
64 |
| - void action() |
65 |
| - { |
66 |
| - timerTicks.ExpectedTicks += timerTicks.ExecutedCount++ == 0 ? expectedDelayTicks : expectedIntervalTicks; |
67 |
| - Assert.Equal(timerTicks.ExpectedTicks, timerTicks.Ticks); |
68 |
| - } |
| 65 | + void action() |
| 66 | + { |
| 67 | + timerTicks.ExpectedTicks += timerTicks.ExecutedCount++ == 0 ? expectedDelayTicks : expectedIntervalTicks; |
| 68 | + Assert.Equal(timerTicks.ExpectedTicks, timerTicks.Ticks); |
69 | 69 | }
|
| 70 | + } |
70 | 71 |
|
71 |
| - [Fact] |
72 |
| - public void TestTimerStartedOnTick() |
73 |
| - { |
74 |
| - var timerTicks = new TimerTicks(); |
| 72 | + [Fact] |
| 73 | + public void TestTimerStartedOnTick() |
| 74 | + { |
| 75 | + var timerTicks = new TimerTicks(); |
75 | 76 |
|
76 |
| - Timer.Init(timerTicks.Ticks); |
| 77 | + Timer.Init(timerTicks.Ticks); |
77 | 78 |
|
78 |
| - var timer = new SelfRunningTimer(timerTicks); |
79 |
| - timer.Start(); |
| 79 | + var timer = new SelfRunningTimer(timerTicks); |
| 80 | + timer.Start(); |
80 | 81 |
|
81 |
| - Timer.Slice(128); |
82 |
| - Assert.Equal(1, timerTicks.ExecutedCount); |
83 |
| - Timer.Slice(256); |
84 |
| - Assert.Equal(2, timerTicks.ExecutedCount); |
85 |
| - } |
| 82 | + Timer.Slice(128); |
| 83 | + Assert.Equal(1, timerTicks.ExecutedCount); |
| 84 | + Timer.Slice(256); |
| 85 | + Assert.Equal(2, timerTicks.ExecutedCount); |
| 86 | + } |
86 | 87 |
|
87 |
| - private class TimerTicks |
88 |
| - { |
89 |
| - public long ExpectedTicks; |
90 |
| - public long Ticks; |
91 |
| - public int ExecutedCount; |
92 |
| - } |
| 88 | + private class TimerTicks |
| 89 | + { |
| 90 | + public long ExpectedTicks; |
| 91 | + public long Ticks; |
| 92 | + public int ExecutedCount; |
| 93 | + } |
93 | 94 |
|
94 |
| - private class SelfRunningTimer : Timer |
95 |
| - { |
96 |
| - private readonly TimerTicks _timerTicks; |
97 |
| - public SelfRunningTimer(TimerTicks ticks) : base(TimeSpan.FromMilliseconds(100)) => _timerTicks = ticks; |
| 95 | + private class SelfRunningTimer : Timer |
| 96 | + { |
| 97 | + private readonly TimerTicks _timerTicks; |
| 98 | + public SelfRunningTimer(TimerTicks ticks) : base(TimeSpan.FromMilliseconds(100)) => _timerTicks = ticks; |
98 | 99 |
|
99 |
| - protected override void OnTick() |
| 100 | + protected override void OnTick() |
| 101 | + { |
| 102 | + if (_timerTicks.ExecutedCount++ == 0) |
100 | 103 | {
|
101 |
| - if (_timerTicks.ExecutedCount++ == 0) |
102 |
| - { |
103 |
| - Delay = TimeSpan.FromMilliseconds(100); |
104 |
| - Start(); |
105 |
| - } |
| 104 | + Delay = TimeSpan.FromMilliseconds(100); |
| 105 | + Start(); |
106 | 106 | }
|
107 | 107 | }
|
108 | 108 | }
|
|
0 commit comments