dotnet / dotnet/orleans

Simple DI for unit tests?

Open
#8,790 3 comments 6 reactions 0 assignees View on GitHub
Needs: triage :mag:
Dominant language
C#
Stars
10.9k
Forks
2.1k
Avg merge
14h 42m
Merged PRs (30d)
354

Description

When configuring an application to use Orleans, you `.UseOrleans(...)` and everything just works, including dependency injection.

When you need to write unit tests that make use of Orleans, my understanding is that you have to use `TestClusterBuilder`.

The problem, based on my understanding, is that the cluster created by `TestClusterBuilder` has its own dependency injection container, completely isolated from the container I've set up for my unit tests.

So, for example, if I register a singleton in my test's container and in the `TestClusterBuilder`, there will be two different singletons, depending on where the singleton is resolved from.

If my unit test already sets up a DI container, how do I tell `TestClusterBuilder` to just use that container? If it matters, we're using NUnit and Autofac.

Here's an example of what that kinda looks like:

```c#
[SetUp]
public void Setup()
{
var webApplicationBuilder = WebApplication.CreateBuilder();
webApplicationBuilder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureContainer(c =>
{
c.RegisterType().AsSelf().SingleInstance();
});

var app = webApplicationBuilder.Build();

var testClusterBuilder = new TestClusterBuilder(1);
testClusterBuilder.AddSiloBuilderConfigurator();
TestCluster = testClusterBuilder.Build();
TestCluster.Deploy();
}

public class TestSiloConfigurations : ISiloConfigurator
{
public void Configure(ISiloBuilder siloBuilder)
{
siloBuilder.ConfigureServices(services =>
{
services.AddSingleton();
});
}
}
```

I created [this StackOverflow post](https://stackoverflow.com/questions/77602348/orleans-testclusterbuilder-cant-share-dependency-injection-registrations) to get some answers, and no one replied, even after I added a 150 point bounty. Maybe I didn't explain the problem well enough. I do know that I've spent a considerable amount of time trying to figure out how to get this stuff working, and I'm out of ideas.

I'd love it if you could just tell me what I'm missing.. or at least confirm that what I want isn't possible.

Contributor guide

Open the contributing guide

Research direction

Start with TestClusterBuilder and the TestSiloConfigurations example in the issue, then trace how the test cluster and application containers are created. Determine whether the existing APIs can share the test container or whether a new configuration path is needed. Done means establishing and documenting a supported way to avoid duplicate singleton registrations, or confirming that shared containers are not supported.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend, distributed-systems, testing-qa
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.