2

We are running multiple unit tests, each of them creates it's own instance of ElasticClient. Some of the tests use "StringEnumConverter" to convert enums, other will use the default convertor (into integers). The problem is that it seems the settings of connection is cached somewhere and it's only the first test that decides how enums will be converted.

This is not a threading issue, since we are running the tests sequentially.

The constructor of the TestBase class looks like this:

public TestsBase(bool serializeEnumsToString = false)
{
    var node = new Uri("http://localhost:9200");

    var settings = new ConnectionSettings(
        node,
        defaultIndex: "my-application" + Guid.NewGuid().ToString()
        );


    if (serializeEnumsToString)
    {
        Console.Write("Setting up the enums convertor");               
        settings.AddContractJsonConverters(type =>
        {
            // TypeCheck to return StringEnumConverter for Enums and Nullable<T> where T : Enum
            if (type.IsEnum ||
                (type.IsGenericType && type.GetGenericTypeDefinition() == typeof (Nullable<>) &&
                 type.GetGenericArguments().First().IsEnum))
            {
                return new StringEnumConverter();
            }

            return null;
        });
    }
    client = new ElasticClient(settings);
}    

Is it possible that there is a cache (on appdomain level?) that would be used any time new instance of ElasticClient is created?

hoonzis
  • 1,717
  • 1
  • 17
  • 32
  • Json.Net Contract Converters are cached after the first hit, per Connection Settings instance, so I would expect a new Connection Settings and ElasticClient to be different each time. Do you have a more complete example that reproduces the issue? – Russ Cam Jun 15 '16 at 08:59
  • What version of NEST are you using? – Russ Cam Jun 15 '16 at 09:01
  • I was able to reproduce this with NEST 1.5 but also with 2.3.0. I currently don't have a failing test for this, but you can observe the behaviour in the latest build of our FluentNest 1.0.0 branch: https://ci.appveyor.com/project/hoonzis/fluentnest In the constructor of each test, we log to the console how enums should be serialized and then we write out each stored entity in console as well, and you can observer that once that first tests decides that enums should be serialized as integers all tests that go after will serialize as integers even when we pass the string enum convertor in. – hoonzis Jun 15 '16 at 09:56
  • let me rephrase it currently this is not failing my tests, because we don't depend on serializer configuration - but a test suite with failing test could be written. You just need 2 test classes, each of them creating own instance of ElasticClient and ConnectionSettings and one of them would apply string enum convertor. Then you can just check how the entities are serialized and observe that it depends only on which of the two tests run first – hoonzis Jun 15 '16 at 10:03

0 Answers0