question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unrelated thread can cause Awaitility to throw an exception

See original GitHub issue

Awaitility sets its own UncaughtExceptionHandler globally (ConditionAwaiter:49), i.e. all spawning threads are affected. This is pretty risky and can lead to many problems, one of them being:

During an awaitility until() call, if a new thread spawns and throws an exception, the exception handler for the awaitility call catches that exception and stores it assuming the exception was caused by the awaitility callable -> the awaitility call fails (eventually even before executing the callable).

Reproducer:

@Test
public void test() {
    new Thread(() -> {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(() -> {
            throw new RuntimeException("Some irrelevant exception");
        }).start();
    }).start();
    Awaitility.await()
        .pollDelay(500, TimeUnit.MILLISECONDS)
        .until(() -> true);
}

will fail with

java.lang.RuntimeException: Some irrelevant exception

	at com.dynatrace.oneagent.atf.common.AwaitilityReproducer.lambda$null$0(AwaitilityReproducer.java:20)
	at java.lang.Thread.run(Thread.java:748)

Tested with versions 1.7 and 3.1.2

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
johanhalebycommented, Sep 13, 2018

@dotstone May be something to reconsider now that two people have experienced issues with this in a short period of time. Thanks the feedback.

0reactions
Darkvatercommented, Mar 4, 2021

Ugh, I spent a whole day and a half tracking this down! It made it all the more tricky as things were failing from a cucumber test which does some magic with the stack trace so I would get a stack trace of a wholly unrelated exception into my cucumber steps. Anyways:

I second doing something with this. The wiki docs mentioning it are not enough, this needs a big fat warning! If I might suggest a solution: it looks like the uncaughtThroeabke is thrown in ConditionAwaiter. Is it possible to wrap it in an Awaitility exception type which:

  1. At least make it clear where the exception is coming from to fail the test
  2. (for bonus points) would include some description how this exception came to be and what can de done to change the behaviour (e.g. turn off)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Use Awaitility to determine something did not happen
Awaitility throws ConditionTimeoutException when timeout is reached. One way to check that nothing changed in a predetermined time is to ...
Read more >
71744 – Concurrently throwing exceptions is not scalable
Multiple threads on multiple cores should be able to concurrently throw exceptions without bothering one another.
Read more >
Using Awaitility with Cucumber for Eventual Consistency checks
Write stable end-to-end Cucumber tests with Awaitility to deal with asynchronous ... boolean correct) throws Exception { var stats = this.
Read more >
Awaitility (Awaitility 4.1.1 API) - Javadoc.io
Awaitility is a small Java DSL for synchronizing (waiting for) asynchronous operations. ... Your test will not fail if another thread throws an...
Read more >
Modern Best Practices for Testing in Java - Philipp Hauer's Blog
Avoid randomized data as it can lead to toggling tests which can be ... Don't @Test public void categoryQueryParameter() throws Exception ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found