Java Code Examples for org.easymock.EasyMock.mock()

The following are Jave code examples for showing how to use mock() of the org.easymock.EasyMock class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: kafka-0.11.0.0-src-with-comment   File: ConsumerNetworkClientTest.java   Source Code and License Vote up 6 votes
@Test
public void doNotBlockIfPollConditionIsSatisfied() {
    NetworkClient mockNetworkClient = EasyMock.mock(NetworkClient.class);
    ConsumerNetworkClient consumerClient = new ConsumerNetworkClient(mockNetworkClient, metadata, time, 100, 1000);

    // expect poll, but with no timeout
    EasyMock.expect(mockNetworkClient.poll(EasyMock.eq(0L), EasyMock.anyLong())).andReturn(Collections.<ClientResponse>emptyList());

    EasyMock.replay(mockNetworkClient);

    consumerClient.poll(Long.MAX_VALUE, time.milliseconds(), new ConsumerNetworkClient.PollCondition() {
        @Override
        public boolean shouldBlock() {
            return false;
        }
    });

    EasyMock.verify(mockNetworkClient);
}
 
Example 2
Project: kafka-0.11.0.0-src-with-comment   File: ConsumerNetworkClientTest.java   Source Code and License Vote up 6 votes
@Test
public void blockWhenPollConditionNotSatisfied() {
    long timeout = 4000L;

    NetworkClient mockNetworkClient = EasyMock.mock(NetworkClient.class);
    ConsumerNetworkClient consumerClient = new ConsumerNetworkClient(mockNetworkClient, metadata, time, 100, 1000);

    EasyMock.expect(mockNetworkClient.inFlightRequestCount()).andReturn(1);
    EasyMock.expect(mockNetworkClient.poll(EasyMock.eq(timeout), EasyMock.anyLong())).andReturn(Collections.<ClientResponse>emptyList());

    EasyMock.replay(mockNetworkClient);

    consumerClient.poll(timeout, time.milliseconds(), new ConsumerNetworkClient.PollCondition() {
        @Override
        public boolean shouldBlock() {
            return true;
        }
    });

    EasyMock.verify(mockNetworkClient);
}
 
Example 3
Project: kafka-0.11.0.0-src-with-comment   File: ConsumerNetworkClientTest.java   Source Code and License Vote up 6 votes
@Test
public void blockOnlyForRetryBackoffIfNoInflightRequests() {
    long retryBackoffMs = 100L;

    NetworkClient mockNetworkClient = EasyMock.mock(NetworkClient.class);
    ConsumerNetworkClient consumerClient = new ConsumerNetworkClient(mockNetworkClient, metadata, time, retryBackoffMs, 1000L);

    EasyMock.expect(mockNetworkClient.inFlightRequestCount()).andReturn(0);
    EasyMock.expect(mockNetworkClient.poll(EasyMock.eq(retryBackoffMs), EasyMock.anyLong())).andReturn(Collections.<ClientResponse>emptyList());

    EasyMock.replay(mockNetworkClient);

    consumerClient.poll(Long.MAX_VALUE, time.milliseconds(), new ConsumerNetworkClient.PollCondition() {
        @Override
        public boolean shouldBlock() {
            return true;
        }
    });

    EasyMock.verify(mockNetworkClient);
}
 
Example 4
Project: cruise-control   File: BrokerFailureDetectorTest.java   Source Code and License Vote up 5 votes
private BrokerFailureDetector createBrokerFailureDetector(Queue<Anomaly> anomalies, Time time) {
  LoadMonitor mockLoadMonitor = EasyMock.mock(LoadMonitor.class);
  EasyMock.expect(mockLoadMonitor.brokersWithPartitions(anyLong())).andAnswer(() -> new HashSet<>(Arrays.asList(0, 1))).anyTimes();
  EasyMock.replay(mockLoadMonitor);
  Properties props = CruiseControlUnitTestUtils.getCruiseControlProperties();
  props.setProperty(KafkaCruiseControlConfig.ZOOKEEPER_CONNECT_CONFIG, zookeeper().getConnectionString());
  KafkaCruiseControlConfig kafkaCruiseControlConfig = new KafkaCruiseControlConfig(props);
  return new BrokerFailureDetector(kafkaCruiseControlConfig,
                                   mockLoadMonitor,
                                   anomalies,
                                   time);
}
 
Example 5
Project: oo-atom   File: AssertStackManipulationsAreSame.java   Source Code and License Vote up 5 votes
/**
 * Ctor.
 * 
 * @param sm Stack manipulation to assert on.
 * @param sm2 Stack manipulation to compare with
 */
public AssertStackManipulationsAreSame(StackManipulation sm, StackManipulation sm2) {
    super(
        sm,
        mv -> {
            Implementation.Context ctx = EasyMock.mock(Implementation.Context.class);
            sm2.apply(mv, ctx);
        }
    );
}
 
