Java Code Examples for org.easymock.Capture#reset()

The following examples show how to use org.easymock.Capture#reset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GossipDeviceStoreTest.java    From onos with Apache License 2.0 6 votes vote down vote up
private <T> void resetCommunicatorExpectingSingleBroadcast(
        Capture<T> message,
        Capture<MessageSubject> subject,
        Capture<Function<T, byte[]>> encoder) {

    message.reset();
    subject.reset();
    encoder.reset();
    reset(clusterCommunicator);
    clusterCommunicator.broadcast(
                capture(message),
                capture(subject),
                capture(encoder));
    expectLastCall().once();
    replay(clusterCommunicator);
}
 
Example 2
Source File: GossipDeviceStoreTest.java    From onos with Apache License 2.0 5 votes vote down vote up
private <T> void resetCommunicatorExpectingNoBroadcast(
        Capture<T> message,
        Capture<MessageSubject> subject,
        Capture<Function<T, byte[]>> encoder) {
    message.reset();
    subject.reset();
    encoder.reset();
    reset(clusterCommunicator);
    replay(clusterCommunicator);
}
 
Example 3
Source File: SlaKillControllerTest.java    From attic-aurora with Apache License 2.0 4 votes vote down vote up
@Test
public <T, E extends Exception> void testSlaKill() throws Exception {
  IJobUpdateDetails updateDetails = IJobUpdateDetails.build(
      new JobUpdateDetails(
          UPDATE.newBuilder(),
          ImmutableList.of(new JobUpdateEvent(JobUpdateStatus.ROLLING_FORWARD, 123L)),
          ImmutableList.of()));
  expect(storageUtil.jobUpdateStore.fetchJobUpdate(UPDATE_ID))
      .andReturn(Optional.of(updateDetails))
      .anyTimes();
  Capture<IJobInstanceUpdateEvent> instanceUpdateEventCapture = newCapture();
  storageUtil.jobUpdateStore.saveJobInstanceUpdateEvent(
      eq(UPDATE_ID),
      capture(instanceUpdateEventCapture));
  expectLastCall().times(2);
  storageUtil.expectTaskFetch(TASK.getAssignedTask().getTaskId(), TASK);
  Capture<Storage.MutateWork<T, E>> workCapture = createCapture();
  slaManager.checkSlaThenAct(
      eq(TASK),
      eq(ISlaPolicy.build(TEST_SLA_POLICY)),
      capture(workCapture),
      eq(ImmutableMap.of()),
      eq(false));
  expectBatchExecute(batchWorker, storageUtil.storage, control);
  expect(backoffStrategy.calculateBackoffMs(0)).andReturn(42L);

  control.replay();

  // Kill command has not been executed yet
  assertEquals(2, killCommandHasExecuted.getCount());

  // Start an SLA-aware kill
  slaKillController.slaKill(
      storageUtil.mutableStoreProvider,
      INSTANCE_KEY,
      TASK,
      UPDATE_ID,
      ISlaPolicy.build(TEST_SLA_POLICY),
      JobUpdateStatus.ROLLING_FORWARD,
      fakeKillCommand);

  // Ensure the SLA_CHECKING_MESSAGE message is added
  assertTrue(
      checkInstanceEventMatches(
          instanceUpdateEventCapture.getValue(),
          INSTANCE_KEY,
          JobUpdateAction.INSTANCE_UPDATING,
          SlaKillController.SLA_CHECKING_MESSAGE));
  instanceUpdateEventCapture.reset();
  assertFalse(instanceUpdateEventCapture.hasCaptured());

  // Pretend SLA passes, executes work
  workCapture.getValue().apply(storageUtil.mutableStoreProvider);
  assertEquals(1, killCommandHasExecuted.getCount());

  // Ensure the SLA_PASSED_MESSAGE message is added
  assertTrue(
      checkInstanceEventMatches(
          instanceUpdateEventCapture.getValue(),
          INSTANCE_KEY,
          JobUpdateAction.INSTANCE_UPDATING,
          SlaKillController.SLA_PASSED_MESSAGE));
}
 
Example 4
Source File: SlaKillControllerTest.java    From attic-aurora with Apache License 2.0 4 votes vote down vote up
/**
 * Test that SLA kills are retried in case the SLA check does not pass.
 */
