use of io.pravega.client.stream.mock.MockConnectionFactoryImpl in project pravega by pravega.
the class RevisionedStreamClientTest method testConditionalWrite.
@Test
public void testConditionalWrite() {
String scope = "scope";
String stream = "stream";
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
@Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
@Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, false);
createScopeAndStream(scope, stream, controller);
MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
@Cleanup SynchronizerClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, streamFactory, streamFactory, streamFactory, streamFactory);
SynchronizerConfig config = SynchronizerConfig.builder().build();
@Cleanup RevisionedStreamClient<String> client = clientFactory.createRevisionedStreamClient(stream, new JavaSerializer<>(), config);
client.writeUnconditionally("a");
Revision revision = client.fetchLatestRevision();
Revision newRevision = client.writeConditionally(revision, "b");
assertNotNull(newRevision);
assertTrue(newRevision.compareTo(revision) > 0);
assertEquals(newRevision, client.fetchLatestRevision());
Revision failed = client.writeConditionally(revision, "fail");
assertNull(failed);
assertEquals(newRevision, client.fetchLatestRevision());
Iterator<Entry<Revision, String>> iter = client.readFrom(revision);
assertTrue(iter.hasNext());
Entry<Revision, String> entry = iter.next();
assertEquals(newRevision, entry.getKey());
assertEquals("b", entry.getValue());
assertFalse(iter.hasNext());
}
use of io.pravega.client.stream.mock.MockConnectionFactoryImpl in project pravega by pravega.
the class ConditionalOutputStreamTest method testSegmentSealed.
@Test(timeout = 10000)
public void testSegmentSealed() throws ConnectionFailedException {
@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);
String mockClientReplyStackTrace = "SomeException";
ClientConnection mock = Mockito.mock(ClientConnection.class);
PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
connectionFactory.provideConnection(location, mock);
setupAppend(connectionFactory, segment, mock, location);
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
ConditionalAppend argument = (ConditionalAppend) invocation.getArgument(0);
ReplyProcessor processor = connectionFactory.getProcessor(location);
processor.process(new WireCommands.SegmentIsSealed(argument.getRequestId(), segment.getScopedName(), mockClientReplyStackTrace, argument.getEventNumber()));
return null;
}
}).when(mock).send(any(ConditionalAppend.class));
AssertExtensions.assertThrows(SegmentSealedException.class, () -> cOut.write(data, 0));
}
use of io.pravega.client.stream.mock.MockConnectionFactoryImpl in project pravega by pravega.
the class ConditionalOutputStreamTest method testRetriesOnInvalidEventNumber.
@SneakyThrows
@Test(timeout = 10000)
public void testRetriesOnInvalidEventNumber() {
@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 objectUnderTest = factory.createConditionalOutputStream(segment, DelegationTokenProviderFactory.create("token", controller, segment, AccessOperation.ANY), EventWriterConfig.builder().build());
ByteBuffer data = ByteBuffer.allocate(10);
ClientConnection clientConnection = Mockito.mock(ClientConnection.class);
PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
connectionFactory.provideConnection(location, clientConnection);
setupAppend(connectionFactory, segment, clientConnection, location);
final AtomicLong retryCounter = 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 (retryCounter.getAndIncrement() < 2) {
processor.process(new WireCommands.InvalidEventNumber(argument.getWriterId(), argument.getRequestId(), ""));
} else {
processor.process(new WireCommands.DataAppended(argument.getRequestId(), argument.getWriterId(), argument.getEventNumber(), 0, -1));
}
return null;
}
}).when(clientConnection).send(any(ConditionalAppend.class));
assertTrue(objectUnderTest.write(data, 0));
assertEquals(3, retryCounter.get());
}
use of io.pravega.client.stream.mock.MockConnectionFactoryImpl in project pravega by pravega.
the class ConditionalOutputStreamTest method handleUnexpectedReplythrowsAppropriateTokenExceptions.
@Test
public void handleUnexpectedReplythrowsAppropriateTokenExceptions() {
@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 ConditionalOutputStreamImpl objectUnderTest = (ConditionalOutputStreamImpl) factory.createConditionalOutputStream(segment, DelegationTokenProviderFactory.create("token", controller, segment, AccessOperation.ANY), EventWriterConfig.builder().build());
AssertExtensions.assertThrows("AuthenticationException wasn't thrown", () -> objectUnderTest.handleUnexpectedReply(new WireCommands.AuthTokenCheckFailed(1L, "SomeException", WireCommands.AuthTokenCheckFailed.ErrorCode.TOKEN_CHECK_FAILED), "test"), e -> e instanceof AuthenticationException);
AssertExtensions.assertThrows("AuthenticationException wasn't thrown", () -> objectUnderTest.handleUnexpectedReply(new WireCommands.AuthTokenCheckFailed(1L, "SomeException", WireCommands.AuthTokenCheckFailed.ErrorCode.UNSPECIFIED), "test"), e -> e instanceof AuthenticationException);
AssertExtensions.assertThrows("TokenExpiredException wasn't thrown", () -> objectUnderTest.handleUnexpectedReply(new WireCommands.AuthTokenCheckFailed(1L, "SomeException", WireCommands.AuthTokenCheckFailed.ErrorCode.TOKEN_EXPIRED), "test"), e -> e instanceof TokenExpiredException);
AssertExtensions.assertThrows("InvalidEventNumber wasn't treated as a connection failure", () -> objectUnderTest.handleUnexpectedReply(new WireCommands.InvalidEventNumber(UUID.randomUUID(), 1, "SomeException"), "test"), e -> e instanceof ConnectionFailedException);
AssertExtensions.assertThrows("Hello wasn't treated as a connection failure", () -> objectUnderTest.handleUnexpectedReply(new WireCommands.Hello(1, 1), "test"), e -> e instanceof ConnectionFailedException);
}
use of io.pravega.client.stream.mock.MockConnectionFactoryImpl in project pravega by pravega.
the class ConditionalOutputStreamTest method testClose.
@Test(timeout = 10000)
public void testClose() {
@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());
cOut.close();
AssertExtensions.assertThrows(IllegalStateException.class, () -> cOut.write(ByteBufferUtils.EMPTY, 0));
}
Aggregations