Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AspNet Core Integration Tests Fail "OneTimeSetUp: System.InvalidOperationException : Configuration for DogStatsdService already performed" #69

Open
cjablonski76 opened this issue Nov 14, 2018 · 6 comments

Comments

@cjablonski76
Copy link

In AspNet Core when writing integration tests to call my Api, we get the exception

OneTimeSetUp: System.InvalidOperationException : Configuration for DogStatsdService already performed"

when our Startup class calls DogStatsd.Configure(statsDConfiguration);

The reason is because for aspnet core integration tests we are going to create the web server for each test, i.e. Startup.cs will run multiple times.

    // repeated for each test
    using (var server = new TestServer(new WebHostBuilder().UseStartup<Startup>()))
    using (var client = server.CreateClient())
    {
        var response = await client.GetAsync("/api/hello");
    }

I found this article describing the scenario and how they solved it for a different third party dependency: https://blog.markvincze.com/tear-down-your-asp-net-core-api-between-integration-tests/

I think we might need a way to unregister/dispose from the static instance of DogStatsd so that _config kept in DogStatsdService can be reset to null.

As a workaround, I just keep a static flag in my Startup logic to stop from calling DogStatsd.Configure(...) more than once. But this is a less than ideal solution in my opinion.

@AdomasBekeras
Copy link

What is the reason in general for not allowing the configuration to be changed? I have a use case which I would believe is quite common, where constant tags need to be updated and since DogStatsd is a static class and Configure cannot be called twice, the only elegant way is to write a wrapper to send tags and not use ConstantTags at all.

@jfloodnet
Copy link

This was actually a breaking change introduced in 3.3.0 - here 2a2d5fd

When upgrading from 3.0.0 to 3.3.0 our code no longer works. Changes like this should not be introduced in minor version increments.

@ogaca-dd
Copy link
Contributor

ogaca-dd commented Jan 7, 2020

Apologies for the delay @cjablonski76,

DogStatsd is just a wrapper over a static instance of DogStatsdService (https://github.com/DataDog/dogstatsd-csharp-client/blob/master/src/StatsdClient/Dogstatsd.cs).
If you have a small number of occurrences of DogStatsd in your code, using an instance of DogStatsdService may help you.

@jfloodnet: Indeed, breaking changes should be introduced only in major version increments.

@avidalcarrasco
Copy link

Maybe a method IsConfigured or a Get property for the configuration can be exposed, so it's easy to check that it's already configured, thus, avoiding the exception.

@ogaca-dd
Copy link
Contributor

@avidalcarrasco : Can you provide more context on how you are using DogStatsD C# client and why you have to know if Configure was already called?

@avidalcarrasco
Copy link

Hello,
In a web application I configured DogStadsD in the Startup file

While doing unit test I create a fake client using the WebApplicationBuilder that will load my Startup.
If you have more than one test in the same "test instance" it will make it fail, because as DogStadsD is static it will try to configure it again in the next test.

When I tried to configure it with the non-static class I have the same problem when I try to create singleton class for my instance, as I want it to be available in all the application.

So having a method to check if it's already configured can avoid an exception for this cases.

I made an example here

Let me know if I'm doing something wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants