Search in sources :

Example 1 with InvocationException

use of com.lonepulse.robozombie.proxy.InvocationException 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 2 with InvocationException

use of com.lonepulse.robozombie.proxy.InvocationException 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)

Aggregations

User (com.lonepulse.robozombie.model.User)2 InvocationException (com.lonepulse.robozombie.proxy.InvocationException)2 Condition (java.util.concurrent.locks.Condition)2 Lock (java.util.concurrent.locks.Lock)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)2 HttpResponse (org.apache.http.HttpResponse)2 Test (org.junit.Test)2