Elasticsearch 5.2 unit tests
Asked Answered
P

1

6

I am writing unit tests for ES5.2, and am having some issues. I need to create a client which connects to a local node for running tests, so I use

esClient = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

This causes problems

java.lang.IllegalStateException: running tests but failed to invoke RandomizedContext#getRandom

So then I used an annotation on my unit test classes as

@RunWith(com.carrotsearch.randomizedtesting.RandomizedRunner.class)

but this gave me an error

SEVERE: 2 threads leaked from TEST scope at {method_name}): 
1) Thread[id=36, name={project_name}writer-thread-0, state=WAITING, group={project_name}-writer]

And the unit tests threw errors as

org.mockito.exceptions.misusing.WrongTypeOfReturnValue

I tried to use the annotation as below on the unit test class

@ThreadLeakScope(ThreadLeakScope.Scope.NONE)

That got rid of some error messages, but there were still concurrency issues in the code. Anyone knows how to handle this? Is there some explicit thread safety methods I should use?

Pratte answered 15/5, 2017 at 16:40 Comment(0)
B
0

Excluding com.carrotsearch.randomizedtesting:randomizedtesting-runner from dependencies works.

Example (build.gradle):

dependencies {
    testCompile(project(path:':other-project')) {
        exclude  group:"com.carrotsearch.randomizedtesting", module:"randomizedtesting-runner"
    }
}

Why?

If you have randomizedtesting-runner in your classpath, ES will load it:

https://github.com/elastic/elasticsearch/blob/7.10/server/src/main/java/org/elasticsearch/common/Randomness.java#L55

And use it:

https://github.com/elastic/elasticsearch/blob/7.10/server/src/main/java/org/elasticsearch/common/Randomness.java#L102

Blinni answered 22/10, 2021 at 9:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.