use of io.pravega.client.segment.impl.Segment in project pravega by pravega.
the class EventStreamReaderTest method testAcquireSegment.
@SuppressWarnings("unused")
@Test(timeout = 10000)
public void testAcquireSegment() throws SegmentSealedException, ReinitializationRequiredException {
AtomicLong clock = new AtomicLong();
MockSegmentStreamFactory segmentStreamFactory = new MockSegmentStreamFactory();
Orderer orderer = new Orderer();
ReaderGroupStateManager groupState = Mockito.mock(ReaderGroupStateManager.class);
EventStreamReaderImpl<byte[]> reader = new EventStreamReaderImpl<>(segmentStreamFactory, segmentStreamFactory, new ByteArraySerializer(), groupState, orderer, clock::get, ReaderConfig.builder().build());
Segment segment1 = Segment.fromScopedName("Foo/Bar/0");
Segment segment2 = Segment.fromScopedName("Foo/Bar/1");
Mockito.when(groupState.acquireNewSegmentsIfNeeded(0L)).thenReturn(ImmutableMap.of(segment1, 0L)).thenReturn(ImmutableMap.of(segment2, 0L)).thenReturn(Collections.emptyMap());
SegmentOutputStream stream1 = segmentStreamFactory.createOutputStreamForSegment(segment1, segmentSealedCallback, writerConfig, "");
SegmentOutputStream stream2 = segmentStreamFactory.createOutputStreamForSegment(segment2, segmentSealedCallback, writerConfig, "");
writeInt(stream1, 1);
writeInt(stream1, 2);
writeInt(stream2, 3);
writeInt(stream2, 4);
reader.readNextEvent(0);
List<SegmentInputStream> readers = reader.getReaders();
assertEquals(1, readers.size());
Assert.assertEquals(segment1, readers.get(0).getSegmentId());
reader.readNextEvent(0);
readers = reader.getReaders();
assertEquals(2, readers.size());
Assert.assertEquals(segment1, readers.get(0).getSegmentId());
Assert.assertEquals(segment2, readers.get(1).getSegmentId());
reader.close();
}
use of io.pravega.client.segment.impl.Segment in project pravega by pravega.
the class EventStreamReaderTest method testEndOfSegmentWithoutSuccessors.
@Test(timeout = 10000)
public void testEndOfSegmentWithoutSuccessors() throws SegmentSealedException, ReinitializationRequiredException {
AtomicLong clock = new AtomicLong();
MockSegmentStreamFactory segmentStreamFactory = new MockSegmentStreamFactory();
Orderer orderer = new Orderer();
ReaderGroupStateManager groupState = Mockito.mock(ReaderGroupStateManager.class);
EventStreamReaderImpl<byte[]> reader = new EventStreamReaderImpl<>(segmentStreamFactory, segmentStreamFactory, new ByteArraySerializer(), groupState, orderer, clock::get, ReaderConfig.builder().build());
Segment segment = Segment.fromScopedName("Foo/Bar/0");
Mockito.when(groupState.acquireNewSegmentsIfNeeded(0L)).thenReturn(ImmutableMap.of(segment, 0L)).thenReturn(Collections.emptyMap());
SegmentOutputStream stream = segmentStreamFactory.createOutputStreamForSegment(segment, segmentSealedCallback, writerConfig, "");
ByteBuffer buffer = writeInt(stream, 1);
EventRead<byte[]> read = reader.readNextEvent(0);
byte[] event = read.getEvent();
assertEquals(buffer, ByteBuffer.wrap(event));
read = reader.readNextEvent(0);
assertNull(read.getEvent());
read = reader.readNextEvent(0);
assertNull(read.getEvent());
assertEquals(0, reader.getReaders().size());
reader.close();
}
use of io.pravega.client.segment.impl.Segment in project pravega by pravega.
the class SynchronizerTest method testLocking.
@Test(timeout = 20000)
public void testLocking() {
String streamName = "streamName";
String scope = "scope";
Segment segment = new Segment(scope, streamName, 0);
BlockingUpdate[] updates = new BlockingUpdate[] { new BlockingUpdate(1), new BlockingUpdate(2), new BlockingUpdate(3), new BlockingUpdate(4) };
MockRevisionedStreamClient client = new MockRevisionedStreamClient();
client.segment = segment;
@Cleanup StateSynchronizerImpl<RevisionedImpl> sync = new StateSynchronizerImpl<RevisionedImpl>(segment, client);
client.init = new BlockingUpdate(0);
client.updates = updates;
client.visableLength = 2;
client.init.latch.release();
updates[0].latch.release();
updates[1].latch.release();
sync.fetchUpdates();
RevisionedImpl state1 = sync.getState();
assertEquals(new RevisionImpl(segment, 2, 2), state1.getRevision());
client.visableLength = 3;
Async.testBlocking(() -> {
sync.fetchUpdates();
}, () -> updates[2].latch.release());
RevisionedImpl state2 = sync.getState();
assertEquals(new RevisionImpl(segment, 3, 3), state2.getRevision());
client.visableLength = 4;
Async.testBlocking(() -> {
sync.fetchUpdates();
}, () -> {
client.visableLength = 3;
sync.getState();
updates[3].latch.release();
});
RevisionedImpl state3 = sync.getState();
assertEquals(new RevisionImpl(segment, 4, 4), state3.getRevision());
}
use of io.pravega.client.segment.impl.Segment 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.segment.impl.Segment in project pravega by pravega.
the class EventStreamWriterTest method testSegmentSealedInSegmentSealed.
@Test
public void testSegmentSealedInSegmentSealed() {
String scope = "scope";
String streamName = "stream";
String routingKey = "RoutingKey";
StreamImpl stream = new StreamImpl(scope, streamName);
Segment segment1 = new Segment(scope, streamName, 0);
Segment segment2 = new Segment(scope, streamName, 1);
Segment segment3 = new Segment(scope, streamName, 2);
EventWriterConfig config = EventWriterConfig.builder().build();
SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
Controller controller = Mockito.mock(Controller.class);
FakeSegmentOutputStream outputStream1 = new FakeSegmentOutputStream(segment1);
FakeSegmentOutputStream outputStream2 = new FakeSegmentOutputStream(segment2);
FakeSegmentOutputStream outputStream3 = new FakeSegmentOutputStream(segment3);
Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment1));
Mockito.when(controller.getSuccessors(segment1)).thenReturn(getReplacement(segment1, segment2));
Mockito.when(controller.getSuccessors(segment2)).thenReturn(getReplacement(segment2, segment3));
Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment1), any(), any(), any())).thenAnswer(i -> {
outputStream1.callBackForSealed = i.getArgument(1);
return outputStream1;
});
Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment2), any(), any(), any())).thenAnswer(i -> {
outputStream2.callBackForSealed = i.getArgument(1);
return outputStream2;
});
Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment3), any(), any(), any())).thenAnswer(i -> {
outputStream3.callBackForSealed = i.getArgument(1);
return outputStream3;
});
JavaSerializer<String> serializer = new JavaSerializer<>();
@Cleanup EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, controller, streamFactory, serializer, config, new InlineExecutor());
writer.writeEvent(routingKey, "Foo");
Mockito.verify(controller).getCurrentSegments(any(), any());
assertEquals(1, outputStream1.getUnackedEventsOnSeal().size());
assertTrue(outputStream2.getUnackedEventsOnSeal().isEmpty());
outputStream1.invokeSealedCallBack();
outputStream2.invokeSealedCallBack();
writer.writeEvent(routingKey, "Bar");
Mockito.verify(controller, Mockito.times(1)).getCurrentSegments(any(), any());
assertEquals(1, outputStream2.getUnackedEventsOnSeal().size());
assertEquals("Foo", serializer.deserialize(outputStream2.getUnackedEventsOnSeal().get(0).getData()));
assertEquals(2, outputStream3.getUnackedEventsOnSeal().size());
assertEquals("Foo", serializer.deserialize(outputStream3.getUnackedEventsOnSeal().get(0).getData()));
assertEquals("Bar", serializer.deserialize(outputStream3.getUnackedEventsOnSeal().get(1).getData()));
}
Aggregations