@Test
public <T, E extends Exception> void testSlaKillRetry() throws Exception {
  IJobUpdateDetails updateDetails = IJobUpdateDetails.build(
      new JobUpdateDetails(
          UPDATE.newBuilder(),
          ImmutableList.of(new JobUpdateEvent(JobUpdateStatus.ROLLING_FORWARD, 123L)),
          ImmutableList.of()));
  expect(storageUtil.jobUpdateStore.fetchJobUpdate(UPDATE_ID))
      .andReturn(Optional.of(updateDetails))
      .anyTimes();
  storageUtil.jobUpdateStore.saveJobInstanceUpdateEvent(eq(UPDATE_ID), anyObject());
  expectLastCall().times(2);
  storageUtil.expectTaskFetch(TASK.getAssignedTask().getTaskId(), TASK).times(2);
  storageUtil.expectTaskFetch(
      TASK.getAssignedTask().getTaskId(),
      IScheduledTask.build(TASK.newBuilder().setStatus(ScheduleStatus.KILLING)));
  Capture<Storage.MutateWork<T, E>> workCapture = createCapture();
  slaManager.checkSlaThenAct(
      eq(TASK),
      eq(ISlaPolicy.build(TEST_SLA_POLICY)),
      capture(workCapture),
      eq(ImmutableMap.of()),
      eq(false));
  expectLastCall().times(2);
  expectBatchExecute(batchWorker, storageUtil.storage, control).times(3);
  expect(backoffStrategy.calculateBackoffMs(0L)).andReturn(42L);
  expect(backoffStrategy.calculateBackoffMs(42L)).andReturn(84L);

  control.replay();

  // Kill command has not been executed yet
  assertFalse(statsProvider.getAllValues().keySet().contains(KILL_ATTEMPTS_STAT_NAME));
  assertFalse(statsProvider.getAllValues().keySet().contains(KILL_SUCCESSES_STAT_NAME));
  assertFalse(workCapture.hasCaptured());
  assertEquals(killCommandHasExecuted.getCount(), 2);

  // Start an SLA-aware kill
  slaKillController.slaKill(
      storageUtil.mutableStoreProvider,
      INSTANCE_KEY,
      TASK,
      UPDATE_ID,
      ISlaPolicy.build(TEST_SLA_POLICY),
      JobUpdateStatus.ROLLING_FORWARD,
      fakeKillCommand);

  // SLA check is called and discarded, pretending it failed
  assertEquals(1, statsProvider.getLongValue(KILL_ATTEMPTS_STAT_NAME));
  assertFalse(statsProvider.getAllValues().keySet().contains(KILL_SUCCESSES_STAT_NAME));
  assertTrue(workCapture.hasCaptured());
  workCapture.reset();
  assertEquals(2, killCommandHasExecuted.getCount());
  assertFalse(workCapture.hasCaptured());

  // Another SLA kill is scheduled assuming the previous attempt failed
  assertEquals(1, clock.countDeferredWork());
  clock.advance(TimeAmount.of(42L, Time.MILLISECONDS));

  // The second SLA check passes and the kill function is called
  assertEquals(2, killCommandHasExecuted.getCount());
  workCapture.getValue().apply(storageUtil.mutableStoreProvider);
  assertEquals(1, killCommandHasExecuted.getCount());
  assertEquals(2, statsProvider.getLongValue(KILL_ATTEMPTS_STAT_NAME));
  assertEquals(1, statsProvider.getLongValue(KILL_SUCCESSES_STAT_NAME));

  // One more SLA kill is scheduled assuming the previous attempt failed. Since the previous
  // attempt did not fail, we do a NOOP since the task is already KILLING
  clock.advance(TimeAmount.of(84L, Time.MILLISECONDS));
  assertEquals(2, statsProvider.getLongValue(KILL_ATTEMPTS_STAT_NAME));
  assertEquals(1, statsProvider.getLongValue(KILL_SUCCESSES_STAT_NAME));
  assertEquals(1, killCommandHasExecuted.getCount());
  assertEquals(0, clock.countDeferredWork());
}
 
