Search in sources :

Example 96 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project hbase by apache.

the class TestTableInputFormat method createIOEScannerTable.

/**
   * Create a table that IOE's on first scanner next call
   *
   * @throws IOException
   */
static Table createIOEScannerTable(byte[] name, final int failCnt) throws IOException {
    // build up a mock scanner stuff to fail the first time
    Answer<ResultScanner> a = new Answer<ResultScanner>() {

        int cnt = 0;

        @Override
        public ResultScanner answer(InvocationOnMock invocation) throws Throwable {
            // first invocation return the busted mock scanner
            if (cnt++ < failCnt) {
                // create mock ResultScanner that always fails.
                Scan scan = mock(Scan.class);
                // avoid npe
                doReturn("bogus".getBytes()).when(scan).getStartRow();
                ResultScanner scanner = mock(ResultScanner.class);
                // simulate TimeoutException / IOException
                doThrow(new IOException("Injected exception")).when(scanner).next();
                return scanner;
            }
            // otherwise return the real scanner.
            return (ResultScanner) invocation.callRealMethod();
        }
    };
    Table htable = spy(createTable(name));
    doAnswer(a).when(htable).getScanner((Scan) anyObject());
    return htable;
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) Table(org.apache.hadoop.hbase.client.Table) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Scan(org.apache.hadoop.hbase.client.Scan) IOException(java.io.IOException)

Example 97 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project hbase by apache.

the class TestZKProcedureControllers method testSimpleZKCohortMemberController.

/**
   * Smaller test to just test the actuation on the cohort member
   * @throws Exception on failure
   */
@Test(timeout = 60000)
public void testSimpleZKCohortMemberController() throws Exception {
    ZooKeeperWatcher watcher = UTIL.getZooKeeperWatcher();
    final String operationName = "instanceTest";
    final Subprocedure sub = Mockito.mock(Subprocedure.class);
    Mockito.when(sub.getName()).thenReturn(operationName);
    final byte[] data = new byte[] { 1, 2, 3 };
    final CountDownLatch prepared = new CountDownLatch(1);
    final CountDownLatch committed = new CountDownLatch(1);
    final ForeignExceptionDispatcher monitor = spy(new ForeignExceptionDispatcher());
    final ZKProcedureMemberRpcs controller = new ZKProcedureMemberRpcs(watcher, "testSimple");
    // mock out cohort member callbacks
    final ProcedureMember member = Mockito.mock(ProcedureMember.class);
    Mockito.doReturn(sub).when(member).createSubprocedure(operationName, data);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            controller.sendMemberAcquired(sub);
            prepared.countDown();
            return null;
        }
    }).when(member).submitSubprocedure(sub);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            controller.sendMemberCompleted(sub, memberData);
            committed.countDown();
            return null;
        }
    }).when(member).receivedReachedGlobalBarrier(operationName);
    // start running the listener
    controller.start(COHORT_NODE_NAME, member);
    // set a prepare node from a 'coordinator'
    String prepare = ZKProcedureUtil.getAcquireBarrierNode(controller.getZkController(), operationName);
    ZKUtil.createSetData(watcher, prepare, ProtobufUtil.prependPBMagic(data));
    // wait for the operation to be prepared
    prepared.await();
    // create the commit node so we update the operation to enter the commit phase
    String commit = ZKProcedureUtil.getReachedBarrierNode(controller.getZkController(), operationName);
    LOG.debug("Found prepared, posting commit node:" + commit);
    ZKUtil.createAndFailSilent(watcher, commit);
    LOG.debug("Commit node:" + commit + ", exists:" + ZKUtil.checkExists(watcher, commit));
    committed.await();
    verify(monitor, never()).receive(Mockito.any(ForeignException.class));
    // XXX: broken due to composition.
    //    verify(member, never()).getManager().controllerConnectionFailure(Mockito.anyString(),
    //      Mockito.any(IOException.class));
    // cleanup after the test
    ZKUtil.deleteNodeRecursively(watcher, controller.getZkController().getBaseZnode());
    assertEquals("Didn't delete prepare node", -1, ZKUtil.checkExists(watcher, prepare));
    assertEquals("Didn't delete commit node", -1, ZKUtil.checkExists(watcher, commit));
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) ForeignExceptionDispatcher(org.apache.hadoop.hbase.errorhandling.ForeignExceptionDispatcher) ZooKeeperWatcher(org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ForeignException(org.apache.hadoop.hbase.errorhandling.ForeignException) Test(org.junit.Test)

Example 98 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project hbase by apache.

the class TestProcedureMember method testSendMemberAcquiredCommsFailure.

/**
   * Make sure we call cleanup etc, when we have an exception during prepare.
   */
@Test(timeout = 60000)
public void testSendMemberAcquiredCommsFailure() throws Exception {
    buildCohortMemberPair();
    // mock an exception on Subprocedure's prepare
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            throw new IOException("Forced IOException in memeber prepare");
        }
    }).when(mockMemberComms).sendMemberAcquired(any(Subprocedure.class));
    // run the operation
    // build a new operation
    Subprocedure subproc = member.createSubprocedure(op, data);
    member.submitSubprocedure(subproc);
    // if the operation doesn't die properly, then this will timeout
    member.closeAndWait(TIMEOUT);
    // make sure everything ran in order
    InOrder order = inOrder(mockMemberComms, spySub);
    order.verify(spySub).acquireBarrier();
    order.verify(mockMemberComms).sendMemberAcquired(eq(spySub));
    // Later phases not run
    order.verify(spySub, never()).insideBarrier();
    order.verify(mockMemberComms, never()).sendMemberCompleted(eq(spySub), eq(data));
    // error recovery path exercised
    order.verify(spySub).cancel(anyString(), any(Exception.class));
    order.verify(spySub).cleanup(any(Exception.class));
}
Also used : InOrder(org.mockito.InOrder) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IOException(java.io.IOException) ForeignException(org.apache.hadoop.hbase.errorhandling.ForeignException) IOException(java.io.IOException) TimeoutException(org.apache.hadoop.hbase.errorhandling.TimeoutException) Test(org.junit.Test)

