Re: Is it possible to have write NonBlocking Junit tests

Colin Vipurs <[email protected]> Fri, 11 Apr 2014 13:25:00 +0100
Newsgroups gmane.comp.java.junit.user
Message-ID <CAKi4272wquskM8XCzPdEL4JvqP_hBkQgtCF_U4Rz8QDbcEdd_w@mail.gmail.com>
If I understand your post correctly you're just swapping blocking for
polling?

The blocking approach has the benefit of finishing as quickly as possible
and not having to wait for the next sleep cycle


On Fri, Apr 11, 2014 at 1:06 AM, Cédric Beust ♔ <[email protected]> wrote:

>
>
> 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.
>>>
>>>
>>>
>>
>>
>>
>  
>



-- 
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.