use of co.cask.cdap.api.flow.flowlet.StreamEvent in project cdap by caskdata.
the class StreamDataFileTestBase method testEmptyFile.
@Test
public void testEmptyFile() throws Exception {
Location dir = StreamFileTestUtils.createTempDir(getLocationFactory());
Location eventFile = dir.getTempFile(".dat");
Location indexFile = dir.getTempFile(".idx");
// Creates a stream file that has no event inside
StreamDataFileWriter writer = new StreamDataFileWriter(Locations.newOutputSupplier(eventFile), Locations.newOutputSupplier(indexFile), 10000L);
writer.close();
// Create a reader that starts from beginning.
StreamDataFileReader reader = StreamDataFileReader.create(Locations.newInputSupplier(eventFile));
List<StreamEvent> events = Lists.newArrayList();
Assert.assertEquals(-1, reader.read(events, 1, 0, TimeUnit.SECONDS));
reader.close();
}
use of co.cask.cdap.api.flow.flowlet.StreamEvent in project cdap by caskdata.
the class StreamEventCodecTest method testEncodeDecodeWithDatumDecoder.
@Test
public void testEncodeDecodeWithDatumDecoder() throws UnsupportedTypeException, IOException {
StreamEvent event = new StreamEvent(Maps.<String, String>newHashMap(), ByteBuffer.wrap("Event string".getBytes(Charsets.UTF_8)));
StreamEventCodec codec = new StreamEventCodec();
ByteBuffer payload = ByteBuffer.wrap(codec.encodePayload(event));
SchemaHash schemaHash = new SchemaHash(payload);
Schema schema = new ReflectionSchemaGenerator().generate(StreamEvent.class);
Assert.assertEquals(schema.getSchemaHash(), schemaHash);
StreamEvent decoded = new ReflectionDatumReader<>(schema, TypeToken.of(StreamEvent.class)).read(new BinaryDecoder(new ByteBufferInputStream(payload)), schema);
Assert.assertEquals(event.getHeaders(), decoded.getHeaders());
Assert.assertEquals(event.getBody(), decoded.getBody());
}
use of co.cask.cdap.api.flow.flowlet.StreamEvent in project cdap by caskdata.
the class StreamEventCodecTest method testEncodeDecode.
@Test
public void testEncodeDecode() {
StreamEvent event = new StreamEvent(Maps.<String, String>newHashMap(), ByteBuffer.wrap("Event string".getBytes(Charsets.UTF_8)));
StreamEventCodec codec = new StreamEventCodec();
StreamEvent decodedEvent = codec.decodePayload(codec.encodePayload(event));
Assert.assertEquals(event.getHeaders(), decodedEvent.getHeaders());
Assert.assertEquals(event.getBody(), decodedEvent.getBody());
}
use of co.cask.cdap.api.flow.flowlet.StreamEvent in project cdap by caskdata.
the class StreamConsumerTestBase method writeEvents.
private List<StreamEvent> writeEvents(FileWriter<StreamEvent> streamWriter, String msgPrefix, int count, Clock clock) throws IOException {
Map<String, String> headers = ImmutableMap.of();
List<StreamEvent> result = Lists.newLinkedList();
for (int i = 0; i < count; i++) {
String msg = msgPrefix + i;
StreamEvent event = new StreamEvent(headers, Charsets.UTF_8.encode(msg), clock.getTime());
result.add(event);
streamWriter.append(event);
}
return result;
}
use of co.cask.cdap.api.flow.flowlet.StreamEvent in project cdap by caskdata.
the class StreamConsumerTestBase method verifyEvents.
private void verifyEvents(StreamConsumer consumer, Collection<StreamEvent> expectedEvents) throws Exception {
TransactionContext txContext = createTxContext(consumer);
txContext.start();
try {
Set<StreamEvent> actualEvents = Sets.newTreeSet(STREAM_EVENT_COMPARATOR);
int size = expectedEvents.size();
DequeueResult<StreamEvent> result = consumer.poll(size == 0 ? 1 : size, 1, TimeUnit.SECONDS);
Iterables.addAll(actualEvents, result);
Assert.assertEquals(expectedEvents, actualEvents);
} finally {
txContext.finish();
}
}
Aggregations