Search in sources :

Example 6 with Capture

use of org.easymock.Capture in project ribbon by Netflix.

the class EurekaNotificationServerListUpdaterTest method testUpdating.

@Test
public void testUpdating() throws Exception {
    EurekaNotificationServerListUpdater serverListUpdater = new EurekaNotificationServerListUpdater(new Provider<EurekaClient>() {

        @Override
        public EurekaClient get() {
            return eurekaClientMock;
        }
    }, testExecutor);
    try {
        Capture<EurekaEventListener> eventListenerCapture = new Capture<EurekaEventListener>();
        eurekaClientMock.registerEventListener(EasyMock.capture(eventListenerCapture));
        EasyMock.replay(eurekaClientMock);
        final AtomicBoolean firstTime = new AtomicBoolean(false);
        final CountDownLatch firstLatch = new CountDownLatch(1);
        final CountDownLatch secondLatch = new CountDownLatch(1);
        serverListUpdater.start(new ServerListUpdater.UpdateAction() {

            @Override
            public void doUpdate() {
                if (firstTime.compareAndSet(false, true)) {
                    firstLatch.countDown();
                } else {
                    secondLatch.countDown();
                }
            }
        });
        eventListenerCapture.getValue().onEvent(new CacheRefreshedEvent());
        Assert.assertTrue(firstLatch.await(2, TimeUnit.SECONDS));
        // wait a bit for the updateQueued flag to be reset
        for (int i = 1; i < 10; i++) {
            if (serverListUpdater.updateQueued.get()) {
                Thread.sleep(i * 100);
            } else {
                break;
            }
        }
        eventListenerCapture.getValue().onEvent(new CacheRefreshedEvent());
        Assert.assertTrue(secondLatch.await(2, TimeUnit.SECONDS));
    } finally {
        serverListUpdater.stop();
        EasyMock.verify(eurekaClientMock);
    }
}
Also used : EurekaClient(com.netflix.discovery.EurekaClient) EurekaEventListener(com.netflix.discovery.EurekaEventListener) CountDownLatch(java.util.concurrent.CountDownLatch) Capture(org.easymock.Capture) ServerListUpdater(com.netflix.loadbalancer.ServerListUpdater) CacheRefreshedEvent(com.netflix.discovery.CacheRefreshedEvent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 7 with Capture

use of org.easymock.Capture in project ribbon by Netflix.

the class EurekaNotificationServerListUpdaterTest method testStopWithCommonExecutor.

@Test
public void testStopWithCommonExecutor() throws Exception {
    EurekaNotificationServerListUpdater serverListUpdater1 = new EurekaNotificationServerListUpdater(new Provider<EurekaClient>() {

        @Override
        public EurekaClient get() {
            return eurekaClientMock;
        }
    }, testExecutor);
    EurekaNotificationServerListUpdater serverListUpdater2 = new EurekaNotificationServerListUpdater(new Provider<EurekaClient>() {

        @Override
        public EurekaClient get() {
            return eurekaClientMock2;
        }
    }, testExecutor);
    Capture<EurekaEventListener> eventListenerCapture = new Capture<EurekaEventListener>();
    eurekaClientMock.registerEventListener(EasyMock.capture(eventListenerCapture));
    Capture<EurekaEventListener> eventListenerCapture2 = new Capture<EurekaEventListener>();
    eurekaClientMock2.registerEventListener(EasyMock.capture(eventListenerCapture2));
    EasyMock.replay(eurekaClientMock);
    EasyMock.replay(eurekaClientMock2);
    final CountDownLatch updateCountLatch = new CountDownLatch(2);
    serverListUpdater1.start(new ServerListUpdater.UpdateAction() {

        @Override
        public void doUpdate() {
            updateCountLatch.countDown();
        }
    });
    serverListUpdater2.start(new ServerListUpdater.UpdateAction() {

        @Override
        public void doUpdate() {
            updateCountLatch.countDown();
        }
    });
    eventListenerCapture.getValue().onEvent(new CacheRefreshedEvent());
    eventListenerCapture2.getValue().onEvent(new CacheRefreshedEvent());
    // latch is for both
    Assert.assertTrue(updateCountLatch.await(2, TimeUnit.SECONDS));
    serverListUpdater1.stop();
    serverListUpdater2.stop();
    EasyMock.verify(eurekaClientMock);
    EasyMock.verify(eurekaClientMock2);
}
Also used : EurekaClient(com.netflix.discovery.EurekaClient) EurekaEventListener(com.netflix.discovery.EurekaEventListener) CountDownLatch(java.util.concurrent.CountDownLatch) Capture(org.easymock.Capture) ServerListUpdater(com.netflix.loadbalancer.ServerListUpdater) CacheRefreshedEvent(com.netflix.discovery.CacheRefreshedEvent) Test(org.junit.Test)

Example 8 with Capture

use of org.easymock.Capture in project rest.li by linkedin.

the class TestRestClientRequestBuilder method clientGeneratedStreamRequest.

//This is similar to clientGeneratedRestRequest above except that it will generate a StreamRequest instead
//of a RestRequest. Note that this will ONLY happen if either acceptResponseAttachments below is 'true' OR
//streamingAttachmentDataSources below is non-null with a size greater then 0. If both of these do not hold,
//then a StreamRequest will not be generated by the RestClient.
@SuppressWarnings({ "unchecked", "rawtypes", "deprecation" })
private <T extends Request> StreamRequest clientGeneratedStreamRequest(Class<T> requestClass, ResourceMethod method, DataMap entityBody, RestClient.ContentType contentType, List<RestClient.AcceptType> acceptTypes, boolean acceptContentTypePerClient, List<Object> streamingAttachmentDataSources, boolean acceptResponseAttachments) throws URISyntaxException {
    // massive setup...
    Client mockClient = EasyMock.createMock(Client.class);
    @SuppressWarnings({ "rawtypes" }) Request<?> mockRequest = EasyMock.createMock(requestClass);
    RecordTemplate mockRecordTemplate = EasyMock.createMock(RecordTemplate.class);
    @SuppressWarnings({ "rawtypes" }) RestResponseDecoder mockResponseDecoder = EasyMock.createMock(RestResponseDecoder.class);
    RestliRequestOptions requestOptions = RestliRequestOptions.DEFAULT_OPTIONS;
    //If there is a desire to receive response attachments, then we must use request options.
    if (!acceptContentTypePerClient || acceptResponseAttachments) {
        requestOptions = new RestliRequestOptions(ProtocolVersionOption.USE_LATEST_IF_AVAILABLE, null, null, contentType, acceptTypes, acceptResponseAttachments);
    }
    setCommonExpectations(mockRequest, method, mockResponseDecoder, requestOptions);
    if (streamingAttachmentDataSources != null && streamingAttachmentDataSources.size() > 0) {
        EasyMock.expect(mockRequest.getStreamingAttachments()).andReturn(streamingAttachmentDataSources).times(2);
    } else {
        EasyMock.expect(mockRequest.getStreamingAttachments()).andReturn(null).times(2);
    }
    setResourceMethodExpectations(method, mockRequest, mockRecordTemplate, entityBody);
    Capture<StreamRequest> streamRequestCapture = new Capture<StreamRequest>();
    EasyMock.expect(mockClient.getMetadata(new URI(HOST + SERVICE_NAME))).andReturn(Collections.<String, Object>emptyMap()).once();
    mockClient.streamRequest(EasyMock.capture(streamRequestCapture), (RequestContext) EasyMock.anyObject(), (Callback<StreamResponse>) EasyMock.anyObject());
    EasyMock.expectLastCall().once();
    EasyMock.replay(mockClient, mockRequest, mockRecordTemplate);
    // do work!
    RestClient restClient;
    if (acceptContentTypePerClient) {
        // configuration per client
        restClient = new RestClient(mockClient, HOST, contentType, acceptTypes);
    } else {
        // configuration per request
        restClient = new RestClient(mockClient, HOST);
    }
    restClient.sendRequest(mockRequest);
    return streamRequestCapture.getValue();
}
Also used : StreamResponse(com.linkedin.r2.message.stream.StreamResponse) ByteString(com.linkedin.data.ByteString) URI(java.net.URI) Capture(org.easymock.Capture) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) RestResponseDecoder(com.linkedin.restli.internal.client.RestResponseDecoder) RecordTemplate(com.linkedin.data.template.RecordTemplate) Client(com.linkedin.r2.transport.common.Client)

