use of org.apache.samza.context.ExternalContext in project samza by apache.
the class TestLocalApplicationRunner method testKill.
@Test
public void testKill() throws Exception {
Map<String, String> cfgs = new HashMap<>();
cfgs.put(ApplicationConfig.APP_PROCESSOR_ID_GENERATOR_CLASS, UUIDGenerator.class.getName());
config = new MapConfig(cfgs);
ProcessorLifecycleListenerFactory mockFactory = (pContext, cfg) -> mock(ProcessorLifecycleListener.class);
mockApp = (StreamApplication) appDesc -> appDesc.withProcessorLifecycleListenerFactory(mockFactory);
prepareTest();
// return the jobConfigs from the planner
doReturn(Collections.singletonList(new JobConfig(new MapConfig(config)))).when(localPlanner).prepareJobs();
StreamProcessor sp = mock(StreamProcessor.class);
CoordinatorStreamStore coordinatorStreamStore = mock(CoordinatorStreamStore.class);
ArgumentCaptor<StreamProcessor.StreamProcessorLifecycleListenerFactory> captor = ArgumentCaptor.forClass(StreamProcessor.StreamProcessorLifecycleListenerFactory.class);
doAnswer(i -> {
ProcessorLifecycleListener listener = captor.getValue().createInstance(sp);
listener.afterStart();
return null;
}).when(sp).start();
doAnswer(new Answer() {
private int count = 0;
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
if (++count == 1) {
ProcessorLifecycleListener listener = captor.getValue().createInstance(sp);
listener.afterStop();
return null;
}
return null;
}
}).when(sp).stop();
ExternalContext externalContext = mock(ExternalContext.class);
doReturn(sp).when(runner).createStreamProcessor(anyObject(), anyObject(), captor.capture(), eq(Optional.of(externalContext)), any(CoordinatorStreamStore.class));
doReturn(coordinatorStreamStore).when(runner).createCoordinatorStreamStore(any(Config.class));
runner.run(externalContext);
runner.kill();
verify(coordinatorStreamStore).init();
verify(coordinatorStreamStore, atLeastOnce()).close();
assertEquals(runner.status(), ApplicationStatus.SuccessfulFinish);
}
use of org.apache.samza.context.ExternalContext in project samza by apache.
the class TestLocalApplicationRunner method testRunStreamTask.
@Test
public void testRunStreamTask() throws Exception {
final Map<String, String> cfgs = new HashMap<>();
cfgs.put(ApplicationConfig.APP_PROCESSOR_ID_GENERATOR_CLASS, UUIDGenerator.class.getName());
cfgs.put(ApplicationConfig.APP_NAME, "test-app");
cfgs.put(ApplicationConfig.APP_ID, "test-appId");
config = new MapConfig(cfgs);
mockApp = new LegacyTaskApplication(IdentityStreamTask.class.getName());
prepareTest();
StreamProcessor sp = mock(StreamProcessor.class);
CoordinatorStreamStore metadataStore = mock(CoordinatorStreamStore.class);
ArgumentCaptor<StreamProcessor.StreamProcessorLifecycleListenerFactory> captor = ArgumentCaptor.forClass(StreamProcessor.StreamProcessorLifecycleListenerFactory.class);
doAnswer(i -> {
ProcessorLifecycleListener listener = captor.getValue().createInstance(sp);
listener.afterStart();
listener.afterStop();
return null;
}).when(sp).start();
ExternalContext externalContext = mock(ExternalContext.class);
doReturn(sp).when(runner).createStreamProcessor(anyObject(), anyObject(), captor.capture(), eq(Optional.of(externalContext)), any(CoordinatorStreamStore.class));
doReturn(metadataStore).when(runner).createCoordinatorStreamStore(any(Config.class));
doReturn(ApplicationStatus.SuccessfulFinish).when(runner).status();
runner.run(externalContext);
verify(metadataStore).init();
verify(metadataStore).close();
assertEquals(ApplicationStatus.SuccessfulFinish, runner.status());
}
use of org.apache.samza.context.ExternalContext in project samza by apache.
the class TestLocalApplicationRunner method testKillWithoutCoordinatorStream.
@Test
public void testKillWithoutCoordinatorStream() throws Exception {
Map<String, String> cfgs = new HashMap<>();
cfgs.put(ApplicationConfig.APP_PROCESSOR_ID_GENERATOR_CLASS, UUIDGenerator.class.getName());
config = new MapConfig(cfgs);
ProcessorLifecycleListenerFactory mockFactory = (pContext, cfg) -> mock(ProcessorLifecycleListener.class);
mockApp = (StreamApplication) appDesc -> appDesc.withProcessorLifecycleListenerFactory(mockFactory);
prepareTest();
// return the jobConfigs from the planner
doReturn(Collections.singletonList(new JobConfig(new MapConfig(config)))).when(localPlanner).prepareJobs();
StreamProcessor sp = mock(StreamProcessor.class);
ArgumentCaptor<StreamProcessor.StreamProcessorLifecycleListenerFactory> captor = ArgumentCaptor.forClass(StreamProcessor.StreamProcessorLifecycleListenerFactory.class);
doAnswer(i -> {
ProcessorLifecycleListener listener = captor.getValue().createInstance(sp);
listener.afterStart();
return null;
}).when(sp).start();
doAnswer(new Answer() {
private int count = 0;
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
if (++count == 1) {
ProcessorLifecycleListener listener = captor.getValue().createInstance(sp);
listener.afterStop();
return null;
}
return null;
}
}).when(sp).stop();
ExternalContext externalContext = mock(ExternalContext.class);
doReturn(sp).when(runner).createStreamProcessor(anyObject(), anyObject(), captor.capture(), eq(Optional.of(externalContext)), any(CoordinatorStreamStore.class));
doReturn(null).when(runner).createCoordinatorStreamStore(any(Config.class));
runner.run(externalContext);
runner.kill();
assertEquals(runner.status(), ApplicationStatus.SuccessfulFinish);
}
use of org.apache.samza.context.ExternalContext in project samza by apache.
the class TestLocalApplicationRunner method testRunFailure.
@Test
public void testRunFailure() throws Exception {
Map<String, String> cfgs = new HashMap<>();
cfgs.put(ApplicationConfig.PROCESSOR_ID, "0");
config = new MapConfig(cfgs);
ProcessorLifecycleListenerFactory mockFactory = (pContext, cfg) -> mock(ProcessorLifecycleListener.class);
mockApp = (StreamApplication) appDesc -> appDesc.withProcessorLifecycleListenerFactory(mockFactory);
prepareTest();
// return the jobConfigs from the planner
doReturn(Collections.singletonList(new JobConfig(new MapConfig(config)))).when(localPlanner).prepareJobs();
StreamProcessor sp = mock(StreamProcessor.class);
CoordinatorStreamStore coordinatorStreamStore = mock(CoordinatorStreamStore.class);
ArgumentCaptor<StreamProcessor.StreamProcessorLifecycleListenerFactory> captor = ArgumentCaptor.forClass(StreamProcessor.StreamProcessorLifecycleListenerFactory.class);
doAnswer(i -> {
throw new Exception("test failure");
}).when(sp).start();
ExternalContext externalContext = mock(ExternalContext.class);
doReturn(sp).when(runner).createStreamProcessor(anyObject(), anyObject(), captor.capture(), eq(Optional.of(externalContext)), any(CoordinatorStreamStore.class));
doReturn(coordinatorStreamStore).when(runner).createCoordinatorStreamStore(any(Config.class));
try {
runner.run(externalContext);
runner.waitForFinish();
} catch (Throwable th) {
assertNotNull(th);
}
verify(coordinatorStreamStore).init();
verify(coordinatorStreamStore, never()).close();
assertEquals(runner.status(), ApplicationStatus.UnsuccessfulFinish);
}
use of org.apache.samza.context.ExternalContext in project samza by apache.
the class QueryTranslator method translate.
/**
* For unit testing only
*/
@VisibleForTesting
void translate(SamzaSqlQueryParser.QueryInfo queryInfo, StreamApplicationDescriptor appDesc, int queryId) {
QueryPlanner planner = new QueryPlanner(sqlConfig.getRelSchemaProviders(), sqlConfig.getInputSystemStreamConfigBySource(), sqlConfig.getUdfMetadata(), sqlConfig.isQueryPlanOptimizerEnabled());
final RelRoot relRoot = planner.plan(queryInfo.getSelectQuery());
SamzaSqlExecutionContext executionContext = new SamzaSqlExecutionContext(sqlConfig);
TranslatorContext translatorContext = new TranslatorContext(appDesc, relRoot, executionContext);
translate(relRoot, sqlConfig.getOutputSystemStreams().get(queryId), translatorContext, queryId);
Map<Integer, TranslatorContext> translatorContexts = new HashMap<>();
translatorContexts.put(queryId, translatorContext.clone());
appDesc.withApplicationTaskContextFactory(new ApplicationTaskContextFactory<SamzaSqlApplicationContext>() {
@Override
public SamzaSqlApplicationContext create(ExternalContext externalContext, JobContext jobContext, ContainerContext containerContext, TaskContext taskContext, ApplicationContainerContext applicationContainerContext) {
return new SamzaSqlApplicationContext(translatorContexts);
}
});
}
Aggregations