Re: Is it possible to have write NonBlocking Junit tests

Cédric Beust ♔ <[email protected]> Thu, 10 Apr 2014 17:06:45 -0700
Newsgroups gmane.comp.java.junit.user
Message-ID <CAOphgJDWOAoMSkni5Z-4ffuwM3L1pTdxYRKz9DfmF7ktihnq2Q@mail.gmail.com>
Here is a different approach that doesn't involve any blocking:

http://beust.com/weblog/2005/02/01/testing-asynchronous-code/


-- 
Cédric



On Thu, Apr 10, 2014 at 12:17 PM, Neil Swingler <[email protected]> wrote:

>
>
> Bah sent an incomplete example by accident. Here is the whole message
>
> Use a SettableFuture from guava. To borrow Colin's example
>
> @Test
> public void testAsync() throws Exception {
>  final SettableFuture<Message> future = SettableFuture.create();
> someAsyncCode(new Handler() {
>  public void handle(Message message) {
>                             future.set(message);
> }
> });
>   assertThat(future.get(), is(something));
> }
>
> - Neil
>
>
> On 10 April 2014 18:32, Colin Vipurs <[email protected]> wrote:
>
>>
>>
>> You could use a locking mechanism shared between your test and handler so
>> that progress continues as quick as possible and then put an overall
>> timeout on your test in the event of failure.
>>
>> Something like this should work just fine:
>>
>> @Test
>> public void testAsync() throws Exception {
>>  final Semaphore lock = new Semaphore(0);
>> someAsyncCode(new Handler() {
>> public void handle() {
>>  // put your assertions in here
>> lock.release();
>> }
>>  });
>>  lock.acquire();
>>  }
>>
>>
>> On Thu, Apr 10, 2014 at 4:21 PM, <[email protected]> wrote:
>>
>>>
>>>
>>> I am writing a Junit tests for Amqp Message Listener.This is how the
>>> test is written.I send a message to a exchange and introduce a delay of 1s
>>> (assuming that the listener will do its job within that time frame).This
>>> approach slows down the build process as there 15-20 tests like these which
>>> take a minimum of 20 seconds to execute.Is there a way in which I can do
>>> this is a non blocking way.For pass a call handler which verifies the tests
>>> after the listener does it job.Something like Actors might be internally
>>> used
>>>
>>
>>
>>
>> --
>> Maybe she awoke to see the roommate's boyfriend swinging from the
>> chandelier wearing a boar's head.
>>
>> Something which you, I, and everyone else would call "Tuesday", of
>> course.
>>
>>
>>
>
>
> 
>