Example 9 with Capture

use of org.easymock.Capture in project rest.li by linkedin.

the class TestRestClientRequestBuilder method clientGeneratedRestRequest.

@SuppressWarnings({ "unchecked", "rawtypes", "deprecation" })
private <T extends Request> RestRequest clientGeneratedRestRequest(Class<T> requestClass, ResourceMethod method, DataMap entityBody, RestClient.ContentType contentType, List<RestClient.AcceptType> acceptTypes, boolean acceptContentTypePerClient) throws URISyntaxException {
    // massive setup...
    Client mockClient = EasyMock.createMock(Client.class);
    @SuppressWarnings({ "rawtypes" }) Request<?> mockRequest = EasyMock.createMock(requestClass);
    RecordTemplate mockRecordTemplate = EasyMock.createMock(RecordTemplate.class);
    @SuppressWarnings({ "rawtypes" }) RestResponseDecoder mockResponseDecoder = EasyMock.createMock(RestResponseDecoder.class);
    RestliRequestOptions requestOptions = RestliRequestOptions.DEFAULT_OPTIONS;
    if (!acceptContentTypePerClient) {
        requestOptions = new RestliRequestOptions(ProtocolVersionOption.USE_LATEST_IF_AVAILABLE, null, null, contentType, acceptTypes, false);
    }
    setCommonExpectations(mockRequest, method, mockResponseDecoder, requestOptions);
    EasyMock.expect(mockRequest.getStreamingAttachments()).andReturn(null).times(2);
    setResourceMethodExpectations(method, mockRequest, mockRecordTemplate, entityBody);
    Capture<RestRequest> restRequestCapture = new Capture<RestRequest>();
    EasyMock.expect(mockClient.getMetadata(new URI(HOST + SERVICE_NAME))).andReturn(Collections.<String, Object>emptyMap()).once();
    mockClient.restRequest(EasyMock.capture(restRequestCapture), (RequestContext) EasyMock.anyObject(), (Callback<RestResponse>) EasyMock.anyObject());
    EasyMock.expectLastCall().once();
    EasyMock.replay(mockClient, mockRequest, mockRecordTemplate);
    // do work!
    RestClient restClient;
    if (acceptContentTypePerClient) {
        // configuration per client
        restClient = new RestClient(mockClient, HOST, contentType, acceptTypes);
    } else {
        // configuration per request
        restClient = new RestClient(mockClient, HOST);
    }
    restClient.sendRequest(mockRequest);
    return restRequestCapture.getValue();
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) URI(java.net.URI) Capture(org.easymock.Capture) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponseDecoder(com.linkedin.restli.internal.client.RestResponseDecoder) RecordTemplate(com.linkedin.data.template.RecordTemplate) Client(com.linkedin.r2.transport.common.Client)

