use of io.pravega.client.stream.EventPointer in project pravega by pravega.
the class EventPointerTest method testEventPointerImpl.
/**
* Simple exercise of event pointers. The test case
* creates an impl instance with some arbitrary values,
* serializes the pointer, deserializes it, and asserts
* that the values obtained are the expected ones.
*/
@Test
public void testEventPointerImpl() {
String scope = "testScope";
String stream = "testStream";
int segmentId = 1;
Segment segment = new Segment(scope, stream, segmentId);
EventPointer pointer = new EventPointerImpl(segment, 10L, 10);
ByteArrayOutputStream bos = null;
// Serialize pointer
try {
bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(pointer);
out.close();
} catch (IOException e) {
fail("Failed to serialize pointer object" + e.getMessage());
}
// Deserialize pointer
pointer = null;
try {
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream in = new ObjectInputStream(bis);
pointer = (EventPointer) in.readObject();
} catch (IOException | ClassNotFoundException e) {
fail("Failed to deserialize object" + e.getMessage());
}
// Verifies that we have been able to deserialize successfully
if (pointer == null) {
fail("Failed to deserialize object");
} else {
StringBuilder name = new StringBuilder();
name.append(scope);
name.append("/");
name.append(stream);
assertTrue("Scoped stream name: " + pointer.asImpl().getSegment().getScopedStreamName(), pointer.asImpl().getSegment().getScopedStreamName().equals(name.toString()));
name.append("/");
name.append(segmentId);
assertTrue("Scoped name: " + pointer.asImpl().getSegment().getScopedName(), pointer.asImpl().getSegment().getScopedName().equals(name.toString()));
assertTrue(pointer.asImpl().getEventStartOffset() == 10L);
assertTrue(pointer.asImpl().getEventLength() == 10);
}
}
use of io.pravega.client.stream.EventPointer in project pravega by pravega.
the class ClientReader method readExact.
@Override
public CompletableFuture<ReadItem> readExact(String streamName, Object address) {
Exceptions.checkNotNullOrEmpty(streamName, "streamName");
Preconditions.checkArgument(address instanceof EventPointer, "Unexpected address type.");
EventPointer a = (EventPointer) address;
StreamReader reader = getReader(streamName);
return CompletableFuture.supplyAsync(() -> reader.readExact(a), this.executor);
}
use of io.pravega.client.stream.EventPointer in project pravega by pravega.
the class ReadTest method testEventPointer.
@Test(timeout = 10000)
public void testEventPointer() throws ReinitializationRequiredException {
String endpoint = "localhost";
String streamName = "abc";
String readerName = "reader";
String readerGroup = "group";
int port = TestUtils.getAvailableListenPort();
String testString = "Hello world ";
String scope = "Scope1";
StreamSegmentStore store = this.serviceBuilder.createStreamSegmentService();
@Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store);
server.startListening();
@Cleanup MockStreamManager streamManager = new MockStreamManager(scope, endpoint, port);
MockClientFactory clientFactory = streamManager.getClientFactory();
ReaderGroupConfig groupConfig = ReaderGroupConfig.builder().disableAutomaticCheckpoints().stream(Stream.of(scope, streamName)).build();
streamManager.createScope(scope);
streamManager.createStream(scope, streamName, null);
streamManager.createReaderGroup(readerGroup, groupConfig);
JavaSerializer<String> serializer = new JavaSerializer<>();
EventStreamWriter<String> producer = clientFactory.createEventWriter(streamName, serializer, EventWriterConfig.builder().build());
for (int i = 0; i < 100; i++) {
producer.writeEvent(testString + i);
}
producer.flush();
@Cleanup EventStreamReader<String> reader = clientFactory.createReader(readerName, readerGroup, serializer, ReaderConfig.builder().build());
try {
EventPointer pointer;
String read;
for (int i = 0; i < 100; i++) {
pointer = reader.readNextEvent(5000).getEventPointer();
read = reader.fetchEvent(pointer);
assertEquals(testString + i, read);
}
} catch (NoSuchEventException e) {
fail("Failed to read event using event pointer");
}
}
Aggregations