Source:
public void testDeleteTasksWithPrefix() { String testCluster = "testDeleteTaskWithPrefix"; String connectorType = "connectorType"; ZkClientInterceptingAdapter adapter = createInterceptingZkAdapter(testCluster); adapter.connect(); List<DatastreamTask> tasks = new ArrayList<>(); for (int i = 0; i < 10; i++) { DatastreamTaskImpl dsTask = new DatastreamTaskImpl(); dsTask.setId("task" + i); String taskPrefix = "taskPrefix" + i; dsTask.setTaskPrefix(taskPrefix); dsTask.setConnectorType(connectorType); dsTask.setZkAdapter(adapter); tasks.add(dsTask); } updateInstanceAssignment(adapter, adapter.getInstanceName(), tasks); ZkClient zkClient = Mockito.spy(adapter.getZkClient()); for (int j = 0; j < 8; j++) { adapter.deleteTasksWithPrefix(connectorType, "taskPrefix" + j); } Mockito.verify(zkClient, Mockito.never()).getChildren(any()); Mockito.verify(zkClient, Mockito.never()).getChildren(any(), anyBoolean()); List<String> leftOverTasks = zkClient.getChildren(KeyBuilder.connector(testCluster, connectorType)); Assert.assertEquals(leftOverTasks.size(), 2); adapter.cleanUpOrphanConnectorTasks(false); leftOverTasks = zkClient.getChildren(KeyBuilder.connector(testCluster, connectorType)); Assert.assertEquals(leftOverTasks.size(), 2); adapter.cleanUpOrphanConnectorTasks(true); leftOverTasks = zkClient.getChildren(KeyBuilder.connector(testCluster, connectorType)); Assert.assertEquals(leftOverTasks.size(), 2); updateInstanceAssignment(adapter, adapter.getInstanceName(), new ArrayList<>()); leftOverTasks = zkClient.getChildren(KeyBuilder.connector(testCluster, connectorType)); Assert.assertEquals(leftOverTasks.size(), 2); adapter.cleanUpOrphanConnectorTasks(false); leftOverTasks = zkClient.getChildren(KeyBuilder.connector(testCluster, connectorType)); Assert.assertEquals(leftOverTasks.size(), 2); adapter.cleanUpOrphanConnectorTasks(true); leftOverTasks = zkClient.getChildren(KeyBuilder.connector(testCluster, connectorType)); Assert.assertEquals(leftOverTasks.size(), 0); adapter.disconnect(); }

Target:
public void testDeleteTasksWithPrefix() { String testCluster = "testDeleteTaskWithPrefix"; String connectorType = "connectorType"; ZkClientInterceptingAdapter adapter = createInterceptingZkAdapter(testCluster); adapter.connect(); List<DatastreamTask> tasks = new ArrayList<>(); for (int i = 0; i < 10; i++) { DatastreamTaskImpl dsTask = new DatastreamTaskImpl(); dsTask.setId("task" + i); String taskPrefix = "taskPrefix" + i; dsTask.setTaskPrefix(taskPrefix); dsTask.setConnectorType(connectorType); dsTask.setZkAdapter(adapter); tasks.add(dsTask); } updateInstanceAssignment(adapter, adapter.getInstanceName(), tasks); ZkClient zkClient = Mockito.spy(adapter.getZkClient()); for (int j = 0; j < 8; j++) { adapter.deleteTasksWithPrefix(connectorType, "taskPrefix" + j); } Mockito.verify(zkClient, Mockito.never()).getChildren(any()); Mockito.verify(zkClient, Mockito.never()).getChildren(any(), anyBoolean()); List<String> leftOverTasks = zkClient.getChildren(KeyBuilder.connector(testCluster, connectorType)); Assert.assertEquals(leftOverTasks.size(), 2); adapter.cleanUpOrphanConnectorTasks(false); leftOverTasks = zkClient.getChildren(KeyBuilder.connector(testCluster, connectorType)); Assert.assertEquals(leftOverTasks.size(), 2); adapter.cleanUpOrphanConnectorTasks(true); leftOverTasks = zkClient.getChildren(KeyBuilder.connector(testCluster, connectorType)); Assert.assertEquals(leftOverTasks.size(), 2); updateInstanceAssignment(adapter, adapter.getInstanceName(), Collections.emptyList()); leftOverTasks = zkClient.getChildren(KeyBuilder.connector(testCluster, connectorType)); Assert.assertEquals(leftOverTasks.size(), 2); adapter.cleanUpOrphanConnectorTasks(false); leftOverTasks = zkClient.getChildren(KeyBuilder.connector(testCluster, connectorType)); Assert.assertEquals(leftOverTasks.size(), 2); adapter.cleanUpOrphanConnectorTasks(true); leftOverTasks = zkClient.getChildren(KeyBuilder.connector(testCluster, connectorType)); Assert.assertEquals(leftOverTasks.size(), 0); adapter.disconnect(); }