From 57828bb285bc126c44c3e273d0bf76d2cafa3962 Mon Sep 17 00:00:00 2001 From: Manuel Menegazzo Date: Thu, 13 Jun 2024 20:59:06 +0200 Subject: [PATCH] Updated Tests Signed-off-by: Manuel Menegazzo --- src/Dapr.Actors/Runtime/ActorManager.cs | 7 ++ .../Runtime/ActorRuntimeTests.cs | 65 ++++++++++++++----- 2 files changed, 57 insertions(+), 15 deletions(-) diff --git a/src/Dapr.Actors/Runtime/ActorManager.cs b/src/Dapr.Actors/Runtime/ActorManager.cs index 89579644a..fb44916d1 100644 --- a/src/Dapr.Actors/Runtime/ActorManager.cs +++ b/src/Dapr.Actors/Runtime/ActorManager.cs @@ -154,6 +154,13 @@ async Task RequestFunc(Actor actor, CancellationToken ct) { awaitable = methodInfo.Invoke(actor, parameters.Length == 0 ? null : new object[] { ct }); } + else if (parameters.Length == 1 || (parameters.Length == 2 && parameters[1].ParameterType == typeof(CancellationToken))) + { + // deserialize using stream. + var type = parameters[0].ParameterType; + var deserializedType = await JsonSerializer.DeserializeAsync(requestBodyStream, type, jsonSerializerOptions); + awaitable = methodInfo.Invoke(actor, parameters.Length == 1 ? new object[] { deserializedType } : new object[] { deserializedType, ct }); + } else { #if NET8_0_OR_GREATER diff --git a/test/Dapr.Actors.Test/Runtime/ActorRuntimeTests.cs b/test/Dapr.Actors.Test/Runtime/ActorRuntimeTests.cs index c74d0b754..ded30de5d 100644 --- a/test/Dapr.Actors.Test/Runtime/ActorRuntimeTests.cs +++ b/test/Dapr.Actors.Test/Runtime/ActorRuntimeTests.cs @@ -1,4 +1,4 @@ -// ------------------------------------------------------------------------ +// ------------------------------------------------------------------------ // Copyright 2021 The Dapr Authors // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -15,19 +15,18 @@ namespace Dapr.Actors.Test { using System; using System.Buffers; - using System.Collections.Generic; - using System.Linq; using System.IO; + using System.Linq; + using System.Reflection; using System.Text; using System.Text.Json; + using System.Threading; using System.Threading.Tasks; using Dapr.Actors; + using Dapr.Actors.Client; using Dapr.Actors.Runtime; using Microsoft.Extensions.Logging; using Xunit; - using Dapr.Actors.Client; - using System.Reflection; - using System.Threading; public sealed class ActorRuntimeTests { @@ -46,7 +45,7 @@ private interface ITestActor : IActor public void TestInferredActorType() { var actorType = typeof(TestActor); - + var options = new ActorRuntimeOptions(); options.Actors.RegisterActor(); var runtime = new ActorRuntime(options, loggerFactory, activatorFactory, proxyFactory); @@ -119,6 +118,10 @@ public interface INotRemotedActor : IActor Task SingleArgumentAsync(bool arg); Task SingleArgumentWithCancellationAsync(bool arg, CancellationToken cancellationToken = default); + + Task MultiArgumentsAsync(bool arg1, string arg2); + + Task MultiArgumentsWithCancellationAsync(bool arg1, string arg2, CancellationToken cancellationToken = default); } public sealed class NotRemotedActor : Actor, INotRemotedActor @@ -147,6 +150,16 @@ public Task SingleArgumentWithCancellationAsync(bool arg, CancellationTo { return Task.FromResult(nameof(SingleArgumentWithCancellationAsync)); } + + public Task MultiArgumentsAsync(bool arg1, string arg2) + { + return Task.FromResult(nameof(MultiArgumentsAsync)); + } + + public Task MultiArgumentsWithCancellationAsync(bool arg1, string arg2, CancellationToken cancellationToken = default) + { + return Task.FromResult(nameof(MultiArgumentsWithCancellationAsync)); + } } public async Task InvokeMethod(string methodName, object arg = null) where T : Actor @@ -167,7 +180,7 @@ public async Task InvokeMethod(string methodName, object arg = null) } using var output = new MemoryStream(); - + await runtime.DispatchWithoutRemotingAsync(typeof(T).Name, ActorId.CreateRandom().ToString(), methodName, input, output); output.Seek(0, SeekOrigin.Begin); @@ -179,17 +192,17 @@ public async Task InvokeMethod(string methodName, object arg = null) public async Task NoRemotingMethodWithNoArguments() { string methodName = nameof(INotRemotedActor.NoArgumentsAsync); - + string result = await InvokeMethod(methodName); - Assert.Equal(methodName, result); + Assert.Equal(methodName, result); } [Fact] public async Task NoRemotingMethodWithNoArgumentsWithCancellation() { string methodName = nameof(INotRemotedActor.NoArgumentsWithCancellationAsync); - + string result = await InvokeMethod(methodName); Assert.Equal(methodName, result); @@ -199,7 +212,7 @@ public async Task NoRemotingMethodWithNoArgumentsWithCancellation() public async Task NoRemotingMethodWithSingleArgument() { string methodName = nameof(INotRemotedActor.SingleArgumentAsync); - + string result = await InvokeMethod(methodName, true); Assert.Equal(methodName, result); @@ -209,12 +222,34 @@ public async Task NoRemotingMethodWithSingleArgument() public async Task NoRemotingMethodWithSingleArgumentWithCancellation() { string methodName = nameof(INotRemotedActor.SingleArgumentWithCancellationAsync); - + string result = await InvokeMethod(methodName, true); Assert.Equal(methodName, result); } + [Fact] + public async Task NoRemotingMethodWithMultiArguments() + { + var arg = new { arg1 = true, arg2 = "abc" }; + string methodName = nameof(INotRemotedActor.MultiArgumentsAsync); + + string result = await InvokeMethod(methodName, arg); + + Assert.Equal(methodName, result); + } + + [Fact] + public async Task NoRemotingMethodWithMultiArgumentsWithCancellation() + { + var arg = new { arg1 = true, arg2 = "abc" }; + string methodName = nameof(INotRemotedActor.MultiArgumentsWithCancellationAsync); + + string result = await InvokeMethod(methodName, arg); + + Assert.Equal(methodName, result); + } + [Fact] public async Task Actor_UsesCustomActivator() { @@ -330,7 +365,7 @@ public async Task TestActorSettingsWithRemindersStoragePartitions() } [Fact] - public async Task TestActorSettingsWithReentrancy() + public async Task TestActorSettingsWithReentrancy() { var actorType = typeof(TestActor); @@ -465,7 +500,7 @@ private class TestActivator : DefaultActorActivator public override Task CreateAsync(ActorHost host) { - CreateCallCount++;; + CreateCallCount++; ; return base.CreateAsync(host); }