Example 6
Project: oo-atom   File: TestsSuiteTest.java   Source Code and License Vote up 5 votes
@Test
public final void generatesTests() throws Throwable {
    FactoryRunner.TestConsumer tc = EasyMock.mock(MockType.STRICT, FactoryRunner.TestConsumer.class);
    {
        tc.accept("foo", new FooTest("foo"));
        tc.accept("bar", new FooTest("bar"));
        tc.accept("baz", new FooTest("baz"));
        EasyMock.replay(tc);
    }
    {
        final TestsSuite suite = new TestsSuite(
            new TestCase(
                "foo",
                new AssertFoo()
            ),
            new TestCase(
                "bar",
                new AssertFoo()
            ),
            new TestCase(
                "baz",
                new AssertFoo()
            )
        );
        suite.produceTests(tc);
        EasyMock.verify(tc);
    }
}
 
Example 7
Project: cruise-control   File: AnomalyDetectorTest.java   Source Code and License Vote up 4 votes
@Test
public void testDelayedCheck() throws InterruptedException, KafkaCruiseControlException {
  LinkedBlockingDeque<Anomaly> anomalies = new LinkedBlockingDeque<>();
  AnomalyNotifier mockAnomalyNotifier = EasyMock.mock(AnomalyNotifier.class);
  BrokerFailureDetector mockBrokerFailureDetector = EasyMock.createNiceMock(BrokerFailureDetector.class);
  GoalViolationDetector mockGoalViolationDetector = EasyMock.createNiceMock(GoalViolationDetector.class);
  ScheduledExecutorService mockDetectorScheduler = EasyMock.mock(ScheduledExecutorService.class);
  ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
  KafkaCruiseControl mockKafkaCruiseControl = EasyMock.mock(KafkaCruiseControl.class);

  EasyMock.expect(mockAnomalyNotifier.onBrokerFailure(EasyMock.isA(BrokerFailures.class)))
         .andReturn(AnomalyNotificationResult.check(1000L));

  // Starting periodic goal violation detection.
  EasyMock.expect(mockDetectorScheduler.scheduleAtFixedRate(EasyMock.eq(mockGoalViolationDetector),
                                                            EasyMock.anyLong(),
                                                            EasyMock.eq(3000L),
                                                            EasyMock.eq(TimeUnit.MILLISECONDS)))
          .andReturn(null);

  // Starting anomaly handler
  EasyMock.expect(mockDetectorScheduler.submit(EasyMock.isA(AnomalyDetector.AnomalyHandlerTask.class)))
          .andDelegateTo(executorService);
  // Schedule a delayed check
  EasyMock.expect(mockDetectorScheduler.schedule(EasyMock.isA(Runnable.class),
                                                 EasyMock.eq(1000L),
                                                 EasyMock.eq(TimeUnit.MILLISECONDS)))
          .andReturn(null);
  mockDetectorScheduler.shutdown();
  EasyMock.expectLastCall().andDelegateTo(executorService);
  EasyMock.expect(mockDetectorScheduler.awaitTermination(3000L, TimeUnit.MILLISECONDS)).andDelegateTo(executorService);
  EasyMock.expect(mockDetectorScheduler.isTerminated()).andDelegateTo(executorService);

  // The following state are used to test the delayed check when executor is busy.
  EasyMock.expect(mockKafkaCruiseControl.state())
          .andReturn(new KafkaCruiseControlState(ExecutorState.noTaskInProgress(), null, null));

  EasyMock.replay(mockAnomalyNotifier);
  EasyMock.replay(mockBrokerFailureDetector);
  EasyMock.replay(mockGoalViolationDetector);
  EasyMock.replay(mockDetectorScheduler);
  EasyMock.replay(mockKafkaCruiseControl);

  AnomalyDetector anomalyDetector = new AnomalyDetector(anomalies, 3000L, mockKafkaCruiseControl, mockAnomalyNotifier,
                                                        mockGoalViolationDetector, mockBrokerFailureDetector,
                                                        mockDetectorScheduler);

  try {
    anomalyDetector.startDetection();
    anomalies.add(new BrokerFailures(Collections.singletonMap(0, 100L)));
    while (!anomalies.isEmpty()) {
      // just wait for the anomalies to be drained.
    }
    anomalyDetector.shutdown();
    assertTrue(executorService.awaitTermination(5000, TimeUnit.MILLISECONDS));
    EasyMock.verify(mockAnomalyNotifier, mockDetectorScheduler, mockKafkaCruiseControl);
  } finally {
    executorService.shutdown();
  }
}
 
