use of org.apache.samza.application.StreamApplication in project samza by apache.
the class TestWindowOperator method testCancellationOfOnceTrigger.
@Test
public void testCancellationOfOnceTrigger() throws Exception {
StreamApplication sgb = new KeyedTumblingWindowStreamApplication(AccumulationMode.ACCUMULATING, Duration.ofSeconds(1), Triggers.count(2));
TestClock testClock = new TestClock();
StreamOperatorTask task = new StreamOperatorTask(sgb, runner, testClock);
task.init(config, taskContext);
List<WindowPane<Integer, Collection<IntegerEnvelope>>> windowPanes = new ArrayList<>();
MessageCollector messageCollector = envelope -> windowPanes.add((WindowPane<Integer, Collection<IntegerEnvelope>>) envelope.getMessage());
task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
Assert.assertEquals(windowPanes.size(), 1);
Assert.assertEquals(windowPanes.get(0).getKey().getPaneId(), "0");
Assert.assertEquals(windowPanes.get(0).getKey().getKey(), new Integer(1));
Assert.assertEquals(windowPanes.get(0).getFiringType(), FiringType.EARLY);
task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
Assert.assertEquals(windowPanes.size(), 1);
testClock.advanceTime(Duration.ofSeconds(1));
task.window(messageCollector, taskCoordinator);
Assert.assertEquals(windowPanes.size(), 2);
Assert.assertEquals(windowPanes.get(0).getKey().getPaneId(), "0");
Assert.assertEquals(windowPanes.get(1).getKey().getPaneId(), "0");
Assert.assertEquals(windowPanes.get(1).getFiringType(), FiringType.DEFAULT);
task.process(new IntegerEnvelope(3), messageCollector, taskCoordinator);
testClock.advanceTime(Duration.ofSeconds(1));
task.window(messageCollector, taskCoordinator);
Assert.assertEquals(windowPanes.size(), 3);
Assert.assertEquals(windowPanes.get(2).getKey().getKey(), new Integer(3));
Assert.assertEquals(windowPanes.get(2).getKey().getPaneId(), "1000");
Assert.assertEquals(windowPanes.get(2).getFiringType(), FiringType.DEFAULT);
Assert.assertEquals((windowPanes.get(2).getMessage()).size(), 1);
}
use of org.apache.samza.application.StreamApplication in project samza by apache.
the class TestWindowOperator method testCancellationOfAnyTrigger.
@Test
public void testCancellationOfAnyTrigger() throws Exception {
StreamApplication sgb = new KeyedTumblingWindowStreamApplication(AccumulationMode.ACCUMULATING, Duration.ofSeconds(1), Triggers.any(Triggers.count(2), Triggers.timeSinceFirstMessage(Duration.ofMillis(500))));
TestClock testClock = new TestClock();
StreamOperatorTask task = new StreamOperatorTask(sgb, runner, testClock);
task.init(config, taskContext);
List<WindowPane<Integer, Collection<IntegerEnvelope>>> windowPanes = new ArrayList<>();
MessageCollector messageCollector = envelope -> windowPanes.add((WindowPane<Integer, Collection<IntegerEnvelope>>) envelope.getMessage());
task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
//assert that the count trigger fired
Assert.assertEquals(windowPanes.size(), 1);
//advance the timer to enable the triggering of the inner timeSinceFirstMessage trigger
testClock.advanceTime(Duration.ofMillis(500));
//assert that the triggering of the count trigger cancelled the inner timeSinceFirstMessage trigger
Assert.assertEquals(windowPanes.size(), 1);
task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
//advance timer by 500 more millis to enable the default trigger
testClock.advanceTime(Duration.ofMillis(500));
task.window(messageCollector, taskCoordinator);
//assert that the default trigger fired
Assert.assertEquals(windowPanes.size(), 2);
Assert.assertEquals(windowPanes.get(1).getFiringType(), FiringType.DEFAULT);
Assert.assertEquals(windowPanes.get(1).getKey().getKey(), new Integer(1));
Assert.assertEquals(windowPanes.get(1).getKey().getPaneId(), "0");
Assert.assertEquals((windowPanes.get(1).getMessage()).size(), 5);
task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
//advance timer by 500 millis to enable the inner timeSinceFirstMessage trigger
testClock.advanceTime(Duration.ofMillis(500));
task.window(messageCollector, taskCoordinator);
Assert.assertEquals(windowPanes.size(), 3);
Assert.assertEquals(windowPanes.get(2).getFiringType(), FiringType.EARLY);
Assert.assertEquals(windowPanes.get(2).getKey().getKey(), new Integer(1));
Assert.assertEquals(windowPanes.get(2).getKey().getPaneId(), "1000");
//advance timer by > 500 millis to enable the default trigger
testClock.advanceTime(Duration.ofMillis(900));
task.window(messageCollector, taskCoordinator);
Assert.assertEquals(windowPanes.size(), 4);
Assert.assertEquals(windowPanes.get(3).getFiringType(), FiringType.DEFAULT);
Assert.assertEquals(windowPanes.get(3).getKey().getKey(), new Integer(1));
Assert.assertEquals(windowPanes.get(3).getKey().getPaneId(), "1000");
}
use of org.apache.samza.application.StreamApplication in project samza by apache.
the class TestLocalApplicationRunner method testRunComplete.
@Test
public void testRunComplete() throws Exception {
final Map<String, String> config = new HashMap<>();
config.put(ApplicationConfig.APP_PROCESSOR_ID_GENERATOR_CLASS, UUIDGenerator.class.getName());
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);
StreamProcessor sp = mock(StreamProcessor.class);
ArgumentCaptor<StreamProcessorLifecycleListener> captor = ArgumentCaptor.forClass(StreamProcessorLifecycleListener.class);
doAnswer(i -> {
StreamProcessorLifecycleListener listener = captor.getValue();
listener.onStart();
listener.onShutdown();
return null;
}).when(sp).start();
LocalApplicationRunner spy = spy(runner);
doReturn(sp).when(spy).createStreamProcessor(anyObject(), anyObject(), captor.capture());
spy.run(app);
assertEquals(spy.status(app), ApplicationStatus.SuccessfulFinish);
}
use of org.apache.samza.application.StreamApplication in project samza by apache.
the class TestLocalApplicationRunner method testStreamCreationWithCoordination.
@Test
public void testStreamCreationWithCoordination() 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);
CoordinationUtils coordinationUtils = mock(CoordinationUtils.class);
LeaderElector leaderElector = new LeaderElector() {
private LeaderElectorListener leaderElectorListener;
@Override
public void setLeaderElectorListener(LeaderElectorListener listener) {
this.leaderElectorListener = listener;
}
@Override
public void tryBecomeLeader() {
leaderElectorListener.onBecomingLeader();
}
@Override
public void resignLeadership() {
}
@Override
public boolean amILeader() {
return false;
}
};
Latch latch = new Latch() {
boolean done = false;
@Override
public void await(long timeout, TimeUnit tu) throws TimeoutException {
// in this test, latch is released before wait
assertTrue(done);
}
@Override
public void countDown() {
done = true;
}
};
when(coordinationUtils.getLeaderElector()).thenReturn(leaderElector);
when(coordinationUtils.getLatch(anyInt(), anyString())).thenReturn(latch);
doReturn(coordinationUtils).when(spy).createCoordinationUtils();
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 TestTaskFactoryUtil method testCreateStreamApplicationWithTaskClass.
@Test
public void testCreateStreamApplicationWithTaskClass() throws Exception {
Config config = new MapConfig(new HashMap<String, String>() {
{
this.put(ApplicationConfig.APP_CLASS, "org.apache.samza.testUtils.TestStreamApplication");
}
});
StreamApplication streamApp = TaskFactoryUtil.createStreamApplication(config);
assertNotNull(streamApp);
config = new MapConfig(new HashMap<String, String>() {
{
this.put("task.class", "org.apache.samza.testUtils.TestAsyncStreamTask");
this.put(ApplicationConfig.APP_CLASS, "org.apache.samza.testUtils.TestStreamApplication");
}
});
try {
TaskFactoryUtil.createStreamApplication(config);
fail("should have failed with invalid config");
} catch (ConfigException ce) {
// expected
}
config = new MapConfig(new HashMap<String, String>() {
{
this.put("task.class", "no.such.class");
this.put(ApplicationConfig.APP_CLASS, "org.apache.samza.testUtils.TestStreamApplication");
}
});
try {
TaskFactoryUtil.createStreamApplication(config);
fail("should have failed with invalid config");
} catch (ConfigException ce) {
// expected
}
}
Aggregations