use of io.pravega.auth.AuthenticationException in project pravega by pravega.
the class RawClientTest method testExceptionHandling.
@Test
public void testExceptionHandling() throws ConnectionFailedException {
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", -1);
@Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
@Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, true);
ClientConnection connection = Mockito.mock(ClientConnection.class);
connectionFactory.provideConnection(endpoint, connection);
Segment segment = new Segment("scope", "test", 0);
RawClient rawClient = new RawClient(controller, connectionFactory, segment);
WireCommands.ReadSegment request1 = new WireCommands.ReadSegment(segment.getScopedName(), 0, 10, "", requestId);
CompletableFuture<Reply> future = rawClient.sendRequest(requestId, request1);
// Verify if the request was sent over the connection.
Mockito.verify(connection).send(Mockito.eq(request1));
assertFalse("Since there is no response the future should not be completed", future.isDone());
ReplyProcessor processor = connectionFactory.getProcessor(endpoint);
processor.processingFailure(new ConnectionFailedException("Custom error"));
assertTrue(future.isCompletedExceptionally());
assertFutureThrows("The future should be completed exceptionally", future, t -> t instanceof ConnectionFailedException);
rawClient.close();
rawClient = new RawClient(controller, connectionFactory, segment);
WireCommands.ReadSegment request2 = new WireCommands.ReadSegment(segment.getScopedName(), 0, 10, "", 2L);
future = rawClient.sendRequest(2L, request2);
// Verify if the request was sent over the connection.
Mockito.verify(connection).send(Mockito.eq(request2));
assertFalse("Since there is no response the future should not be completed", future.isDone());
processor = connectionFactory.getProcessor(endpoint);
processor.authTokenCheckFailed(new WireCommands.AuthTokenCheckFailed(2L, "", WireCommands.AuthTokenCheckFailed.ErrorCode.TOKEN_CHECK_FAILED));
assertTrue(future.isCompletedExceptionally());
assertFutureThrows("The future should be completed exceptionally", future, t -> t instanceof AuthenticationException);
rawClient.close();
}
use of io.pravega.auth.AuthenticationException 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.auth.AuthenticationException in project pravega by pravega.
the class SegmentHelperTest method createTransaction.
@Test
public void createTransaction() {
MockConnectionFactory factory = new MockConnectionFactory();
@Cleanup SegmentHelper helper = new SegmentHelper(factory, new MockHostControllerStore(), executorService());
UUID txId = new UUID(0, 0L);
CompletableFuture<Void> retVal = helper.createTransaction("", "", 0L, txId, "", System.nanoTime(), 1024 * 1024L);
long requestId = ((MockConnection) (factory.connection)).getRequestId();
factory.rp.process(new WireCommands.AuthTokenCheckFailed(requestId, "SomeException"));
AssertExtensions.assertThrows("", () -> retVal.join(), ex -> ex instanceof WireCommandFailedException && ex.getCause() instanceof AuthenticationException);
CompletableFuture<Void> result = helper.createTransaction("", "", 0L, new UUID(0L, 0L), "", System.nanoTime(), 1024 * 1024L);
requestId = ((MockConnection) (factory.connection)).getRequestId();
factory.rp.process(new WireCommands.SegmentCreated(requestId, getQualifiedStreamSegmentName("", "", 0L)));
result.join();
result = helper.createTransaction("", "", 0L, new UUID(0L, 0L), "", System.nanoTime(), 1024 * 1024L);
requestId = ((MockConnection) (factory.connection)).getRequestId();
factory.rp.process(new WireCommands.SegmentAlreadyExists(requestId, getQualifiedStreamSegmentName("", "", 0L), ""));
result.join();
Supplier<CompletableFuture<?>> futureSupplier = () -> helper.createTransaction("", "", 0L, txId, "", System.nanoTime(), 1024 * 1024L);
validateProcessingFailureCFE(factory, futureSupplier);
testConnectionFailure(factory, futureSupplier);
}
use of io.pravega.auth.AuthenticationException in project pravega by pravega.
the class SegmentHelperTest method commitTransaction.
@Test
public void commitTransaction() {
MockConnectionFactory factory = new MockConnectionFactory();
String scope = "testScope";
String stream = "testStream";
String delegationToken = "";
long sourceSegmentId = 1L;
long targetSegmentId = 1L;
UUID txnId = new UUID(0, 0L);
List<UUID> txnIdList = List.of(txnId);
@Cleanup SegmentHelper helper = new SegmentHelper(factory, new MockHostControllerStore(), executorService());
CompletableFuture<List<Long>> retVal = helper.mergeTxnSegments(scope, stream, targetSegmentId, sourceSegmentId, txnIdList, delegationToken, System.nanoTime());
long requestId = ((MockConnection) (factory.connection)).getRequestId();
factory.rp.process(new WireCommands.AuthTokenCheckFailed(requestId, "SomeException"));
AssertExtensions.assertThrows("", () -> retVal.join(), ex -> ex instanceof WireCommandFailedException && ex.getCause() instanceof AuthenticationException);
CompletableFuture<List<Long>> result = helper.mergeTxnSegments(scope, stream, targetSegmentId, sourceSegmentId, txnIdList, delegationToken, System.nanoTime());
requestId = ((MockConnection) (factory.connection)).getRequestId();
factory.rp.process(new WireCommands.SegmentsBatchMerged(requestId, getQualifiedStreamSegmentName(scope, stream, targetSegmentId), List.of(getQualifiedStreamSegmentName(scope, stream, sourceSegmentId)), List.of(10L)));
result.join();
CompletableFuture<List<Long>> resultException = helper.mergeTxnSegments(scope, stream, targetSegmentId, sourceSegmentId, txnIdList, delegationToken, System.nanoTime());
requestId = ((MockConnection) (factory.connection)).getRequestId();
factory.rp.process(new WireCommands.NoSuchSegment(requestId, getQualifiedStreamSegmentName(scope, stream, targetSegmentId), "", 0L));
AssertExtensions.assertThrows("", () -> resultException.join(), ex -> ex instanceof WireCommandFailedException && ((WireCommandFailedException) ex).getReason().equals(WireCommandFailedException.Reason.SegmentDoesNotExist));
Supplier<CompletableFuture<?>> futureSupplier = () -> helper.mergeTxnSegments(scope, stream, targetSegmentId, sourceSegmentId, txnIdList, delegationToken, System.nanoTime());
validateProcessingFailureCFE(factory, futureSupplier);
testConnectionFailure(factory, futureSupplier);
}
use of io.pravega.auth.AuthenticationException in project pravega by pravega.
the class TableSegmentImplTest method testReconnect.
/**
* Tests the ability to reconnect in the following situations:
* - {@link ConnectionFailedException}
* - {@link AuthenticationException})
* - Unexpected replies.
* <p>
* These should result in automatic retries and reconnects up to a certain point.
*/
@Test
public void testReconnect() throws Exception {
val keys = Arrays.asList(100L, 200L, 300L);
val entries = Arrays.asList(versionedEntry(keys.get(0), "one hundred", 1L), versionedEntry(keys.get(1), "two hundred", 2L), versionedEntry(keys.get(2), "three hundred", 3L));
val versions = entries.stream().map(e -> e.getKey().getVersion().getSegmentVersion()).collect(Collectors.toList());
// All retryable replies. WrongHost and AuthTokenCheckFailed are converted into exceptions by RawClient, while all
// others need to be handled by TableSegmentImpl.handleReply.
val failureReplies = Arrays.<Function<Long, Reply>>asList(requestId -> new WireCommands.WrongHost(requestId, SEGMENT.getScopedName(), "NewHost", ""), requestId -> new WireCommands.AuthTokenCheckFailed(requestId, "", WireCommands.AuthTokenCheckFailed.ErrorCode.TOKEN_EXPIRED), requestId -> new WireCommands.OperationUnsupported(requestId, "Intentional", ""));
for (val fr : failureReplies) {
// TableSegment.put()
testConnectionFailure(ts -> ts.put(entries.get(0)), fr, requestId -> new WireCommands.TableEntriesUpdated(requestId, versions), result -> Assert.assertEquals("", (long) versions.get(0), result.getSegmentVersion()));
// TableSegment.get()
val wireEntries = entries.subList(0, 1);
testConnectionFailure(ts -> ts.get(buf(keys.get(0))), fr, requestId -> new WireCommands.TableRead(requestId, SEGMENT.getScopedName(), toWireEntries(wireEntries, null)), result -> Assert.assertTrue("", entryEquals(wireEntries.get(0), result)));
// TableSegment.remove()
testConnectionFailure(ts -> ts.remove(unversionedKey(keys.get(0))), fr, requestId -> new WireCommands.TableKeysRemoved(requestId, SEGMENT.getScopedName()), result -> {
});
// Iterators. It is sufficient to test one of them.
val args = SegmentIteratorArgs.builder().maxItemsAtOnce(1).fromKey(Unpooled.wrappedBuffer(new byte[] { 1 })).toKey(Unpooled.wrappedBuffer(new byte[] { 1 })).build();
testConnectionFailure(ts -> ts.entryIterator(args).getNext(), fr, requestId -> new WireCommands.TableEntriesRead(requestId, SEGMENT.getScopedName(), toWireEntries(entries, null), Unpooled.wrappedBuffer(new byte[1])), result -> AssertExtensions.assertListEquals("", entries, result.getItems(), this::entryEquals));
}
}
Aggregations