Example 8
Project: cruise-control   File: AnomalyDetectorTest.java   Source Code and License Vote up 4 votes
@Test
public void testFix() throws InterruptedException, KafkaCruiseControlException {
  LinkedBlockingDeque<Anomaly> anomalies = new LinkedBlockingDeque<>();
  AnomalyNotifier mockAnomalyNotifier = EasyMock.mock(AnomalyNotifier.class);
  BrokerFailureDetector mockBrokerFailureDetector = EasyMock.createNiceMock(BrokerFailureDetector.class);
  GoalViolationDetector mockGoalViolationDetector = EasyMock.createNiceMock(GoalViolationDetector.class);
  ScheduledExecutorService mockDetectorScheduler = EasyMock.mock(ScheduledExecutorService.class);
  ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
  KafkaCruiseControl mockKafkaCruiseControl = EasyMock.mock(KafkaCruiseControl.class);

  EasyMock.expect(mockAnomalyNotifier.onGoalViolation(EasyMock.isA(GoalViolations.class)))
          .andReturn(AnomalyNotificationResult.fix());

  // Starting periodic goal violation detection.
  EasyMock.expect(mockDetectorScheduler.scheduleAtFixedRate(EasyMock.eq(mockGoalViolationDetector),
                                                            EasyMock.anyLong(),
                                                            EasyMock.eq(3000L),
                                                            EasyMock.eq(TimeUnit.MILLISECONDS)))
          .andReturn(null);

  // Starting anomaly handler
  EasyMock.expect(mockDetectorScheduler.submit(EasyMock.isA(AnomalyDetector.AnomalyHandlerTask.class)))
          .andDelegateTo(executorService);

  mockDetectorScheduler.shutdown();
  EasyMock.expectLastCall().andDelegateTo(executorService);
  EasyMock.expect(mockDetectorScheduler.awaitTermination(3000L, TimeUnit.MILLISECONDS)).andDelegateTo(executorService);
  EasyMock.expect(mockDetectorScheduler.isTerminated()).andDelegateTo(executorService);

  // The following state are used to test the delayed check when executor is busy.
  EasyMock.expect(mockKafkaCruiseControl.state())
          .andReturn(new KafkaCruiseControlState(ExecutorState.noTaskInProgress(), null, null));
  EasyMock.expect(mockKafkaCruiseControl.rebalance(Collections.emptyList(), false, null))
          .andReturn(null);

  EasyMock.replay(mockAnomalyNotifier);
  EasyMock.replay(mockBrokerFailureDetector);
  EasyMock.replay(mockGoalViolationDetector);
  EasyMock.replay(mockDetectorScheduler);
  EasyMock.replay(mockKafkaCruiseControl);

  AnomalyDetector anomalyDetector = new AnomalyDetector(anomalies, 3000L, mockKafkaCruiseControl, mockAnomalyNotifier,
                                                        mockGoalViolationDetector, mockBrokerFailureDetector,
                                                        mockDetectorScheduler);

  try {
    anomalyDetector.startDetection();
    anomalies.add(new GoalViolations());
    while (!anomalies.isEmpty()) {
      // Just wait for the anomalies to be drained.
    }
    anomalyDetector.shutdown();
    assertTrue(executorService.awaitTermination(5000, TimeUnit.MILLISECONDS));
    EasyMock.verify(mockAnomalyNotifier, mockDetectorScheduler, mockKafkaCruiseControl);
  } finally {
    executorService.shutdown();
  }
}
 
