use of io.pravega.client.stream.mock.MockConnectionFactoryImpl in project pravega by pravega.
the class ConditionalOutputStreamTest method testOnlyOneWriteAtATime.
/**
* It is necessary to only have one outstanding conditional append per writerId to make sure the status can be resolved in the event of a reconnect.
*/
@Test(timeout = 10000)
public void testOnlyOneWriteAtATime() throws ConnectionFailedException, SegmentSealedException {
@Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
@Cleanup MockController controller = new MockController("localhost", 0, connectionFactory, true);
ConditionalOutputStreamFactory factory = new ConditionalOutputStreamFactoryImpl(controller, connectionFactory);
Segment segment = new Segment("scope", "testWrite", 1);
@Cleanup ConditionalOutputStream cOut = factory.createConditionalOutputStream(segment, DelegationTokenProviderFactory.create("token", controller, segment, AccessOperation.ANY), EventWriterConfig.builder().build());
ByteBuffer data = ByteBuffer.allocate(10);
ClientConnection mock = Mockito.mock(ClientConnection.class);
PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
connectionFactory.provideConnection(location, mock);
setupAppend(connectionFactory, segment, mock, location);
LinkedBlockingQueue<Boolean> replies = new LinkedBlockingQueue<>();
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
ConditionalAppend argument = (ConditionalAppend) invocation.getArgument(0);
ReplyProcessor processor = connectionFactory.getProcessor(location);
if (replies.take()) {
processor.process(new WireCommands.DataAppended(argument.getRequestId(), argument.getWriterId(), argument.getEventNumber(), 0, -1));
} else {
processor.process(new WireCommands.ConditionalCheckFailed(argument.getWriterId(), argument.getEventNumber(), argument.getRequestId()));
}
return null;
}
}).when(mock).send(any(ConditionalAppend.class));
replies.add(true);
replies.add(false);
assertTrue(cOut.write(data, 0));
assertFalse(cOut.write(data, 1));
AssertExtensions.assertBlocks(() -> {
assertTrue(cOut.write(data, 2));
}, () -> {
replies.add(true);
});
AssertExtensions.assertBlocks(() -> {
assertFalse(cOut.write(data, 3));
}, () -> {
replies.add(false);
});
AssertExtensions.assertBlocks(() -> {
assertTrue(cOut.write(data, 4));
}, () -> {
AssertExtensions.assertBlocks(() -> {
assertFalse(cOut.write(data, 5));
}, () -> {
replies.add(true);
replies.add(false);
});
});
AssertExtensions.assertBlocks(() -> {
assertFalse(cOut.write(data, 6));
}, () -> {
AssertExtensions.assertBlocks(() -> {
assertTrue(cOut.write(data, 7));
}, () -> {
replies.add(false);
replies.add(true);
});
});
}
use of io.pravega.client.stream.mock.MockConnectionFactoryImpl in project pravega by pravega.
the class ConditionalOutputStreamTest method testRetries.
@Test(timeout = 10000)
public void testRetries() throws ConnectionFailedException, SegmentSealedException {
@Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
@Cleanup MockController controller = new MockController("localhost", 0, connectionFactory, true);
ConditionalOutputStreamFactory factory = new ConditionalOutputStreamFactoryImpl(controller, connectionFactory);
Segment segment = new Segment("scope", "testWrite", 1);
@Cleanup ConditionalOutputStream cOut = factory.createConditionalOutputStream(segment, DelegationTokenProviderFactory.create("token", controller, segment, AccessOperation.ANY), EventWriterConfig.builder().build());
ByteBuffer data = ByteBuffer.allocate(10);
ClientConnection mock = Mockito.mock(ClientConnection.class);
PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
connectionFactory.provideConnection(location, mock);
setupAppend(connectionFactory, segment, mock, location);
final AtomicLong count = new AtomicLong(0);
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
ConditionalAppend argument = (ConditionalAppend) invocation.getArgument(0);
ReplyProcessor processor = connectionFactory.getProcessor(location);
if (count.getAndIncrement() < 2) {
processor.connectionDropped();
} else {
processor.process(new WireCommands.DataAppended(argument.getRequestId(), argument.getWriterId(), argument.getEventNumber(), 0, -1));
}
return null;
}
}).when(mock).send(any(ConditionalAppend.class));
assertTrue(cOut.write(data, 0));
assertEquals(3, count.get());
}
use of io.pravega.client.stream.mock.MockConnectionFactoryImpl in project pravega by pravega.
the class AsyncSegmentInputStreamTest method testCloseAbortsRead.
@Test(timeout = 10000)
public void testCloseAbortsRead() throws InterruptedException, ExecutionException {
Segment segment = new Segment("scope", "testRetry", 4);
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
@Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
@Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, true);
Semaphore dataAvailable = new Semaphore(0);
@Cleanup AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(controller, connectionFactory, segment, DelegationTokenProviderFactory.createWithEmptyToken(), dataAvailable);
ClientConnection c = mock(ClientConnection.class);
connectionFactory.provideConnection(endpoint, c);
// Make sure connection is established.
in.getConnection().get();
CompletableFuture<SegmentRead> read = in.read(1234, 5678);
assertFalse(read.isDone());
in.close();
assertThrows(ConnectionClosedException.class, () -> Futures.getThrowingException(read));
verify(c).close();
assertEquals(0, dataAvailable.availablePermits());
}
use of io.pravega.client.stream.mock.MockConnectionFactoryImpl in project pravega by pravega.
the class AsyncSegmentInputStreamTest method testRead.
@Test(timeout = 10000)
public void testRead() throws ConnectionFailedException {
Segment segment = new Segment("scope", "testRead", 1);
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
@Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
@Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, true);
Semaphore dataAvailable = new Semaphore(0);
@Cleanup AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(controller, connectionFactory, segment, DelegationTokenProviderFactory.createWithEmptyToken(), dataAvailable);
ClientConnection c = mock(ClientConnection.class);
connectionFactory.provideConnection(endpoint, c);
WireCommands.SegmentRead segmentRead = new WireCommands.SegmentRead(segment.getScopedName(), 1234, false, false, Unpooled.EMPTY_BUFFER, in.getRequestId());
CompletableFuture<SegmentRead> readFuture = in.read(1234, 5678);
assertEquals(0, dataAvailable.availablePermits());
AssertExtensions.assertBlocks(() -> readFuture.get(), () -> {
ReplyProcessor processor = connectionFactory.getProcessor(endpoint);
processor.process(segmentRead);
});
assertEquals(1, dataAvailable.availablePermits());
verify(c).send(eq(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, "", in.getRequestId())));
assertTrue(Futures.isSuccessful(readFuture));
assertEquals(segmentRead, readFuture.join());
verifyNoMoreInteractions(c);
}
use of io.pravega.client.stream.mock.MockConnectionFactoryImpl in project pravega by pravega.
the class AsyncSegmentInputStreamTest method testWrongOffsetReturned.
@Test(timeout = 10000)
public void testWrongOffsetReturned() throws ConnectionFailedException {
Segment segment = new Segment("scope", "testWrongOffsetReturned", 0);
byte[] good = new byte[] { 0, 1, 2, 3, 4 };
byte[] bad = new byte[] { 9, 8, 7, 6 };
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
@Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
@Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, true);
Semaphore dataAvailable = new Semaphore(0);
@Cleanup AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(controller, connectionFactory, segment, DelegationTokenProviderFactory.createWithEmptyToken(), dataAvailable);
ClientConnection c = mock(ClientConnection.class);
connectionFactory.provideConnection(endpoint, c);
CompletableFuture<SegmentRead> readFuture = in.read(1234, 5678);
assertEquals(0, dataAvailable.availablePermits());
AssertExtensions.assertBlocks(() -> readFuture.get(), () -> {
ReplyProcessor processor = connectionFactory.getProcessor(endpoint);
processor.process(new WireCommands.SegmentRead(segment.getScopedName(), 1235, false, false, Unpooled.wrappedBuffer(bad), in.getRequestId()));
processor.process(new WireCommands.SegmentRead(segment.getScopedName(), 1234, false, false, Unpooled.wrappedBuffer(good), in.getRequestId()));
});
assertEquals(2, dataAvailable.availablePermits());
verify(c).send(eq(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, "", in.getRequestId())));
assertTrue(Futures.isSuccessful(readFuture));
assertEquals(Unpooled.wrappedBuffer(good), readFuture.join().getData());
verifyNoMoreInteractions(c);
}
Aggregations