Example 10 with Capture

use of org.easymock.Capture in project rest.li by linkedin.

the class RestClientTest method testShutdown.

@Test
public void testShutdown() {
    Client client = EasyMock.createMock(Client.class);
    @SuppressWarnings("unchecked") Callback<None> callback = EasyMock.createMock(Callback.class);
    Capture<Callback<None>> callbackCapture = new Capture<Callback<None>>();
    // Underlying client's shutdown should be invoked with correct callback
    client.shutdown(EasyMock.capture(callbackCapture));
    EasyMock.replay(client);
    // No methods should be invoked on the callback
    EasyMock.replay(callback);
    RestClient restClient = new RestClient(client, "d2://");
    restClient.shutdown(callback);
    EasyMock.verify(client);
    EasyMock.verify(callback);
    EasyMock.reset(callback);
    None none = None.none();
    callback.onSuccess(none);
    EasyMock.replay(callback);
    Callback<None> captured = callbackCapture.getValue();
    captured.onSuccess(none);
    EasyMock.verify(callback);
}
Also used : FutureCallback(com.linkedin.common.callback.FutureCallback) Callback(com.linkedin.common.callback.Callback) Client(com.linkedin.r2.transport.common.Client) None(com.linkedin.common.util.None) Capture(org.easymock.Capture) Test(org.testng.annotations.Test)

Aggregations

Capture (org.easymock.Capture)43 Test (org.junit.Test)19 INetworkManagementEventObserver (android.net.INetworkManagementEventObserver)12 Intent (android.content.Intent)8 IConnectivityManager (android.net.IConnectivityManager)8 INetworkManagementService (android.os.INetworkManagementService)8 TrustedTime (android.util.TrustedTime)8 CacheRefreshedEvent (com.netflix.discovery.CacheRefreshedEvent)6 EurekaEventListener (com.netflix.discovery.EurekaEventListener)6 ByteString (com.linkedin.data.ByteString)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Callback (com.linkedin.common.callback.Callback)4 EurekaClient (com.netflix.discovery.EurekaClient)4 ServerListUpdater (com.netflix.loadbalancer.ServerListUpdater)4 AlarmManager (android.app.AlarmManager)3 IActivityManager (android.app.IActivityManager)3 IAlarmManager (android.app.IAlarmManager)3 INotificationManager (android.app.INotificationManager)3 IProcessObserver (android.app.IProcessObserver)3 PendingIntent (android.app.PendingIntent)3