Search in sources :

Example 1 with TimeOutWatcher

use of com.vodafone360.people.service.utils.TimeOutWatcher in project 360-Engine-for-Android by 360.

the class TimeOutWatcher method fireRequestExpired.

/**
     * Creates a TimeOut event and adds it to the response queue. FIXME: this
     * assumes that adding a request to the response queue will trigger a
     * synchronous removeRequest() call with the same thread.
     * 
     * @param request the request that has timed out
     */
private void fireRequestExpired(Request request) {
    // create a list with a server error containing a timeout
    final List<BaseDataType> data = new ArrayList<BaseDataType>(1);
    final ServerError timeoutError = new ServerError(ServerError.ErrorType.REQUEST_TIMEOUT, request.getRequestId());
    timeoutError.errorDescription = "TimeOutWatcher detected that the request id=[" + request.getRequestId() + "] has timed out.";
    data.add(timeoutError);
    // set the request as expired
    request.expired = true;
    // add the timeout error to the response queue
    LogUtils.logW("TimeOutWatcher.fireRequestExpired(): " + "adding a timeout error to the response queue for reqId=[" + request.getRequestId() + "]");
    QueueManager.getInstance().addResponse(new DecodedResponse(request.getRequestId(), data, request.mEngineId, DecodedResponse.ResponseType.SERVER_ERROR.ordinal()));
}
Also used : DecodedResponse(com.vodafone360.people.service.io.ResponseQueue.DecodedResponse) ServerError(com.vodafone360.people.datatypes.ServerError) ArrayList(java.util.ArrayList) BaseDataType(com.vodafone360.people.datatypes.BaseDataType)

Example 2 with TimeOutWatcher

use of com.vodafone360.people.service.utils.TimeOutWatcher in project 360-Engine-for-Android by 360.

the class TimeOutWatcherTest method setUp.

protected void setUp() throws Exception {
    super.setUp();
    mApplication = (MainApplication) Instrumentation.newApplication(MainApplication.class, getInstrumentation().getTargetContext());
    mApplication.onCreate();
    mWatcher = new TimeOutWatcher();
}
Also used : TimeOutWatcher(com.vodafone360.people.service.utils.TimeOutWatcher)

Example 3 with TimeOutWatcher

use of com.vodafone360.people.service.utils.TimeOutWatcher in project 360-Engine-for-Android by 360.

the class TimeOutWatcherTest method testThrowingTimeout.

/**
     * Tests that a timeout error is thrown if the response of the request has not been received after the timeout period. 
     */
@Suppress
public void testThrowingTimeout() {
    Log.i("testThrowingTimeout()", "-begin");
    final Request request = createRequestWithTimeout(TIMEOUT_2000_MS);
    // check that the response queue is empty for the engine with EngineId.UNDEFINED id (we use this one for the test)
    DecodedResponse response = ResponseQueue.getInstance().getNextResponse(EngineId.UNDEFINED);
    assertNull(response);
    // adding the request to the queue should add it to the TimeOutWatcher
    final int reqId = QueueManager.getInstance().addRequest(request);
    // check that the response queue is still empty 
    response = ResponseQueue.getInstance().getNextResponse(EngineId.UNDEFINED);
    assertNull(response);
    // let's give at least 2 times the timeout to the system before checking
    long stopTime = System.currentTimeMillis() + (TIMEOUT_2000_MS * 2);
    while (System.currentTimeMillis() < stopTime) {
        try {
            Thread.sleep(TIMEOUT_2000_MS);
        } catch (InterruptedException ie) {
            Log.i("testThrowingTimeout()", "Error while sleeping: " + ie);
        }
    }
    // check that the response is still empty 
    response = ResponseQueue.getInstance().getNextResponse(EngineId.UNDEFINED);
    assertNotNull(response);
    // check response request id is the same as the request id
    assertEquals(reqId, response.mReqId.intValue());
    // check the timeout error returned is as expected
    assertNotNull(response.mDataTypes);
    assertEquals(1, response.mDataTypes.size());
    BaseDataType error = response.mDataTypes.get(0);
    assertTrue(error instanceof ServerError);
    ServerError srvError = (ServerError) error;
    assertEquals(ServerError.ErrorType.REQUEST_TIMEOUT, srvError.getType());
    Log.i("testThrowingTimeout()", "-end");
}
Also used : DecodedResponse(com.vodafone360.people.service.io.ResponseQueue.DecodedResponse) ServerError(com.vodafone360.people.datatypes.ServerError) Request(com.vodafone360.people.service.io.Request) BaseDataType(com.vodafone360.people.datatypes.BaseDataType) Suppress(android.test.suitebuilder.annotation.Suppress)

Aggregations

BaseDataType (com.vodafone360.people.datatypes.BaseDataType)2 ServerError (com.vodafone360.people.datatypes.ServerError)2 DecodedResponse (com.vodafone360.people.service.io.ResponseQueue.DecodedResponse)2 Suppress (android.test.suitebuilder.annotation.Suppress)1 Request (com.vodafone360.people.service.io.Request)1 TimeOutWatcher (com.vodafone360.people.service.utils.TimeOutWatcher)1 ArrayList (java.util.ArrayList)1