Giter Club home page Giter Club logo

asyncunit's Introduction

drawing

AsyncUnit

Build Status Hits-of-Code SonarQube

Javadocs Maven Central License

Simple tool for testing asynchronous code. It allows you to assert operations in threads. It will wait for threads to finish and will propagate errors to the main thread.

Inspiration got from concurrentunit.

Advantages over concurentunit:

  • simpler usage
  • no restrictions on assertion actions
  • more control over exception propagation

Usage

Wrap asynchronous part of the code using AsyncFlow.prepare and use AsyncFlow.await to block the main thread. If asynchronous part fails exceptionally, the test will also fail.

@Test
public void doesSomeWorkInSeparateThread() throws Exception {

    new Thread(
        AsyncFlow.prepare(() -> {
            work = doSomeWork();
            assertNotNull(work);
        })
    ).start();
    
    AsyncFlow.await();
}

Assertions in multiple threads.

@Test
public void doesMultipleWorkInThreads() throws Throwable {

    for (int i = 0; i < 4; i++)
    {
        new Thread(
            AsyncFlow.prepare(() -> {
                work = doSomeWork();
                assertNotNull(work);
            })
        ).start();
    }
    
    AsyncFlow.await(1000, 4);
}

AsyncFlow.await will work properly only if AsyncFlow.prepare is called in the same thread where await is triggered. This will usually be the main test thread. However, if you need flexibility, you can instantiate AsyncFlow and prepare flow lazy:

@Test
public void supportsLazyFlowPreparation() throws Exception {

    AsyncFlow.Single flow = new AsyncFlow.Single();

    new Thread(() -> {
        flow.prepare(() -> {
             work = doSomeWork();
             assertNotNull(work);
        }).run();
    }).start();
    
    flow.await();
}

By default, tool propagates every Throwable from an async flow back to the main thread. You can customize this by specifying which exceptions you want to propagate. For example:

new AsyncFlow.Single(
    IllegalStateException.class,
    IOException.class,
    CustomException.class    
);

Contribution

You can contribute by forking the repo and sending a pull request. Make sure your branch builds without any warnings/issues:

mvn clean install

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.