Search in sources :

Example 1 with EventPointer

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);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) Segment(io.pravega.client.segment.impl.Segment) EventPointer(io.pravega.client.stream.EventPointer) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 2 with EventPointer

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);
}
Also used : EventStreamReader(io.pravega.client.stream.EventStreamReader) EventPointer(io.pravega.client.stream.EventPointer)

Example 3 with EventPointer

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");
    }
}
Also used : ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) NoSuchEventException(io.pravega.client.segment.impl.NoSuchEventException) Cleanup(lombok.Cleanup) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) JavaSerializer(io.pravega.client.stream.impl.JavaSerializer) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) EventPointer(io.pravega.client.stream.EventPointer) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) MockStreamManager(io.pravega.client.stream.mock.MockStreamManager) Test(org.junit.Test)

Aggregations

EventPointer (io.pravega.client.stream.EventPointer)3 Test (org.junit.Test)2 NoSuchEventException (io.pravega.client.segment.impl.NoSuchEventException)1 Segment (io.pravega.client.segment.impl.Segment)1 EventStreamReader (io.pravega.client.stream.EventStreamReader)1 ReaderGroupConfig (io.pravega.client.stream.ReaderGroupConfig)1 JavaSerializer (io.pravega.client.stream.impl.JavaSerializer)1 MockClientFactory (io.pravega.client.stream.mock.MockClientFactory)1 MockStreamManager (io.pravega.client.stream.mock.MockStreamManager)1 StreamSegmentStore (io.pravega.segmentstore.contracts.StreamSegmentStore)1 PravegaConnectionListener (io.pravega.segmentstore.server.host.handler.PravegaConnectionListener)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Cleanup (lombok.Cleanup)1