use of org.apache.samza.application.StreamApplication in project samza by apache.
the class TestWindowOperator method testTumblingWindowsAccumulatingMode.
@Test
public void testTumblingWindowsAccumulatingMode() throws Exception {
StreamApplication sgb = new KeyedTumblingWindowStreamApplication(AccumulationMode.ACCUMULATING, Duration.ofSeconds(1), Triggers.repeat(Triggers.count(2)));
List<WindowPane<Integer, Collection<IntegerEnvelope>>> windowPanes = new ArrayList<>();
TestClock testClock = new TestClock();
StreamOperatorTask task = new StreamOperatorTask(sgb, runner, testClock);
task.init(config, taskContext);
MessageCollector messageCollector = envelope -> windowPanes.add((WindowPane<Integer, Collection<IntegerEnvelope>>) envelope.getMessage());
integers.forEach(n -> task.process(new IntegerEnvelope(n), messageCollector, taskCoordinator));
testClock.advanceTime(Duration.ofSeconds(1));
task.window(messageCollector, taskCoordinator);
Assert.assertEquals(windowPanes.size(), 7);
Assert.assertEquals(windowPanes.get(0).getKey().getKey(), new Integer(1));
Assert.assertEquals((windowPanes.get(0).getMessage()).size(), 2);
Assert.assertEquals(windowPanes.get(1).getKey().getKey(), new Integer(2));
Assert.assertEquals((windowPanes.get(1).getMessage()).size(), 2);
Assert.assertEquals(windowPanes.get(2).getKey().getKey(), new Integer(1));
Assert.assertEquals((windowPanes.get(2).getMessage()).size(), 4);
Assert.assertEquals(windowPanes.get(3).getKey().getKey(), new Integer(2));
Assert.assertEquals((windowPanes.get(3).getMessage()).size(), 4);
}
use of org.apache.samza.application.StreamApplication in project samza by apache.
the class TestLocalApplicationRunner method testStreamCreation.
@Test
public void testStreamCreation() throws Exception {
Map<String, String> config = new HashMap<>();
LocalApplicationRunner runner = new LocalApplicationRunner(new MapConfig(config));
StreamApplication app = mock(StreamApplication.class);
doNothing().when(app).init(anyObject(), anyObject());
ExecutionPlanner planner = mock(ExecutionPlanner.class);
Field plannerField = runner.getClass().getSuperclass().getDeclaredField("planner");
plannerField.setAccessible(true);
plannerField.set(runner, planner);
StreamManager streamManager = mock(StreamManager.class);
Field streamManagerField = runner.getClass().getSuperclass().getDeclaredField("streamManager");
streamManagerField.setAccessible(true);
streamManagerField.set(runner, streamManager);
ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
ExecutionPlan plan = new ExecutionPlan() {
@Override
public List<JobConfig> getJobConfigs() {
return Collections.emptyList();
}
@Override
public List<StreamSpec> getIntermediateStreams() {
return Collections.singletonList(new StreamSpec("test-stream", "test-stream", "test-system"));
}
@Override
public String getPlanAsJson() throws Exception {
return "";
}
};
when(planner.plan(anyObject())).thenReturn(plan);
LocalApplicationRunner spy = spy(runner);
try {
spy.run(app);
} catch (Throwable t) {
//no jobs exception
assertNotNull(t);
}
verify(streamManager).createStreams(captor.capture());
List<StreamSpec> streamSpecs = captor.getValue();
assertEquals(streamSpecs.size(), 1);
assertEquals(streamSpecs.get(0).getId(), "test-stream");
}
use of org.apache.samza.application.StreamApplication in project samza by apache.
the class TestLocalApplicationRunner method testRunFailure.
@Test
public void testRunFailure() throws Exception {
final Map<String, String> config = new HashMap<>();
config.put(ApplicationConfig.PROCESSOR_ID, "0");
LocalApplicationRunner runner = new LocalApplicationRunner(new MapConfig(config));
StreamApplication app = mock(StreamApplication.class);
doNothing().when(app).init(anyObject(), anyObject());
ExecutionPlanner planner = mock(ExecutionPlanner.class);
Field plannerField = runner.getClass().getSuperclass().getDeclaredField("planner");
plannerField.setAccessible(true);
plannerField.set(runner, planner);
ExecutionPlan plan = new ExecutionPlan() {
@Override
public List<JobConfig> getJobConfigs() {
return Collections.singletonList(new JobConfig(new MapConfig(config)));
}
@Override
public List<StreamSpec> getIntermediateStreams() {
return Collections.emptyList();
}
@Override
public String getPlanAsJson() throws Exception {
return "";
}
};
when(planner.plan(anyObject())).thenReturn(plan);
Throwable t = new Throwable("test failure");
StreamProcessor sp = mock(StreamProcessor.class);
ArgumentCaptor<StreamProcessorLifecycleListener> captor = ArgumentCaptor.forClass(StreamProcessorLifecycleListener.class);
doAnswer(i -> {
StreamProcessorLifecycleListener listener = captor.getValue();
listener.onFailure(t);
return null;
}).when(sp).start();
LocalApplicationRunner spy = spy(runner);
doReturn(sp).when(spy).createStreamProcessor(anyObject(), anyObject(), captor.capture());
try {
spy.run(app);
} catch (Throwable th) {
assertNotNull(th);
}
assertEquals(spy.status(app), ApplicationStatus.UnsuccessfulFinish);
}
use of org.apache.samza.application.StreamApplication in project samza by apache.
the class TestTaskFactoryUtil method testStreamTaskClassWithInvalidStreamApplication.
@Test
public void testStreamTaskClassWithInvalidStreamApplication() throws Exception {
Config config = new MapConfig(new HashMap<String, String>() {
{
this.put(ApplicationConfig.APP_CLASS, "org.apache.samza.testUtils.InvalidStreamApplication");
}
});
try {
TaskFactoryUtil.createStreamApplication(config);
fail("Should have failed w/ no.such.class");
} catch (ConfigException ce) {
// expected
}
config = new MapConfig(new HashMap<String, String>() {
{
this.put("task.class", "org.apache.samza.testUtils.TestStreamTask");
this.put(ApplicationConfig.APP_CLASS, "");
}
});
StreamApplication streamApp = TaskFactoryUtil.createStreamApplication(config);
Object retFactory = TaskFactoryUtil.createTaskFactory(config, streamApp, mockRunner);
assertTrue(retFactory instanceof StreamTaskFactory);
assertTrue(((StreamTaskFactory) retFactory).createInstance() instanceof TestStreamTask);
config = new MapConfig(new HashMap<String, String>() {
{
this.put("task.class", "");
this.put(ApplicationConfig.APP_CLASS, "org.apache.samza.testUtils.InvalidStreamApplication");
}
});
try {
TaskFactoryUtil.createStreamApplication(config);
fail("Should have failed w/ no class not found");
} catch (ConfigException cne) {
// expected
}
}
use of org.apache.samza.application.StreamApplication in project samza by apache.
the class TestTaskFactoryUtil method testAsyncStreamTaskWithInvalidStreamGraphBuilder.
@Test
public void testAsyncStreamTaskWithInvalidStreamGraphBuilder() throws Exception {
Config config = new MapConfig(new HashMap<String, String>() {
{
this.put(ApplicationConfig.APP_CLASS, "org.apache.samza.testUtils.InvalidStreamApplication");
}
});
try {
TaskFactoryUtil.createStreamApplication(config);
fail("Should have failed w/ no.such.class");
} catch (ConfigException cfe) {
// expected
}
config = new MapConfig(new HashMap<String, String>() {
{
this.put("task.class", "org.apache.samza.testUtils.TestAsyncStreamTask");
this.put(ApplicationConfig.APP_CLASS, "");
}
});
StreamApplication streamApp = TaskFactoryUtil.createStreamApplication(config);
Object retFactory = TaskFactoryUtil.createTaskFactory(config, streamApp, mockRunner);
assertTrue(retFactory instanceof AsyncStreamTaskFactory);
assertTrue(((AsyncStreamTaskFactory) retFactory).createInstance() instanceof TestAsyncStreamTask);
config = new MapConfig(new HashMap<String, String>() {
{
this.put("task.class", "org.apache.samza.testUtils.TestAsyncStreamTask");
this.put(ApplicationConfig.APP_CLASS, null);
}
});
streamApp = TaskFactoryUtil.createStreamApplication(config);
retFactory = TaskFactoryUtil.createTaskFactory(config, streamApp, mockRunner);
assertTrue(retFactory instanceof AsyncStreamTaskFactory);
assertTrue(((AsyncStreamTaskFactory) retFactory).createInstance() instanceof TestAsyncStreamTask);
}
Aggregations