IActorStateManager injection rate in Azure Service

For the Azure Service Fabric State Service, you can enter IReliableStateManageras follows:

ServiceRuntime.RegisterServiceAsync("MyServiceType", context =>
{
  IReliableStateManager stateManager = new ReliableStateManager(context);
  return new MyService(stateManager);
}

And so you can make fun IStateManagerof unit tests for MyService.

The same is not possible for a player with a fortune. IActorStateManagerIt is only the internal market: Microsoft.ServiceFabric.Actors.Runtime.ActorStateManager. So, how do I unit test a discreet actor?

At some point in my actor methods, the call is made to IActorStateManager, but since I cannot introduce this dependency, unit tests seem impossible.

Is there a way around this or is there another solution?

+4
source share
2

, IActorStateManager , , . ( ) -, , , .

+3

- , IStateManager . , actorImpl . :

public interface IMyActor01 : IActor
{
    Task<int> GetCountAsync();
    Task SetCountAsync(int count);
}

public class MyActor01Impl : IMyActor01
{
    private readonly IActorStateManager StateManager;

    public MyActor01Impl(IActorStateManager stateManager)
    {
        this.StateManager = stateManager;
        this.StateManager.TryAddStateAsync("count", 0);
    }

    public Task<int> GetCountAsync()
    {
        return this.StateManager.GetStateAsync<int>("count");
    }

    public Task SetCountAsync(int count)
    {
        return this.StateManager.AddOrUpdateStateAsync("count", count, (key, value) => count > value ? count : value);
    }
}

[StatePersistence(StatePersistence.Persisted)]
internal class MyActor01 : Actor, IMyActor01
{
    private MyActor01Impl Impl;

    protected override Task OnActivateAsync()
    {
        ActorEventSource.Current.ActorMessage(this, "Actor activated.");
        this.Impl = new MyActor01Impl(this.StateManager);
        return Task.FromResult(true);
    }

    Task<int> IMyActor01.GetCountAsync()
    {
        return this.Impl.GetCountAsync();
    }

    Task IMyActor01.SetCountAsync(int count)
    {
        return this.Impl.SetCountAsync(count);
    }
}

[TestFixture]
public class TestFixture01
{
    [Test]
    public void Test01()
    {
        //ARRANGE
        var dictionary = new Dictionary<string, object>();
        var impl = new MyActor01Impl(new StubStateManager(dictionary));

        //ACT
        impl.SetCountAsync(12).Wait();

        //ASSERT
        Assert.AreEquals(12, impl.GetCountAsync().Result);
        //or
        Assert.AreEquals(12, (int)dictionary["count"]);
    }
}

StubStateManager, .

0

All Articles