use of org.apache.samza.test.framework.system.descriptors.InMemoryOutputDescriptor in project samza by apache.
the class TestRunner method consumeStream.
/**
* Gets the contents of the output stream represented by {@code outputDescriptor} after {@link TestRunner#run(Duration)}
* has completed
*
* @param outputDescriptor describes the stream to be consumed
* @param timeout timeout for consumption of stream in Ms
* @param <StreamMessageType> type of message
*
* @return a map whose key is {@code partitionId} and value is messages in partition
* @throws SamzaException Thrown when a poll is incomplete
*/
public static <StreamMessageType> Map<Integer, List<StreamMessageType>> consumeStream(InMemoryOutputDescriptor outputDescriptor, Duration timeout) throws SamzaException {
Preconditions.checkNotNull(outputDescriptor);
String streamId = outputDescriptor.getStreamId();
String systemName = outputDescriptor.getSystemName();
Set<SystemStreamPartition> ssps = new HashSet<>();
Set<String> streamIds = new HashSet<>();
streamIds.add(streamId);
SystemFactory factory = new InMemorySystemFactory();
Config config = new MapConfig(outputDescriptor.toConfig(), outputDescriptor.getSystemDescriptor().toConfig());
Map<String, SystemStreamMetadata> metadata = factory.getAdmin(systemName, config).getSystemStreamMetadata(streamIds);
SystemConsumer consumer = factory.getConsumer(systemName, config, null);
String name = (String) outputDescriptor.getPhysicalName().orElse(streamId);
metadata.get(name).getSystemStreamPartitionMetadata().keySet().forEach(partition -> {
SystemStreamPartition temp = new SystemStreamPartition(systemName, streamId, partition);
ssps.add(temp);
consumer.register(temp, "0");
});
long t = System.currentTimeMillis();
Map<SystemStreamPartition, List<IncomingMessageEnvelope>> output = new HashMap<>();
HashSet<SystemStreamPartition> didNotReachEndOfStream = new HashSet<>(ssps);
while (System.currentTimeMillis() < t + timeout.toMillis()) {
Map<SystemStreamPartition, List<IncomingMessageEnvelope>> currentState = null;
try {
currentState = consumer.poll(ssps, 10);
} catch (InterruptedException e) {
throw new SamzaException("Timed out while consuming stream \n" + e.getMessage());
}
for (Map.Entry<SystemStreamPartition, List<IncomingMessageEnvelope>> entry : currentState.entrySet()) {
SystemStreamPartition ssp = entry.getKey();
output.computeIfAbsent(ssp, k -> new LinkedList<IncomingMessageEnvelope>());
List<IncomingMessageEnvelope> currentBuffer = entry.getValue();
int totalMessagesToFetch = Integer.valueOf(metadata.get(outputDescriptor.getStreamId()).getSystemStreamPartitionMetadata().get(ssp.getPartition()).getUpcomingOffset());
if (output.get(ssp).size() + currentBuffer.size() == totalMessagesToFetch) {
didNotReachEndOfStream.remove(entry.getKey());
ssps.remove(entry.getKey());
}
output.get(ssp).addAll(currentBuffer);
}
if (didNotReachEndOfStream.isEmpty()) {
break;
}
}
if (!didNotReachEndOfStream.isEmpty()) {
throw new IllegalStateException("Could not poll for all system stream partitions");
}
return output.entrySet().stream().collect(Collectors.toMap(entry -> entry.getKey().getPartition().getPartitionId(), entry -> entry.getValue().stream().map(e -> (StreamMessageType) e.getMessage()).collect(Collectors.toList())));
}
use of org.apache.samza.test.framework.system.descriptors.InMemoryOutputDescriptor in project samza by apache.
the class TestContext method testSamzaJobFailureForSyncTask.
/**
* Samza job logic expects integers, but doubles are passed here which results in failure
*/
@Test(expected = SamzaException.class)
public void testSamzaJobFailureForSyncTask() {
List<Double> inputList = Arrays.asList(1.2, 2.3, 3.33, 4.5);
InMemorySystemDescriptor isd = new InMemorySystemDescriptor("test");
InMemoryInputDescriptor<Double> imid = isd.getInputDescriptor("doubles", new NoOpSerde<Double>());
InMemoryOutputDescriptor imod = isd.getOutputDescriptor("output", new NoOpSerde<>());
TestRunner.of(MyStreamTestTask.class).addInputStream(imid, inputList).addOutputStream(imod, 1).addExternalContext(new TestContext(10)).run(Duration.ofSeconds(1));
}
use of org.apache.samza.test.framework.system.descriptors.InMemoryOutputDescriptor in project samza by apache.
the class AsyncStreamTaskIntegrationTest method testAsyncTaskWithSinglePartition.
@Test
public void testAsyncTaskWithSinglePartition() throws Exception {
List<Integer> inputList = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> outputList = Arrays.asList(10, 20, 30, 40, 50);
InMemorySystemDescriptor isd = new InMemorySystemDescriptor("async-test");
InMemoryInputDescriptor<Integer> imid = isd.getInputDescriptor("ints", new NoOpSerde<Integer>());
InMemoryOutputDescriptor imod = isd.getOutputDescriptor("ints-out", new NoOpSerde<>());
TestRunner.of(MyAsyncStreamTask.class).addInputStream(imid, inputList).addOutputStream(imod, 1).run(Duration.ofSeconds(2));
Assert.assertThat(TestRunner.consumeStream(imod, Duration.ofMillis(1000)).get(0), IsIterableContainingInOrder.contains(outputList.toArray()));
}
use of org.apache.samza.test.framework.system.descriptors.InMemoryOutputDescriptor in project samza by apache.
the class StreamApplicationIntegrationTest method testStatefulJoinWithLocalTable.
@Test
public void testStatefulJoinWithLocalTable() {
Random random = new Random();
List<KV<String, TestTableData.PageView>> pageViews = Arrays.asList(TestTableData.generatePageViews(10)).stream().map(x -> KV.of(PAGEKEYS[random.nextInt(PAGEKEYS.length)], x)).collect(Collectors.toList());
List<KV<String, TestTableData.Profile>> profiles = Arrays.asList(TestTableData.generateProfiles(10)).stream().map(x -> KV.of(PAGEKEYS[random.nextInt(PAGEKEYS.length)], x)).collect(Collectors.toList());
InMemorySystemDescriptor isd = new InMemorySystemDescriptor("test");
InMemoryInputDescriptor<KV<String, TestTableData.PageView>> pageViewStreamDesc = isd.getInputDescriptor("PageView", new NoOpSerde<KV<String, TestTableData.PageView>>());
InMemoryInputDescriptor<KV<String, TestTableData.Profile>> profileStreamDesc = isd.getInputDescriptor("Profile", new NoOpSerde<KV<String, TestTableData.Profile>>()).shouldBootstrap();
InMemoryOutputDescriptor<TestTableData.EnrichedPageView> outputStreamDesc = isd.getOutputDescriptor("EnrichedPageView", new NoOpSerde<>());
InMemoryOutputDescriptor<String> joinKeysDescriptor = isd.getOutputDescriptor("JoinPageKeys", new NoOpSerde<>());
TestRunner.of(new PageViewProfileViewJoinApplication()).addInputStream(pageViewStreamDesc, pageViews).addInputStream(profileStreamDesc, profiles).addOutputStream(outputStreamDesc, 1).addOutputStream(joinKeysDescriptor, 1).run(Duration.ofSeconds(2));
Assert.assertEquals(10, TestRunner.consumeStream(outputStreamDesc, Duration.ofSeconds(1)).get(0).size());
Assert.assertEquals(10, TestRunner.consumeStream(joinKeysDescriptor, Duration.ofSeconds(1)).get(0).size());
}
use of org.apache.samza.test.framework.system.descriptors.InMemoryOutputDescriptor in project samza by apache.
the class AsyncStreamTaskIntegrationTest method testAsyncTaskWithMultiplePartitionMultithreaded.
@Test
public void testAsyncTaskWithMultiplePartitionMultithreaded() throws Exception {
Map<Integer, List<KV>> inputPartitionData = new HashMap<>();
Map<Integer, List<Integer>> expectedOutputPartitionData = new HashMap<>();
genData(inputPartitionData, expectedOutputPartitionData);
InMemorySystemDescriptor isd = new InMemorySystemDescriptor("async-test");
InMemoryInputDescriptor<KV> imid = isd.getInputDescriptor("ints", new NoOpSerde<>());
InMemoryOutputDescriptor imod = isd.getOutputDescriptor("ints-out", new NoOpSerde<>());
TestRunner.of(MyAsyncStreamTask.class).addInputStream(imid, inputPartitionData).addOutputStream(imod, 5).addConfig("task.max.concurrency", "4").run(Duration.ofSeconds(2));
StreamAssert.containsInAnyOrder(expectedOutputPartitionData, imod, Duration.ofMillis(1000));
}
Aggregations