use of org.apache.samza.test.framework.system.descriptors.InMemorySystemDescriptor in project samza by apache.
the class TestLocalTableWithLowLevelApiEndToEnd method testTableWithLowLevelApi.
@Test
public void testTableWithLowLevelApi() {
InMemorySystemDescriptor isd = new InMemorySystemDescriptor("test");
InMemoryInputDescriptor<TestTableData.PageView> inputDescriptor = isd.getInputDescriptor("PageView", new NoOpSerde<>());
TestRunner.of(new TestLocalTableWithConfigRewriterEndToEnd.MyTaskApplication()).addInputStream(inputDescriptor, TestTableData.generatePartitionedPageViews(40, 4)).addConfig("job.config.rewriters", "my-rewriter").addConfig("job.config.rewriter.my-rewriter.class", TestLocalTableWithConfigRewriterEndToEnd.MyConfigRewriter.class.getName()).run(Duration.ofSeconds(10));
}
use of org.apache.samza.test.framework.system.descriptors.InMemorySystemDescriptor in project samza by apache.
the class TestSchedulerFunction method testImmediateTimer.
@Test
public void testImmediateTimer() {
final InMemorySystemDescriptor isd = new InMemorySystemDescriptor("test");
final InMemoryInputDescriptor<Integer> imid = isd.getInputDescriptor("test-input", new IntegerSerde());
StreamApplication app = new StreamApplication() {
@Override
public void describe(StreamApplicationDescriptor appDescriptor) {
appDescriptor.getInputStream(imid).map(new TestFunction());
}
};
TestRunner.of(app).addInputStream(imid, Arrays.asList(1, 2, 3, 4, 5)).run(Duration.ofSeconds(1));
assertTrue(timerFired.get());
}
use of org.apache.samza.test.framework.system.descriptors.InMemorySystemDescriptor in project samza by apache.
the class TestRunner method initializeInMemoryInputStream.
/**
* Creates an in memory stream with {@link InMemorySystemFactory} and feeds its partition with stream of messages
* @param partitionData key of the map represents partitionId and value represents messages in the partition
* @param descriptor describes a stream to initialize with the in memory system
*/
private <StreamMessageType> void initializeInMemoryInputStream(InMemoryInputDescriptor<?> descriptor, Map<Integer, Iterable<StreamMessageType>> partitionData) {
String systemName = descriptor.getSystemName();
String streamName = (String) descriptor.getPhysicalName().orElse(descriptor.getStreamId());
if (this.app instanceof LegacyTaskApplication) {
// for legacy applications that only specify task.class.
if (configs.containsKey(TaskConfig.INPUT_STREAMS)) {
configs.put(TaskConfig.INPUT_STREAMS, configs.get(TaskConfig.INPUT_STREAMS).concat("," + systemName + "." + streamName));
} else {
configs.put(TaskConfig.INPUT_STREAMS, systemName + "." + streamName);
}
}
InMemorySystemDescriptor imsd = (InMemorySystemDescriptor) descriptor.getSystemDescriptor();
imsd.withInMemoryScope(this.inMemoryScope);
addConfig(descriptor.toConfig());
addConfig(descriptor.getSystemDescriptor().toConfig());
addSerdeConfigs(descriptor);
StreamSpec spec = new StreamSpec(descriptor.getStreamId(), streamName, systemName, partitionData.size());
SystemFactory factory = new InMemorySystemFactory();
Config config = new MapConfig(descriptor.toConfig(), descriptor.getSystemDescriptor().toConfig());
factory.getAdmin(systemName, config).createStream(spec);
InMemorySystemProducer producer = (InMemorySystemProducer) factory.getProducer(systemName, config, null);
SystemStream sysStream = new SystemStream(systemName, streamName);
partitionData.forEach((partitionId, partition) -> {
partition.forEach(e -> {
Object key = e instanceof KV ? ((KV) e).getKey() : null;
Object value = e instanceof KV ? ((KV) e).getValue() : e;
if (value instanceof IncomingMessageEnvelope) {
producer.send((IncomingMessageEnvelope) value);
} else {
producer.send(systemName, new OutgoingMessageEnvelope(sysStream, Integer.valueOf(partitionId), key, value));
}
});
producer.send(systemName, new OutgoingMessageEnvelope(sysStream, Integer.valueOf(partitionId), null, new EndOfStreamMessage(null)));
});
}
use of org.apache.samza.test.framework.system.descriptors.InMemorySystemDescriptor in project samza by apache.
the class TestRunner method addOutputStream.
/**
* Adds the provided output stream to the test application. Default configs and user added configs have a higher
* precedence over system and stream descriptor generated configs.
* @param streamDescriptor describes the stream that is supposed to be output for the Samza application
* @param partitionCount partition count of output stream
* @return this {@link TestRunner}
*/
public TestRunner addOutputStream(InMemoryOutputDescriptor<?> streamDescriptor, int partitionCount) {
Preconditions.checkNotNull(streamDescriptor);
Preconditions.checkState(partitionCount >= 1);
InMemorySystemDescriptor imsd = (InMemorySystemDescriptor) streamDescriptor.getSystemDescriptor();
imsd.withInMemoryScope(this.inMemoryScope);
Config config = new MapConfig(streamDescriptor.toConfig(), streamDescriptor.getSystemDescriptor().toConfig());
InMemorySystemFactory factory = new InMemorySystemFactory();
String physicalName = (String) streamDescriptor.getPhysicalName().orElse(streamDescriptor.getStreamId());
StreamSpec spec = new StreamSpec(streamDescriptor.getStreamId(), physicalName, streamDescriptor.getSystemName(), partitionCount);
factory.getAdmin(streamDescriptor.getSystemName(), config).createStream(spec);
addConfig(streamDescriptor.toConfig());
addConfig(streamDescriptor.getSystemDescriptor().toConfig());
addSerdeConfigs(streamDescriptor);
return this;
}
use of org.apache.samza.test.framework.system.descriptors.InMemorySystemDescriptor in project samza by apache.
the class TestRepartitionWindowApp method testRepartitionedSessionWindowCounter.
@Test
public void testRepartitionedSessionWindowCounter() throws Exception {
Map<Integer, List<KV<String, PageView>>> pageViews = new HashMap<>();
pageViews.put(0, ImmutableList.of(KV.of("userId1", new PageView("india", "5.com", "userId1")), KV.of("userId1", new PageView("india", "2.com", "userId1"))));
pageViews.put(1, ImmutableList.of(KV.of("userId2", new PageView("china", "4.com", "userId2")), KV.of("userId1", new PageView("india", "3.com", "userId1"))));
pageViews.put(2, ImmutableList.of(KV.of("userId1", new PageView("india", "1.com", "userId1"))));
InMemorySystemDescriptor sd = new InMemorySystemDescriptor(SYSTEM);
InMemoryInputDescriptor<KV<String, PageView>> inputDescriptor = sd.getInputDescriptor(INPUT_TOPIC, KVSerde.of(new NoOpSerde<>(), new NoOpSerde<>()));
/*
* Technically, this should have a message type of KV, because a KV is passed to sendTo, but
* StreamAssert.containsInAnyOrder requires the type to match the output type of the actual messages. In
* high-level, sendTo splits up the KV, so the actual messages are just the "V" part of the KV.
* TestRunner only uses NoOpSerde anyways, so it doesn't matter if the typing isn't KV.
*/
InMemoryOutputDescriptor<String> outputDescriptor = sd.getOutputDescriptor(OUTPUT_TOPIC, new NoOpSerde<>());
TestRunner.of(new RepartitionWindowApp()).addInputStream(inputDescriptor, pageViews).addOutputStream(outputDescriptor, 1).addConfig("task.window.ms", "1000").run(Duration.ofSeconds(10));
StreamAssert.containsInAnyOrder(Arrays.asList("userId1 4", "userId2 1"), outputDescriptor, Duration.ofSeconds(1));
}
Aggregations