|
5 | 5 | using System.Linq.Expressions; |
6 | 6 | using System.Reflection; |
7 | 7 | using System.Text.Json; |
| 8 | +using System.Threading; |
8 | 9 | using System.Threading.Tasks; |
9 | 10 | using DotVVM.Framework.Compilation.Binding; |
10 | 11 | using DotVVM.Framework.Compilation.Javascript; |
@@ -247,5 +248,44 @@ public async Task Validation_CustomPrimitives() |
247 | 248 | internal static void CustomPrimitivesValidation(TestViewModel viewModel, [RegularExpression(@"\d\d7", ErrorMessage = "Vehicle must have lucky number.")] VehicleNumber vehicle) |
248 | 249 | { |
249 | 250 | } |
| 251 | + |
| 252 | + [TestMethod] |
| 253 | + public async Task CancellationToken_IsInjectedFromContext() |
| 254 | + { |
| 255 | + var plan = new StaticCommandInvocationPlan( |
| 256 | + ((Func<CancellationToken, bool>)MethodWithCancellationToken).Method, |
| 257 | + [ new StaticCommandParameterPlan(StaticCommandParameterType.CurrentCancellationToken, null) ] |
| 258 | + ); |
| 259 | + var result = await Invoke(plan); |
| 260 | + // The method should have been called successfully (not thrown an exception) |
| 261 | + Assert.IsTrue((bool)result); |
| 262 | + } |
| 263 | + |
| 264 | + [TestMethod] |
| 265 | + public async Task CancellationToken_OptionalParameter_IsInjectedFromContext() |
| 266 | + { |
| 267 | + var plan = new StaticCommandInvocationPlan( |
| 268 | + ((Func<string, CancellationToken, Task<string>>)MethodWithOptionalCancellationToken).Method, |
| 269 | + new[] { |
| 270 | + new StaticCommandParameterPlan(StaticCommandParameterType.Argument, typeof(string)), |
| 271 | + new StaticCommandParameterPlan(StaticCommandParameterType.CurrentCancellationToken, null) |
| 272 | + } |
| 273 | + ); |
| 274 | + var result = await Invoke(plan, ("hello", "/Input")); |
| 275 | + Assert.AreEqual("hello", result); |
| 276 | + } |
| 277 | + |
| 278 | + [AllowStaticCommand] |
| 279 | + internal static bool MethodWithCancellationToken(CancellationToken cancellationToken) |
| 280 | + { |
| 281 | + return !cancellationToken.IsCancellationRequested; |
| 282 | + } |
| 283 | + |
| 284 | + [AllowStaticCommand] |
| 285 | + internal static async Task<string> MethodWithOptionalCancellationToken(string input, CancellationToken cancellationToken = default) |
| 286 | + { |
| 287 | + await Task.Yield(); |
| 288 | + return input; |
| 289 | + } |
250 | 290 | } |
251 | 291 | } |
0 commit comments