| | | 1 | | using AmbientServices.Utilities; |
| | | 2 | | using AmbientServices; |
| | | 3 | | using Microsoft.VisualStudio.TestTools.UnitTesting; |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Linq; |
| | | 8 | | using System.Runtime.CompilerServices; |
| | | 9 | | using System.Text; |
| | | 10 | | using System.Threading; |
| | | 11 | | using System.Threading.Tasks; |
| | | 12 | | |
| | | 13 | | namespace AmbientServices.Test |
| | | 14 | | { |
| | | 15 | | [TestClass] |
| | | 16 | | public class TestAsync |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Performs basic tests on the <see cref="Async"/> class. |
| | | 20 | | /// </summary> |
| | | 21 | | [TestMethod] |
| | | 22 | | public void Async_Basic() |
| | | 23 | | { |
| | 1 | 24 | | Assert.AreNotEqual(Async.DefaultAsyncContext, Async.SinglethreadedContext); |
| | 1 | 25 | | int result = Async.RunTaskSync(() => |
| | 1 | 26 | | { |
| | 1 | 27 | | Task<int> task = Async_BasicAsync(); |
| | 1 | 28 | | return task; |
| | 1 | 29 | | }); |
| | 1 | 30 | | } |
| | | 31 | | /// <summary> |
| | | 32 | | /// Performs basic tests on the <see cref="Async"/> class. |
| | | 33 | | /// </summary> |
| | | 34 | | [TestMethod] |
| | | 35 | | public void Async_AlreadyRun() |
| | | 36 | | { |
| | 1 | 37 | | Async.RunTaskSync(() => Task.CompletedTask); |
| | 1 | 38 | | } |
| | | 39 | | /// <summary> |
| | | 40 | | /// Performs basic tests on the <see cref="Async"/> class. |
| | | 41 | | /// </summary> |
| | | 42 | | [TestMethod] |
| | | 43 | | public void Async_NotAlreadyRun() |
| | | 44 | | { |
| | 1 | 45 | | Async.RunTaskSync(() => new Task(() => { })); |
| | 1 | 46 | | } |
| | | 47 | | /// <summary> |
| | | 48 | | /// Performs basic tests on the <see cref="Async"/> class. |
| | | 49 | | /// </summary> |
| | | 50 | | [TestMethod] |
| | | 51 | | public void Async_AggregateExceptionUnwrap() |
| | | 52 | | { |
| | 1 | 53 | | Assert.ThrowsExactly<ExpectedException>(() => Async.RunTaskSync(() => throw new AggregateException(new Expec |
| | 1 | 54 | | } |
| | | 55 | | /// <summary> |
| | | 56 | | /// Performs basic tests on the <see cref="Async"/> class. |
| | | 57 | | /// </summary> |
| | | 58 | | [TestMethod] |
| | | 59 | | public void Async_AggregateExceptionUnwrapWithTask() |
| | | 60 | | { |
| | 1 | 61 | | Assert.ThrowsExactly<ExpectedException>(() => Async.RunTaskSync(async () => { await Task.Delay(10); throw ne |
| | 1 | 62 | | } |
| | | 63 | | /// <summary> |
| | | 64 | | /// Performs basic tests on the <see cref="Async"/> class. |
| | | 65 | | /// </summary> |
| | | 66 | | [TestMethod] |
| | | 67 | | public void Async_AggregateExceptionUnwrapWithReturn() |
| | | 68 | | { |
| | 1 | 69 | | Assert.AreNotEqual(Async.DefaultAsyncContext, Async.SinglethreadedContext); |
| | 1 | 70 | | Assert.ThrowsExactly<ExpectedException>(() => Async.RunTaskSync(async () => |
| | 1 | 71 | | { |
| | 1 | 72 | | await Task.Delay(10); |
| | 1 | 73 | | return await Async_BasicAsyncThrow(new AggregateException(new ExpectedException(nameof(Async_AggregateEx |
| | 0 | 74 | | })); |
| | 1 | 75 | | } |
| | | 76 | | /// <summary> |
| | | 77 | | /// Performs basic tests on the <see cref="Async"/> class. |
| | | 78 | | /// </summary> |
| | | 79 | | [TestMethod] |
| | | 80 | | public void Async_AggregateExceptionCantUnwrap() |
| | | 81 | | { |
| | 1 | 82 | | Assert.ThrowsExactly<AggregateException>(() => Async.RunTaskSync(() => throw new AggregateException(new Expe |
| | 1 | 83 | | } |
| | | 84 | | /// <summary> |
| | | 85 | | /// Performs basic tests on the <see cref="Async"/> class. |
| | | 86 | | /// </summary> |
| | | 87 | | [TestMethod] |
| | | 88 | | public void Async_AggregateExceptionCantUnwrapWithTask() |
| | | 89 | | { |
| | 1 | 90 | | Assert.ThrowsExactly<AggregateException>(() => Async.RunTaskSync(async () => { await Task.Delay(10); throw n |
| | 1 | 91 | | } |
| | | 92 | | /// <summary> |
| | | 93 | | /// Performs basic tests on the <see cref="Async"/> class. |
| | | 94 | | /// </summary> |
| | | 95 | | [TestMethod] |
| | | 96 | | public void Async_AggregateExceptionCantUnwrapWithReturn() |
| | | 97 | | { |
| | 1 | 98 | | Assert.AreNotEqual(Async.DefaultAsyncContext, Async.SinglethreadedContext); |
| | 1 | 99 | | Assert.ThrowsExactly<AggregateException>(() => Async.RunTaskSync(async () => |
| | 1 | 100 | | { |
| | 1 | 101 | | await Task.Delay(10); |
| | 1 | 102 | | return await Async_BasicAsyncThrow(new AggregateException(new ExpectedException(nameof(Async_AggregateEx |
| | 0 | 103 | | })); |
| | 1 | 104 | | } |
| | | 105 | | /// <summary> |
| | | 106 | | /// Performs basic tests on the <see cref="Async"/> class. |
| | | 107 | | /// </summary> |
| | | 108 | | [TestMethod] |
| | | 109 | | public void Async_SynchronizeVariations() |
| | | 110 | | { |
| | 1 | 111 | | TimeSpan timeout = TimeSpan.FromSeconds(60); |
| | 1 | 112 | | Async.RunTaskSync(() => |
| | 1 | 113 | | { |
| | 1 | 114 | | Task task = Async_BasicAsyncPart2(); |
| | 1 | 115 | | return task; |
| | 1 | 116 | | }); |
| | 1 | 117 | | Async.RunTaskSync(() => Async_BasicAsyncPart2(default)); |
| | 1 | 118 | | int result = Async.RunTaskSync(Async_BasicAsyncPart3); |
| | 1 | 119 | | result = Async.RunTaskSync(() => Async_BasicAsyncPart4(default)); |
| | 1 | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Performs basic tests on the <see cref="Async"/> class. |
| | | 124 | | /// </summary> |
| | | 125 | | [TestMethod] |
| | | 126 | | public void Async_Misc() |
| | | 127 | | { |
| | | 128 | | // try to create a copy |
| | 1 | 129 | | System.Threading.SynchronizationContext context = SynchronousSynchronizationContext.Default.CreateCopy(); |
| | | 130 | | // try to send a delegate (should execute synchronously) |
| | 1 | 131 | | int set = 0; |
| | 1 | 132 | | SynchronousSynchronizationContext.Default.Send(new SendOrPostCallback(o => |
| | 1 | 133 | | { |
| | 1 | 134 | | set = 1; |
| | 1 | 135 | | }), |
| | 1 | 136 | | this); |
| | 1 | 137 | | Assert.AreEqual(1, set); |
| | | 138 | | |
| | 1 | 139 | | Assert.AreEqual(1, SynchronousTaskScheduler.Default.MaximumConcurrencyLevel); |
| | 1 | 140 | | } |
| | | 141 | | |
| | | 142 | | private Task<int> Async_BasicAsyncThrow(Exception ex) |
| | | 143 | | { |
| | 1 | 144 | | throw ex; |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | private async Task<int> Async_BasicAsync() |
| | | 148 | | { |
| | 1 | 149 | | await Async_BasicAsyncPart1(); |
| | 1 | 150 | | await Async_BasicAsyncPart2(); |
| | 1 | 151 | | await Async_BasicAsyncWithLock(); |
| | 1 | 152 | | int result = await Async_BasicAsyncPart3(); |
| | 1 | 153 | | return result; |
| | 1 | 154 | | } |
| | | 155 | | |
| | | 156 | | private async Task Async_BasicAsyncPart1() |
| | | 157 | | { |
| | 1 | 158 | | await Task.Delay(200); |
| | 1 | 159 | | await Task.Delay(50); |
| | 1 | 160 | | } |
| | | 161 | | |
| | | 162 | | private async Task Async_BasicAsyncPart2(CancellationToken cancel = default) |
| | | 163 | | { |
| | 1 | 164 | | Thread.Sleep(200); |
| | 1 | 165 | | Thread.Sleep(50); |
| | 1 | 166 | | await Task.Yield(); |
| | 1 | 167 | | } |
| | | 168 | | private async Task<int> Async_BasicAsyncPart3() |
| | | 169 | | { |
| | 1 | 170 | | await Task.Delay(50); |
| | 1 | 171 | | return 1378902; |
| | 1 | 172 | | } |
| | | 173 | | private async Task<int> Async_BasicAsyncPart4(CancellationToken cancel = default) |
| | | 174 | | { |
| | 1 | 175 | | await Task.Delay(50, cancel); |
| | 1 | 176 | | return 1378902; |
| | 1 | 177 | | } |
| | | 178 | | |
| | | 179 | | private async Task Async_BasicAsyncWithLock(CancellationToken cancel = default) |
| | | 180 | | { |
| | 1 | 181 | | using SemaphoreSlim lock2 = new(1); |
| | 1 | 182 | | await lock2.WaitAsync(1000, cancel); |
| | | 183 | | try |
| | | 184 | | { |
| | 1 | 185 | | await Task.Delay(50); |
| | 1 | 186 | | await Task.Yield(); |
| | 1 | 187 | | } |
| | | 188 | | finally |
| | | 189 | | { |
| | 1 | 190 | | lock2.Release(); |
| | | 191 | | } |
| | 1 | 192 | | } |
| | | 193 | | |
| | | 194 | | #if NETSTANDARD2_1 || NETCOREAPP3_1_OR_GREATER || NET5_0_OR_GREATER |
| | | 195 | | /// <summary> |
| | | 196 | | /// Performs basic tests on the <see cref="Async"/> class. |
| | | 197 | | /// </summary> |
| | | 198 | | [TestMethod] |
| | | 199 | | public void Async_Enumerable() |
| | | 200 | | { |
| | 1 | 201 | | int count = 0; |
| | 1 | 202 | | foreach (int i in Async.AsyncEnumerableToEnumerable(() => TestAsyncEnum(10, 100))) |
| | | 203 | | { |
| | 1 | 204 | | ++count; |
| | | 205 | | } |
| | 1 | 206 | | Assert.AreEqual(100, count); |
| | 1 | 207 | | } |
| | | 208 | | private async IAsyncEnumerable<int> TestAsyncEnum(int delayMilliseconds, int limit, [EnumeratorCancellation] Can |
| | | 209 | | { |
| | 1 | 210 | | for (int loop = 0; loop < limit; ++loop) |
| | | 211 | | { |
| | 1 | 212 | | await Task.Delay(delayMilliseconds); |
| | 1 | 213 | | yield return loop; |
| | | 214 | | } |
| | 1 | 215 | | } |
| | | 216 | | /// <summary> |
| | | 217 | | /// Performs basic tests on the <see cref="Async"/> class. |
| | | 218 | | /// </summary> |
| | | 219 | | [TestMethod] |
| | | 220 | | public void Async_Synchronize() |
| | | 221 | | { |
| | 1 | 222 | | Assert.AreNotEqual(Async.DefaultAsyncContext, Async.SinglethreadedContext); |
| | 1 | 223 | | Async.RunTaskSync(AsyncTest); |
| | 1 | 224 | | } |
| | | 225 | | private async Task AsyncTest() |
| | | 226 | | { |
| | 1 | 227 | | await Task.Delay(10); |
| | 1 | 228 | | } |
| | | 229 | | #endif |
| | | 230 | | |
| | | 231 | | |
| | | 232 | | /// <summary> |
| | | 233 | | /// Performs basic tests on the <see cref="Async"/> class. |
| | | 234 | | /// </summary> |
| | | 235 | | [TestMethod] |
| | | 236 | | public void Async_SyncInAsyncInSyncVoid() |
| | | 237 | | { |
| | 1 | 238 | | int mainThreadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 239 | | int threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 240 | | for (int count = 0; count < 3; ++count) |
| | | 241 | | { |
| | 1 | 242 | | Async.RunTaskSync(() => Loop1(mainThreadId)); |
| | 1 | 243 | | threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 244 | | Assert.AreEqual(mainThreadId, Thread.CurrentThread.ManagedThreadId); |
| | | 245 | | } |
| | 1 | 246 | | } |
| | | 247 | | private async Task Loop1(int mainThreadId) |
| | | 248 | | { |
| | 1 | 249 | | Assert.IsTrue(Async.UsingSynchronousExecution); |
| | 1 | 250 | | int threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 251 | | for (int count = 0; count < 3; ++count) |
| | | 252 | | { |
| | 1 | 253 | | Async.RunTaskSync(() => Loop2(mainThreadId, Thread.CurrentThread.ManagedThreadId)); |
| | 1 | 254 | | threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 255 | | Assert.AreEqual(mainThreadId, threadId); |
| | | 256 | | } |
| | 1 | 257 | | Assert.IsTrue(Async.UsingSynchronousExecution); |
| | 1 | 258 | | for (int count = 0; count < 3; ++count) |
| | | 259 | | { |
| | 1 | 260 | | await Async.RunTaskAsync(() => Loop2(mainThreadId, Thread.CurrentThread.ManagedThreadId)); |
| | 1 | 261 | | threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 262 | | Assert.AreEqual(mainThreadId, threadId); // RunAsync, unlike a native await, keeps us on the same thr |
| | | 263 | | } |
| | 1 | 264 | | Assert.IsTrue(Async.UsingSynchronousExecution); |
| | 1 | 265 | | for (int count = 0; count < 3; ++count) |
| | | 266 | | { |
| | 1 | 267 | | await Loop2(mainThreadId, Thread.CurrentThread.ManagedThreadId); // this native await on a non-specia |
| | 1 | 268 | | threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 269 | | Assert.IsFalse(Async.UsingSynchronousExecution); |
| | 1 | 270 | | Assert.AreNotEqual(mainThreadId, threadId); |
| | | 271 | | } |
| | 1 | 272 | | } |
| | | 273 | | private async Task Loop2(int mainThreadId, int mainThreadId2) |
| | | 274 | | { |
| | 1 | 275 | | int threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 276 | | Assert.AreEqual(threadId, mainThreadId2); |
| | 1 | 277 | | for (int count = 0; count < 3; ++count) |
| | | 278 | | { |
| | 1 | 279 | | await Async.RunTask(() => Task.Delay(10)); |
| | 1 | 280 | | threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 281 | | if (Async.UsingSynchronousExecution) |
| | | 282 | | { |
| | 1 | 283 | | Assert.AreEqual(mainThreadId, threadId); |
| | 1 | 284 | | Assert.AreEqual(mainThreadId2, threadId); |
| | | 285 | | } |
| | | 286 | | else |
| | | 287 | | { |
| | 1 | 288 | | Assert.AreNotEqual(mainThreadId, threadId); |
| | | 289 | | // when we're not using sync execution, although the main thread is unavailable to execute tasks (be |
| | | 290 | | // main thread 2 *is* available, so we could end up using that here, so we can't check be sure that |
| | | 291 | | } |
| | | 292 | | } |
| | 1 | 293 | | for (int count = 0; count < 3; ++count) |
| | | 294 | | { |
| | 1 | 295 | | await Task.Delay(10); |
| | 1 | 296 | | threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 297 | | if (Async.UsingSynchronousExecution) |
| | | 298 | | { |
| | 0 | 299 | | Assert.AreEqual(mainThreadId, threadId); |
| | 0 | 300 | | Assert.AreEqual(mainThreadId2, threadId); |
| | | 301 | | } |
| | | 302 | | else |
| | | 303 | | { |
| | 1 | 304 | | Assert.AreNotEqual(mainThreadId, threadId); |
| | | 305 | | // when we're not using sync execution, although the main thread is unavailable to execute tasks (be |
| | | 306 | | // main thread 2 *is* available, so we could end up using that here, so we can't check be sure that |
| | | 307 | | } |
| | | 308 | | } |
| | 1 | 309 | | } |
| | | 310 | | |
| | | 311 | | |
| | | 312 | | /// <summary> |
| | | 313 | | /// Performs basic tests on the <see cref="Async"/> class. |
| | | 314 | | /// </summary> |
| | | 315 | | [TestMethod] |
| | | 316 | | public void Async_SyncInAsyncInSyncWithReturn() |
| | | 317 | | { |
| | 1 | 318 | | int mainThreadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 319 | | int threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 320 | | for (int count = 0; count < 3; ++count) |
| | | 321 | | { |
| | 1 | 322 | | int result = Async.RunTaskSync(() => LoopWithReturn1(mainThreadId)); |
| | 1 | 323 | | threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 324 | | Assert.AreEqual(mainThreadId, Thread.CurrentThread.ManagedThreadId); |
| | | 325 | | } |
| | 1 | 326 | | } |
| | | 327 | | private async Task<int> LoopWithReturn1(int mainThreadId) |
| | | 328 | | { |
| | 1 | 329 | | Assert.IsTrue(Async.UsingSynchronousExecution); |
| | 1 | 330 | | int threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 331 | | for (int count = 0; count < 3; ++count) |
| | | 332 | | { |
| | 1 | 333 | | int result = Async.RunTaskSync(() => LoopWithReturn2(mainThreadId, Thread.CurrentThread.ManagedThreadId) |
| | 1 | 334 | | threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 335 | | Assert.AreEqual(mainThreadId, threadId); |
| | | 336 | | } |
| | 1 | 337 | | Assert.IsTrue(Async.UsingSynchronousExecution); |
| | 1 | 338 | | for (int count = 0; count < 3; ++count) |
| | | 339 | | { |
| | 1 | 340 | | int result = await Async.RunTaskAsync(() => LoopWithReturn2(mainThreadId, Thread.CurrentThread.ManagedTh |
| | 1 | 341 | | threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 342 | | Assert.AreEqual(mainThreadId, threadId); // RunAsync, unlike a native await, keeps us on the same thr |
| | | 343 | | } |
| | 1 | 344 | | Assert.IsTrue(Async.UsingSynchronousExecution); |
| | 1 | 345 | | for (int count = 0; count < 3; ++count) |
| | | 346 | | { |
| | 1 | 347 | | int result = await LoopWithReturn2(mainThreadId, Thread.CurrentThread.ManagedThreadId); // this nativ |
| | 1 | 348 | | threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 349 | | Assert.IsFalse(Async.UsingSynchronousExecution); |
| | 1 | 350 | | Assert.AreNotEqual(mainThreadId, threadId); |
| | | 351 | | } |
| | 1 | 352 | | return 0; |
| | 1 | 353 | | } |
| | | 354 | | private async Task<int> LoopWithReturn2(int mainThreadId, int mainThreadId2) |
| | | 355 | | { |
| | 1 | 356 | | int threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 357 | | Assert.AreEqual(threadId, mainThreadId2); |
| | 1 | 358 | | for (int count = 0; count < 3; ++count) |
| | | 359 | | { |
| | 1 | 360 | | int result = await Async.RunTask(() => Task.FromResult(0)); |
| | 1 | 361 | | threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 362 | | if (Async.UsingSynchronousExecution) |
| | | 363 | | { |
| | 1 | 364 | | Assert.AreEqual(mainThreadId, threadId); |
| | 1 | 365 | | Assert.AreEqual(mainThreadId2, threadId); |
| | | 366 | | } |
| | | 367 | | else |
| | | 368 | | { |
| | 1 | 369 | | Assert.AreNotEqual(mainThreadId, threadId); |
| | | 370 | | // when we're not using sync execution, although the main thread is unavailable to execute tasks (be |
| | | 371 | | // main thread 2 *is* available, so we could end up using that here, so we can't check be sure that |
| | | 372 | | } |
| | | 373 | | } |
| | 1 | 374 | | for (int count = 0; count < 3; ++count) |
| | | 375 | | { |
| | 1 | 376 | | await Task.Delay(10); |
| | 1 | 377 | | threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 378 | | if (Async.UsingSynchronousExecution) |
| | | 379 | | { |
| | 0 | 380 | | Assert.AreEqual(mainThreadId, threadId); |
| | 0 | 381 | | Assert.AreEqual(mainThreadId2, threadId); |
| | | 382 | | } |
| | | 383 | | else |
| | | 384 | | { |
| | 1 | 385 | | Assert.AreNotEqual(mainThreadId, threadId); |
| | | 386 | | // when we're not using sync execution, although the main thread is unavailable to execute tasks (be |
| | | 387 | | // main thread 2 *is* available, so we could end up using that here, so we can't check be sure that |
| | | 388 | | } |
| | | 389 | | } |
| | 1 | 390 | | return 0; |
| | 1 | 391 | | } |
| | | 392 | | |
| | | 393 | | |
| | | 394 | | /// <summary> |
| | | 395 | | /// Performs basic tests on the <see cref="Async"/> class. |
| | | 396 | | /// </summary> |
| | | 397 | | [TestMethod] |
| | | 398 | | public void Async_ValueTaskSyncInAsyncInSyncVoid() |
| | | 399 | | { |
| | 1 | 400 | | int mainThreadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 401 | | int threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 402 | | for (int count = 0; count < 3; ++count) |
| | | 403 | | { |
| | 1 | 404 | | Async.RunSync(() => ValueTaskLoop1(mainThreadId)); |
| | 1 | 405 | | threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 406 | | Assert.AreEqual(mainThreadId, Thread.CurrentThread.ManagedThreadId); |
| | | 407 | | } |
| | 1 | 408 | | } |
| | | 409 | | private async ValueTask ValueTaskLoop1(int mainThreadId) |
| | | 410 | | { |
| | 1 | 411 | | Assert.IsTrue(Async.UsingSynchronousExecution); |
| | 1 | 412 | | int threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 413 | | for (int count = 0; count < 3; ++count) |
| | | 414 | | { |
| | 1 | 415 | | Async.RunSync(() => ValueTaskLoop2(mainThreadId, Thread.CurrentThread.ManagedThreadId)); |
| | 1 | 416 | | threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 417 | | Assert.AreEqual(mainThreadId, threadId); |
| | | 418 | | } |
| | 1 | 419 | | Assert.IsTrue(Async.UsingSynchronousExecution); |
| | 1 | 420 | | for (int count = 0; count < 3; ++count) |
| | | 421 | | { |
| | 1 | 422 | | await Async.RunAsync(() => ValueTaskLoop2(mainThreadId, Thread.CurrentThread.ManagedThreadId)); |
| | 1 | 423 | | threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 424 | | Assert.AreEqual(mainThreadId, threadId); // RunAsync, unlike a native await, keeps us on the same thr |
| | | 425 | | } |
| | 1 | 426 | | Assert.IsTrue(Async.UsingSynchronousExecution); |
| | 1 | 427 | | for (int count = 0; count < 3; ++count) |
| | | 428 | | { |
| | 1 | 429 | | await ValueTaskLoop2(mainThreadId, Thread.CurrentThread.ManagedThreadId); // this native await on a n |
| | 1 | 430 | | threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 431 | | Assert.IsFalse(Async.UsingSynchronousExecution); |
| | 1 | 432 | | Assert.AreNotEqual(mainThreadId, threadId); |
| | | 433 | | } |
| | 1 | 434 | | } |
| | | 435 | | private async ValueTask ValueTaskLoop2(int mainThreadId, int mainThreadId2) |
| | | 436 | | { |
| | 1 | 437 | | int threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 438 | | Assert.AreEqual(threadId, mainThreadId2); |
| | 1 | 439 | | for (int count = 0; count < 3; ++count) |
| | | 440 | | { |
| | 1 | 441 | | await Async.RunTask(() => Task.Delay(10)); |
| | 1 | 442 | | threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 443 | | if (Async.UsingSynchronousExecution) |
| | | 444 | | { |
| | 1 | 445 | | Assert.AreEqual(mainThreadId, threadId); |
| | 1 | 446 | | Assert.AreEqual(mainThreadId2, threadId); |
| | | 447 | | } |
| | | 448 | | else |
| | | 449 | | { |
| | 1 | 450 | | Assert.AreNotEqual(mainThreadId, threadId); |
| | | 451 | | // when we're not using sync execution, although the main thread is unavailable to execute tasks (be |
| | | 452 | | // main thread 2 *is* available, so we could end up using that here, so we can't check be sure that |
| | | 453 | | } |
| | | 454 | | } |
| | 1 | 455 | | for (int count = 0; count < 3; ++count) |
| | | 456 | | { |
| | 1 | 457 | | await Task.Delay(10); |
| | 1 | 458 | | threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 459 | | if (Async.UsingSynchronousExecution) |
| | | 460 | | { |
| | 0 | 461 | | Assert.AreEqual(mainThreadId, threadId); |
| | 0 | 462 | | Assert.AreEqual(mainThreadId2, threadId); |
| | | 463 | | } |
| | | 464 | | else |
| | | 465 | | { |
| | 1 | 466 | | Assert.AreNotEqual(mainThreadId, threadId); |
| | | 467 | | // when we're not using sync execution, although the main thread is unavailable to execute tasks (be |
| | | 468 | | // main thread 2 *is* available, so we could end up using that here, so we can't check be sure that |
| | | 469 | | } |
| | | 470 | | } |
| | 1 | 471 | | } |
| | | 472 | | |
| | | 473 | | |
| | | 474 | | /// <summary> |
| | | 475 | | /// Performs basic tests on the <see cref="Async"/> class. |
| | | 476 | | /// </summary> |
| | | 477 | | [TestMethod] |
| | | 478 | | public void Async_ValueTaskSyncInAsyncInSyncWithReturn() |
| | | 479 | | { |
| | 1 | 480 | | int mainThreadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 481 | | int threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 482 | | for (int count = 0; count < 3; ++count) |
| | | 483 | | { |
| | 1 | 484 | | int result = Async.RunSync(() => ValueTaskLoopWithReturn1(mainThreadId)); |
| | 1 | 485 | | threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 486 | | Assert.AreEqual(mainThreadId, Thread.CurrentThread.ManagedThreadId); |
| | | 487 | | } |
| | 1 | 488 | | } |
| | | 489 | | private async ValueTask<int> ValueTaskLoopWithReturn1(int mainThreadId) |
| | | 490 | | { |
| | 1 | 491 | | Assert.IsTrue(Async.UsingSynchronousExecution); |
| | 1 | 492 | | int threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 493 | | for (int count = 0; count < 3; ++count) |
| | | 494 | | { |
| | 1 | 495 | | int result = Async.RunSync(() => ValueTaskLoopWithReturn2(mainThreadId, Thread.CurrentThread.ManagedThre |
| | 1 | 496 | | threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 497 | | Assert.AreEqual(mainThreadId, threadId); |
| | | 498 | | } |
| | 1 | 499 | | Assert.IsTrue(Async.UsingSynchronousExecution); |
| | 1 | 500 | | for (int count = 0; count < 3; ++count) |
| | | 501 | | { |
| | 1 | 502 | | int result = await Async.RunAsync(() => ValueTaskLoopWithReturn2(mainThreadId, Thread.CurrentThread.Mana |
| | 1 | 503 | | threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 504 | | Assert.AreEqual(mainThreadId, threadId); // RunAsync, unlike a native await, keeps us on the same thr |
| | | 505 | | } |
| | 1 | 506 | | Assert.IsTrue(Async.UsingSynchronousExecution); |
| | 1 | 507 | | for (int count = 0; count < 3; ++count) |
| | | 508 | | { |
| | 1 | 509 | | int result = await ValueTaskLoopWithReturn2(mainThreadId, Thread.CurrentThread.ManagedThreadId); // t |
| | 1 | 510 | | threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 511 | | Assert.IsFalse(Async.UsingSynchronousExecution); |
| | 1 | 512 | | Assert.AreNotEqual(mainThreadId, threadId); |
| | | 513 | | } |
| | 1 | 514 | | return 0; |
| | 1 | 515 | | } |
| | | 516 | | private async ValueTask<int> ValueTaskLoopWithReturn2(int mainThreadId, int mainThreadId2) |
| | | 517 | | { |
| | 1 | 518 | | int threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 519 | | Assert.AreEqual(threadId, mainThreadId2); |
| | 1 | 520 | | for (int count = 0; count < 3; ++count) |
| | | 521 | | { |
| | 1 | 522 | | int result = await Async.Run(() => new ValueTask<int>(0)); |
| | 1 | 523 | | threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 524 | | if (Async.UsingSynchronousExecution) |
| | | 525 | | { |
| | 1 | 526 | | Assert.AreEqual(mainThreadId, threadId); |
| | 1 | 527 | | Assert.AreEqual(mainThreadId2, threadId); |
| | | 528 | | } |
| | | 529 | | else |
| | | 530 | | { |
| | 1 | 531 | | Assert.AreNotEqual(mainThreadId, threadId); |
| | | 532 | | // when we're not using sync execution, although the main thread is unavailable to execute tasks (be |
| | | 533 | | // main thread 2 *is* available, so we could end up using that here, so we can't check to be sure th |
| | | 534 | | } |
| | | 535 | | } |
| | 1 | 536 | | for (int count = 0; count < 3; ++count) |
| | | 537 | | { |
| | 1 | 538 | | await Async.Run(() => default); |
| | 1 | 539 | | threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 540 | | if (Async.UsingSynchronousExecution) |
| | | 541 | | { |
| | 1 | 542 | | Assert.AreEqual(mainThreadId, threadId); |
| | 1 | 543 | | Assert.AreEqual(mainThreadId2, threadId); |
| | | 544 | | } |
| | | 545 | | else |
| | | 546 | | { |
| | 1 | 547 | | Assert.AreNotEqual(mainThreadId, threadId); |
| | | 548 | | // when we're not using sync execution, although the main thread is unavailable to execute tasks (be |
| | | 549 | | // main thread 2 *is* available, so we could end up using that here, so we can't check to be sure th |
| | | 550 | | } |
| | | 551 | | } |
| | 1 | 552 | | for (int count = 0; count < 3; ++count) |
| | | 553 | | { |
| | 1 | 554 | | await Task.Delay(10); |
| | 1 | 555 | | threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 556 | | if (Async.UsingSynchronousExecution) |
| | | 557 | | { |
| | 0 | 558 | | Assert.AreEqual(mainThreadId, threadId); |
| | 0 | 559 | | Assert.AreEqual(mainThreadId2, threadId); |
| | | 560 | | } |
| | | 561 | | else |
| | | 562 | | { |
| | 1 | 563 | | Assert.AreNotEqual(mainThreadId, threadId); |
| | | 564 | | // when we're not using sync execution, although the main thread is unavailable to execute tasks (be |
| | | 565 | | // main thread 2 *is* available, so we could end up using that here, so we can't check to be sure th |
| | | 566 | | } |
| | | 567 | | } |
| | 1 | 568 | | return 0; |
| | 1 | 569 | | } |
| | | 570 | | /// <summary> |
| | | 571 | | /// Performs basic tests on the <see cref="Async"/> class. |
| | | 572 | | /// </summary> |
| | | 573 | | [TestMethod] |
| | | 574 | | public void Sync_Enumerator() |
| | | 575 | | { |
| | 1 | 576 | | int count = 0; |
| | 1 | 577 | | foreach (int ret in 1.ToSingleItemEnumerable()) |
| | | 578 | | { |
| | 1 | 579 | | Assert.AreEqual(1, ++count); |
| | 1 | 580 | | Assert.AreEqual(1, ret); |
| | | 581 | | } |
| | 1 | 582 | | } |
| | | 583 | | #if NETSTANDARD2_1 || NETCOREAPP3_1_OR_GREATER || NET5_0_OR_GREATER |
| | | 584 | | /// <summary> |
| | | 585 | | /// Performs basic tests on the <see cref="Async"/> class. |
| | | 586 | | /// </summary> |
| | | 587 | | [TestMethod] |
| | | 588 | | public void Synchronized_AsyncEnumerator() |
| | | 589 | | { |
| | 1 | 590 | | int mainThreadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 591 | | int threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 592 | | foreach (int ret in Async.AsyncEnumerableToEnumerable(() => EnumerateAsync(10, default))) |
| | | 593 | | { |
| | 1 | 594 | | Assert.AreEqual(mainThreadId, Thread.CurrentThread.ManagedThreadId); |
| | | 595 | | } |
| | 1 | 596 | | } |
| | | 597 | | /// <summary> |
| | | 598 | | /// Performs basic tests on the <see cref="Async"/> class. |
| | | 599 | | /// </summary> |
| | | 600 | | [TestMethod] |
| | | 601 | | public void Sync_AsyncEnumerator() |
| | | 602 | | { |
| | 1 | 603 | | int mainThreadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 604 | | int threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 605 | | Async.RunTaskSync(async () => |
| | 1 | 606 | | { |
| | 1 | 607 | | await foreach (int ret in EnumerateAsync(10, default)) |
| | 1 | 608 | | { |
| | 1 | 609 | | Assert.AreEqual(mainThreadId, Thread.CurrentThread.ManagedThreadId); |
| | 1 | 610 | | } |
| | 1 | 611 | | }); |
| | 1 | 612 | | Async.RunSync(() => Async.AwaitForEach(EnumerateAsync(10, default), t => |
| | 1 | 613 | | { |
| | 1 | 614 | | Assert.AreEqual(mainThreadId, Thread.CurrentThread.ManagedThreadId); |
| | 1 | 615 | | }, default)); |
| | 1 | 616 | | Async.RunSync(() => Async.AwaitForEach(EnumerateAsync(10, default), (t, c) => |
| | 1 | 617 | | { |
| | 1 | 618 | | Assert.AreEqual(mainThreadId, Thread.CurrentThread.ManagedThreadId); |
| | 1 | 619 | | return default; |
| | 1 | 620 | | }, default)); |
| | 1 | 621 | | Async.RunTaskSync(async () => |
| | 1 | 622 | | { |
| | 1 | 623 | | foreach (int ret in await EnumerateAsync(10, default).GetAsyncEnumerator().ToListAsync()) |
| | 1 | 624 | | { |
| | 1 | 625 | | Assert.AreEqual(mainThreadId, Thread.CurrentThread.ManagedThreadId); |
| | 1 | 626 | | } |
| | 1 | 627 | | }); |
| | 1 | 628 | | Async.RunTaskSync(async () => |
| | 1 | 629 | | { |
| | 1 | 630 | | foreach (int ret in await EnumerateAsync(10, default).GetAsyncEnumerator().ToArrayAsync()) |
| | 1 | 631 | | { |
| | 1 | 632 | | Assert.AreEqual(mainThreadId, Thread.CurrentThread.ManagedThreadId); |
| | 1 | 633 | | } |
| | 1 | 634 | | }); |
| | 1 | 635 | | Async.RunTaskSync(async () => |
| | 1 | 636 | | { |
| | 1 | 637 | | foreach (int ret in await EnumerateAsync(10, default).GetAsyncEnumerator().ToEnumerableAsync()) |
| | 1 | 638 | | { |
| | 1 | 639 | | Assert.AreEqual(mainThreadId, Thread.CurrentThread.ManagedThreadId); |
| | 1 | 640 | | } |
| | 1 | 641 | | }); |
| | 1 | 642 | | Async.RunTaskSync(async () => |
| | 1 | 643 | | { |
| | 1 | 644 | | await foreach (int ret in new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }.ToAsyncEnumerable()) |
| | 1 | 645 | | { |
| | 1 | 646 | | Assert.AreEqual(mainThreadId, Thread.CurrentThread.ManagedThreadId); |
| | 1 | 647 | | } |
| | 1 | 648 | | }); |
| | 1 | 649 | | Async.RunTaskSync(async () => |
| | 1 | 650 | | { |
| | 1 | 651 | | await foreach (int ret in ((IEnumerable<int>)new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).GetEnumerator( |
| | 1 | 652 | | { |
| | 1 | 653 | | Assert.AreEqual(mainThreadId, Thread.CurrentThread.ManagedThreadId); |
| | 1 | 654 | | } |
| | 1 | 655 | | }); |
| | 1 | 656 | | Async.RunTaskSync(async () => |
| | 1 | 657 | | { |
| | 1 | 658 | | await foreach (int ret in 1.ToSingleItemAsyncEnumerable()) |
| | 1 | 659 | | { |
| | 1 | 660 | | Assert.AreEqual(mainThreadId, Thread.CurrentThread.ManagedThreadId); |
| | 1 | 661 | | } |
| | 1 | 662 | | }); |
| | 1 | 663 | | Async.RunTaskSync(async () => |
| | 1 | 664 | | { |
| | 1 | 665 | | await foreach (int ret in Array.Empty<int>().ToAsyncEnumerable()) |
| | 1 | 666 | | { |
| | 0 | 667 | | Assert.Fail("There should be no entries!"); |
| | 1 | 668 | | } |
| | 1 | 669 | | }); |
| | 1 | 670 | | Async.RunTaskSync(async () => |
| | 1 | 671 | | { |
| | 1 | 672 | | await foreach (int ret in ((IEnumerable<int>)Array.Empty<int>()).GetEnumerator().ToAsyncEnumerable()) |
| | 1 | 673 | | { |
| | 0 | 674 | | Assert.Fail("There should be no entries!"); |
| | 1 | 675 | | } |
| | 1 | 676 | | }); |
| | 1 | 677 | | foreach (int ret in Async.AsyncEnumerableToEnumerable(() => EnumerateAsync(10, default))) |
| | | 678 | | { |
| | 1 | 679 | | Assert.AreEqual(mainThreadId, Thread.CurrentThread.ManagedThreadId); |
| | | 680 | | } |
| | 1 | 681 | | } |
| | | 682 | | /// <summary> |
| | | 683 | | /// Performs basic tests on the <see cref="Async"/> class. |
| | | 684 | | /// </summary> |
| | | 685 | | [TestMethod] |
| | | 686 | | public async Task Async_AsyncEnumerator() |
| | | 687 | | { |
| | 1 | 688 | | int mainThreadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 689 | | int threadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 690 | | int limit = 10; |
| | 1 | 691 | | int max = 0; |
| | 1 | 692 | | await foreach (int ret in EnumerateAsync(10, default)) |
| | | 693 | | { |
| | 1 | 694 | | max = Math.Max(max, ret); |
| | | 695 | | } |
| | 1 | 696 | | Assert.AreEqual(limit - 1, max); |
| | 1 | 697 | | max = 0; |
| | 1 | 698 | | await Async.Run(() => Async.AwaitForEach(EnumerateAsync(limit, default), ret => |
| | 1 | 699 | | { |
| | 1 | 700 | | max = Math.Max(max, ret); |
| | 1 | 701 | | }, default)); |
| | 1 | 702 | | Assert.AreEqual(limit - 1, max); |
| | 1 | 703 | | max = 0; |
| | 1 | 704 | | await Async.Run(() => Async.AwaitForEach(EnumerateAsync(limit, default), (ret, c) => |
| | 1 | 705 | | { |
| | 1 | 706 | | max = Math.Max(max, ret); |
| | 1 | 707 | | return default; |
| | 1 | 708 | | }, default)); |
| | 1 | 709 | | Assert.AreEqual(limit - 1, max); |
| | 1 | 710 | | max = 0; |
| | 1 | 711 | | max = 0; |
| | 1 | 712 | | foreach (int ret in await EnumerateAsync(limit, default).GetAsyncEnumerator().ToListAsync()) |
| | | 713 | | { |
| | 1 | 714 | | max = Math.Max(max, ret); |
| | | 715 | | } |
| | 1 | 716 | | Assert.AreEqual(limit - 1, max); |
| | 1 | 717 | | max = 0; |
| | 1 | 718 | | foreach (int ret in await EnumerateAsync(limit, default).GetAsyncEnumerator().ToArrayAsync()) |
| | | 719 | | { |
| | 1 | 720 | | max = Math.Max(max, ret); |
| | | 721 | | } |
| | 1 | 722 | | Assert.AreEqual(limit - 1, max); |
| | 1 | 723 | | max = 0; |
| | 1 | 724 | | foreach (int ret in await EnumerateAsync(limit, default).GetAsyncEnumerator().ToEnumerableAsync()) |
| | | 725 | | { |
| | 1 | 726 | | max = Math.Max(max, ret); |
| | | 727 | | } |
| | 1 | 728 | | Assert.AreEqual(limit - 1, max); |
| | 1 | 729 | | max = 0; |
| | 1 | 730 | | foreach (int ret in Async.AsyncEnumerableToEnumerable(() => EnumerateAsync(limit, default))) |
| | | 731 | | { |
| | 1 | 732 | | max = Math.Max(max, ret); |
| | | 733 | | } |
| | 1 | 734 | | Assert.AreEqual(limit - 1, max); |
| | 1 | 735 | | } |
| | | 736 | | private async IAsyncEnumerable<int> EnumerateAsync(int limit, [EnumeratorCancellation] CancellationToken cancel |
| | | 737 | | { |
| | 1 | 738 | | for (int ret = 0; ret < limit; ++ret) |
| | | 739 | | { |
| | 1 | 740 | | cancel.ThrowIfCancellationRequested(); |
| | 1 | 741 | | await Async.RunTask(() => Task.Delay(10)); |
| | 1 | 742 | | yield return ret; |
| | | 743 | | } |
| | 1 | 744 | | } |
| | | 745 | | |
| | | 746 | | /// <summary> |
| | | 747 | | /// Performs basic tests on the <see cref="Async"/> class. |
| | | 748 | | /// </summary> |
| | | 749 | | [TestMethod] |
| | | 750 | | public async Task Async_AsyncEnumeratorExceptions() |
| | | 751 | | { |
| | 1 | 752 | | int mainThreadId = Thread.CurrentThread.ManagedThreadId; |
| | 1 | 753 | | await Assert.ThrowsExactlyAsync<ArgumentNullException>(async () => |
| | 1 | 754 | | { |
| | 1 | 755 | | await foreach (int ret in ((int[])null!).ToAsyncEnumerable()) |
| | 1 | 756 | | { |
| | 0 | 757 | | Assert.Fail("Should be empty!"); |
| | 1 | 758 | | } |
| | 1 | 759 | | }); |
| | 1 | 760 | | await Assert.ThrowsExactlyAsync<ArgumentNullException>(async () => |
| | 1 | 761 | | { |
| | 1 | 762 | | await foreach (int ret in ((IEnumerator<int>)null!).ToAsyncEnumerable()) |
| | 1 | 763 | | { |
| | 0 | 764 | | Assert.Fail("Should be empty!"); |
| | 1 | 765 | | } |
| | 1 | 766 | | }); |
| | 0 | 767 | | } |
| | | 768 | | private IAsyncEnumerable<int> NullAsyncEnumerable() |
| | | 769 | | { |
| | 0 | 770 | | return null!; |
| | | 771 | | } |
| | | 772 | | /// <summary> |
| | | 773 | | /// Performs basic tests on the <see cref="Async"/> class. |
| | | 774 | | /// </summary> |
| | | 775 | | [TestMethod] |
| | | 776 | | public void Async_InfiniteEnumeratorAsyncToSync() |
| | | 777 | | { |
| | 1 | 778 | | foreach (int ret in Async.AsyncEnumerableToEnumerable(() => InfiniteEnumerateAsync(default))) |
| | | 779 | | { |
| | 1 | 780 | | if (ret >= 10) break; |
| | | 781 | | } |
| | 1 | 782 | | } |
| | | 783 | | private async IAsyncEnumerable<int> InfiniteEnumerateAsync([EnumeratorCancellation] CancellationToken cancel = d |
| | | 784 | | { |
| | 1 | 785 | | int ret = 0; |
| | | 786 | | while (true) |
| | | 787 | | { |
| | 1 | 788 | | await Async.RunTask(() => Task.Delay(10)); |
| | 1 | 789 | | yield return ++ret; |
| | | 790 | | } |
| | 1 | 791 | | } |
| | | 792 | | #endif |
| | | 793 | | [TestMethod] |
| | | 794 | | public void SynchronousSynchronizationContextExceptions() |
| | | 795 | | { |
| | 1 | 796 | | SynchronousSynchronizationContext c = SynchronousSynchronizationContext.Default; |
| | 1 | 797 | | Assert.ThrowsExactly<ArgumentNullException>(() => c.Send(null!, null)); |
| | 1 | 798 | | Assert.ThrowsExactly<ArgumentNullException>(() => c.Post(null!, null)); |
| | 1 | 799 | | } |
| | | 800 | | [TestMethod] |
| | | 801 | | public void Async_ArgumentNullExceptions() |
| | | 802 | | { |
| | 1 | 803 | | Assert.ThrowsExactly<ArgumentNullException>(() => Async.ConvertAggregateException(null!)); |
| | 1 | 804 | | Assert.ThrowsExactly<ArgumentNullException>(() => |
| | 1 | 805 | | { |
| | 1 | 806 | | foreach (var x in Async.AsyncEnumerableToEnumerable<IAsyncEnumerable<int>>(null!)) |
| | 1 | 807 | | { |
| | 1 | 808 | | } |
| | 0 | 809 | | }); |
| | 1 | 810 | | } |
| | | 811 | | #if NETSTANDARD2_1 || NETCOREAPP3_1_OR_GREATER || NET5_0_OR_GREATER |
| | | 812 | | [TestMethod] |
| | | 813 | | public async Task Async_ArgumentNullExceptionsAsync() |
| | | 814 | | { |
| | 1 | 815 | | await Assert.ThrowsExactlyAsync<ArgumentNullException>(async () => await Async.AwaitForEach<int>(null!, n => |
| | 1 | 816 | | await Assert.ThrowsExactlyAsync<ArgumentNullException>(async () => await Async.AwaitForEach<int>(1.ToSingleI |
| | 1 | 817 | | await Assert.ThrowsExactlyAsync<ArgumentNullException>(async () => await Async.AwaitForEach<int>(null!, (i, |
| | 1 | 818 | | await Assert.ThrowsExactlyAsync<ArgumentNullException>(async () => await Async.AwaitForEach<int>(1.ToSingleI |
| | 1 | 819 | | } |
| | | 820 | | #endif |
| | | 821 | | [TestMethod] |
| | | 822 | | public void FilterAsyncFromStackTrace() |
| | | 823 | | { |
| | | 824 | | FilteredStackTrace trace; |
| | 1 | 825 | | Async.RunTaskSync(async () => |
| | 1 | 826 | | { |
| | 1 | 827 | | string? projectPath = AssemblyUtilities.GetCallingCodeSourceFolder(1, 1)?.TrimEnd(System.IO.Path.Directo |
| | 1 | 828 | | FilteredStackTrace.AddSourcePathToErase(projectPath ?? "Z:\\"); |
| | 1 | 829 | | FilteredStackTrace.AddNamespaceToFilter("Async.Synchronize"); |
| | 1 | 830 | | trace = new FilteredStackTrace(new ExpectedException("This is a test"), 0, true); |
| | 1 | 831 | | Assert.IsTrue(string.IsNullOrEmpty(trace.ToString().Trim())); |
| | 1 | 832 | | await Task.CompletedTask; |
| | 1 | 833 | | }); |
| | 1 | 834 | | } |
| | | 835 | | [TestMethod] |
| | | 836 | | public async Task ArgumentNullExceptions() |
| | | 837 | | { |
| | 1 | 838 | | await Assert.ThrowsExactlyAsync<ArgumentNullException>(() => Async.RunTask(null!)); |
| | 1 | 839 | | await Assert.ThrowsExactlyAsync<ArgumentNullException>(() => Async.RunTask<int>(null!)); |
| | 1 | 840 | | await Assert.ThrowsExactlyAsync<ArgumentNullException>(() => Async.Run(null!).AsTask()); |
| | 1 | 841 | | await Assert.ThrowsExactlyAsync<ArgumentNullException>(() => Async.Run<int>(null!).AsTask()); |
| | 1 | 842 | | } |
| | | 843 | | } |
| | | 844 | | } |