Search in sources :

Example 26 with InvocationOnMock

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

the class CheckpointCoordinatorTest method testPeriodicTriggering.

@Test
public void testPeriodicTriggering() {
    try {
        final JobID jid = new JobID();
        final long start = System.currentTimeMillis();
        // create some mock execution vertices and trigger some checkpoint
        final ExecutionAttemptID triggerAttemptID = new ExecutionAttemptID();
        final ExecutionAttemptID ackAttemptID = new ExecutionAttemptID();
        final ExecutionAttemptID commitAttemptID = new ExecutionAttemptID();
        ExecutionVertex triggerVertex = mockExecutionVertex(triggerAttemptID);
        ExecutionVertex ackVertex = mockExecutionVertex(ackAttemptID);
        ExecutionVertex commitVertex = mockExecutionVertex(commitAttemptID);
        final AtomicInteger numCalls = new AtomicInteger();
        final Execution execution = triggerVertex.getCurrentExecutionAttempt();
        doAnswer(new Answer<Void>() {

            private long lastId = -1;

            private long lastTs = -1;

            @Override
            public Void answer(InvocationOnMock invocation) throws Throwable {
                long id = (Long) invocation.getArguments()[0];
                long ts = (Long) invocation.getArguments()[1];
                assertTrue(id > lastId);
                assertTrue(ts >= lastTs);
                assertTrue(ts >= start);
                lastId = id;
                lastTs = ts;
                numCalls.incrementAndGet();
                return null;
            }
        }).when(execution).triggerCheckpoint(anyLong(), anyLong(), any(CheckpointOptions.class));
        CheckpointCoordinator coord = new CheckpointCoordinator(jid, // periodic interval is 10 ms
        10, // timeout is very long (200 s)
        200000, 0, Integer.MAX_VALUE, ExternalizedCheckpointSettings.none(), new ExecutionVertex[] { triggerVertex }, new ExecutionVertex[] { ackVertex }, new ExecutionVertex[] { commitVertex }, new StandaloneCheckpointIDCounter(), new StandaloneCompletedCheckpointStore(2), null, Executors.directExecutor());
        coord.startCheckpointScheduler();
        long timeout = System.currentTimeMillis() + 60000;
        do {
            Thread.sleep(20);
        } while (timeout > System.currentTimeMillis() && numCalls.get() < 5);
        assertTrue(numCalls.get() >= 5);
        coord.stopCheckpointScheduler();
        // for 400 ms, no further calls may come.
        // there may be the case that one trigger was fired and about to
        // acquire the lock, such that after cancelling it will still do
        // the remainder of its work
        int numCallsSoFar = numCalls.get();
        Thread.sleep(400);
        assertTrue(numCallsSoFar == numCalls.get() || numCallsSoFar + 1 == numCalls.get());
        // start another sequence of periodic scheduling
        numCalls.set(0);
        coord.startCheckpointScheduler();
        timeout = System.currentTimeMillis() + 60000;
        do {
            Thread.sleep(20);
        } while (timeout > System.currentTimeMillis() && numCalls.get() < 5);
        assertTrue(numCalls.get() >= 5);
        coord.stopCheckpointScheduler();
        // for 400 ms, no further calls may come
        // there may be the case that one trigger was fired and about to
        // acquire the lock, such that after cancelling it will still do
        // the remainder of its work
        numCallsSoFar = numCalls.get();
        Thread.sleep(400);
        assertTrue(numCallsSoFar == numCalls.get() || numCallsSoFar + 1 == numCalls.get());
        coord.shutdown(JobStatus.FINISHED);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : ExecutionAttemptID(org.apache.flink.runtime.executiongraph.ExecutionAttemptID) ExecutionVertex(org.apache.flink.runtime.executiongraph.ExecutionVertex) AcknowledgeCheckpoint(org.apache.flink.runtime.messages.checkpoint.AcknowledgeCheckpoint) DeclineCheckpoint(org.apache.flink.runtime.messages.checkpoint.DeclineCheckpoint) IOException(java.io.IOException) Execution(org.apache.flink.runtime.executiongraph.Execution) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvocationOnMock(org.mockito.invocation.InvocationOnMock) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Example 27 with InvocationOnMock

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

the class CheckpointCoordinatorTest method testMaxConcurrentAttempts.

private void testMaxConcurrentAttempts(int maxConcurrentAttempts) {
    try {
        final JobID jid = new JobID();
        // create some mock execution vertices and trigger some checkpoint
        final ExecutionAttemptID triggerAttemptID = new ExecutionAttemptID();
        final ExecutionAttemptID ackAttemptID = new ExecutionAttemptID();
        final ExecutionAttemptID commitAttemptID = new ExecutionAttemptID();
        ExecutionVertex triggerVertex = mockExecutionVertex(triggerAttemptID);
        ExecutionVertex ackVertex = mockExecutionVertex(ackAttemptID);
        ExecutionVertex commitVertex = mockExecutionVertex(commitAttemptID);
        final AtomicInteger numCalls = new AtomicInteger();
        final Execution execution = triggerVertex.getCurrentExecutionAttempt();
        doAnswer(new Answer<Void>() {

            @Override
            public Void answer(InvocationOnMock invocation) throws Throwable {
                numCalls.incrementAndGet();
                return null;
            }
        }).when(execution).triggerCheckpoint(anyLong(), anyLong(), any(CheckpointOptions.class));
        doAnswer(new Answer<Void>() {

            @Override
            public Void answer(InvocationOnMock invocation) throws Throwable {
                numCalls.incrementAndGet();
                return null;
            }
        }).when(execution).notifyCheckpointComplete(anyLong(), anyLong());
        CheckpointCoordinator coord = new CheckpointCoordinator(jid, // periodic interval is 10 ms
        10, // timeout is very long (200 s)
        200000, // no extra delay
        0L, maxConcurrentAttempts, ExternalizedCheckpointSettings.none(), new ExecutionVertex[] { triggerVertex }, new ExecutionVertex[] { ackVertex }, new ExecutionVertex[] { commitVertex }, new StandaloneCheckpointIDCounter(), new StandaloneCompletedCheckpointStore(2), null, Executors.directExecutor());
        coord.startCheckpointScheduler();
        // after a while, there should be exactly as many checkpoints
        // as concurrently permitted
        long now = System.currentTimeMillis();
        long timeout = now + 60000;
        long minDuration = now + 100;
        do {
            Thread.sleep(20);
        } while ((now = System.currentTimeMillis()) < minDuration || (numCalls.get() < maxConcurrentAttempts && now < timeout));
        assertEquals(maxConcurrentAttempts, numCalls.get());
        verify(triggerVertex.getCurrentExecutionAttempt(), times(maxConcurrentAttempts)).triggerCheckpoint(anyLong(), anyLong(), any(CheckpointOptions.class));
        // now, once we acknowledge one checkpoint, it should trigger the next one
        coord.receiveAcknowledgeMessage(new AcknowledgeCheckpoint(jid, ackAttemptID, 1L));
        // this should have immediately triggered a new checkpoint
        now = System.currentTimeMillis();
        timeout = now + 60000;
        do {
            Thread.sleep(20);
        } while (numCalls.get() < maxConcurrentAttempts + 1 && now < timeout);
        assertEquals(maxConcurrentAttempts + 1, numCalls.get());
        // no further checkpoints should happen
        Thread.sleep(200);
        assertEquals(maxConcurrentAttempts + 1, numCalls.get());
        coord.shutdown(JobStatus.FINISHED);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : ExecutionAttemptID(org.apache.flink.runtime.executiongraph.ExecutionAttemptID) ExecutionVertex(org.apache.flink.runtime.executiongraph.ExecutionVertex) IOException(java.io.IOException) AcknowledgeCheckpoint(org.apache.flink.runtime.messages.checkpoint.AcknowledgeCheckpoint) Execution(org.apache.flink.runtime.executiongraph.Execution) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvocationOnMock(org.mockito.invocation.InvocationOnMock) JobID(org.apache.flink.api.common.JobID)

Example 28 with InvocationOnMock

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

the class YarnApplicationMasterRunnerTest method testCreateTaskExecutorContext.

@Test
public void testCreateTaskExecutorContext() throws Exception {
    File root = folder.getRoot();
    File home = new File(root, "home");
    boolean created = home.mkdir();
    assertTrue(created);
    Answer<?> getDefault = new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            return invocationOnMock.getArguments()[1];
        }
    };
    Configuration flinkConf = new Configuration();
    YarnConfiguration yarnConf = mock(YarnConfiguration.class);
    doAnswer(getDefault).when(yarnConf).get(anyString(), anyString());
    doAnswer(getDefault).when(yarnConf).getInt(anyString(), anyInt());
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            return new String[] { (String) invocationOnMock.getArguments()[1] };
        }
    }).when(yarnConf).getStrings(anyString(), Mockito.<String>anyVararg());
    Map<String, String> env = ImmutableMap.<String, String>builder().put(ENV_APP_ID, "foo").put(ENV_CLIENT_HOME_DIR, home.getAbsolutePath()).put(ENV_CLIENT_SHIP_FILES, "").put(ENV_FLINK_CLASSPATH, "").put(ENV_HADOOP_USER_NAME, "foo").put(FLINK_JAR_PATH, root.toURI().toString()).build();
    ContaineredTaskManagerParameters tmParams = mock(ContaineredTaskManagerParameters.class);
    Configuration taskManagerConf = new Configuration();
    String workingDirectory = root.getAbsolutePath();
    Class<?> taskManagerMainClass = YarnApplicationMasterRunnerTest.class;
    ContainerLaunchContext ctx = Utils.createTaskExecutorContext(flinkConf, yarnConf, env, tmParams, taskManagerConf, workingDirectory, taskManagerMainClass, LOG);
    assertEquals("file", ctx.getLocalResources().get("flink.jar").getResource().getScheme());
}
Also used : Configuration(org.apache.flink.configuration.Configuration) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) ContaineredTaskManagerParameters(org.apache.flink.runtime.clusterframework.ContaineredTaskManagerParameters) Matchers.anyString(org.mockito.Matchers.anyString) ContainerLaunchContext(org.apache.hadoop.yarn.api.records.ContainerLaunchContext) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) InvocationOnMock(org.mockito.invocation.InvocationOnMock) File(java.io.File) Test(org.junit.Test)