Example 99 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock 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 100 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project hbase by apache.

the class TestServerNonceManager method testStopWaiting.

@Test
public void testStopWaiting() throws Exception {
    final ServerNonceManager nm = createManager();
    nm.setConflictWaitIterationMs(1);
    Stoppable stoppingStoppable = createStoppable();
    Mockito.when(stoppingStoppable.isStopped()).thenAnswer(new Answer<Boolean>() {

        AtomicInteger answer = new AtomicInteger(3);

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            return 0 < answer.decrementAndGet();
        }
    });
    nm.startOperation(NO_NONCE, 1, createStoppable());
    TestRunnable tr = new TestRunnable(nm, 1, null, stoppingStoppable);
    Thread t = tr.start();
    waitForThreadToBlockOrExit(t);
    // thread must eventually throw
    t.join();
    tr.propagateError();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Stoppable(org.apache.hadoop.hbase.Stoppable) Test(org.junit.Test)

Aggregations

InvocationOnMock (org.mockito.invocation.InvocationOnMock)947 Test (org.junit.Test)544 Answer (org.mockito.stubbing.Answer)245 Matchers.anyString (org.mockito.Matchers.anyString)104 HashMap (java.util.HashMap)101 ArrayList (java.util.ArrayList)96 Before (org.junit.Before)96 Mockito.doAnswer (org.mockito.Mockito.doAnswer)96 IOException (java.io.IOException)86 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)73 Test (org.testng.annotations.Test)59 CountDownLatch (java.util.concurrent.CountDownLatch)57 List (java.util.List)56 File (java.io.File)49 Configuration (org.apache.hadoop.conf.Configuration)46 AtomicReference (java.util.concurrent.atomic.AtomicReference)44 Context (android.content.Context)39 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)36 ServiceCallback (com.microsoft.azure.mobile.http.ServiceCallback)33 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)32