Example 9
Project: cruise-control   File: AnomalyDetectorTest.java   Source Code and License Vote up 4 votes
@Test
public void testExecutionInProgress() throws InterruptedException, KafkaCruiseControlException {
  LinkedBlockingDeque<Anomaly> anomalies = new LinkedBlockingDeque<>();
  AnomalyNotifier mockAnomalyNotifier = EasyMock.mock(AnomalyNotifier.class);
  BrokerFailureDetector mockBrokerFailureDetector = EasyMock.createNiceMock(BrokerFailureDetector.class);
  GoalViolationDetector mockGoalViolationDetector = EasyMock.createNiceMock(GoalViolationDetector.class);
  ScheduledExecutorService mockDetectorScheduler = EasyMock.mock(ScheduledExecutorService.class);
  ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
  KafkaCruiseControl mockKafkaCruiseControl = EasyMock.mock(KafkaCruiseControl.class);

  // Starting periodic goal violation detection.
  EasyMock.expect(mockDetectorScheduler.scheduleAtFixedRate(EasyMock.eq(mockGoalViolationDetector),
                                                            EasyMock.anyLong(),
                                                            EasyMock.eq(3000L),
                                                            EasyMock.eq(TimeUnit.MILLISECONDS)))
          .andReturn(null);

  // Starting anomaly handler
  EasyMock.expect(mockDetectorScheduler.submit(EasyMock.isA(AnomalyDetector.AnomalyHandlerTask.class)))
          .andDelegateTo(executorService);
  // For detector shutdown.
  mockDetectorScheduler.shutdown();
  EasyMock.expectLastCall().andDelegateTo(executorService);
  EasyMock.expect(mockDetectorScheduler.awaitTermination(3000L, TimeUnit.MILLISECONDS)).andDelegateTo(executorService);
  EasyMock.expect(mockDetectorScheduler.isTerminated()).andDelegateTo(executorService);

  // The following state are used to test the delayed check when executor is busy.
  EasyMock.expect(mockKafkaCruiseControl.state())
          .andReturn(new KafkaCruiseControlState(
              ExecutorState.replicaMovementInProgress(1, Collections.emptySet(), Collections.emptySet(),
                                                      1, 1),
              null, null));

  EasyMock.replay(mockAnomalyNotifier);
  EasyMock.replay(mockBrokerFailureDetector);
  EasyMock.replay(mockGoalViolationDetector);
  EasyMock.replay(mockDetectorScheduler);
  EasyMock.replay(mockKafkaCruiseControl);

  AnomalyDetector anomalyDetector = new AnomalyDetector(anomalies, 3000L, mockKafkaCruiseControl, mockAnomalyNotifier,
                                                        mockGoalViolationDetector, mockBrokerFailureDetector,
                                                        mockDetectorScheduler);

  try {
    anomalyDetector.startDetection();
    anomalies.add(new GoalViolations());
    while (!anomalies.isEmpty()) {
      // Just wait for the anomalies to be drained.
    }
    anomalyDetector.shutdown();
    assertTrue(executorService.awaitTermination(5000, TimeUnit.MILLISECONDS));

    EasyMock.verify(mockAnomalyNotifier, mockDetectorScheduler, mockKafkaCruiseControl);
  } finally {
    executorService.shutdown();
  }
}
 
Example 10
Project: cruise-control   File: LoadMonitorTest.java   Source Code and License Vote up 4 votes
private TestContext prepareContext() {
  // Create mock metadata client.
  Metadata metadata = getMetadata(Arrays.asList(T0P0, T0P1, T1P0, T1P1));
  MetadataClient mockMetadataClient = EasyMock.mock(MetadataClient.class);
  EasyMock.expect(mockMetadataClient.cluster())
          .andReturn(metadata.fetch())
          .anyTimes();
  EasyMock.expect(mockMetadataClient.clusterAndGeneration())
          .andReturn(new MetadataClient.ClusterAndGeneration(metadata.fetch(), 0))
          .anyTimes();
  EasyMock.expect(mockMetadataClient.metadata()).andReturn(metadata).anyTimes();
  EasyMock.expect(mockMetadataClient.refreshMetadata())
          .andReturn(new MetadataClient.ClusterAndGeneration(metadata.fetch(), 0))
          .anyTimes();
  EasyMock.expect(mockMetadataClient.refreshMetadata(anyLong()))
          .andReturn(new MetadataClient.ClusterAndGeneration(metadata.fetch(), 0))
          .anyTimes();
  EasyMock.replay(mockMetadataClient);

  // create load monitor.
  Properties props = CruiseControlUnitTestUtils.getCruiseControlProperties();
  props.put(KafkaCruiseControlConfig.NUM_LOAD_SNAPSHOTS_CONFIG, Integer.toString(NUM_SNAPSHOT_WINDOWS));
  props.put(KafkaCruiseControlConfig.MIN_SAMPLES_PER_LOAD_SNAPSHOT_CONFIG,
            Integer.toString(MIN_SAMPLES_PER_SNAPSHOT_WINDOW));
  props.put(KafkaCruiseControlConfig.LOAD_SNAPSHOT_WINDOW_MS_CONFIG, Long.toString(SNAPSHOT_WINDOW_MS));
  props.put("cleanup.policy", DEFAULT_CLEANUP_POLICY);
  props.put(KafkaCruiseControlConfig.SAMPLE_STORE_CLASS_CONFIG, NoopSampleStore.class.getName());
  KafkaCruiseControlConfig config = new KafkaCruiseControlConfig(props);
  LoadMonitor loadMonitor = new LoadMonitor(config, mockMetadataClient, _time, new MetricRegistry());

  MetricSampleAggregator aggregator = loadMonitor.aggregator();
  MetricCompletenessChecker checker = loadMonitor.completenessChecker();

  if (!Load.initialized()) {
    Load.init(config);
  }
  ModelParameters.init(config);
  loadMonitor.startUp();
  while (loadMonitor.state().state() != LoadMonitorTaskRunner.LoadMonitorTaskRunnerState.RUNNING) {
    try {
      Thread.sleep(1);
    } catch (InterruptedException e) {
      // let it go.
    }
  }

  return new TestContext(loadMonitor, aggregator, checker, config, metadata);
}