Search in sources :

Example 46 with Answer

use of org.mockito.stubbing.Answer in project hbase by apache.

the class TestBulkLoad method shouldBulkLoadManyFamilyHLog.

@Test
public void shouldBulkLoadManyFamilyHLog() throws IOException {
    when(log.append(any(HRegionInfo.class), any(WALKey.class), argThat(bulkLogWalEditType(WALEdit.BULK_LOAD)), any(boolean.class))).thenAnswer(new Answer() {

        public Object answer(InvocationOnMock invocation) {
            WALKey walKey = invocation.getArgumentAt(1, WALKey.class);
            MultiVersionConcurrencyControl mvcc = walKey.getMvcc();
            if (mvcc != null) {
                MultiVersionConcurrencyControl.WriteEntry we = mvcc.begin();
                walKey.setWriteEntry(we);
            }
            return 01L;
        }

        ;
    });
    testRegionWithFamilies(family1, family2).bulkLoadHFiles(withFamilyPathsFor(family1, family2), false, null);
    verify(log).sync(anyLong());
}
Also used : HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) WALKey(org.apache.hadoop.hbase.wal.WALKey) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 47 with Answer

use of org.mockito.stubbing.Answer in project crate by crate.

the class TestingHelpers method newMockedThreadPool.

public static ThreadPool newMockedThreadPool() {
    ThreadPool threadPool = Mockito.mock(ThreadPool.class);
    final ExecutorService executorService = Executors.newSingleThreadExecutor();
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            executorService.shutdown();
            return null;
        }
    }).when(threadPool).shutdown();
    when(threadPool.executor(anyString())).thenReturn(executorService);
    try {
        doAnswer(new Answer() {

            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                executorService.awaitTermination(1, TimeUnit.SECONDS);
                return null;
            }
        }).when(threadPool).awaitTermination(anyLong(), any(TimeUnit.class));
    } catch (InterruptedException e) {
        throw Throwables.propagate(e);
    }
    return threadPool;
}
Also used : Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ExecutorService(java.util.concurrent.ExecutorService) TimeUnit(java.util.concurrent.TimeUnit)

Example 48 with Answer

use of org.mockito.stubbing.Answer in project pinot by linkedin.

the class TableSizeReaderTest method setUp.

@BeforeClass
public void setUp() throws IOException {
    helix = mock(PinotHelixResourceManager.class);
    when(helix.hasOfflineTable(anyString())).thenAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            String table = (String) invocationOnMock.getArguments()[0];
            return table.indexOf("offline") >= 0;
        }
    });
    when(helix.hasRealtimeTable(anyString())).thenAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            String table = (String) invocationOnMock.getArguments()[0];
            return table.indexOf("realtime") >= 0;
        }
    });
    int counter = 0;
    // server0
    FakeSizeServer s = new FakeSizeServer(Arrays.asList("s1", "s2", "s3"), serverPortStart + counter);
    s.start(URI_PATH, createHandler(200, s.sizes, 0));
    serverMap.put(serverName(counter), s);
    ++counter;
    // server1
    s = new FakeSizeServer(Arrays.asList("s2", "s5"), serverPortStart + counter);
    s.start(URI_PATH, createHandler(200, s.sizes, 0));
    serverMap.put(serverName(counter), s);
    ++counter;
    // server2
    s = new FakeSizeServer(Arrays.asList("s3", "s6"), serverPortStart + counter);
    s.start(URI_PATH, createHandler(404, s.sizes, 0));
    serverMap.put(serverName(counter), s);
    ++counter;
    // server3
    s = new FakeSizeServer(Arrays.asList("r1", "r2"), serverPortStart + counter);
    s.start(URI_PATH, createHandler(200, s.sizes, 0));
    serverMap.put(serverName(counter), s);
    ++counter;
    // server4
    s = new FakeSizeServer(Arrays.asList("r2"), serverPortStart + counter);
    s.start(URI_PATH, createHandler(200, s.sizes, 0));
    serverMap.put(serverName(counter), s);
    ++counter;
    // server5 ... timing out server
    s = new FakeSizeServer(Arrays.asList("s3", "s5"), serverPortStart + counter);
    s.start(URI_PATH, createHandler(200, s.sizes, timeoutMsec * 100));
    serverMap.put(serverName(counter), s);
    ++counter;
}
Also used : Answer(org.mockito.stubbing.Answer) PinotHelixResourceManager(com.linkedin.pinot.controller.helix.core.PinotHelixResourceManager) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Matchers.anyString(org.mockito.Matchers.anyString) BeforeClass(org.testng.annotations.BeforeClass)

Example 49 with Answer

use of org.mockito.stubbing.Answer in project android-volley by mcxiaoke.

the class RequestQueueIntegrationTest method add_dedupeByCacheKey.

/**
     * Asserts that requests with same cache key are processed in order.
     *
     * Needs to be an integration test because relies on complex interations between various queues
     */
