|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Runtime.CompilerServices; |
| 6 | +using System.Threading.Tasks; |
| 7 | + |
| 8 | +class RuntimeAsync |
| 9 | +{ |
| 10 | + public const int Pass = 100; |
| 11 | + public const int Fail = -1; |
| 12 | + |
| 13 | + internal static int Run() |
| 14 | + { |
| 15 | + if (TaskReturningAsyncTest.Run() != Pass) |
| 16 | + return Fail; |
| 17 | + |
| 18 | + if (ValueTaskReturningAsyncTest.Run() != Pass) |
| 19 | + return Fail; |
| 20 | + |
| 21 | + if (TaskWithResultTest.Run() != Pass) |
| 22 | + return Fail; |
| 23 | + |
| 24 | + if (AwaitCompletedTaskTest.Run() != Pass) |
| 25 | + return Fail; |
| 26 | + |
| 27 | + return Pass; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// Test basic Task-returning async method |
| 32 | +class TaskReturningAsyncTest |
| 33 | +{ |
| 34 | + [RuntimeAsyncMethodGeneration(false)] |
| 35 | + static async Task SimpleAsyncMethod() |
| 36 | + { |
| 37 | + await Task.CompletedTask; |
| 38 | + } |
| 39 | + |
| 40 | + public static int Run() |
| 41 | + { |
| 42 | + try |
| 43 | + { |
| 44 | + var task = SimpleAsyncMethod(); |
| 45 | + task.Wait(); |
| 46 | + Console.WriteLine("TaskReturningAsyncTest passed"); |
| 47 | + return RuntimeAsync.Pass; |
| 48 | + } |
| 49 | + catch (Exception ex) |
| 50 | + { |
| 51 | + Console.WriteLine($"TaskReturningAsyncTest failed: {ex}"); |
| 52 | + return RuntimeAsync.Fail; |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// Test ValueTask-returning async method |
| 58 | +class ValueTaskReturningAsyncTest |
| 59 | +{ |
| 60 | + [RuntimeAsyncMethodGeneration(false)] |
| 61 | + static async ValueTask SimpleValueTaskAsync() |
| 62 | + { |
| 63 | + await Task.CompletedTask; |
| 64 | + } |
| 65 | + |
| 66 | + public static int Run() |
| 67 | + { |
| 68 | + try |
| 69 | + { |
| 70 | + var task = SimpleValueTaskAsync(); |
| 71 | + task.AsTask().Wait(); |
| 72 | + Console.WriteLine("ValueTaskReturningAsyncTest passed"); |
| 73 | + return RuntimeAsync.Pass; |
| 74 | + } |
| 75 | + catch (Exception ex) |
| 76 | + { |
| 77 | + Console.WriteLine($"ValueTaskReturningAsyncTest failed: {ex}"); |
| 78 | + return RuntimeAsync.Fail; |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// Test Task<T> returning async method with result |
| 84 | +class TaskWithResultTest |
| 85 | +{ |
| 86 | + [RuntimeAsyncMethodGeneration(false)] |
| 87 | + static async Task<int> ComputeAsync() |
| 88 | + { |
| 89 | + await Task.CompletedTask; |
| 90 | + return 42; |
| 91 | + } |
| 92 | + |
| 93 | + public static int Run() |
| 94 | + { |
| 95 | + try |
| 96 | + { |
| 97 | + var task = ComputeAsync(); |
| 98 | + int result = task.Result; |
| 99 | + if (result == 42) |
| 100 | + { |
| 101 | + Console.WriteLine("TaskWithResultTest passed"); |
| 102 | + return RuntimeAsync.Pass; |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + Console.WriteLine($"TaskWithResultTest failed: expected 42, got {result}"); |
| 107 | + return RuntimeAsync.Fail; |
| 108 | + } |
| 109 | + } |
| 110 | + catch (Exception ex) |
| 111 | + { |
| 112 | + Console.WriteLine($"TaskWithResultTest failed: {ex}"); |
| 113 | + return RuntimeAsync.Fail; |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// Test awaiting a completed task (optimized fast path) |
| 119 | +class AwaitCompletedTaskTest |
| 120 | +{ |
| 121 | + [RuntimeAsyncMethodGeneration(false)] |
| 122 | + static async Task<string> GetMessageAsync() |
| 123 | + { |
| 124 | + await Task.CompletedTask; |
| 125 | + return "Hello"; |
| 126 | + } |
| 127 | + |
| 128 | + [RuntimeAsyncMethodGeneration(false)] |
| 129 | + static async Task<string> CombineAsync() |
| 130 | + { |
| 131 | + string msg1 = await GetMessageAsync(); |
| 132 | + string msg2 = await GetMessageAsync(); |
| 133 | + return msg1 + " " + msg2; |
| 134 | + } |
| 135 | + |
| 136 | + public static int Run() |
| 137 | + { |
| 138 | + try |
| 139 | + { |
| 140 | + var task = CombineAsync(); |
| 141 | + string result = task.Result; |
| 142 | + if (result == "Hello Hello") |
| 143 | + { |
| 144 | + Console.WriteLine("AwaitCompletedTaskTest passed"); |
| 145 | + return RuntimeAsync.Pass; |
| 146 | + } |
| 147 | + else |
| 148 | + { |
| 149 | + Console.WriteLine($"AwaitCompletedTaskTest failed: expected 'Hello Hello', got '{result}'"); |
| 150 | + return RuntimeAsync.Fail; |
| 151 | + } |
| 152 | + } |
| 153 | + catch (Exception ex) |
| 154 | + { |
| 155 | + Console.WriteLine($"AwaitCompletedTaskTest failed: {ex}"); |
| 156 | + return RuntimeAsync.Fail; |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments