Best Easymock code snippet using org.easymock.EasyMock.expectLastCall
Source:DistributedHerderTest.java
...163 expectRebalance(1, Arrays.asList(CONN1), Arrays.asList(TASK1));164 expectPostRebalanceCatchup(SNAPSHOT);165 worker.startConnector(EasyMock.eq(CONN1), EasyMock.<Map<String, String>>anyObject(), EasyMock.<ConnectorContext>anyObject(),166 EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED));167 PowerMock.expectLastCall().andReturn(true);168 EasyMock.expect(worker.isRunning(CONN1)).andReturn(true);169 EasyMock.expect(worker.connectorTaskConfigs(CONN1, MAX_TASKS, null)).andReturn(TASK_CONFIGS);170 worker.startTask(EasyMock.eq(TASK1), EasyMock.<Map<String, String>>anyObject(), EasyMock.<Map<String, String>>anyObject(),171 EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED));172 PowerMock.expectLastCall().andReturn(true);173 member.poll(EasyMock.anyInt());174 PowerMock.expectLastCall();175 PowerMock.replayAll();176 herder.tick();177 PowerMock.verifyAll();178 }179 @Test180 public void testRebalance() throws Exception {181 // Join group and get assignment182 EasyMock.expect(member.memberId()).andStubReturn("member");183 expectRebalance(1, Arrays.asList(CONN1), Arrays.asList(TASK1));184 expectPostRebalanceCatchup(SNAPSHOT);185 worker.startConnector(EasyMock.eq(CONN1), EasyMock.<Map<String, String>>anyObject(), EasyMock.<ConnectorContext>anyObject(),186 EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED));187 PowerMock.expectLastCall().andReturn(true);188 EasyMock.expect(worker.isRunning(CONN1)).andReturn(true);189 EasyMock.expect(worker.connectorTaskConfigs(CONN1, MAX_TASKS, null)).andReturn(TASK_CONFIGS);190 worker.startTask(EasyMock.eq(TASK1), EasyMock.<Map<String, String>>anyObject(), EasyMock.<Map<String, String>>anyObject(),191 EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED));192 PowerMock.expectLastCall().andReturn(true);193 member.poll(EasyMock.anyInt());194 PowerMock.expectLastCall();195 expectRebalance(Arrays.asList(CONN1), Arrays.asList(TASK1), ConnectProtocol.Assignment.NO_ERROR,196 1, Arrays.asList(CONN1), Arrays.<ConnectorTaskId>asList());197 // and the new assignment started198 worker.startConnector(EasyMock.eq(CONN1), EasyMock.<Map<String, String>>anyObject(), EasyMock.<ConnectorContext>anyObject(),199 EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED));200 PowerMock.expectLastCall().andReturn(true);201 EasyMock.expect(worker.isRunning(CONN1)).andReturn(true);202 EasyMock.expect(worker.connectorTaskConfigs(CONN1, MAX_TASKS, null)).andReturn(TASK_CONFIGS);203 member.poll(EasyMock.anyInt());204 PowerMock.expectLastCall();205 PowerMock.replayAll();206 herder.tick();207 herder.tick();208 PowerMock.verifyAll();209 }210 @Test211 public void testRebalanceFailedConnector() throws Exception {212 // Join group and get assignment213 EasyMock.expect(member.memberId()).andStubReturn("member");214 expectRebalance(1, Arrays.asList(CONN1), Arrays.asList(TASK1));215 expectPostRebalanceCatchup(SNAPSHOT);216 worker.startConnector(EasyMock.eq(CONN1), EasyMock.<Map<String, String>>anyObject(), EasyMock.<ConnectorContext>anyObject(),217 EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED));218 PowerMock.expectLastCall().andReturn(true);219 EasyMock.expect(worker.isRunning(CONN1)).andReturn(true);220 EasyMock.expect(worker.connectorTaskConfigs(CONN1, MAX_TASKS, null)).andReturn(TASK_CONFIGS);221 worker.startTask(EasyMock.eq(TASK1), EasyMock.<Map<String, String>>anyObject(), EasyMock.<Map<String, String>>anyObject(),222 EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED));223 PowerMock.expectLastCall().andReturn(true);224 member.poll(EasyMock.anyInt());225 PowerMock.expectLastCall();226 expectRebalance(Arrays.asList(CONN1), Arrays.asList(TASK1), ConnectProtocol.Assignment.NO_ERROR,227 1, Arrays.asList(CONN1), Arrays.<ConnectorTaskId>asList());228 // and the new assignment started229 worker.startConnector(EasyMock.eq(CONN1), EasyMock.<Map<String, String>>anyObject(), EasyMock.<ConnectorContext>anyObject(),230 EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED));231 PowerMock.expectLastCall().andReturn(true);232 EasyMock.expect(worker.isRunning(CONN1)).andReturn(false);233 // worker is not running, so we should see no call to connectorTaskConfigs()234 member.poll(EasyMock.anyInt());235 PowerMock.expectLastCall();236 PowerMock.replayAll();237 herder.tick();238 herder.tick();239 PowerMock.verifyAll();240 }241 @Test242 public void testHaltCleansUpWorker() {243 EasyMock.expect(worker.connectorNames()).andReturn(Collections.singleton(CONN1));244 worker.stopConnector(CONN1);245 PowerMock.expectLastCall().andReturn(true);246 EasyMock.expect(worker.taskIds()).andReturn(Collections.singleton(TASK1));247 worker.stopAndAwaitTask(TASK1);248 PowerMock.expectLastCall();249 member.stop();250 PowerMock.expectLastCall();251 configBackingStore.stop();252 PowerMock.expectLastCall();253 statusBackingStore.stop();254 PowerMock.expectLastCall();255 worker.stop();256 PowerMock.expectLastCall();257 PowerMock.replayAll();258 herder.halt();259 PowerMock.verifyAll();260 }261 @Test262 public void testCreateConnector() throws Exception {263 EasyMock.expect(member.memberId()).andStubReturn("leader");264 expectRebalance(1, Collections.<String>emptyList(), Collections.<ConnectorTaskId>emptyList());265 expectPostRebalanceCatchup(SNAPSHOT);266 member.wakeup();267 PowerMock.expectLastCall();268 // config validation269 ConnectorFactory connectorFactoryMock = PowerMock.createMock(ConnectorFactory.class);270 EasyMock.expect(worker.getConnectorFactory()).andStubReturn(connectorFactoryMock);271 Connector connectorMock = PowerMock.createMock(Connector.class);272 EasyMock.expect(connectorFactoryMock.newConnector(EasyMock.anyString())).andReturn(connectorMock);273 EasyMock.expect(connectorMock.config()).andReturn(new ConfigDef());274 EasyMock.expect(connectorMock.validate(CONN2_CONFIG)).andReturn(new Config(Collections.<ConfigValue>emptyList()));275 // CONN2 is new, should succeed276 configBackingStore.putConnectorConfig(CONN2, CONN2_CONFIG);277 PowerMock.expectLastCall();278 ConnectorInfo info = new ConnectorInfo(CONN2, CONN2_CONFIG, Collections.<ConnectorTaskId>emptyList());279 putConnectorCallback.onCompletion(null, new Herder.Created<>(true, info));280 PowerMock.expectLastCall();281 member.poll(EasyMock.anyInt());282 PowerMock.expectLastCall();283 // No immediate action besides this -- change will be picked up via the config log284 PowerMock.replayAll();285 herder.putConnectorConfig(CONN2, CONN2_CONFIG, false, putConnectorCallback);286 herder.tick();287 PowerMock.verifyAll();288 }289 @Test290 public void testCreateConnectorFailedBasicValidation() throws Exception {291 EasyMock.expect(member.memberId()).andStubReturn("leader");292 expectRebalance(1, Collections.<String>emptyList(), Collections.<ConnectorTaskId>emptyList());293 expectPostRebalanceCatchup(SNAPSHOT);294 HashMap<String, String> config = new HashMap<>(CONN2_CONFIG);295 config.remove(ConnectorConfig.NAME_CONFIG);296 member.wakeup();297 PowerMock.expectLastCall();298 // config validation299 ConnectorFactory connectorFactoryMock = PowerMock.createMock(ConnectorFactory.class);300 EasyMock.expect(worker.getConnectorFactory()).andStubReturn(connectorFactoryMock);301 Connector connectorMock = PowerMock.createMock(Connector.class);302 EasyMock.expect(connectorFactoryMock.newConnector(EasyMock.anyString())).andReturn(connectorMock);303 EasyMock.expect(connectorMock.config()).andReturn(new ConfigDef());304 EasyMock.expect(connectorMock.validate(config)).andReturn(new Config(Collections.<ConfigValue>emptyList()));305 // CONN2 creation should fail306 Capture<Throwable> error = EasyMock.newCapture();307 putConnectorCallback.onCompletion(EasyMock.capture(error), EasyMock.<Herder.Created<ConnectorInfo>>isNull());308 PowerMock.expectLastCall();309 member.poll(EasyMock.anyInt());310 PowerMock.expectLastCall();311 // No immediate action besides this -- change will be picked up via the config log312 PowerMock.replayAll();313 herder.putConnectorConfig(CONN2, config, false, putConnectorCallback);314 herder.tick();315 assertTrue(error.hasCaptured());316 assertTrue(error.getValue() instanceof BadRequestException);317 PowerMock.verifyAll();318 }319 @Test320 public void testCreateConnectorFailedCustomValidation() throws Exception {321 EasyMock.expect(member.memberId()).andStubReturn("leader");322 expectRebalance(1, Collections.<String>emptyList(), Collections.<ConnectorTaskId>emptyList());323 expectPostRebalanceCatchup(SNAPSHOT);324 member.wakeup();325 PowerMock.expectLastCall();326 // config validation327 ConnectorFactory connectorFactoryMock = PowerMock.createMock(ConnectorFactory.class);328 EasyMock.expect(worker.getConnectorFactory()).andStubReturn(connectorFactoryMock);329 Connector connectorMock = PowerMock.createMock(Connector.class);330 EasyMock.expect(connectorFactoryMock.newConnector(EasyMock.anyString())).andReturn(connectorMock);331 ConfigDef configDef = new ConfigDef();332 configDef.define("foo.bar", ConfigDef.Type.STRING, ConfigDef.Importance.HIGH, "foo.bar doc");333 EasyMock.expect(connectorMock.config()).andReturn(configDef);334 ConfigValue validatedValue = new ConfigValue("foo.bar");335 validatedValue.addErrorMessage("Failed foo.bar validation");336 EasyMock.expect(connectorMock.validate(CONN2_CONFIG)).andReturn(new Config(singletonList(validatedValue)));337 // CONN2 creation should fail338 Capture<Throwable> error = EasyMock.newCapture();339 putConnectorCallback.onCompletion(EasyMock.capture(error), EasyMock.<Herder.Created<ConnectorInfo>>isNull());340 PowerMock.expectLastCall();341 member.poll(EasyMock.anyInt());342 PowerMock.expectLastCall();343 // No immediate action besides this -- change will be picked up via the config log344 PowerMock.replayAll();345 herder.putConnectorConfig(CONN2, CONN2_CONFIG, false, putConnectorCallback);346 herder.tick();347 assertTrue(error.hasCaptured());348 assertTrue(error.getValue() instanceof BadRequestException);349 PowerMock.verifyAll();350 }351 @Test352 public void testConnectorNameConflictsWithWorkerGroupId() throws Exception {353 EasyMock.expect(member.memberId()).andStubReturn("leader");354 expectRebalance(1, Collections.<String>emptyList(), Collections.<ConnectorTaskId>emptyList());355 expectPostRebalanceCatchup(SNAPSHOT);356 member.wakeup();357 PowerMock.expectLastCall();358 Map<String, String> config = new HashMap<>(CONN2_CONFIG);359 config.put(ConnectorConfig.NAME_CONFIG, "test-group");360 // config validation361 ConnectorFactory connectorFactoryMock = PowerMock.createMock(ConnectorFactory.class);362 EasyMock.expect(worker.getConnectorFactory()).andStubReturn(connectorFactoryMock);363 Connector connectorMock = PowerMock.createMock(SinkConnector.class);364 EasyMock.expect(connectorFactoryMock.newConnector(EasyMock.anyString())).andReturn(connectorMock);365 EasyMock.expect(connectorMock.config()).andReturn(new ConfigDef());366 EasyMock.expect(connectorMock.validate(config)).andReturn(new Config(Collections.<ConfigValue>emptyList()));367 // CONN2 creation should fail because the worker group id (connect-test-group) conflicts with368 // the consumer group id we would use for this sink369 Capture<Throwable> error = EasyMock.newCapture();370 putConnectorCallback.onCompletion(EasyMock.capture(error), EasyMock.isNull(Herder.Created.class));371 PowerMock.expectLastCall();372 member.poll(EasyMock.anyInt());373 PowerMock.expectLastCall();374 // No immediate action besides this -- change will be picked up via the config log375 PowerMock.replayAll();376 herder.putConnectorConfig(CONN2, config, false, putConnectorCallback);377 herder.tick();378 assertTrue(error.hasCaptured());379 assertTrue(error.getValue() instanceof BadRequestException);380 PowerMock.verifyAll();381 }382 @Test383 public void testCreateConnectorAlreadyExists() throws Exception {384 EasyMock.expect(member.memberId()).andStubReturn("leader");385 expectRebalance(1, Collections.<String>emptyList(), Collections.<ConnectorTaskId>emptyList());386 expectPostRebalanceCatchup(SNAPSHOT);387 member.wakeup();388 PowerMock.expectLastCall();389 // CONN1 already exists390 putConnectorCallback.onCompletion(EasyMock.<AlreadyExistsException>anyObject(), EasyMock.<Herder.Created<ConnectorInfo>>isNull());391 PowerMock.expectLastCall();392 member.poll(EasyMock.anyInt());393 PowerMock.expectLastCall();394 // No immediate action besides this -- change will be picked up via the config log395 PowerMock.replayAll();396 herder.putConnectorConfig(CONN1, CONN1_CONFIG, false, putConnectorCallback);397 herder.tick();398 PowerMock.verifyAll();399 }400 @Test401 public void testDestroyConnector() throws Exception {402 EasyMock.expect(member.memberId()).andStubReturn("leader");403 // Start with one connector404 expectRebalance(1, Arrays.asList(CONN1), Collections.<ConnectorTaskId>emptyList());405 expectPostRebalanceCatchup(SNAPSHOT);406 worker.startConnector(EasyMock.eq(CONN1), EasyMock.<Map<String, String>>anyObject(), EasyMock.<ConnectorContext>anyObject(),407 EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED));408 PowerMock.expectLastCall().andReturn(true);409 EasyMock.expect(worker.isRunning(CONN1)).andReturn(true);410 EasyMock.expect(worker.connectorTaskConfigs(CONN1, MAX_TASKS, null)).andReturn(TASK_CONFIGS);411 // And delete the connector412 member.wakeup();413 PowerMock.expectLastCall();414 configBackingStore.removeConnectorConfig(CONN1);415 PowerMock.expectLastCall();416 putConnectorCallback.onCompletion(null, new Herder.Created<ConnectorInfo>(false, null));417 PowerMock.expectLastCall();418 member.poll(EasyMock.anyInt());419 PowerMock.expectLastCall();420 // No immediate action besides this -- change will be picked up via the config log421 PowerMock.replayAll();422 herder.deleteConnectorConfig(CONN1, putConnectorCallback);423 herder.tick();424 PowerMock.verifyAll();425 }426 @Test427 public void testRestartConnector() throws Exception {428 EasyMock.expect(worker.connectorTaskConfigs(CONN1, MAX_TASKS, null)).andStubReturn(TASK_CONFIGS);429 // get the initial assignment430 EasyMock.expect(member.memberId()).andStubReturn("leader");431 expectRebalance(1, singletonList(CONN1), Collections.<ConnectorTaskId>emptyList());432 expectPostRebalanceCatchup(SNAPSHOT);433 member.poll(EasyMock.anyInt());434 PowerMock.expectLastCall();435 worker.startConnector(EasyMock.eq(CONN1), EasyMock.<Map<String, String>>anyObject(), EasyMock.<ConnectorContext>anyObject(),436 EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED));437 PowerMock.expectLastCall().andReturn(true);438 EasyMock.expect(worker.isRunning(CONN1)).andReturn(true);439 // now handle the connector restart440 member.wakeup();441 PowerMock.expectLastCall();442 member.ensureActive();443 PowerMock.expectLastCall();444 member.poll(EasyMock.anyInt());445 PowerMock.expectLastCall();446 worker.stopConnector(CONN1);447 PowerMock.expectLastCall().andReturn(true);448 worker.startConnector(EasyMock.eq(CONN1), EasyMock.<Map<String, String>>anyObject(), EasyMock.<ConnectorContext>anyObject(),449 EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED));450 PowerMock.expectLastCall().andReturn(true);451 EasyMock.expect(worker.isRunning(CONN1)).andReturn(true);452 PowerMock.replayAll();453 herder.tick();454 FutureCallback<Void> callback = new FutureCallback<>();455 herder.restartConnector(CONN1, callback);456 herder.tick();457 callback.get(1000L, TimeUnit.MILLISECONDS);458 PowerMock.verifyAll();459 }460 @Test461 public void testRestartUnknownConnector() throws Exception {462 // get the initial assignment463 EasyMock.expect(member.memberId()).andStubReturn("leader");464 expectRebalance(1, Collections.<String>emptyList(), Collections.<ConnectorTaskId>emptyList());465 expectPostRebalanceCatchup(SNAPSHOT);466 member.poll(EasyMock.anyInt());467 PowerMock.expectLastCall();468 // now handle the connector restart469 member.wakeup();470 PowerMock.expectLastCall();471 member.ensureActive();472 PowerMock.expectLastCall();473 member.poll(EasyMock.anyInt());474 PowerMock.expectLastCall();475 PowerMock.replayAll();476 herder.tick();477 FutureCallback<Void> callback = new FutureCallback<>();478 herder.restartConnector(CONN2, callback);479 herder.tick();480 try {481 callback.get(1000L, TimeUnit.MILLISECONDS);482 fail("Expected NotLeaderException to be raised");483 } catch (ExecutionException e) {484 assertTrue(e.getCause() instanceof NotFoundException);485 }486 PowerMock.verifyAll();487 }488 @Test489 public void testRestartConnectorRedirectToLeader() throws Exception {490 // get the initial assignment491 EasyMock.expect(member.memberId()).andStubReturn("member");492 expectRebalance(1, Collections.<String>emptyList(), Collections.<ConnectorTaskId>emptyList());493 expectPostRebalanceCatchup(SNAPSHOT);494 member.poll(EasyMock.anyInt());495 PowerMock.expectLastCall();496 // now handle the connector restart497 member.wakeup();498 PowerMock.expectLastCall();499 member.ensureActive();500 PowerMock.expectLastCall();501 member.poll(EasyMock.anyInt());502 PowerMock.expectLastCall();503 PowerMock.replayAll();504 herder.tick();505 FutureCallback<Void> callback = new FutureCallback<>();506 herder.restartConnector(CONN1, callback);507 herder.tick();508 try {509 callback.get(1000L, TimeUnit.MILLISECONDS);510 fail("Expected NotLeaderException to be raised");511 } catch (ExecutionException e) {512 assertTrue(e.getCause() instanceof NotLeaderException);513 }514 PowerMock.verifyAll();515 }516 @Test517 public void testRestartConnectorRedirectToOwner() throws Exception {518 // get the initial assignment519 EasyMock.expect(member.memberId()).andStubReturn("leader");520 expectRebalance(1, Collections.<String>emptyList(), Collections.<ConnectorTaskId>emptyList());521 expectPostRebalanceCatchup(SNAPSHOT);522 member.poll(EasyMock.anyInt());523 PowerMock.expectLastCall();524 // now handle the connector restart525 member.wakeup();526 PowerMock.expectLastCall();527 member.ensureActive();528 PowerMock.expectLastCall();529 member.poll(EasyMock.anyInt());530 PowerMock.expectLastCall();531 String ownerUrl = "ownerUrl";532 EasyMock.expect(member.ownerUrl(CONN1)).andReturn(ownerUrl);533 PowerMock.replayAll();534 herder.tick();535 FutureCallback<Void> callback = new FutureCallback<>();536 herder.restartConnector(CONN1, callback);537 herder.tick();538 try {539 callback.get(1000L, TimeUnit.MILLISECONDS);540 fail("Expected NotLeaderException to be raised");541 } catch (ExecutionException e) {542 assertTrue(e.getCause() instanceof NotAssignedException);543 NotAssignedException notAssignedException = (NotAssignedException) e.getCause();544 assertEquals(ownerUrl, notAssignedException.forwardUrl());545 }546 PowerMock.verifyAll();547 }548 @Test549 public void testRestartTask() throws Exception {550 EasyMock.expect(worker.connectorTaskConfigs(CONN1, MAX_TASKS, null)).andStubReturn(TASK_CONFIGS);551 // get the initial assignment552 EasyMock.expect(member.memberId()).andStubReturn("leader");553 expectRebalance(1, Collections.<String>emptyList(), singletonList(TASK0));554 expectPostRebalanceCatchup(SNAPSHOT);555 member.poll(EasyMock.anyInt());556 PowerMock.expectLastCall();557 worker.startTask(EasyMock.eq(TASK0), EasyMock.<Map<String, String>>anyObject(), EasyMock.<Map<String, String>>anyObject(),558 EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED));559 PowerMock.expectLastCall().andReturn(true);560 // now handle the task restart561 member.wakeup();562 PowerMock.expectLastCall();563 member.ensureActive();564 PowerMock.expectLastCall();565 member.poll(EasyMock.anyInt());566 PowerMock.expectLastCall();567 worker.stopAndAwaitTask(TASK0);568 PowerMock.expectLastCall();569 worker.startTask(EasyMock.eq(TASK0), EasyMock.<Map<String, String>>anyObject(), EasyMock.<Map<String, String>>anyObject(),570 EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED));571 PowerMock.expectLastCall().andReturn(true);572 PowerMock.replayAll();573 herder.tick();574 FutureCallback<Void> callback = new FutureCallback<>();575 herder.restartTask(TASK0, callback);576 herder.tick();577 callback.get(1000L, TimeUnit.MILLISECONDS);578 PowerMock.verifyAll();579 }580 @Test581 public void testRestartUnknownTask() throws Exception {582 // get the initial assignment583 EasyMock.expect(member.memberId()).andStubReturn("member");584 expectRebalance(1, Collections.<String>emptyList(), Collections.<ConnectorTaskId>emptyList());585 expectPostRebalanceCatchup(SNAPSHOT);586 member.poll(EasyMock.anyInt());587 PowerMock.expectLastCall();588 member.wakeup();589 PowerMock.expectLastCall();590 member.ensureActive();591 PowerMock.expectLastCall();592 member.poll(EasyMock.anyInt());593 PowerMock.expectLastCall();594 PowerMock.replayAll();595 FutureCallback<Void> callback = new FutureCallback<>();596 herder.tick();597 herder.restartTask(new ConnectorTaskId("blah", 0), callback);598 herder.tick();599 try {600 callback.get(1000L, TimeUnit.MILLISECONDS);601 fail("Expected NotLeaderException to be raised");602 } catch (ExecutionException e) {603 assertTrue(e.getCause() instanceof NotFoundException);604 }605 PowerMock.verifyAll();606 }607 @Test608 public void testRequestProcessingOrder() throws Exception {609 final DistributedHerder.HerderRequest req1 = herder.addRequest(100, null, null);610 final DistributedHerder.HerderRequest req2 = herder.addRequest(10, null, null);611 final DistributedHerder.HerderRequest req3 = herder.addRequest(200, null, null);612 final DistributedHerder.HerderRequest req4 = herder.addRequest(200, null, null);613 assertEquals(req2, herder.requests.pollFirst()); // lowest delay614 assertEquals(req1, herder.requests.pollFirst()); // next lowest delay615 assertEquals(req3, herder.requests.pollFirst()); // same delay as req4, but added first616 assertEquals(req4, herder.requests.pollFirst());617 }618 @Test619 public void testRestartTaskRedirectToLeader() throws Exception {620 // get the initial assignment621 EasyMock.expect(member.memberId()).andStubReturn("member");622 expectRebalance(1, Collections.<String>emptyList(), Collections.<ConnectorTaskId>emptyList());623 expectPostRebalanceCatchup(SNAPSHOT);624 member.poll(EasyMock.anyInt());625 PowerMock.expectLastCall();626 // now handle the task restart627 member.wakeup();628 PowerMock.expectLastCall();629 member.ensureActive();630 PowerMock.expectLastCall();631 member.poll(EasyMock.anyInt());632 PowerMock.expectLastCall();633 PowerMock.replayAll();634 herder.tick();635 FutureCallback<Void> callback = new FutureCallback<>();636 herder.restartTask(TASK0, callback);637 herder.tick();638 try {639 callback.get(1000L, TimeUnit.MILLISECONDS);640 fail("Expected NotLeaderException to be raised");641 } catch (ExecutionException e) {642 assertTrue(e.getCause() instanceof NotLeaderException);643 }644 PowerMock.verifyAll();645 }646 @Test647 public void testRestartTaskRedirectToOwner() throws Exception {648 // get the initial assignment649 EasyMock.expect(member.memberId()).andStubReturn("leader");650 expectRebalance(1, Collections.<String>emptyList(), Collections.<ConnectorTaskId>emptyList());651 expectPostRebalanceCatchup(SNAPSHOT);652 member.poll(EasyMock.anyInt());653 PowerMock.expectLastCall();654 // now handle the task restart655 String ownerUrl = "ownerUrl";656 EasyMock.expect(member.ownerUrl(TASK0)).andReturn(ownerUrl);657 member.wakeup();658 PowerMock.expectLastCall();659 member.ensureActive();660 PowerMock.expectLastCall();661 member.poll(EasyMock.anyInt());662 PowerMock.expectLastCall();663 PowerMock.replayAll();664 herder.tick();665 FutureCallback<Void> callback = new FutureCallback<>();666 herder.restartTask(TASK0, callback);667 herder.tick();668 try {669 callback.get(1000L, TimeUnit.MILLISECONDS);670 fail("Expected NotLeaderException to be raised");671 } catch (ExecutionException e) {672 assertTrue(e.getCause() instanceof NotAssignedException);673 NotAssignedException notAssignedException = (NotAssignedException) e.getCause();674 assertEquals(ownerUrl, notAssignedException.forwardUrl());675 }676 PowerMock.verifyAll();677 }678 @Test679 public void testConnectorConfigAdded() {680 // If a connector was added, we need to rebalance681 EasyMock.expect(member.memberId()).andStubReturn("member");682 // join, no configs so no need to catch up on config topic683 expectRebalance(-1, Collections.<String>emptyList(), Collections.<ConnectorTaskId>emptyList());684 member.poll(EasyMock.anyInt());685 PowerMock.expectLastCall();686 // apply config687 member.wakeup();688 member.ensureActive();689 PowerMock.expectLastCall();690 // Checks for config updates and starts rebalance691 EasyMock.expect(configBackingStore.snapshot()).andReturn(SNAPSHOT);692 member.requestRejoin();693 PowerMock.expectLastCall();694 // Performs rebalance and gets new assignment695 expectRebalance(Collections.<String>emptyList(), Collections.<ConnectorTaskId>emptyList(),696 ConnectProtocol.Assignment.NO_ERROR, 1, Arrays.asList(CONN1), Collections.<ConnectorTaskId>emptyList());697 worker.startConnector(EasyMock.eq(CONN1), EasyMock.<Map<String, String>>anyObject(), EasyMock.<ConnectorContext>anyObject(),698 EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED));699 PowerMock.expectLastCall().andReturn(true);700 EasyMock.expect(worker.isRunning(CONN1)).andReturn(true);701 EasyMock.expect(worker.connectorTaskConfigs(CONN1, MAX_TASKS, null)).andReturn(TASK_CONFIGS);702 member.poll(EasyMock.anyInt());703 PowerMock.expectLastCall();704 PowerMock.replayAll();705 herder.tick(); // join706 configUpdateListener.onConnectorConfigUpdate(CONN1); // read updated config707 herder.tick(); // apply config708 herder.tick(); // do rebalance709 PowerMock.verifyAll();710 }711 @Test712 public void testConnectorConfigUpdate() throws Exception {713 // Connector config can be applied without any rebalance714 EasyMock.expect(member.memberId()).andStubReturn("member");715 EasyMock.expect(worker.connectorNames()).andStubReturn(Collections.singleton(CONN1));716 // join717 expectRebalance(1, Arrays.asList(CONN1), Collections.<ConnectorTaskId>emptyList());718 expectPostRebalanceCatchup(SNAPSHOT);719 worker.startConnector(EasyMock.eq(CONN1), EasyMock.<Map<String, String>>anyObject(), EasyMock.<ConnectorContext>anyObject(),720 EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED));721 PowerMock.expectLastCall().andReturn(true);722 EasyMock.expect(worker.isRunning(CONN1)).andReturn(true);723 EasyMock.expect(worker.connectorTaskConfigs(CONN1, MAX_TASKS, null)).andReturn(TASK_CONFIGS);724 member.poll(EasyMock.anyInt());725 PowerMock.expectLastCall();726 // apply config727 member.wakeup();728 member.ensureActive();729 PowerMock.expectLastCall();730 EasyMock.expect(configBackingStore.snapshot()).andReturn(SNAPSHOT); // for this test, it doesn't matter if we use the same config snapshot731 worker.stopConnector(CONN1);732 PowerMock.expectLastCall().andReturn(true);733 worker.startConnector(EasyMock.eq(CONN1), EasyMock.<Map<String, String>>anyObject(), EasyMock.<ConnectorContext>anyObject(),734 EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED));735 PowerMock.expectLastCall().andReturn(true);736 EasyMock.expect(worker.isRunning(CONN1)).andReturn(true);737 EasyMock.expect(worker.connectorTaskConfigs(CONN1, MAX_TASKS, null)).andReturn(TASK_CONFIGS);738 member.poll(EasyMock.anyInt());739 PowerMock.expectLastCall();740 PowerMock.replayAll();741 herder.tick(); // join742 configUpdateListener.onConnectorConfigUpdate(CONN1); // read updated config743 herder.tick(); // apply config744 PowerMock.verifyAll();745 }746 @Test747 public void testConnectorPaused() throws Exception {748 // ensure that target state changes are propagated to the worker749 EasyMock.expect(member.memberId()).andStubReturn("member");750 EasyMock.expect(worker.connectorNames()).andStubReturn(Collections.singleton(CONN1));751 // join752 expectRebalance(1, Arrays.asList(CONN1), Collections.<ConnectorTaskId>emptyList());753 expectPostRebalanceCatchup(SNAPSHOT);754 worker.startConnector(EasyMock.eq(CONN1), EasyMock.<Map<String, String>>anyObject(), EasyMock.<ConnectorContext>anyObject(),755 EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED));756 PowerMock.expectLastCall().andReturn(true);757 EasyMock.expect(worker.isRunning(CONN1)).andReturn(true);758 EasyMock.expect(worker.connectorTaskConfigs(CONN1, MAX_TASKS, null)).andReturn(TASK_CONFIGS);759 member.poll(EasyMock.anyInt());760 PowerMock.expectLastCall();761 // handle the state change762 member.wakeup();763 member.ensureActive();764 PowerMock.expectLastCall();765 EasyMock.expect(configBackingStore.snapshot()).andReturn(SNAPSHOT_PAUSED_CONN1);766 PowerMock.expectLastCall();767 worker.setTargetState(CONN1, TargetState.PAUSED);768 PowerMock.expectLastCall();769 member.poll(EasyMock.anyInt());770 PowerMock.expectLastCall();771 PowerMock.replayAll();772 herder.tick(); // join773 configUpdateListener.onConnectorTargetStateChange(CONN1); // state changes to paused774 herder.tick(); // worker should apply the state change775 PowerMock.verifyAll();776 }777 @Test778 public void testConnectorResumed() throws Exception {779 EasyMock.expect(member.memberId()).andStubReturn("member");780 EasyMock.expect(worker.connectorNames()).andStubReturn(Collections.singleton(CONN1));781 // start with the connector paused782 expectRebalance(1, Arrays.asList(CONN1), Collections.<ConnectorTaskId>emptyList());783 expectPostRebalanceCatchup(SNAPSHOT_PAUSED_CONN1);784 worker.startConnector(EasyMock.eq(CONN1), EasyMock.<Map<String, String>>anyObject(), EasyMock.<ConnectorContext>anyObject(),785 EasyMock.eq(herder), EasyMock.eq(TargetState.PAUSED));786 PowerMock.expectLastCall().andReturn(true);787 member.poll(EasyMock.anyInt());788 PowerMock.expectLastCall();789 // handle the state change790 member.wakeup();791 member.ensureActive();792 PowerMock.expectLastCall();793 EasyMock.expect(configBackingStore.snapshot()).andReturn(SNAPSHOT);794 PowerMock.expectLastCall();795 // we expect reconfiguration after resuming796 EasyMock.expect(worker.isRunning(CONN1)).andReturn(true);797 EasyMock.expect(worker.connectorTaskConfigs(CONN1, MAX_TASKS, null)).andReturn(TASK_CONFIGS);798 worker.setTargetState(CONN1, TargetState.STARTED);799 PowerMock.expectLastCall();800 member.poll(EasyMock.anyInt());801 PowerMock.expectLastCall();802 PowerMock.replayAll();803 herder.tick(); // join804 configUpdateListener.onConnectorTargetStateChange(CONN1); // state changes to started805 herder.tick(); // apply state change806 PowerMock.verifyAll();807 }808 @Test809 public void testUnknownConnectorPaused() throws Exception {810 EasyMock.expect(member.memberId()).andStubReturn("member");811 EasyMock.expect(worker.connectorNames()).andStubReturn(Collections.singleton(CONN1));812 // join813 expectRebalance(1, Collections.<String>emptyList(), singletonList(TASK0));814 expectPostRebalanceCatchup(SNAPSHOT);815 worker.startTask(EasyMock.eq(TASK0), EasyMock.<Map<String, String>>anyObject(), EasyMock.<Map<String, String>>anyObject(),816 EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED));817 PowerMock.expectLastCall().andReturn(true);818 member.poll(EasyMock.anyInt());819 PowerMock.expectLastCall();820 // state change is ignored since we have no target state821 member.wakeup();822 member.ensureActive();823 PowerMock.expectLastCall();824 EasyMock.expect(configBackingStore.snapshot()).andReturn(SNAPSHOT);825 PowerMock.expectLastCall();826 member.poll(EasyMock.anyInt());827 PowerMock.expectLastCall();828 PowerMock.replayAll();829 herder.tick(); // join830 configUpdateListener.onConnectorTargetStateChange("unknown-connector");831 herder.tick(); // continue832 PowerMock.verifyAll();833 }834 @Test835 public void testConnectorPausedRunningTaskOnly() throws Exception {836 // even if we don't own the connector, we should still propagate target state837 // changes to the worker so that tasks will transition correctly838 EasyMock.expect(member.memberId()).andStubReturn("member");839 EasyMock.expect(worker.connectorNames()).andStubReturn(Collections.<String>emptySet());840 // join841 expectRebalance(1, Collections.<String>emptyList(), singletonList(TASK0));842 expectPostRebalanceCatchup(SNAPSHOT);843 worker.startTask(EasyMock.eq(TASK0), EasyMock.<Map<String, String>>anyObject(), EasyMock.<Map<String, String>>anyObject(),844 EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED));845 PowerMock.expectLastCall().andReturn(true);846 member.poll(EasyMock.anyInt());847 PowerMock.expectLastCall();848 // handle the state change849 member.wakeup();850 member.ensureActive();851 PowerMock.expectLastCall();852 EasyMock.expect(configBackingStore.snapshot()).andReturn(SNAPSHOT_PAUSED_CONN1);853 PowerMock.expectLastCall();854 worker.setTargetState(CONN1, TargetState.PAUSED);855 PowerMock.expectLastCall();856 member.poll(EasyMock.anyInt());857 PowerMock.expectLastCall();858 PowerMock.replayAll();859 herder.tick(); // join860 configUpdateListener.onConnectorTargetStateChange(CONN1); // state changes to paused861 herder.tick(); // apply state change862 PowerMock.verifyAll();863 }864 @Test865 public void testConnectorResumedRunningTaskOnly() throws Exception {866 // even if we don't own the connector, we should still propagate target state867 // changes to the worker so that tasks will transition correctly868 EasyMock.expect(member.memberId()).andStubReturn("member");869 EasyMock.expect(worker.connectorNames()).andStubReturn(Collections.<String>emptySet());870 // join871 expectRebalance(1, Collections.<String>emptyList(), singletonList(TASK0));872 expectPostRebalanceCatchup(SNAPSHOT_PAUSED_CONN1);873 worker.startTask(EasyMock.eq(TASK0), EasyMock.<Map<String, String>>anyObject(), EasyMock.<Map<String, String>>anyObject(),874 EasyMock.eq(herder), EasyMock.eq(TargetState.PAUSED));875 PowerMock.expectLastCall().andReturn(true);876 member.poll(EasyMock.anyInt());877 PowerMock.expectLastCall();878 // handle the state change879 member.wakeup();880 member.ensureActive();881 PowerMock.expectLastCall();882 EasyMock.expect(configBackingStore.snapshot()).andReturn(SNAPSHOT);883 PowerMock.expectLastCall();884 worker.setTargetState(CONN1, TargetState.STARTED);885 PowerMock.expectLastCall();886 EasyMock.expect(worker.isRunning(CONN1)).andReturn(false);887 member.poll(EasyMock.anyInt());888 PowerMock.expectLastCall();889 PowerMock.replayAll();890 herder.tick(); // join891 configUpdateListener.onConnectorTargetStateChange(CONN1); // state changes to paused892 herder.tick(); // apply state change893 PowerMock.verifyAll();894 }895 @Test896 public void testTaskConfigAdded() {897 // Task config always requires rebalance898 EasyMock.expect(member.memberId()).andStubReturn("member");899 // join900 expectRebalance(-1, Collections.<String>emptyList(), Collections.<ConnectorTaskId>emptyList());901 member.poll(EasyMock.anyInt());902 PowerMock.expectLastCall();903 // apply config904 member.wakeup();905 member.ensureActive();906 PowerMock.expectLastCall();907 // Checks for config updates and starts rebalance908 EasyMock.expect(configBackingStore.snapshot()).andReturn(SNAPSHOT);909 member.requestRejoin();910 PowerMock.expectLastCall();911 // Performs rebalance and gets new assignment912 expectRebalance(Collections.<String>emptyList(), Collections.<ConnectorTaskId>emptyList(),913 ConnectProtocol.Assignment.NO_ERROR, 1, Collections.<String>emptyList(),914 Arrays.asList(TASK0));915 worker.startTask(EasyMock.eq(TASK0), EasyMock.<Map<String, String>>anyObject(), EasyMock.<Map<String, String>>anyObject(),916 EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED));917 PowerMock.expectLastCall().andReturn(true);918 member.poll(EasyMock.anyInt());919 PowerMock.expectLastCall();920 PowerMock.replayAll();921 herder.tick(); // join922 configUpdateListener.onTaskConfigUpdate(Arrays.asList(TASK0, TASK1, TASK2)); // read updated config923 herder.tick(); // apply config924 herder.tick(); // do rebalance925 PowerMock.verifyAll();926 }927 @Test928 public void testJoinLeaderCatchUpFails() throws Exception {929 // Join group and as leader fail to do assignment930 EasyMock.expect(member.memberId()).andStubReturn("leader");931 expectRebalance(Collections.<String>emptyList(), Collections.<ConnectorTaskId>emptyList(),932 ConnectProtocol.Assignment.CONFIG_MISMATCH, 1, Collections.<String>emptyList(),933 Collections.<ConnectorTaskId>emptyList());934 // Reading to end of log times out935 configBackingStore.refresh(EasyMock.anyLong(), EasyMock.anyObject(TimeUnit.class));936 EasyMock.expectLastCall().andThrow(new TimeoutException());937 member.maybeLeaveGroup();938 EasyMock.expectLastCall();939 PowerMock.expectPrivate(herder, "backoff", DistributedConfig.WORKER_UNSYNC_BACKOFF_MS_DEFAULT);940 member.requestRejoin();941 // After backoff, restart the process and this time succeed942 expectRebalance(1, Arrays.asList(CONN1), Arrays.asList(TASK1));943 expectPostRebalanceCatchup(SNAPSHOT);944 worker.startConnector(EasyMock.eq(CONN1), EasyMock.<Map<String, String>>anyObject(), EasyMock.<ConnectorContext>anyObject(),945 EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED));946 PowerMock.expectLastCall().andReturn(true);947 EasyMock.expect(worker.connectorTaskConfigs(CONN1, MAX_TASKS, null)).andReturn(TASK_CONFIGS);948 worker.startTask(EasyMock.eq(TASK1), EasyMock.<Map<String, String>>anyObject(), EasyMock.<Map<String, String>>anyObject(),949 EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED));950 PowerMock.expectLastCall().andReturn(true);951 EasyMock.expect(worker.isRunning(CONN1)).andReturn(true);952 member.poll(EasyMock.anyInt());953 PowerMock.expectLastCall();954 PowerMock.replayAll();955 herder.tick();956 herder.tick();957 PowerMock.verifyAll();958 }959 @Test960 public void testAccessors() throws Exception {961 EasyMock.expect(member.memberId()).andStubReturn("leader");962 expectRebalance(1, Collections.<String>emptyList(), Collections.<ConnectorTaskId>emptyList());963 expectPostRebalanceCatchup(SNAPSHOT);964 member.wakeup();965 PowerMock.expectLastCall().anyTimes();966 // list connectors, get connector info, get connector config, get task configs967 member.poll(EasyMock.anyInt());968 PowerMock.expectLastCall();969 PowerMock.replayAll();970 FutureCallback<Collection<String>> listConnectorsCb = new FutureCallback<>();971 herder.connectors(listConnectorsCb);972 FutureCallback<ConnectorInfo> connectorInfoCb = new FutureCallback<>();973 herder.connectorInfo(CONN1, connectorInfoCb);974 FutureCallback<Map<String, String>> connectorConfigCb = new FutureCallback<>();975 herder.connectorConfig(CONN1, connectorConfigCb);976 FutureCallback<List<TaskInfo>> taskConfigsCb = new FutureCallback<>();977 herder.taskConfigs(CONN1, taskConfigsCb);978 herder.tick();979 assertTrue(listConnectorsCb.isDone());980 assertEquals(Collections.singleton(CONN1), listConnectorsCb.get());981 assertTrue(connectorInfoCb.isDone());982 ConnectorInfo info = new ConnectorInfo(CONN1, CONN1_CONFIG, Arrays.asList(TASK0, TASK1, TASK2));983 assertEquals(info, connectorInfoCb.get());984 assertTrue(connectorConfigCb.isDone());985 assertEquals(CONN1_CONFIG, connectorConfigCb.get());986 assertTrue(taskConfigsCb.isDone());987 assertEquals(Arrays.asList(988 new TaskInfo(TASK0, TASK_CONFIG),989 new TaskInfo(TASK1, TASK_CONFIG),990 new TaskInfo(TASK2, TASK_CONFIG)),991 taskConfigsCb.get());992 PowerMock.verifyAll();993 }994 @Test995 public void testPutConnectorConfig() throws Exception {996 EasyMock.expect(member.memberId()).andStubReturn("leader");997 expectRebalance(1, Arrays.asList(CONN1), Collections.<ConnectorTaskId>emptyList());998 expectPostRebalanceCatchup(SNAPSHOT);999 worker.startConnector(EasyMock.eq(CONN1), EasyMock.<Map<String, String>>anyObject(), EasyMock.<ConnectorContext>anyObject(),1000 EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED));1001 PowerMock.expectLastCall().andReturn(true);1002 EasyMock.expect(worker.isRunning(CONN1)).andReturn(true);1003 EasyMock.expect(worker.connectorTaskConfigs(CONN1, MAX_TASKS, null)).andReturn(TASK_CONFIGS);1004 // list connectors, get connector info, get connector config, get task configs1005 member.wakeup();1006 PowerMock.expectLastCall().anyTimes();1007 member.poll(EasyMock.anyInt());1008 PowerMock.expectLastCall();1009 // Poll loop for second round of calls1010 member.ensureActive();1011 PowerMock.expectLastCall();1012 // config validation1013 ConnectorFactory connectorFactoryMock = PowerMock.createMock(ConnectorFactory.class);1014 EasyMock.expect(worker.getConnectorFactory()).andStubReturn(connectorFactoryMock);1015 Connector connectorMock = PowerMock.createMock(Connector.class);1016 EasyMock.expect(connectorFactoryMock.newConnector(EasyMock.anyString())).andReturn(connectorMock);1017 EasyMock.expect(connectorMock.config()).andReturn(new ConfigDef());1018 EasyMock.expect(connectorMock.validate(CONN1_CONFIG_UPDATED)).andReturn(new Config(Collections.<ConfigValue>emptyList()));1019 configBackingStore.putConnectorConfig(CONN1, CONN1_CONFIG_UPDATED);1020 PowerMock.expectLastCall().andAnswer(new IAnswer<Object>() {1021 @Override1022 public Object answer() throws Throwable {1023 // Simulate response to writing config + waiting until end of log to be read1024 configUpdateListener.onConnectorConfigUpdate(CONN1);1025 return null;1026 }1027 });1028 // As a result of reconfig, should need to update snapshot. With only connector updates, we'll just restart1029 // connector without rebalance1030 EasyMock.expect(configBackingStore.snapshot()).andReturn(SNAPSHOT_UPDATED_CONN1_CONFIG);1031 worker.stopConnector(CONN1);1032 PowerMock.expectLastCall().andReturn(true);1033 worker.startConnector(EasyMock.eq(CONN1), EasyMock.<Map<String, String>>anyObject(), EasyMock.<ConnectorContext>anyObject(),1034 EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED));1035 PowerMock.expectLastCall().andReturn(true);1036 EasyMock.expect(worker.isRunning(CONN1)).andReturn(true);1037 EasyMock.expect(worker.connectorTaskConfigs(CONN1, MAX_TASKS, null)).andReturn(TASK_CONFIGS);1038 member.poll(EasyMock.anyInt());1039 PowerMock.expectLastCall();1040 // Third tick just to read the config1041 member.ensureActive();1042 PowerMock.expectLastCall();1043 member.poll(EasyMock.anyInt());1044 PowerMock.expectLastCall();1045 PowerMock.replayAll();1046 // Should pick up original config1047 FutureCallback<Map<String, String>> connectorConfigCb = new FutureCallback<>();1048 herder.connectorConfig(CONN1, connectorConfigCb);1049 herder.tick();1050 assertTrue(connectorConfigCb.isDone());1051 assertEquals(CONN1_CONFIG, connectorConfigCb.get());1052 // Apply new config.1053 FutureCallback<Herder.Created<ConnectorInfo>> putConfigCb = new FutureCallback<>();1054 herder.putConnectorConfig(CONN1, CONN1_CONFIG_UPDATED, true, putConfigCb);1055 herder.tick();1056 assertTrue(putConfigCb.isDone());1057 ConnectorInfo updatedInfo = new ConnectorInfo(CONN1, CONN1_CONFIG_UPDATED, Arrays.asList(TASK0, TASK1, TASK2));1058 assertEquals(new Herder.Created<>(false, updatedInfo), putConfigCb.get());1059 // Check config again to validate change1060 connectorConfigCb = new FutureCallback<>();1061 herder.connectorConfig(CONN1, connectorConfigCb);1062 herder.tick();1063 assertTrue(connectorConfigCb.isDone());1064 assertEquals(CONN1_CONFIG_UPDATED, connectorConfigCb.get());1065 PowerMock.verifyAll();1066 }1067 @Test1068 public void testInconsistentConfigs() throws Exception {1069 // FIXME: if we have inconsistent configs, we need to request forced reconfig + write of the connector's task configs1070 // This requires inter-worker communication, so needs the REST API1071 }1072 private void expectRebalance(final long offset,1073 final List<String> assignedConnectors,1074 final List<ConnectorTaskId> assignedTasks) {1075 expectRebalance(null, null, ConnectProtocol.Assignment.NO_ERROR, offset, assignedConnectors, assignedTasks);1076 }1077 // Handles common initial part of rebalance callback. Does not handle instantiation of connectors and tasks.1078 private void expectRebalance(final Collection<String> revokedConnectors,1079 final List<ConnectorTaskId> revokedTasks,1080 final short error,1081 final long offset,1082 final List<String> assignedConnectors,1083 final List<ConnectorTaskId> assignedTasks) {1084 member.ensureActive();1085 PowerMock.expectLastCall().andAnswer(new IAnswer<Object>() {1086 @Override1087 public Object answer() throws Throwable {1088 if (revokedConnectors != null)1089 rebalanceListener.onRevoked("leader", revokedConnectors, revokedTasks);1090 ConnectProtocol.Assignment assignment = new ConnectProtocol.Assignment(1091 error, "leader", "leaderUrl", offset, assignedConnectors, assignedTasks);1092 rebalanceListener.onAssigned(assignment, 0);1093 return null;1094 }1095 });1096 if (revokedConnectors != null) {1097 for (String connector : revokedConnectors) {1098 worker.stopConnector(connector);1099 PowerMock.expectLastCall().andReturn(true);1100 }1101 }1102 if (revokedTasks != null && !revokedTasks.isEmpty()) {1103 worker.stopAndAwaitTask(EasyMock.anyObject(ConnectorTaskId.class));1104 PowerMock.expectLastCall();1105 }1106 if (revokedConnectors != null) {1107 statusBackingStore.flush();1108 PowerMock.expectLastCall();1109 }1110 member.wakeup();1111 PowerMock.expectLastCall();1112 }1113 private void expectPostRebalanceCatchup(final ClusterConfigState readToEndSnapshot) throws TimeoutException {1114 configBackingStore.refresh(EasyMock.anyLong(), EasyMock.anyObject(TimeUnit.class));1115 EasyMock.expectLastCall();1116 EasyMock.expect(configBackingStore.snapshot()).andReturn(readToEndSnapshot);1117 }1118 // We need to use a real class here due to some issue with mocking java.lang.Class1119 private abstract class BogusSourceConnector extends SourceConnector {1120 }1121 private abstract class BogusSourceTask extends SourceTask {1122 }1123}...
Source:Mocks.java
...18import static org.easymock.EasyMock.createMock;19import static org.easymock.EasyMock.createNiceMock;20import static org.easymock.EasyMock.eq;21import static org.easymock.EasyMock.expect;22import static org.easymock.EasyMock.expectLastCall;23import static org.easymock.EasyMock.isA;24import static org.easymock.EasyMock.replay;25import com.android.io.IAbstractFolder;26import com.android.io.IAbstractResource;27import org.easymock.Capture;28import org.easymock.EasyMock;29import org.easymock.IAnswer;30import org.eclipse.core.resources.IFile;31import org.eclipse.core.resources.IFolder;32import org.eclipse.core.resources.IProject;33import org.eclipse.core.resources.IResource;34import org.eclipse.core.runtime.IPath;35import org.eclipse.core.runtime.IProgressMonitor;36import org.eclipse.core.runtime.Path;37import org.eclipse.jdt.core.IClasspathEntry;38import org.eclipse.jdt.core.IJavaProject;39import org.eclipse.jdt.core.JavaCore;40import java.util.Map;41public class Mocks {42 public static IJavaProject createProject(IClasspathEntry[] entries, IPath outputLocation)43 throws Exception {44 IJavaProject javaProject = createMock(IJavaProject.class);45 final Capture<IClasspathEntry[]> capturedEntries = new Capture<IClasspathEntry[]>();46 Capture<IPath> capturedOutput = new Capture<IPath>();47 capturedEntries.setValue(entries);48 capturedOutput.setValue(outputLocation);49 IProject project = createProject();50 expect(javaProject.getProject()).andReturn(project).anyTimes();51 expect(javaProject.getOutputLocation()).andReturn(capturedOutput.getValue()).anyTimes();52 expect(javaProject.getRawClasspath()).andAnswer(new IAnswer<IClasspathEntry[]>() {53 @Override54 public IClasspathEntry[] answer() throws Throwable {55 return capturedEntries.getValue();56 }57 }).anyTimes();58 javaProject.setRawClasspath(capture(capturedEntries), isA(IProgressMonitor.class));59 expectLastCall().anyTimes();60 javaProject.setRawClasspath(capture(capturedEntries), capture(capturedOutput),61 isA(IProgressMonitor.class));62 expectLastCall().anyTimes();63 final Capture<String> capturedCompliance = new Capture<String>();64 capturedCompliance.setValue("1.4");65 final Capture<String> capturedSource = new Capture<String>();66 capturedSource.setValue("1.4");67 final Capture<String> capturedTarget = new Capture<String>();68 capturedTarget.setValue("1.4");69 expect(javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true)).andAnswer(70 new IAnswer<String>() {71 @Override72 public String answer() throws Throwable {73 return capturedCompliance.getValue();74 }75 });76 expect(javaProject.getOption(JavaCore.COMPILER_SOURCE, true)).andAnswer(77 new IAnswer<String>() {78 @Override79 public String answer() throws Throwable {80 return capturedSource.getValue();81 }82 });83 expect(javaProject.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true)).andAnswer(84 new IAnswer<String>() {85 @Override86 public String answer() throws Throwable {87 return capturedTarget.getValue();88 }89 });90 javaProject.setOption(eq(JavaCore.COMPILER_COMPLIANCE), capture(capturedCompliance));91 expectLastCall().anyTimes();92 javaProject.setOption(eq(JavaCore.COMPILER_SOURCE), capture(capturedSource));93 expectLastCall().anyTimes();94 javaProject.setOption(eq(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM),95 capture(capturedTarget));96 expectLastCall().anyTimes();97 replay(javaProject);98 return javaProject;99 }100 /**101 * Creates a mock implementation of {@link IFile}.102 * <p/>103 * Supported methods:104 * <ul>105 * <li>IFile#getName()</li>106 * <li>IFile#getLocation()</li>107 * </ul>108 */109 public static IFile createFile(String fileName) {110 IFile file = createNiceMock(IFile.class);...
Source:Log4jServletFilterTest.java
...25import org.junit.Test;26import static org.easymock.EasyMock.createStrictMock;27import static org.easymock.EasyMock.eq;28import static org.easymock.EasyMock.expect;29import static org.easymock.EasyMock.expectLastCall;30import static org.easymock.EasyMock.replay;31import static org.easymock.EasyMock.reset;32import static org.easymock.EasyMock.same;33import static org.easymock.EasyMock.verify;34public class Log4jServletFilterTest {35 private FilterConfig filterConfig;36 private ServletContext servletContext;37 private Log4jWebLifeCycle initializer;38 private Log4jServletFilter filter;39 @Before40 public void setUp() {41 this.filterConfig = createStrictMock(FilterConfig.class);42 this.servletContext = createStrictMock(ServletContext.class);43 this.initializer = createStrictMock(Log4jWebLifeCycle.class);44 this.filter = new Log4jServletFilter();45 }46 @After47 public void tearDown() {48 verify(this.filterConfig, this.servletContext, this.initializer);49 }50 @Test51 public void testInitAndDestroy() throws Exception {52 expect(this.filterConfig.getServletContext()).andReturn(this.servletContext);53 expect(this.servletContext.getAttribute(Log4jWebSupport.SUPPORT_ATTRIBUTE)).andReturn(this.initializer);54 this.initializer.clearLoggerContext();55 expectLastCall();56 replay(this.filterConfig, this.servletContext, this.initializer);57 this.filter.init(this.filterConfig);58 verify(this.filterConfig, this.servletContext, this.initializer);59 reset(this.filterConfig, this.servletContext, this.initializer);60 this.initializer.setLoggerContext();61 expectLastCall();62 replay(this.filterConfig, this.servletContext, this.initializer);63 this.filter.destroy();64 }65 @Test(expected = IllegalStateException.class)66 public void testDestroy() {67 replay(this.filterConfig, this.servletContext, this.initializer);68 this.filter.destroy();69 }70 @Test71 public void testDoFilterFirstTime() throws Exception {72 expect(this.filterConfig.getServletContext()).andReturn(this.servletContext);73 expect(this.servletContext.getAttribute(Log4jWebSupport.SUPPORT_ATTRIBUTE)).andReturn(this.initializer);74 this.initializer.clearLoggerContext();75 expectLastCall();76 replay(this.filterConfig, this.servletContext, this.initializer);77 this.filter.init(this.filterConfig);78 verify(this.filterConfig, this.servletContext, this.initializer);79 reset(this.filterConfig, this.servletContext, this.initializer);80 final ServletRequest request = createStrictMock(ServletRequest.class);81 final ServletResponse response = createStrictMock(ServletResponse.class);82 final FilterChain chain = createStrictMock(FilterChain.class);83 expect(request.getAttribute(Log4jServletFilter.ALREADY_FILTERED_ATTRIBUTE)).andReturn(null);84 request.setAttribute(eq(Log4jServletFilter.ALREADY_FILTERED_ATTRIBUTE), eq(Boolean.TRUE));85 expectLastCall();86 this.initializer.setLoggerContext();87 expectLastCall();88 chain.doFilter(same(request), same(response));89 expectLastCall();90 this.initializer.clearLoggerContext();91 expectLastCall();92 replay(this.filterConfig, this.servletContext, this.initializer, request, response, chain);93 this.filter.doFilter(request, response, chain);94 verify(request, response, chain);95 }96 @Test97 public void testDoFilterSecondTime() throws Exception {98 expect(this.filterConfig.getServletContext()).andReturn(this.servletContext);99 expect(this.servletContext.getAttribute(Log4jWebSupport.SUPPORT_ATTRIBUTE)).andReturn(this.initializer);100 this.initializer.clearLoggerContext();101 expectLastCall();102 replay(this.filterConfig, this.servletContext, this.initializer);103 this.filter.init(this.filterConfig);104 verify(this.filterConfig, this.servletContext, this.initializer);105 reset(this.filterConfig, this.servletContext, this.initializer);106 final ServletRequest request = createStrictMock(ServletRequest.class);107 final ServletResponse response = createStrictMock(ServletResponse.class);108 final FilterChain chain = createStrictMock(FilterChain.class);109 expect(request.getAttribute(Log4jServletFilter.ALREADY_FILTERED_ATTRIBUTE)).andReturn(true);110 expectLastCall();111 chain.doFilter(same(request), same(response));112 expectLastCall();113 replay(this.filterConfig, this.servletContext, this.initializer, request, response, chain);114 this.filter.doFilter(request, response, chain);115 verify(request, response, chain);116 }117}...
expectLastCall
Using AI Code Generation
1package org.easymock.test;2import static org.easymock.EasyMock.expectLastCall;3import static org.easymock.EasyMock.replay;4import static org.easymock.EasyMock.verify;5import static org.easymock.EasyMock.createMock;6import org.easymock.IAnswer;7public class MockTest {8 public static void main(String[] args) {9 IAnswer<String> answer = new IAnswer<String>() {10 public String answer() throws Throwable {11 return "Hello World";12 }13 };14 MockInterface mock = createMock(MockInterface.class);15 mock.sayHello();16 expectLastCall().andAnswer(answer);17 replay(mock);18 System.out.println(mock.sayHello());19 verify(mock);20 }21}22package org.easymock.test;23import static org.easymock.EasyMock.expectLastCall;24import static org.easymock.EasyMock.replay;25import static org.easymock.EasyMock.verify;26import static org.easymock.EasyMock.createMock;27import org.easymock.IAnswer;28public class MockTest {29 public static void main(String[] args) {30 IAnswer<String> answer = new IAnswer<String>() {31 public String answer() throws Throwable {32 return "Hello World";33 }34 };35 MockInterface mock = createMock(MockInterface.class);36 mock.sayHello();37 expectLastCall().andAnswer(answer);38 replay(mock);39 System.out.println(mock.sayHello());40 verify(mock);41 }42}43package org.easymock.test;44import static org.easymock.EasyMock.expectLastCall;45import static org.easymock.EasyMock.replay;46import static org.easymock.EasyMock.verify;47import static org.easymock.EasyMock.createMock;48import org.easymock.IAnswer;49public class MockTest {50 public static void main(String[] args) {51 IAnswer<String> answer = new IAnswer<String>() {52 public String answer() throws Throwable {53 return "Hello World";54 }55 };56 MockInterface mock = createMock(MockInterface.class);
expectLastCall
Using AI Code Generation
1package com.ack.junit.mock;2import static org.easymock.EasyMock.expectLastCall;3import static org.easymock.EasyMock.replay;4import static org.easymock.EasyMock.verify;5import static org.junit.Assert.assertEquals;6import org.easymock.EasyMock;7import org.junit.Test;8public class MockTest {9 public void testMock() {10 MockInterface mock = EasyMock.createMock( MockInterface.class );11 mock.doSomething( "Hello" );12 expectLastCall();13 replay( mock );14 mock.doSomething( "Hello" );15 verify( mock );16 }17 public void testMock2() {18 MockInterface mock = EasyMock.createMock( MockInterface.class );19 mock.doSomething( "Hello" );20 expectLastCall().andThrow( new RuntimeException( "Hello" ) );21 replay( mock );22 try {23 mock.doSomething( "Hello" );24 }25 catch( RuntimeException e ) {26 assertEquals( "Hello", e.getMessage() );27 }28 verify( mock );29 }30}31package com.ack.junit.mock;32public interface MockInterface {33 public void doSomething( String message );34}35package com.ack.junit.mock;36import static org.easymock.EasyMock.expectLastCall;37import static org.easymock.EasyMock.replay;38import static org.easymock.EasyMock.verify;39import static org.junit.Assert.assertEquals;40import org.easymock.EasyMock;41import org.junit.Test;42public class MockTest {43 public void testMock() {44 MockInterface mock = EasyMock.createMock( MockInterface.class );45 mock.doSomething( "Hello" );46 expectLastCall();47 replay( mock );48 mock.doSomething( "Hello" );49 verify( mock );50 }
expectLastCall
Using AI Code Generation
1import org.easymock.EasyMock;2import org.easymock.IMocksControl;3public class 1 {4 public static void main(String[] args) {5 IMocksControl control = EasyMock.createControl();6 Sample mock = control.createMock(Sample.class);7 EasyMock.expect(mock.doSomething()).andThrow(new Exception());8 control.replay();9 try {10 mock.doSomething();11 } catch (Exception e) {12 System.out.println("Exception caught");13 }14 }15}
expectLastCall
Using AI Code Generation
1import org.easymock.EasyMock;2import org.easymock.IMocksControl;3public class 1 {4 public static void main(String[] args) {5 IMocksControl control = EasyMock.createControl();6 Interface mock = control.createMock(Interface.class);7 mock.method();8 EasyMock.expectLastCall().andReturn("return value");9 control.replay();10 System.out.println(mock.method());11 control.verify();12 }13}
expectLastCall
Using AI Code Generation
1import org.easymock.EasyMock;2import org.easymock.IExpectationSetters;3import org.junit.Test;4public class Test1 {5 public void test1() {6 Demo d = EasyMock.createMock(Demo.class);7 EasyMock.expect(d.get()).andReturn("Hello");8 IExpectationSetters<String> ies = EasyMock.expect(d.get());9 ies.andReturn("World");10 EasyMock.expect(d.get()).andReturn("Hello");11 EasyMock.expect(d.get()).andReturn("World");12 EasyMock.replay(d);13 System.out.println(d.get());14 System.out.println(d.get());15 System.out.println(d.get());16 System.out.println(d.get());17 EasyMock.verify(d);18 }19}20public interface Demo {21 public String get();22}23Related posts: How to use expect() method of org.easymock.EasyMock class? How to use andReturn() method of org.easymock.EasyMock class? How to use andStubReturn() method of org.easymock.EasyMock class? How to use andThrow() method of org.easymock.EasyMock class? How to use andStubThrow() method of org.easymock.EasyMock class? How to use andAnswer() method of org.easymock.EasyMock class? How to use andStubAnswer() method of org.easymock.EasyMock class? How to use andReturn() method of org.easymock.IMocksControl class? How to use andStubReturn() method of org.easymock.IMocksControl class? How to use andThrow() method of org.easymock.IMocksControl class? How to use andStubThrow() method of org.easymock.IMocksControl class? How to use andAnswer() method of org.easymock.IMocksControl class? How to use andStubAnswer() method of org.easymock.IMocksControl class? How to use andDelegateTo() method of org.easymock.IMocksControl
expectLastCall
Using AI Code Generation
1import org.easymock.EasyMock;2import java.util.List;3import java.util.ArrayList;4import java.util.Iterator;5public class Test {6 public static void main(String[] args) {7 List mock = EasyMock.createMock(List.class);8 Iterator iterator = EasyMock.createMock(Iterator.class);9 EasyMock.expect(mock.iterator()).andReturn(iterator);10 EasyMock.expect(iterator.hasNext()).andReturn(false);11 EasyMock.replay(mock);12 EasyMock.replay(iterator);13 System.out.println(mock.iterator().hasNext());14 }15}16import org.easymock.EasyMock;17import java.util.List;18import java.util.ArrayList;19import java.util.Iterator;20public class Test {21 public static void main(String[] args) {22 List mock = EasyMock.createMock(List.class);23 Iterator iterator = EasyMock.createMock(Iterator.class);24 EasyMock.expect(mock.iterator()).andReturn(iterator);25 EasyMock.expect(iterator.next()).andReturn("hello");26 EasyMock.replay(mock);27 EasyMock.replay(iterator);28 System.out.println(mock.iterator().next());29 }30}31import org.easymock.EasyMock;32import java.util.List;33import java.util.ArrayList;34import java.util.Iterator;35public class Test {36 public static void main(String[] args) {37 List mock = EasyMock.createMock(List.class);38 Iterator iterator = EasyMock.createMock(Iterator.class);39 EasyMock.expect(mock.iterator()).andReturn(iterator);40 EasyMock.expect(iterator.next()).andReturn("hello");41 EasyMock.expect(iterator.next()).andReturn("world");42 EasyMock.replay(mock);43 EasyMock.replay(iterator);44 System.out.println(mock.iterator().next());45 System.out.println(mock.iterator().next());46 }47}48import org.easymock.EasyMock;49import java.util.List;50import java.util.ArrayList;51import java.util.Iterator;52public class Test {53 public static void main(String[] args) {54 List mock = EasyMock.createMock(List.class);55 Iterator iterator = EasyMock.createMock(Iterator.class);56 EasyMock.expect(mock.iterator()).andReturn(iterator);57 EasyMock.expect(iterator.next()).andReturn("hello");58 EasyMock.expect(iterator.next()).andReturn("world");59 EasyMock.replay(mock);60 EasyMock.replay(iterator);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!