@Test
public void add_dedupeByCacheKey() throws Exception {
    // Enqueue 2 requests with the same cache key. The first request takes 20ms. Assert that the
    // second request is only handled after the first one has been parsed and delivered.
    Request req1 = new MockRequest();
    Request req2 = new MockRequest();
    RequestFinishedListener listener = mock(RequestFinishedListener.class);
    Answer<NetworkResponse> delayAnswer = new Answer<NetworkResponse>() {

        @Override
        public NetworkResponse answer(InvocationOnMock invocationOnMock) throws Throwable {
            Thread.sleep(20);
            return mock(NetworkResponse.class);
        }
    };
    //delay only for first
    when(mMockNetwork.performRequest(req1)).thenAnswer(delayAnswer);
    when(mMockNetwork.performRequest(req2)).thenReturn(mock(NetworkResponse.class));
    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 3, mDelivery);
    queue.addRequestFinishedListener(listener);
    queue.add(req1);
    queue.add(req2);
    queue.start();
    // you cannot do strict order verification with mockito 1.9.5 :(
    // as an alternative, first verify no requests have finished, then verify req1 goes through
    verifyNoMoreInteractions(listener);
    verify(listener, timeout(100)).onRequestFinished(req1);
    verify(listener, timeout(10)).onRequestFinished(req2);
    queue.stop();
}
Also used : Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockRequest(com.android.volley.mock.MockRequest) MockRequest(com.android.volley.mock.MockRequest) NoCache(com.android.volley.toolbox.NoCache) RequestFinishedListener(com.android.volley.RequestQueue.RequestFinishedListener) Test(org.junit.Test)

Example 50 with Answer

use of org.mockito.stubbing.Answer in project android-volley by mcxiaoke.

the class RequestQueueIntegrationTest method add_requestProcessedInCorrectOrder.

@Test
public void add_requestProcessedInCorrectOrder() throws Exception {
    // Enqueue 2 requests with different cache keys, and different priorities. The second, higher priority request
    // takes 20ms.
    // Assert that first request is only handled after the first one has been parsed and delivered.
    MockRequest lowerPriorityReq = new MockRequest();
    MockRequest higherPriorityReq = new MockRequest();
    lowerPriorityReq.setCacheKey("1");
    higherPriorityReq.setCacheKey("2");
    lowerPriorityReq.setPriority(Priority.LOW);
    higherPriorityReq.setPriority(Priority.HIGH);
    RequestFinishedListener listener = mock(RequestFinishedListener.class);
    Answer<NetworkResponse> delayAnswer = new Answer<NetworkResponse>() {

        @Override
        public NetworkResponse answer(InvocationOnMock invocationOnMock) throws Throwable {
            Thread.sleep(20);
            return mock(NetworkResponse.class);
        }
    };
    //delay only for higher request
    when(mMockNetwork.performRequest(higherPriorityReq)).thenAnswer(delayAnswer);
    when(mMockNetwork.performRequest(lowerPriorityReq)).thenReturn(mock(NetworkResponse.class));
    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 1, mDelivery);
    queue.addRequestFinishedListener(listener);
    queue.add(lowerPriorityReq);
    queue.add(higherPriorityReq);
    queue.start();
    // you cannot do strict order verification in combination with timeouts with mockito 1.9.5 :(
    // as an alternative, first verify no requests have finished, while higherPriorityReq should be processing
    verifyNoMoreInteractions(listener);
    // verify higherPriorityReq goes through first
    verify(listener, timeout(100)).onRequestFinished(higherPriorityReq);
    // verify lowerPriorityReq goes last
    verify(listener, timeout(10)).onRequestFinished(lowerPriorityReq);
    queue.stop();
}
Also used : Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockRequest(com.android.volley.mock.MockRequest) NoCache(com.android.volley.toolbox.NoCache) RequestFinishedListener(com.android.volley.RequestQueue.RequestFinishedListener) Test(org.junit.Test)

Aggregations

Answer (org.mockito.stubbing.Answer)308 InvocationOnMock (org.mockito.invocation.InvocationOnMock)289 Test (org.junit.Test)180 Mockito.doAnswer (org.mockito.Mockito.doAnswer)114 Before (org.junit.Before)47 Matchers.anyString (org.mockito.Matchers.anyString)42 HashMap (java.util.HashMap)36 ArrayList (java.util.ArrayList)35 PowerMockito.doAnswer (org.powermock.api.mockito.PowerMockito.doAnswer)29 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)26 AtomicReference (java.util.concurrent.atomic.AtomicReference)24 IOException (java.io.IOException)23 Context (android.content.Context)20 File (java.io.File)19 HashSet (java.util.HashSet)17 List (java.util.List)16 Map (java.util.Map)13 Test (org.testng.annotations.Test)13 CountDownLatch (java.util.concurrent.CountDownLatch)12 Handler (android.os.Handler)11