Example 5
Source File: SlaKillControllerTest.java    From attic-aurora with Apache License 2.0 4 votes vote down vote up
@Test
public <T, E extends Exception> void testSlaKillRollingBack() throws Exception {
  IJobUpdateDetails updateDetails = IJobUpdateDetails.build(
      new JobUpdateDetails(
          UPDATE.newBuilder(),
          ImmutableList.of(new JobUpdateEvent(JobUpdateStatus.ROLLING_BACK, 123L)),
          ImmutableList.of()));
  expect(storageUtil.jobUpdateStore.fetchJobUpdate(UPDATE_ID))
      .andReturn(Optional.of(updateDetails))
      .anyTimes();
  Capture<IJobInstanceUpdateEvent> instanceUpdateEventCapture = newCapture();
  storageUtil.jobUpdateStore.saveJobInstanceUpdateEvent(
      eq(UPDATE_ID),
      capture(instanceUpdateEventCapture));
  expectLastCall().times(2);
  storageUtil.expectTaskFetch(TASK.getAssignedTask().getTaskId(), TASK);
  Capture<Storage.MutateWork<T, E>> workCapture = createCapture();
  slaManager.checkSlaThenAct(
      eq(TASK),
      eq(ISlaPolicy.build(TEST_SLA_POLICY)),
      capture(workCapture),
      eq(ImmutableMap.of()),
      eq(false));
  expectBatchExecute(batchWorker, storageUtil.storage, control);
  expect(backoffStrategy.calculateBackoffMs(0)).andReturn(42L);

  control.replay();

  // Kill command has not been executed yet
  assertEquals(2, killCommandHasExecuted.getCount());

  // Start an SLA-aware kill
  slaKillController.slaKill(
      storageUtil.mutableStoreProvider,
      INSTANCE_KEY,
      TASK,
      UPDATE_ID,
      ISlaPolicy.build(TEST_SLA_POLICY),
      JobUpdateStatus.ROLLING_BACK,
      fakeKillCommand);

  // Ensure the SLA_CHECKING_MESSAGE message is added with ROLLING_BACK action
  assertTrue(
      checkInstanceEventMatches(
          instanceUpdateEventCapture.getValue(),
          INSTANCE_KEY,
          JobUpdateAction.INSTANCE_ROLLING_BACK,
          SlaKillController.SLA_CHECKING_MESSAGE));
  instanceUpdateEventCapture.reset();
  assertFalse(instanceUpdateEventCapture.hasCaptured());

  // Pretend SLA passes, executes work
  workCapture.getValue().apply(storageUtil.mutableStoreProvider);
  assertEquals(1, killCommandHasExecuted.getCount());

  // Ensure the SLA_PASSED_MESSAGE message is added with ROLLING_BACK action
  assertTrue(
      checkInstanceEventMatches(
          instanceUpdateEventCapture.getValue(),
          INSTANCE_KEY,
          JobUpdateAction.INSTANCE_ROLLING_BACK,
          SlaKillController.SLA_PASSED_MESSAGE));
}
 
Example 6
Source File: ChromeDevToolsServiceImplTest.java    From chrome-devtools-java-client with Apache License 2.0 3 votes vote down vote up
@Test
public void testEventReceivedWithOff() throws InterruptedException {
  // Non existing event handler
  service.accept("{\"method\":\"Domain.name\",\"params\":{\"testProperty\":\"testValue\"}}");

  Capture<TestMessage> testMessageCapture = Capture.newInstance();

  EventHandler<TestMessage> eventHandler = testMessageCapture::setValue;

  EventListener eventListener =
      service.addEventListener("Domain", "name", eventHandler, TestMessage.class);

  expectEventExecutorCall(1);

  replayAll();

  service.accept("{\"method\":\"Domain.name\",\"params\":{\"testProperty\":\"testValue\"}}");

  verifyAll();
  resetAll();

  assertNotNull(testMessageCapture.getValue());
  assertEquals("testValue", testMessageCapture.getValue().getTestProperty());

  testMessageCapture.reset();

  eventListener.off();

  replayAll();

  service.accept("{\"method\":\"Domain.name\",\"params\":{\"testProperty\":\"testValue\"}}");

  verifyAll();

  assertFalse(testMessageCapture.hasCaptured());
}
 
