Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Toolkit for testing multi-threaded code
Java Shell

README.md

ConcurrentUnit

Build Status Maven Central

A simple, zero-dependency toolkit for testing multi-threaded code. Supports Java 1.6+.

Introduction

ConcurrentUnit was created to help developers test multi-threaded or asynchronous code. It allows you to perform assertions and wait for operations in any thread, with failures being properly reported back to the main test thread. If an assertion fails, your test fails, regardless of which thread the assertion came from.

Usage

  1. Create a Waiter
  2. Use Waiter.await to block the main test thread.
  3. Use the Waiter.assert calls from any thread to perform assertions.
  4. Once expected assertions are completed, use Waiter.resume call to unblock the main thread.

When your test runs, assertion failures will result in the main thread being interrupted and the failure thrown. If an await call times out before all expected resume calls occur, the test will fail with a TimeoutException.

Examples

Consider a test for a message bus that delivers messages asynchronously:

@Test
public void shouldDeliverMessage() throws Throwable {
  final Waiter waiter = new Waiter();

  messageBus.registerHandler(message -> {
    // Called on separate thread
    waiter.assertEquals(message, "foo");
    waiter.resume();
  };

  messageBus.send("foo");

  // Wait for resume() to be called
  waiter.await(1000);
}

We can also handle wait for multiple resume calls:

@Test
public void shouldDeliverMessages() throws Throwable {
  final Waiter waiter = new Waiter();

  messageBus.registerHandler(message -> {
    waiter.assertEquals(message, "foo");
    waiter.resume();
  };

  messageBus.send("foo");
  messageBus.send("foo");
  messageBus.send("foo");

  // Wait for resume() to be called 3 times
  waiter.await(1000, 3);
}

If an assertion fails in any thread, the test will fail as expected:

@Test(expected = AssertionError.class)
public void shouldFail() throws Throwable {
  final Waiter waiter = new Waiter();

  new Thread(() -> {
    waiter.assertTrue(false);
  }).start();

  waiter.await();
}

TimeoutException is thrown if resume is not called before the await time is exceeded:

@Test(expected = TimeoutException.class)
public void shouldTimeout() throws Throwable {
  new Waiter().await(1);
}

Alternatively

As a more concise alternative to using the Waiter class, you can extend the ConcurrentTestCase:

class SomeTest extends ConcurrentTestCase {
  @Test
  public void shouldSucceed() throws Throwable {
    new Thread(() -> {
      doSomeWork();
      threadAssertTrue(true);
      resume();
    }).start();

    await(1000);
  }
}

Other Examples

More example usages can be found in the WaiterTest or in the following projects:

Docs

JavaDocs are available here.

License

Copyright 2010-2015 Jonathan Halterman - Released under the Apache 2.0 license.

Something went wrong with that request. Please try again.