use of org.apache.samza.coordinator.JobCoordinator in project samza by apache.
the class TestStreamProcessor method testStopByProcessor.
/**
* Tests stop() method when Container AND JobCoordinator are running
*/
@Test
public void testStopByProcessor() {
JobCoordinator mockJobCoordinator = mock(JobCoordinator.class);
final CountDownLatch processorListenerStop = new CountDownLatch(1);
final CountDownLatch processorListenerStart = new CountDownLatch(1);
TestableStreamProcessor processor = new TestableStreamProcessor(new MapConfig(), new HashMap<>(), mock(StreamTaskFactory.class), new StreamProcessorLifecycleListener() {
@Override
public void onStart() {
processorListenerStart.countDown();
}
@Override
public void onShutdown() {
processorListenerStop.countDown();
}
@Override
public void onFailure(Throwable t) {
}
}, mockJobCoordinator);
Map containers = mock(Map.class);
doReturn(true).when(containers).containsKey(anyString());
when(containers.get(anyString())).thenReturn(mock(ContainerModel.class));
JobModel mockJobModel = mock(JobModel.class);
when(mockJobModel.getContainers()).thenReturn(containers);
final CountDownLatch coordinatorStop = new CountDownLatch(1);
final Thread jcThread = new Thread(() -> {
try {
processor.jobCoordinatorListener.onNewJobModel("1", mockJobModel);
coordinatorStop.await();
processor.jobCoordinatorListener.onCoordinatorStop();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
doAnswer(invocation -> {
coordinatorStop.countDown();
return null;
}).when(mockJobCoordinator).stop();
doAnswer(invocation -> {
jcThread.start();
return null;
}).when(mockJobCoordinator).start();
try {
processor.start();
processorListenerStart.await();
Assert.assertEquals(SamzaContainerStatus.STARTED, processor.containerReference.getStatus());
// This block is required for the mockRunloop is actually start.
// Otherwise, processor.stop gets triggered before mockRunloop begins to block
processor.runLoopStartForMain.await();
processor.stop();
processorListenerStop.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
use of org.apache.samza.coordinator.JobCoordinator in project samza by apache.
the class TestZkStreamProcessorBase method createStreamProcessor.
protected StreamProcessor createStreamProcessor(final String pId, Map<String, String> map, final Object mutexStart, final Object mutexStop) {
map.put(ApplicationConfig.PROCESSOR_ID, pId);
Config config = new MapConfig(map);
JobCoordinator jobCoordinator = Util.<JobCoordinatorFactory>getObj(new JobCoordinatorConfig(config).getJobCoordinatorFactoryClassName()).getJobCoordinator(config);
StreamProcessorLifecycleListener listener = new StreamProcessorLifecycleListener() {
@Override
public void onStart() {
if (mutexStart != null) {
synchronized (mutexStart) {
mutexStart.notifyAll();
}
}
LOG.info("onStart is called for pid=" + pId);
}
@Override
public void onShutdown() {
if (mutexStop != null) {
synchronized (mutexStart) {
mutexStart.notify();
}
}
LOG.info("onShutdown is called for pid=" + pId);
}
@Override
public void onFailure(Throwable t) {
LOG.info("onFailure is called for pid=" + pId);
}
};
StreamProcessor processor = new StreamProcessor(config, new HashMap<>(), (StreamTaskFactory) TestStreamTask::new, listener, jobCoordinator);
return processor;
}
Aggregations