Example 29 with InvocationOnMock

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

the class TestDelegationTokenRenewer method testStopRenewalWhenFsGone.

@Test
public void testStopRenewalWhenFsGone() throws IOException, InterruptedException {
    Configuration conf = mock(Configuration.class);
    Token<?> token = mock(Token.class);
    doReturn(new Text("myservice")).when(token).getService();
    doAnswer(new Answer<Long>() {

        public Long answer(InvocationOnMock invocation) {
            return Time.now() + RENEW_CYCLE;
        }
    }).when(token).renew(any(Configuration.class));
    RenewableFileSystem fs = mock(RenewableFileSystem.class);
    doReturn(conf).when(fs).getConf();
    doReturn(token).when(fs).getRenewToken();
    renewer.addRenewAction(fs);
    assertEquals(1, renewer.getRenewQueueLength());
    Thread.sleep(RENEW_CYCLE);
    verify(token, atLeast(1)).renew(eq(conf));
    verify(token, atMost(2)).renew(eq(conf));
    // drop weak ref
    fs = null;
    System.gc();
    System.gc();
    System.gc();
    // next renew should detect the fs as gone
    Thread.sleep(RENEW_CYCLE);
    verify(token, atLeast(1)).renew(eq(conf));
    verify(token, atMost(2)).renew(eq(conf));
    assertEquals(0, renewer.getRenewQueueLength());
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Text(org.apache.hadoop.io.Text) Test(org.junit.Test)

Example 30 with InvocationOnMock

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

the class TestDelegationTokenRenewer method testGetNewTokenOnRenewFailure.

@Test
public void testGetNewTokenOnRenewFailure() throws IOException, InterruptedException {
    Text service = new Text("myservice");
    Configuration conf = mock(Configuration.class);
    final Token<?> token1 = mock(Token.class);
    doReturn(service).when(token1).getService();
    doThrow(new IOException("boom")).when(token1).renew(eq(conf));
    final Token<?> token2 = mock(Token.class);
    doReturn(service).when(token2).getService();
    doAnswer(new Answer<Long>() {

        public Long answer(InvocationOnMock invocation) {
            return Time.now() + RENEW_CYCLE;
        }
    }).when(token2).renew(eq(conf));
    RenewableFileSystem fs = mock(RenewableFileSystem.class);
    doReturn(conf).when(fs).getConf();
    doReturn(token1).doReturn(token2).when(fs).getRenewToken();
    doReturn(token2).when(fs).getDelegationToken(null);
    doAnswer(new Answer<Token<?>[]>() {

        public Token<?>[] answer(InvocationOnMock invocation) {
            return new Token<?>[] { token2 };
        }
    }).when(fs).addDelegationTokens(null, null);
    renewer.addRenewAction(fs);
    assertEquals(1, renewer.getRenewQueueLength());
    Thread.sleep(RENEW_CYCLE);
    verify(fs).getRenewToken();
    verify(token1, atLeast(1)).renew(eq(conf));
    verify(token1, atMost(2)).renew(eq(conf));
    verify(fs).addDelegationTokens(null, null);
    verify(fs).setDelegationToken(eq(token2));
    assertEquals(1, renewer.getRenewQueueLength());
    renewer.removeRenewAction(fs);
    verify(token2).cancel(eq(conf));
    assertEquals(0, renewer.getRenewQueueLength());
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Text(org.apache.hadoop.io.Text) IOException(java.io.IOException) 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