Example 7
Source File: ChromeDevToolsServiceImplTest.java    From chrome-devtools-java-client with Apache License 2.0 3 votes vote down vote up
@Test
public void testEventReceivedWithUnsubscribe() throws InterruptedException {
  // Non existing event handler
  service.accept("{\"method\":\"Domain.name\",\"params\":{\"testProperty\":\"testValue\"}}");

  Capture<TestMessage> testMessageCapture = Capture.newInstance();

  EventHandler<TestMessage> eventHandler = testMessageCapture::setValue;

  EventListener eventListener =
      service.addEventListener("Domain", "name", eventHandler, TestMessage.class);

  expectEventExecutorCall(1);
  replayAll();

  service.accept("{\"method\":\"Domain.name\",\"params\":{\"testProperty\":\"testValue\"}}");

  verifyAll();
  resetAll();

  assertNotNull(testMessageCapture.getValue());
  assertEquals("testValue", testMessageCapture.getValue().getTestProperty());

  testMessageCapture.reset();

  eventListener.unsubscribe();

  replayAll();

  service.accept("{\"method\":\"Domain.name\",\"params\":{\"testProperty\":\"testValue\"}}");

  verifyAll();

  assertFalse(testMessageCapture.hasCaptured());
}
 
Example 8
Source File: ChromeDevToolsServiceImplTest.java    From chrome-devtools-java-client with Apache License 2.0 3 votes vote down vote up
@Test
public void testEventReceivedWithUnsubscribeOnService() throws InterruptedException {
  // Non existing event handler
  service.accept("{\"method\":\"Domain.name\",\"params\":{\"testProperty\":\"testValue\"}}");

  Capture<TestMessage> testMessageCapture = Capture.newInstance();

  EventHandler<TestMessage> eventHandler = testMessageCapture::setValue;

  EventListener eventListener =
      service.addEventListener("Domain", "name", eventHandler, TestMessage.class);

  expectEventExecutorCall(1);

  replayAll();

  service.accept("{\"method\":\"Domain.name\",\"params\":{\"testProperty\":\"testValue\"}}");

  verifyAll();
  resetAll();

  assertNotNull(testMessageCapture.getValue());
  assertEquals("testValue", testMessageCapture.getValue().getTestProperty());

  service.removeEventListener(eventListener);

  testMessageCapture.reset();

  replayAll();

  service.accept("{\"method\":\"Domain.name\",\"params\":{\"testProperty\":\"testValue\"}}");

  verifyAll();

  assertFalse(testMessageCapture.hasCaptured());
}
 
Example 9
Source File: ChromeDevToolsServiceImplTest.java    From chrome-devtools-java-client with Apache License 2.0 2 votes vote down vote up
@Test
public void testEventReceivedHandlerThrowsException() throws InterruptedException {
  // Non existing event handler
  service.accept("{\"method\":\"Domain.name\",\"params\":{\"testProperty\":\"testValue\"}}");

  Capture<TestMessage> testMessageCapture = Capture.newInstance();

  EventHandler<TestMessage> eventHandlerThrowsException =
      event -> {
        throw new RuntimeException("test");
      };
  EventListener eventListenerWithException =
      service.addEventListener("Domain", "name", eventHandlerThrowsException, TestMessage.class);

  EventHandler<TestMessage> eventHandler = testMessageCapture::setValue;

  EventListener eventListener =
      service.addEventListener("Domain", "name", eventHandler, TestMessage.class);

  expectEventExecutorCall(1);

  replayAll();

  service.accept("{\"method\":\"Domain.name\",\"params\":{\"testProperty\":\"testValue\"}}");

  verifyAll();
  resetAll();

  assertNotNull(testMessageCapture.getValue());
  assertEquals("testValue", testMessageCapture.getValue().getTestProperty());

  service.removeEventListener(eventListenerWithException);

  testMessageCapture.reset();

  expectEventExecutorCall(1);

  replayAll();

  service.accept("{\"method\":\"Domain.name\",\"params\":{\"testProperty\":\"testValue\"}}");

  verifyAll();

  assertNotNull(testMessageCapture.getValue());
  assertEquals("testValue", testMessageCapture.getValue().getTestProperty());
}