Skip to content

Commit

Permalink
Updated Tests
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Menegazzo <[email protected]>
  • Loading branch information
m3nax committed Jun 13, 2024
1 parent 01dc7cc commit 57828bb
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 15 deletions.
7 changes: 7 additions & 0 deletions src/Dapr.Actors/Runtime/ActorManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ async Task<object> 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
Expand Down
65 changes: 50 additions & 15 deletions test/Dapr.Actors.Test/Runtime/ActorRuntimeTests.cs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
{
Expand All @@ -46,7 +45,7 @@ private interface ITestActor : IActor
public void TestInferredActorType()
{
var actorType = typeof(TestActor);

var options = new ActorRuntimeOptions();
options.Actors.RegisterActor<TestActor>();
var runtime = new ActorRuntime(options, loggerFactory, activatorFactory, proxyFactory);
Expand Down Expand Up @@ -119,6 +118,10 @@ public interface INotRemotedActor : IActor
Task<string> SingleArgumentAsync(bool arg);

Task<string> SingleArgumentWithCancellationAsync(bool arg, CancellationToken cancellationToken = default);

Task<string> MultiArgumentsAsync(bool arg1, string arg2);

Task<string> MultiArgumentsWithCancellationAsync(bool arg1, string arg2, CancellationToken cancellationToken = default);
}

public sealed class NotRemotedActor : Actor, INotRemotedActor
Expand Down Expand Up @@ -147,6 +150,16 @@ public Task<string> SingleArgumentWithCancellationAsync(bool arg, CancellationTo
{
return Task.FromResult(nameof(SingleArgumentWithCancellationAsync));
}

public Task<string> MultiArgumentsAsync(bool arg1, string arg2)
{
return Task.FromResult(nameof(MultiArgumentsAsync));
}

public Task<string> MultiArgumentsWithCancellationAsync(bool arg1, string arg2, CancellationToken cancellationToken = default)
{
return Task.FromResult(nameof(MultiArgumentsWithCancellationAsync));
}
}

public async Task<string> InvokeMethod<T>(string methodName, object arg = null) where T : Actor
Expand All @@ -167,7 +180,7 @@ public async Task<string> InvokeMethod<T>(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);
Expand All @@ -179,17 +192,17 @@ public async Task<string> InvokeMethod<T>(string methodName, object arg = null)
public async Task NoRemotingMethodWithNoArguments()
{
string methodName = nameof(INotRemotedActor.NoArgumentsAsync);

string result = await InvokeMethod<NotRemotedActor>(methodName);

Assert.Equal(methodName, result);
Assert.Equal(methodName, result);
}

[Fact]
public async Task NoRemotingMethodWithNoArgumentsWithCancellation()
{
string methodName = nameof(INotRemotedActor.NoArgumentsWithCancellationAsync);

string result = await InvokeMethod<NotRemotedActor>(methodName);

Assert.Equal(methodName, result);
Expand All @@ -199,7 +212,7 @@ public async Task NoRemotingMethodWithNoArgumentsWithCancellation()
public async Task NoRemotingMethodWithSingleArgument()
{
string methodName = nameof(INotRemotedActor.SingleArgumentAsync);

string result = await InvokeMethod<NotRemotedActor>(methodName, true);

Assert.Equal(methodName, result);
Expand All @@ -209,12 +222,34 @@ public async Task NoRemotingMethodWithSingleArgument()
public async Task NoRemotingMethodWithSingleArgumentWithCancellation()
{
string methodName = nameof(INotRemotedActor.SingleArgumentWithCancellationAsync);

string result = await InvokeMethod<NotRemotedActor>(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<NotRemotedActor>(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<NotRemotedActor>(methodName, arg);

Assert.Equal(methodName, result);
}

[Fact]
public async Task Actor_UsesCustomActivator()
{
Expand Down Expand Up @@ -330,7 +365,7 @@ public async Task TestActorSettingsWithRemindersStoragePartitions()
}

[Fact]
public async Task TestActorSettingsWithReentrancy()
public async Task TestActorSettingsWithReentrancy()
{
var actorType = typeof(TestActor);

Expand Down Expand Up @@ -465,7 +500,7 @@ private class TestActivator : DefaultActorActivator

public override Task<ActorActivatorState> CreateAsync(ActorHost host)
{
CreateCallCount++;;
CreateCallCount++; ;
return base.CreateAsync(host);
}

Expand Down

0 comments on commit 57828bb

Please sign in to comment.