Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Service Fabric Unit Testing and Dependency Injection

I can't test a Reliable Service/Actor by just calling it's constructor and then test it's methods. var testService = new SomeService(); throws a NullReferenceException. So what can I do with deployed Service..

I understand that deployed SF Reliable Services/Actors are not standard .NET classes, and unit testing of deployed S/A maybe a strange idea.

Anyway now I'm trying to give it a try.

For example. I've just deployed a Service, than in the test I've created a Proxy object and added item into input queue of Service. Then I need to assert that input queue count = 1. And it works if I've just deployed a Service and no other Clients/Services/Actors have used it's input queue. But next time this test will be failed that's the problem. I need make the Service to stop operatating with other consumers, drop it's queue and than test it. For this purpose I can create some TestMode property and some methods like PropareoForTests/TestingCompleted and call them from test client before and after testing.

Is this is a bad idea to do it like that. Maybe are there some guidelines for unit testing SF? Thanks.

UPDATE:

While investigating Service Fabric Web Reference Application example I've found this TODO string:

/// TODO: Temporary property-injection for an IServiceProxyWrapper until constructor injection is available.

Does it mean that SF Services will improve it's DI support? What about actors?

like image 843
AsValeO Avatar asked Nov 15 '15 21:11

AsValeO


1 Answers

Actually you can test Reliable Services and Actors the same way you'd test any other class in .NET! They're only special in that they use certain hooks into the underlying platform, but other than that you can instantiate your service or actor class normally and call methods on it.

Currently, Reliable Services are a little easier to unit test because the primary hook into the platform, the State Manager, is an interface that's pluggable through the constructor.

For example, your service class might look like this:

EDIT: Updated with the GA release API (2.0.135)

class MyService : StatefulService
{
  public MyService (StatefulServiceContext context, IReliableStateManager stateManager)
      :base (context, stateManager)
  {
  }

  public void MyMethod()
  {
    // do stuff..
  }
}

Then you can test your service class like so:

[TestMethod]
public TestMyMethod()
{
  MockReliableStateManager stateManager = new MockReliableStateManager();
  MyService target = new MyService(stateManager);

  target.MyMethod();
  // validate results and all that good stuff
}

We have a full working example of actual services with lots of dependencies being unit tested available on GitHub: https://github.com/Azure-Samples/service-fabric-dotnet-management-party-cluster

This example has IReliableStateManager and IReliableDictionary mocks as well that you can use as a starting point for your own unit tests.

like image 92
Vaclav Turecek Avatar answered Sep 29 '22 07:09

Vaclav Turecek