diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java b/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java index 6caef7748..ebe939234 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java @@ -833,7 +833,7 @@ public class WhisperServerService extends Application maybeSchedulePush(account, device)); + .doFinally(_ -> maybeSendNewMessagePushNotification(account, device)); } - /// If the device potentially has more messages available, schedule a push notification. - private void maybeSchedulePush(final Account account, final Device device) { + /// If the device potentially has more messages available, send a push notification. + private void maybeSendNewMessagePushNotification(final Account account, final Device device) { messagesManager.mayHaveMessages(account.getAccountIdentifier(), device) .thenAccept(mayHaveMessages -> { if (mayHaveMessages) { - pushNotificationScheduler.scheduleDelayedNotification(account, device, - CLOSE_WITH_PENDING_MESSAGES_NOTIFICATION_DELAY); + try { + pushNotificationManager.sendNewMessageNotification(account, device.getId(), true); + } catch (NotPushRegisteredException _) { + } } }); } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/push/PushNotificationScheduler.java b/service/src/main/java/org/whispersystems/textsecuregcm/push/PushNotificationScheduler.java index 37b2a79c2..3ff435c64 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/push/PushNotificationScheduler.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/push/PushNotificationScheduler.java @@ -23,7 +23,6 @@ import java.util.Optional; import java.util.UUID; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; -import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; import java.util.function.BiFunction; @@ -38,7 +37,6 @@ import org.whispersystems.textsecuregcm.storage.Device; import org.whispersystems.textsecuregcm.util.ExceptionUtils; import org.whispersystems.textsecuregcm.util.Pair; import org.whispersystems.textsecuregcm.util.RedisClusterUtil; -import org.whispersystems.textsecuregcm.util.ResilienceUtil; import org.whispersystems.textsecuregcm.util.Util; import reactor.core.publisher.Mono; @@ -49,7 +47,6 @@ public class PushNotificationScheduler implements Managed { private static final String PENDING_BACKGROUND_APN_NOTIFICATIONS_KEY_PREFIX = "PENDING_BACKGROUND_APN"; private static final String PENDING_BACKGROUND_FCM_NOTIFICATIONS_KEY_PREFIX = "PENDING_BACKGROUND_FCM"; private static final String LAST_BACKGROUND_NOTIFICATION_TIMESTAMP_KEY_PREFIX = "LAST_BACKGROUND_NOTIFICATION"; - private static final String PENDING_DELAYED_NOTIFICATIONS_KEY_PREFIX = "DELAYED"; @VisibleForTesting static final String NEXT_SLOT_TO_PROCESS_KEY = "pending_notification_next_slot"; @@ -59,16 +56,12 @@ public class PushNotificationScheduler implements Managed { private static final String BACKGROUND_NOTIFICATION_SCHEDULED_COUNTER_NAME = name(PushNotificationScheduler.class, "backgroundNotification", "scheduled"); private static final String BACKGROUND_NOTIFICATION_SENT_COUNTER_NAME = name(PushNotificationScheduler.class, "backgroundNotification", "sent"); - private static final String DELAYED_NOTIFICATION_SCHEDULED_COUNTER_NAME = name(PushNotificationScheduler.class, "delayedNotificationScheduled"); - private static final String DELAYED_NOTIFICATION_SENT_COUNTER_NAME = name(PushNotificationScheduler.class, "delayedNotificationSent"); - private static final String TOKEN_TYPE_TAG = "tokenType"; private static final String ACCEPTED_TAG = "accepted"; private final APNSender apnSender; private final FcmSender fcmSender; private final AccountsManager accountsManager; private final FaultTolerantRedisClusterClient pushSchedulingCluster; - private final ScheduledExecutorService retryExecutor; private final Clock clock; private final ClusterLuaScript scheduleBackgroundNotificationScript; @@ -80,8 +73,6 @@ public class PushNotificationScheduler implements Managed { private final AtomicBoolean running = new AtomicBoolean(false); - private static final String RETRY_NAME = ResilienceUtil.name(PushNotificationScheduler.class); - class NotificationWorker implements Runnable { private final int maxConcurrency; @@ -118,8 +109,7 @@ public class PushNotificationScheduler implements Managed { connection.sync().incr(NEXT_SLOT_TO_PROCESS_KEY)) % SlotHash.SLOT_COUNT); return processScheduledBackgroundNotifications(PushNotification.TokenType.APN, slot) - + processScheduledBackgroundNotifications(PushNotification.TokenType.FCM, slot) - + processScheduledDelayedNotifications(slot); + + processScheduledBackgroundNotifications(PushNotification.TokenType.FCM, slot); } @VisibleForTesting @@ -129,12 +119,6 @@ public class PushNotificationScheduler implements Managed { } - @VisibleForTesting - long processScheduledDelayedNotifications(final int slot) { - return processScheduledNotifications(getDelayedNotificationQueueKey(slot), - PushNotificationScheduler.this::sendDelayedNotification); - } - private long processScheduledNotifications(final String queueKey, final BiFunction> sendNotificationFunction) { @@ -163,8 +147,7 @@ public class PushNotificationScheduler implements Managed { final FcmSender fcmSender, final AccountsManager accountsManager, final int dedicatedProcessWorkerThreadCount, - final int workerMaxConcurrency, - final ScheduledExecutorService retryExecutor) throws IOException { + final int workerMaxConcurrency) throws IOException { this(pushSchedulingCluster, apnSender, @@ -172,8 +155,7 @@ public class PushNotificationScheduler implements Managed { accountsManager, Clock.systemUTC(), dedicatedProcessWorkerThreadCount, - workerMaxConcurrency, - retryExecutor); + workerMaxConcurrency); } @VisibleForTesting @@ -183,8 +165,7 @@ public class PushNotificationScheduler implements Managed { final AccountsManager accountsManager, final Clock clock, final int dedicatedProcessThreadCount, - final int workerMaxConcurrency, - final ScheduledExecutorService retryExecutor) throws IOException { + final int workerMaxConcurrency) throws IOException { this.apnSender = apnSender; this.fcmSender = fcmSender; @@ -196,7 +177,6 @@ public class PushNotificationScheduler implements Managed { "lua/apn/schedule_background_notification.lua", ScriptOutputType.VALUE); this.workerThreads = new Thread[dedicatedProcessThreadCount]; - this.retryExecutor = retryExecutor; for (int i = 0; i < this.workerThreads.length; i++) { this.workerThreads[i] = new Thread(new NotificationWorker(workerMaxConcurrency), "PushNotificationScheduler-" + i); @@ -226,31 +206,6 @@ public class PushNotificationScheduler implements Managed { .thenRun(Util.NOOP); } - /** - * Schedules a "new message" push notification to be delivered to the given device after at least the given duration. - * If another notification had previously been scheduled, calling this method will replace the previously-scheduled - * delivery time with the given time. - * - * @param account the account to which the target device belongs - * @param device the device to which to deliver a "new message" push notification - * @param minDelay the minimum delay after which to deliver the notification - * - * @return a future that completes once the notification has been scheduled - */ - public CompletableFuture scheduleDelayedNotification(final Account account, final Device device, final Duration minDelay) { - final long deliveryTime = clock.instant().plus(minDelay).toEpochMilli(); - - return ResilienceUtil.getGeneralRedisRetry(RETRY_NAME) - .executeCompletionStage(retryExecutor, () -> pushSchedulingCluster.withCluster(connection -> - connection.async().zadd(getDelayedNotificationQueueKey(account, device), - deliveryTime, - encodeAciAndDeviceId(account, device))) - .thenRun(() -> Metrics.counter(DELAYED_NOTIFICATION_SCHEDULED_COUNTER_NAME, - TOKEN_TYPE_TAG, getTokenType(device)) - .increment())) - .toCompletableFuture(); - } - /** * Cancel scheduled notifications for the given account and device. * @@ -259,8 +214,7 @@ public class PushNotificationScheduler implements Managed { public CompletionStage cancelScheduledNotifications(Account account, Device device) { return CompletableFuture.allOf( cancelBackgroundNotifications(PushNotification.TokenType.FCM, account, device), - cancelBackgroundNotifications(PushNotification.TokenType.APN, account, device), - cancelDelayedNotifications(account, device)); + cancelBackgroundNotifications(PushNotification.TokenType.APN, account, device)); } @VisibleForTesting @@ -271,15 +225,6 @@ public class PushNotificationScheduler implements Managed { .toCompletableFuture(); } - @VisibleForTesting - CompletableFuture cancelDelayedNotifications(final Account account, final Device device) { - return pushSchedulingCluster.withCluster(connection -> - connection.async().zrem(getDelayedNotificationQueueKey(account, device), - encodeAciAndDeviceId(account, device))) - .thenRun(Util.NOOP) - .toCompletableFuture(); - } - @Override public synchronized void start() { running.set(true); @@ -322,32 +267,6 @@ public class PushNotificationScheduler implements Managed { .toCompletableFuture(); } - @VisibleForTesting - CompletableFuture sendDelayedNotification(final Account account, final Device device) { - if (StringUtils.isAllBlank(device.getApnId(), device.getGcmId())) { - return CompletableFuture.completedFuture(null); - } - - final boolean isApnsDevice = StringUtils.isNotBlank(device.getApnId()); - - final PushNotification pushNotification = new PushNotification( - isApnsDevice ? device.getApnId() : device.getGcmId(), - isApnsDevice ? PushNotification.TokenType.APN : PushNotification.TokenType.FCM, - PushNotification.NotificationType.NOTIFICATION, - null, - account, - device, - true, null); - - final PushNotificationSender pushNotificationSender = isApnsDevice ? apnSender : fcmSender; - - return pushNotificationSender.sendNotification(pushNotification) - .thenAccept(response -> Metrics.counter(DELAYED_NOTIFICATION_SENT_COUNTER_NAME, - TOKEN_TYPE_TAG, getTokenType(device), - ACCEPTED_TAG, String.valueOf(response.accepted())) - .increment()); - } - @VisibleForTesting static String encodeAciAndDeviceId(final Account account, final Device device) { // Note: This does not include a device registration id. If a device is unlinked and a new device is linked with @@ -400,15 +319,6 @@ public class PushNotificationScheduler implements Managed { return LAST_BACKGROUND_NOTIFICATION_TIMESTAMP_KEY_PREFIX + "::{" + encodeAciAndDeviceId(account, device) + "}"; } - @VisibleForTesting - static String getDelayedNotificationQueueKey(final Account account, final Device device) { - return getDelayedNotificationQueueKey(SlotHash.getSlot(encodeAciAndDeviceId(account, device))); - } - - private static String getDelayedNotificationQueueKey(final int slot) { - return PENDING_DELAYED_NOTIFICATIONS_KEY_PREFIX + "::{" + RedisClusterUtil.getMinimalHashTag(slot) + "}"; - } - @VisibleForTesting Optional getLastBackgroundApnsNotificationTimestamp(final Account account, final Device device) { return Optional.ofNullable( @@ -426,25 +336,6 @@ public class PushNotificationScheduler implements Managed { .map(timestamp -> Instant.ofEpochMilli(timestamp.longValue())); } - @VisibleForTesting - Optional getNextScheduledDelayedNotificationTimestamp(final Account account, final Device device) { - return Optional.ofNullable( - pushSchedulingCluster.withCluster(connection -> - connection.sync().zscore(getDelayedNotificationQueueKey(account, device), - encodeAciAndDeviceId(account, device)))) - .map(timestamp -> Instant.ofEpochMilli(timestamp.longValue())); - } - - private static String getTokenType(final Device device) { - if (StringUtils.isNotBlank(device.getApnId())) { - return "apns"; - } else if (StringUtils.isNotBlank(device.getGcmId())) { - return "fcm"; - } else { - return "unknown"; - } - } - private static String getPushToken(final PushNotification.TokenType tokenType, final Device device) { return switch (tokenType) { case FCM -> device.getGcmId(); diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/websocket/AuthenticatedConnectListener.java b/service/src/main/java/org/whispersystems/textsecuregcm/websocket/AuthenticatedConnectListener.java index 779d4abfc..4ad46a32c 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/websocket/AuthenticatedConnectListener.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/websocket/AuthenticatedConnectListener.java @@ -70,13 +70,11 @@ public class AuthenticatedConnectListener implements WebSocketConnectListener { messagesManager, messageMetrics, pushNotificationManager, - pushNotificationScheduler, account, device, client, messageDeliveryScheduler, - clientReleaseManager, - experimentEnrollmentManager) + clientReleaseManager) ); } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/websocket/WebSocketConnection.java b/service/src/main/java/org/whispersystems/textsecuregcm/websocket/WebSocketConnection.java index 863df9e48..cae22c75d 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/websocket/WebSocketConnection.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/websocket/WebSocketConnection.java @@ -13,7 +13,6 @@ import io.micrometer.core.instrument.Metrics; import io.micrometer.core.instrument.Tag; import io.micrometer.core.instrument.Tags; import io.micrometer.core.instrument.Timer; -import java.time.Duration; import java.util.Collections; import java.util.List; import java.util.Optional; @@ -27,14 +26,13 @@ import org.eclipse.jetty.util.ConstantThrowable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.whispersystems.textsecuregcm.entities.MessageProtos.Envelope; -import org.whispersystems.textsecuregcm.experiment.ExperimentEnrollmentManager; import org.whispersystems.textsecuregcm.identity.AciServiceIdentifier; import org.whispersystems.textsecuregcm.identity.ServiceIdentifier; import org.whispersystems.textsecuregcm.metrics.MessageMetrics; import org.whispersystems.textsecuregcm.metrics.MetricsUtil; import org.whispersystems.textsecuregcm.metrics.UserAgentTagUtil; +import org.whispersystems.textsecuregcm.push.NotPushRegisteredException; import org.whispersystems.textsecuregcm.push.PushNotificationManager; -import org.whispersystems.textsecuregcm.push.PushNotificationScheduler; import org.whispersystems.textsecuregcm.push.ReceiptSender; import org.whispersystems.textsecuregcm.storage.Account; import org.whispersystems.textsecuregcm.storage.ClientReleaseManager; @@ -79,8 +77,6 @@ public class WebSocketConnection { @VisibleForTesting static final int MESSAGE_SENDER_MAX_CONCURRENCY = 256; - private static final Duration CLOSE_WITH_PENDING_MESSAGES_NOTIFICATION_DELAY = Duration.ofMinutes(1); - private static final Logger logger = LoggerFactory.getLogger(WebSocketConnection.class); private static final Logger droppedErrorLogger = LoggerFactory.getLogger("droppedError"); @@ -88,8 +84,6 @@ public class WebSocketConnection { private final MessagesManager messagesManager; private final MessageMetrics messageMetrics; private final PushNotificationManager pushNotificationManager; - private final PushNotificationScheduler pushNotificationScheduler; - private final ExperimentEnrollmentManager experimentEnrollmentManager; private final Account authenticatedAccount; private final Device authenticatedDevice; @@ -109,25 +103,21 @@ public class WebSocketConnection { final MessagesManager messagesManager, final MessageMetrics messageMetrics, final PushNotificationManager pushNotificationManager, - final PushNotificationScheduler pushNotificationScheduler, final Account authenticatedAccount, final Device authenticatedDevice, final WebSocketClient client, final Scheduler messageDeliveryScheduler, - final ClientReleaseManager clientReleaseManager, - final ExperimentEnrollmentManager experimentEnrollmentManager) { + final ClientReleaseManager clientReleaseManager) { this.receiptSender = receiptSender; this.messagesManager = messagesManager; this.messageMetrics = messageMetrics; this.pushNotificationManager = pushNotificationManager; - this.pushNotificationScheduler = pushNotificationScheduler; this.authenticatedAccount = authenticatedAccount; this.authenticatedDevice = authenticatedDevice; this.client = client; this.messageDeliveryScheduler = messageDeliveryScheduler; this.clientReleaseManager = clientReleaseManager; - this.experimentEnrollmentManager = experimentEnrollmentManager; this.messageStream = messagesManager.getMessages(authenticatedAccount.getAccountIdentifier(), authenticatedDevice); @@ -194,9 +184,10 @@ public class WebSocketConnection { messagesManager.mayHaveMessages(authenticatedAccount.getAccountIdentifier(), authenticatedDevice) .thenAccept(mayHaveMessages -> { if (mayHaveMessages) { - pushNotificationScheduler.scheduleDelayedNotification(authenticatedAccount, - authenticatedDevice, - CLOSE_WITH_PENDING_MESSAGES_NOTIFICATION_DELAY); + try { + pushNotificationManager.sendNewMessageNotification(authenticatedAccount, authenticatedDevice.getId(), true); + } catch (NotPushRegisteredException _) { + } } }); } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/workers/CommandDependencies.java b/service/src/main/java/org/whispersystems/textsecuregcm/workers/CommandDependencies.java index 0dc263aa4..216de7f3c 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/workers/CommandDependencies.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/workers/CommandDependencies.java @@ -446,7 +446,7 @@ public record CommandDependencies( APNSender apnSender = new APNSender(apnSenderExecutor, Clock.systemUTC(), configuration.getApnConfiguration()); FcmSender fcmSender = new FcmSender(fcmSenderExecutor, configuration.getFcmConfiguration().credentials().value()); PushNotificationScheduler pushNotificationScheduler = new PushNotificationScheduler(pushSchedulerCluster, - apnSender, fcmSender, accountsManager, 0, 0, retryExecutor); + apnSender, fcmSender, accountsManager, 0, 0); PushNotificationManager pushNotificationManager = new PushNotificationManager(accountsManager, apnSender, fcmSender, pushNotificationScheduler); diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/workers/ScheduledApnPushNotificationSenderServiceCommand.java b/service/src/main/java/org/whispersystems/textsecuregcm/workers/ScheduledApnPushNotificationSenderServiceCommand.java index 356c0b609..759a593e1 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/workers/ScheduledApnPushNotificationSenderServiceCommand.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/workers/ScheduledApnPushNotificationSenderServiceCommand.java @@ -90,8 +90,7 @@ public class ScheduledApnPushNotificationSenderServiceCommand extends ServerComm deps.fcmSender(), deps.accountsManager(), namespace.getInt(WORKER_COUNT), - namespace.getInt(MAX_CONCURRENCY), - retryExecutor + namespace.getInt(MAX_CONCURRENCY) ); environment.lifecycle().manage(pushNotificationScheduler); diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/MessageDispatcherIntegrationTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/MessageDispatcherIntegrationTest.java index ae1ac99b0..a1b67335c 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/MessageDispatcherIntegrationTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/MessageDispatcherIntegrationTest.java @@ -54,7 +54,6 @@ import org.whispersystems.textsecuregcm.entities.MessageProtos.Envelope; import org.whispersystems.textsecuregcm.identity.AciServiceIdentifier; import org.whispersystems.textsecuregcm.metrics.MessageMetrics; import org.whispersystems.textsecuregcm.push.PushNotificationManager; -import org.whispersystems.textsecuregcm.push.PushNotificationScheduler; import org.whispersystems.textsecuregcm.push.ReceiptSender; import org.whispersystems.textsecuregcm.redis.RedisServerExtension; import org.whispersystems.textsecuregcm.storage.Account; @@ -160,7 +159,6 @@ class MessageDispatcherIntegrationTest { sharedExecutorService, Clock.systemUTC()), new MessageMetrics(), mock(PushNotificationManager.class), - mock(PushNotificationScheduler.class), disconnectionRequestManager, mock(ClientReleaseManager.class)); } diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/MessageDispatcherTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/MessageDispatcherTest.java index 6abc6faff..501d1f80b 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/MessageDispatcherTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/MessageDispatcherTest.java @@ -41,6 +41,7 @@ import org.whispersystems.textsecuregcm.identity.AciServiceIdentifier; import org.whispersystems.textsecuregcm.identity.IdentityType; import org.whispersystems.textsecuregcm.identity.ServiceIdentifier; import org.whispersystems.textsecuregcm.metrics.MessageMetrics; +import org.whispersystems.textsecuregcm.push.NotPushRegisteredException; import org.whispersystems.textsecuregcm.push.PushNotificationManager; import org.whispersystems.textsecuregcm.push.PushNotificationScheduler; import org.whispersystems.textsecuregcm.push.ReceiptSender; @@ -74,7 +75,6 @@ class MessageDispatcherTest { private PushNotificationManager pushNotificationManager; private MessageDispatcher dispatcher; private DisconnectionRequestManager disconnectionRequestManager; - private PushNotificationScheduler pushNotificationScheduler; @BeforeEach void setUp() { @@ -87,7 +87,6 @@ class MessageDispatcherTest { receiptSender = mock(ReceiptSender.class); pushNotificationManager = mock(PushNotificationManager.class); disconnectionRequestManager = mock(DisconnectionRequestManager.class); - pushNotificationScheduler = mock(PushNotificationScheduler.class); when(device.getId()).thenReturn(DEVICE_ID); when(device.isPrimary()).thenReturn(true); @@ -97,14 +96,13 @@ class MessageDispatcherTest { messagesManager, new MessageMetrics(), pushNotificationManager, - pushNotificationScheduler, disconnectionRequestManager, mock(ClientReleaseManager.class)); } @ParameterizedTest @ValueSource(booleans = {false, true}) - void drainSingleMessage(final boolean mayHaveMoreMessages) { + void drainSingleMessage(final boolean mayHaveMoreMessages) throws NotPushRegisteredException { final ServiceIdentifier sourceServiceId = new AciServiceIdentifier(UUID.randomUUID()); final long timestamp = 17; @@ -127,8 +125,9 @@ class MessageDispatcherTest { verify(disconnectionRequestManager).removeListener(eq(ACI), eq(DEVICE_ID), any()); verify(pushNotificationManager).handleMessagesRetrieved(account, device, TEST_UA); verify(messagesManager).mayHaveMessages(ACI, device); - verify(pushNotificationScheduler, times(mayHaveMoreMessages ? 1 : 0)) - .scheduleDelayedNotification(account, device, MessageDispatcher.CLOSE_WITH_PENDING_MESSAGES_NOTIFICATION_DELAY); + verify(pushNotificationManager, times(mayHaveMoreMessages ? 1 : 0)) + .sendNewMessageNotification(account, device.getId(), true); + verifyNoInteractions(receiptSender); } diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/push/PushNotificationSchedulerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/push/PushNotificationSchedulerTest.java index 162fd640c..c126bc370 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/push/PushNotificationSchedulerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/push/PushNotificationSchedulerTest.java @@ -16,17 +16,14 @@ import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; import io.lettuce.core.cluster.SlotHash; -import java.time.Duration; import java.time.Instant; import java.time.temporal.ChronoUnit; import java.util.Optional; import java.util.UUID; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; -import java.util.concurrent.ScheduledExecutorService; import org.apache.commons.lang3.RandomStringUtils; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; @@ -89,7 +86,7 @@ class PushNotificationSchedulerTest { .thenReturn(CompletableFuture.completedFuture(new SendPushNotificationResult(true, Optional.empty(), false, Optional.empty()))); pushNotificationScheduler = new PushNotificationScheduler(REDIS_CLUSTER_EXTENSION.getRedisCluster(), - apnSender, fcmSender, accountsManager, clock, 1, 1, mock(ScheduledExecutorService.class)); + apnSender, fcmSender, accountsManager, clock, 1, 1); } @ParameterizedTest @@ -207,51 +204,6 @@ class PushNotificationSchedulerTest { verify(apnSender, never()).sendNotification(any()); } - @Test - void testScheduleDelayedNotification() { - clock.pin(Instant.now()); - - assertEquals(Optional.empty(), - pushNotificationScheduler.getNextScheduledDelayedNotificationTimestamp(account, device)); - - pushNotificationScheduler.scheduleDelayedNotification(account, device, Duration.ofMinutes(1)).join(); - - assertEquals(Optional.of(clock.instant().truncatedTo(ChronoUnit.MILLIS).plus(Duration.ofMinutes(1))), - pushNotificationScheduler.getNextScheduledDelayedNotificationTimestamp(account, device)); - - pushNotificationScheduler.scheduleDelayedNotification(account, device, Duration.ofMinutes(2)).join(); - - assertEquals(Optional.of(clock.instant().truncatedTo(ChronoUnit.MILLIS).plus(Duration.ofMinutes(2))), - pushNotificationScheduler.getNextScheduledDelayedNotificationTimestamp(account, device)); - } - - @Test - void testCancelDelayedNotification() { - pushNotificationScheduler.scheduleDelayedNotification(account, device, Duration.ofMinutes(1)).join(); - pushNotificationScheduler.cancelDelayedNotifications(account, device).join(); - - assertEquals(Optional.empty(), - pushNotificationScheduler.getNextScheduledDelayedNotificationTimestamp(account, device)); - } - - @Test - void testProcessScheduledDelayedNotifications() { - final PushNotificationScheduler.NotificationWorker worker = pushNotificationScheduler.new NotificationWorker(1); - final int slot = SlotHash.getSlot(PushNotificationScheduler.getDelayedNotificationQueueKey(account, device)); - - clock.pin(Instant.now()); - - pushNotificationScheduler.scheduleDelayedNotification(account, device, Duration.ofMinutes(1)).join(); - - assertEquals(0, worker.processScheduledDelayedNotifications(slot)); - - clock.pin(clock.instant().plus(Duration.ofMinutes(1))); - - assertEquals(1, worker.processScheduledDelayedNotifications(slot)); - assertEquals(Optional.empty(), - pushNotificationScheduler.getNextScheduledDelayedNotificationTimestamp(account, device)); - } - @ParameterizedTest @CsvSource({ "1, true", @@ -266,7 +218,7 @@ class PushNotificationSchedulerTest { final AccountsManager accountsManager = mock(AccountsManager.class); pushNotificationScheduler = new PushNotificationScheduler(redisCluster, apnSender, fcmSender, - accountsManager, dedicatedThreadCount, 1, mock(ScheduledExecutorService.class)); + accountsManager, dedicatedThreadCount, 1); pushNotificationScheduler.start(); pushNotificationScheduler.stop(); diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/websocket/WebSocketConnectionIntegrationTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/websocket/WebSocketConnectionIntegrationTest.java index e5f928a2e..4c91b1e83 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/websocket/WebSocketConnectionIntegrationTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/websocket/WebSocketConnectionIntegrationTest.java @@ -200,13 +200,11 @@ class WebSocketConnectionIntegrationTest { new MessagesManager(messagesDynamoDb, foundationDbMessageStore, reportMessageManager, sharedExecutorService, Clock.systemUTC()), new MessageMetrics(), mock(PushNotificationManager.class), - mock(PushNotificationScheduler.class), account, device, webSocketClient, messageDeliveryScheduler, - clientReleaseManager, - mock(ExperimentEnrollmentManager.class) + clientReleaseManager ); final List expectedMessages = new ArrayList<>(dynamoDbMessageCount + foundationDbMessageCount); @@ -285,13 +283,11 @@ class WebSocketConnectionIntegrationTest { new MessagesManager(messagesDynamoDb, foundationDbMessageStore, reportMessageManager, sharedExecutorService, Clock.systemUTC()), new MessageMetrics(), mock(PushNotificationManager.class), - mock(PushNotificationScheduler.class), account, device, webSocketClient, messageDeliveryScheduler, - clientReleaseManager, - mock(ExperimentEnrollmentManager.class) + clientReleaseManager ); final int dynamoDbMessageCount = 77; @@ -406,13 +402,11 @@ class WebSocketConnectionIntegrationTest { new MessagesManager(messagesDynamoDb, foundationDbMessageStore, reportMessageManager, sharedExecutorService, Clock.systemUTC()), new MessageMetrics(), mock(PushNotificationManager.class), - mock(PushNotificationScheduler.class), account, device, webSocketClient, messageDeliveryScheduler, - clientReleaseManager, - mock(ExperimentEnrollmentManager.class) + clientReleaseManager ); final int dynamoDBMessageCount = 207; @@ -493,13 +487,11 @@ class WebSocketConnectionIntegrationTest { messagesManager, new MessageMetrics(), mock(PushNotificationManager.class), - mock(PushNotificationScheduler.class), account, device, webSocketClient, messageDeliveryScheduler, - clientReleaseManager, - mock(ExperimentEnrollmentManager.class)); + clientReleaseManager); final WebSocketResponseMessage successResponse = mock(WebSocketResponseMessage.class); when(successResponse.getStatus()).thenReturn(200); diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/websocket/WebSocketConnectionTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/websocket/WebSocketConnectionTest.java index 844df9176..c826a2f09 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/websocket/WebSocketConnectionTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/websocket/WebSocketConnectionTest.java @@ -119,13 +119,11 @@ class WebSocketConnectionTest { messagesManager, new MessageMetrics(), mock(PushNotificationManager.class), - mock(PushNotificationScheduler.class), account, device, client, Schedulers.immediate(), - clientReleaseManager, - mock(ExperimentEnrollmentManager.class)); + clientReleaseManager); } @Test