|
| 1 | +namespace RecurrentTasks |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using Microsoft.Extensions.Logging; |
| 5 | + using Microsoft.Extensions.DependencyInjection; |
| 6 | + using Xunit; |
| 7 | + |
| 8 | + public class TaskBaseTests : IDisposable |
| 9 | + { |
| 10 | + private SampleTask sampleTask; |
| 11 | + |
| 12 | + public TaskBaseTests() |
| 13 | + { |
| 14 | + var lf = new LoggerFactory(); |
| 15 | + lf.AddConsole(); |
| 16 | + |
| 17 | + var serviceProvider = new ServiceCollection().BuildServiceProvider(); |
| 18 | + sampleTask = new SampleTask( |
| 19 | + lf, |
| 20 | + TimeSpan.FromSeconds(5), |
| 21 | + serviceProvider.GetService<IServiceScopeFactory>() |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | + public void Dispose() |
| 26 | + { |
| 27 | + if (sampleTask != null) |
| 28 | + { |
| 29 | + if (sampleTask.IsStarted) |
| 30 | + { |
| 31 | + sampleTask.Stop(); |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public void Task_CanStart() |
| 38 | + { |
| 39 | + // must start after 2 seconds |
| 40 | + sampleTask.Start(TimeSpan.FromSeconds(2)); |
| 41 | + |
| 42 | + // waiting 5 seconds max, then failing |
| 43 | + Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5))); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public void Task_CanNotStartTwice() |
| 48 | + { |
| 49 | + // must start after 1 seconds |
| 50 | + sampleTask.Start(TimeSpan.FromSeconds(1)); |
| 51 | + |
| 52 | + // waiting 5 seconds max (then failing) |
| 53 | + Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5))); |
| 54 | + |
| 55 | + // and real test - trying to start again |
| 56 | + var ex = Assert.Throws<InvalidOperationException>(() => sampleTask.Start()); |
| 57 | + } |
| 58 | + |
| 59 | + [Fact] |
| 60 | + public void Task_RunAgainAndAgain() |
| 61 | + { |
| 62 | + // must start after 2 seconds |
| 63 | + sampleTask.Start(TimeSpan.FromSeconds(2)); |
| 64 | + |
| 65 | + // waiting 5 seconds max, then failing |
| 66 | + Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5))); |
| 67 | + |
| 68 | + // resetting event |
| 69 | + sampleTask.TaskRunCalled.Reset(); |
| 70 | + |
| 71 | + // waiting for next run - twice default interval |
| 72 | + Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(10))); |
| 73 | + } |
| 74 | + |
| 75 | + [Fact] |
| 76 | + public void Task_CanStop() |
| 77 | + { |
| 78 | + // must start after 2 seconds |
| 79 | + sampleTask.Start(TimeSpan.FromSeconds(2)); |
| 80 | + |
| 81 | + // waiting 5 seconds max, then failing |
| 82 | + Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5))); |
| 83 | + |
| 84 | + sampleTask.TaskRunCalled.Reset(); |
| 85 | + sampleTask.Stop(); |
| 86 | + |
| 87 | + // should NOT run again - waiting twice default interval |
| 88 | + Assert.False(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(10))); |
| 89 | + } |
| 90 | + |
| 91 | + [Fact] |
| 92 | + public void Task_CanNotStopTwice() |
| 93 | + { |
| 94 | + // must start after 2 seconds |
| 95 | + sampleTask.Start(TimeSpan.FromSeconds(2)); |
| 96 | + |
| 97 | + // waiting 5 seconds max, then failing |
| 98 | + Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5))); |
| 99 | + |
| 100 | + sampleTask.Stop(); |
| 101 | + |
| 102 | + System.Threading.Thread.Sleep(500); // wait for real stop |
| 103 | + |
| 104 | + // and real test - trying to stop again |
| 105 | + var ex = Assert.Throws<InvalidOperationException>(() => sampleTask.Stop()); |
| 106 | + } |
| 107 | + |
| 108 | + [Fact] |
| 109 | + public void Task_IsStarted_Works() |
| 110 | + { |
| 111 | + Assert.False(sampleTask.IsStarted); |
| 112 | + |
| 113 | + sampleTask.Start(TimeSpan.FromSeconds(2)); |
| 114 | + |
| 115 | + Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5))); |
| 116 | + |
| 117 | + Assert.True(sampleTask.IsStarted); |
| 118 | + |
| 119 | + sampleTask.Stop(); |
| 120 | + |
| 121 | + System.Threading.Thread.Sleep(500); // wait for real stop |
| 122 | + |
| 123 | + Assert.False(sampleTask.IsStarted); |
| 124 | + } |
| 125 | + |
| 126 | + [Fact] |
| 127 | + public void Task_IsRunningRightNow_Works() |
| 128 | + { |
| 129 | + Assert.False(sampleTask.IsRunningRightNow, "Already running... WFT???"); |
| 130 | + |
| 131 | + sampleTask.CanContinueRun.Reset(); // do not complete 'Run' without permission! |
| 132 | + |
| 133 | + sampleTask.Start(TimeSpan.FromSeconds(2)); |
| 134 | + Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5))); |
| 135 | + |
| 136 | + Assert.True(sampleTask.IsRunningRightNow, "Oops, IsRunningRightNow is not 'true'. Something is broken!!!"); |
| 137 | + |
| 138 | + sampleTask.CanContinueRun.Set(); |
| 139 | + |
| 140 | + sampleTask.Stop(); |
| 141 | + |
| 142 | + System.Threading.Thread.Sleep(500); // wait for real stop |
| 143 | + |
| 144 | + Assert.False(sampleTask.IsRunningRightNow, "Ooops, IsRunningRightNow is still 'true'.... WTF???"); |
| 145 | + } |
| 146 | + |
| 147 | + [Fact] |
| 148 | + public void Task_RunImmediately_Works() |
| 149 | + { |
| 150 | + // must start after 2 seconds |
| 151 | + sampleTask.Start(TimeSpan.FromSeconds(2)); |
| 152 | + |
| 153 | + // waiting 5 seconds max, then failing |
| 154 | + Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5)), "Failed to start first time"); |
| 155 | + |
| 156 | + sampleTask.TaskRunCalled.Reset(); |
| 157 | + |
| 158 | + sampleTask.TryRunImmediately(); |
| 159 | + |
| 160 | + // waiting very little time, not 'full' 5 secs |
| 161 | + Assert.True(sampleTask.TaskRunCalled.Wait(1000), "Not run immediately :( "); |
| 162 | + } |
| 163 | + |
| 164 | + [Fact] |
| 165 | + public void Task_RunningAgainAfterException() |
| 166 | + { |
| 167 | + sampleTask.MustThrowError = true; |
| 168 | + sampleTask.Start(TimeSpan.FromSeconds(2)); |
| 169 | + |
| 170 | + // waiting 5 seconds max, then failing |
| 171 | + Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5))); |
| 172 | + |
| 173 | + sampleTask.TaskRunCalled.Reset(); |
| 174 | + |
| 175 | + // should run again - waiting twice default interval |
| 176 | + Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(10))); |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments