Search in sources :

Example 26 with Condition

use of java.util.concurrent.locks.Condition in project RoboZombie by sahan.

the class AsyncEndpointTest method testAsyncErrorCallbackError.

/**
	 * <p>Tests an erroneous asynchronous request where the implementation of the 
	 * {@link AsyncHandler#onError(Exception)} callback throws an exception.</p>
	 *  
	 * @since 1.3.0
	 */
@Test
public final void testAsyncErrorCallbackError() throws InterruptedException {
    Robolectric.getFakeHttpLayer().interceptHttpRequests(false);
    String subpath = "/errorcallbackerror", body = "non-JSON-content";
    stubFor(get(urlEqualTo(subpath)).willReturn(aResponse().withStatus(200).withBody(body)));
    final Lock lock = new ReentrantLock();
    final Condition condition = lock.newCondition();
    asyncEndpoint.asyncErrorCallbackError(new AsyncHandler<User>() {

        @Override
        public void onSuccess(HttpResponse httpResponse, User user) {
        }

        @Override
        public void onError(InvocationException error) {
            try {
                throw new IllegalStateException();
            } finally {
                lock.lock();
                condition.signal();
                lock.unlock();
            }
        }
    });
    lock.lock();
    condition.await();
    lock.unlock();
    verify(getRequestedFor(urlEqualTo(subpath)));
    //verify that the asynchronous request executor has survived the exception
    successScenario();
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) User(com.lonepulse.robozombie.model.User) InvocationException(com.lonepulse.robozombie.proxy.InvocationException) HttpResponse(org.apache.http.HttpResponse) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) Test(org.junit.Test)

Example 27 with Condition

use of java.util.concurrent.locks.Condition in project RoboZombie by sahan.

the class AsyncEndpointTest method testAsyncFailure.

/**
	 * <p>Tests asynchronous request execution with @{@link Async} and 
	 * {@link AsyncHandler#onFailure(HttpResponse)}.</p>
	 *  
	 * @since 1.3.0
	 */
@Test
public final void testAsyncFailure() throws InterruptedException {
    Robolectric.getFakeHttpLayer().interceptHttpRequests(false);
    String subpath = "/asyncfailure", body = "hello";
    stubFor(get(urlEqualTo(subpath)).willReturn(aResponse().withStatus(403).withBody(body)));
    final Object[] content = new Object[1];
    final Lock lock = new ReentrantLock();
    final Condition condition = lock.newCondition();
    asyncEndpoint.asyncFailure(new AsyncHandler<String>() {

        @Override
        public void onSuccess(HttpResponse httpResponse, String e) {
        }

        @Override
        public void onFailure(HttpResponse httpResponse) {
            lock.lock();
            content[0] = httpResponse;
            condition.signal();
            lock.unlock();
        }
    });
    lock.lock();
    condition.await();
    lock.unlock();
    verify(getRequestedFor(urlEqualTo(subpath)));
    assertTrue(content[0] != null);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) HttpResponse(org.apache.http.HttpResponse) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) Test(org.junit.Test)

Example 28 with Condition

use of java.util.concurrent.locks.Condition in project RoboZombie by sahan.

the class AsyncEndpointTest method testAsyncError.

/**
	 * <p>Tests asynchronous request execution with @{@link Async} and 
	 * {@link AsyncHandler#onError(Exception)}.</p>
	 *  
	 * @since 1.3.0
	 */
@Test
public final void testAsyncError() throws InterruptedException {
    Robolectric.getFakeHttpLayer().interceptHttpRequests(false);
    String subpath = "/asyncerror", body = "non-JSON-content";
    stubFor(get(urlEqualTo(subpath)).willReturn(aResponse().withStatus(200).withBody(body)));
    final Object[] content = new Object[1];
    final Lock lock = new ReentrantLock();
    final Condition condition = lock.newCondition();
    asyncEndpoint.asyncError(new AsyncHandler<User>() {

        @Override
        public void onSuccess(HttpResponse httpResponse, User user) {
        }

        @Override
        public void onError(InvocationException error) {
            lock.lock();
            content[0] = error;
            condition.signal();
            lock.unlock();
        }
    });
    lock.lock();
    condition.await();
    lock.unlock();
    verify(getRequestedFor(urlEqualTo(subpath)));
    assertTrue(content[0] != null);
    assertTrue(content[0] instanceof InvocationException);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) User(com.lonepulse.robozombie.model.User) InvocationException(com.lonepulse.robozombie.proxy.InvocationException) HttpResponse(org.apache.http.HttpResponse) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) Test(org.junit.Test)

Example 29 with Condition

use of java.util.concurrent.locks.Condition in project intellij-community by JetBrains.

the class AwaitNotInLoopInspection method foo.

public void foo() {
    Condition condition;
    try {
        lock.wait();
        condition.await();
        condition.awaitUninterruptibly();
        condition.awaitNanos(300);
        condition.awaitUntil(new Date());
        condition.await(300, TimeUnit.MICROSECONDS);
    } catch (InterruptedException e) {
    }
}
Also used : Condition(java.util.concurrent.locks.Condition) Date(java.sql.Date)

Example 30 with Condition

use of java.util.concurrent.locks.Condition in project intellij-community by JetBrains.

the class ObjectNotifyInspection method foo.

public void foo() {
    final Object bar = new Object();
    bar.notify();
    notify();
    Condition condition = null;
    condition.signal();
    condition.notify();
    condition.notifyAll();
}
Also used : Condition(java.util.concurrent.locks.Condition)

Aggregations

Condition (java.util.concurrent.locks.Condition)34 ReentrantLock (java.util.concurrent.locks.ReentrantLock)20 Lock (java.util.concurrent.locks.Lock)14 Test (org.junit.Test)9 HttpResponse (org.apache.http.HttpResponse)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 ByteBuffer (java.nio.ByteBuffer)3 User (com.lonepulse.robozombie.model.User)2 InvocationException (com.lonepulse.robozombie.proxy.InvocationException)2 UUID (java.util.UUID)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)2 Event (org.apache.ignite.events.Event)2 GridMessageListener (org.apache.ignite.internal.managers.communication.GridMessageListener)2 GridLocalEventListener (org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener)2 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)2 Test (org.testng.annotations.Test)2 TaskMessage (backtype.storm.messaging.TaskMessage)1 AbstractCacheListener (co.paralleluniverse.galaxy.AbstractCacheListener)1