From aa829af43bddd54b2c7865385a3c8069379201bd Mon Sep 17 00:00:00 2001 From: Chris Eager Date: Mon, 31 Oct 2022 11:25:39 -0500 Subject: [PATCH] Handle expected case of empty flux in message deletion --- .../textsecuregcm/storage/MessagesDynamoDb.java | 6 +++--- .../textsecuregcm/storage/MessagesDynamoDbTest.java | 13 ++++++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/MessagesDynamoDb.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/MessagesDynamoDb.java index 6135b5043..294989118 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/MessagesDynamoDb.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/MessagesDynamoDb.java @@ -175,9 +175,9 @@ public class MessagesDynamoDb extends AbstractDynamoDbStore { } return null; }) - .last() - .toFuture() - .thenApply(Optional::ofNullable); + .map(Optional::ofNullable) + .last(Optional.empty()) // if the flux is empty, last() will throw without a default + .toFuture(); } public CompletableFuture> deleteMessage(final UUID destinationAccountUuid, diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/storage/MessagesDynamoDbTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/storage/MessagesDynamoDbTest.java index eae1802ad..7d76b1069 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/storage/MessagesDynamoDbTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/storage/MessagesDynamoDbTest.java @@ -11,6 +11,7 @@ import com.google.protobuf.ByteString; import java.time.Duration; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import java.util.Random; import java.util.UUID; import java.util.concurrent.ExecutorService; @@ -240,15 +241,25 @@ class MessagesDynamoDbTest { assertThat(load(secondDestinationUuid, 1, MessagesDynamoDb.RESULT_SET_CHUNK_SIZE)).isNotNull() .hasSize(1).element(0).isEqualTo(MESSAGE2); - messagesDynamoDb.deleteMessageByDestinationAndGuid(secondDestinationUuid, + final Optional deletedMessage = messagesDynamoDb.deleteMessageByDestinationAndGuid( + secondDestinationUuid, UUID.fromString(MESSAGE2.getServerGuid())).get(5, TimeUnit.SECONDS); + assertThat(deletedMessage).isPresent(); + assertThat(load(destinationUuid, 1, MessagesDynamoDb.RESULT_SET_CHUNK_SIZE)).isNotNull().hasSize(1) .element(0).isEqualTo(MESSAGE1); assertThat(load(destinationUuid, 2, MessagesDynamoDb.RESULT_SET_CHUNK_SIZE)).isNotNull().hasSize(1) .element(0).isEqualTo(MESSAGE3); assertThat(load(secondDestinationUuid, 1, MessagesDynamoDb.RESULT_SET_CHUNK_SIZE)).isNotNull() .isEmpty(); + + final Optional alreadyDeletedMessage = messagesDynamoDb.deleteMessageByDestinationAndGuid( + secondDestinationUuid, + UUID.fromString(MESSAGE2.getServerGuid())).get(5, TimeUnit.SECONDS); + + assertThat(alreadyDeletedMessage).isNotPresent(); + } @Test