mirror of
https://github.com/signalapp/Signal-Server
synced 2026-04-21 16:18:00 +01:00
Add ZeroTtlDevicePushNotificationExperiment
This commit is contained in:
@@ -69,7 +69,8 @@ class APNSenderTest {
|
||||
(Answer) invocationOnMock -> new MockPushNotificationFuture<>(invocationOnMock.getArgument(0), response));
|
||||
|
||||
PushNotification pushNotification = new PushNotification(DESTINATION_DEVICE_TOKEN, PushNotification.TokenType.APN,
|
||||
PushNotification.NotificationType.NOTIFICATION, null, destinationAccount, destinationDevice, urgent);
|
||||
PushNotification.NotificationType.NOTIFICATION, null, destinationAccount, destinationDevice, urgent,
|
||||
Optional.empty());
|
||||
|
||||
final SendPushNotificationResult result = apnSender.sendNotification(pushNotification).join();
|
||||
|
||||
@@ -113,7 +114,8 @@ class APNSenderTest {
|
||||
(Answer) invocationOnMock -> new MockPushNotificationFuture<>(invocationOnMock.getArgument(0), response));
|
||||
|
||||
PushNotification pushNotification = new PushNotification(DESTINATION_DEVICE_TOKEN, PushNotification.TokenType.APN,
|
||||
PushNotification.NotificationType.NOTIFICATION, null, destinationAccount, destinationDevice, true);
|
||||
PushNotification.NotificationType.NOTIFICATION, null, destinationAccount, destinationDevice, true,
|
||||
Optional.empty());
|
||||
|
||||
when(destinationDevice.getApnId()).thenReturn(DESTINATION_DEVICE_TOKEN);
|
||||
when(destinationDevice.getPushTimestamp()).thenReturn(System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(11));
|
||||
@@ -144,7 +146,8 @@ class APNSenderTest {
|
||||
(Answer) invocationOnMock -> new MockPushNotificationFuture<>(invocationOnMock.getArgument(0), response));
|
||||
|
||||
PushNotification pushNotification = new PushNotification(DESTINATION_DEVICE_TOKEN, PushNotification.TokenType.APN,
|
||||
PushNotification.NotificationType.NOTIFICATION, null, destinationAccount, destinationDevice, true);
|
||||
PushNotification.NotificationType.NOTIFICATION, null, destinationAccount, destinationDevice, true,
|
||||
Optional.empty());
|
||||
|
||||
final SendPushNotificationResult result = apnSender.sendNotification(pushNotification).join();
|
||||
|
||||
@@ -171,7 +174,8 @@ class APNSenderTest {
|
||||
new IOException("lost connection")));
|
||||
|
||||
PushNotification pushNotification = new PushNotification(DESTINATION_DEVICE_TOKEN, PushNotification.TokenType.APN,
|
||||
PushNotification.NotificationType.NOTIFICATION, null, destinationAccount, destinationDevice, true);
|
||||
PushNotification.NotificationType.NOTIFICATION, null, destinationAccount, destinationDevice, true,
|
||||
Optional.empty());
|
||||
|
||||
assertThatThrownBy(() -> apnSender.sendNotification(pushNotification).join())
|
||||
.isInstanceOf(CompletionException.class)
|
||||
|
||||
@@ -54,7 +54,7 @@ class FcmSenderTest {
|
||||
|
||||
@Test
|
||||
void testSendMessage() {
|
||||
final PushNotification pushNotification = new PushNotification("foo", PushNotification.TokenType.FCM, PushNotification.NotificationType.NOTIFICATION, null, null, null, true);
|
||||
final PushNotification pushNotification = new PushNotification("foo", PushNotification.TokenType.FCM, PushNotification.NotificationType.NOTIFICATION, null, null, null, true, Optional.empty());
|
||||
|
||||
final SettableApiFuture<String> sendFuture = SettableApiFuture.create();
|
||||
sendFuture.set("message-id");
|
||||
@@ -71,7 +71,7 @@ class FcmSenderTest {
|
||||
|
||||
@Test
|
||||
void testSendMessageRejected() {
|
||||
final PushNotification pushNotification = new PushNotification("foo", PushNotification.TokenType.FCM, PushNotification.NotificationType.NOTIFICATION, null, null, null, true);
|
||||
final PushNotification pushNotification = new PushNotification("foo", PushNotification.TokenType.FCM, PushNotification.NotificationType.NOTIFICATION, null, null, null, true, Optional.empty());
|
||||
|
||||
final FirebaseMessagingException invalidArgumentException = mock(FirebaseMessagingException.class);
|
||||
when(invalidArgumentException.getMessagingErrorCode()).thenReturn(MessagingErrorCode.INVALID_ARGUMENT);
|
||||
@@ -91,7 +91,7 @@ class FcmSenderTest {
|
||||
|
||||
@Test
|
||||
void testSendMessageUnregistered() {
|
||||
final PushNotification pushNotification = new PushNotification("foo", PushNotification.TokenType.FCM, PushNotification.NotificationType.NOTIFICATION, null, null, null, true);
|
||||
final PushNotification pushNotification = new PushNotification("foo", PushNotification.TokenType.FCM, PushNotification.NotificationType.NOTIFICATION, null, null, null, true, Optional.empty());
|
||||
|
||||
final FirebaseMessagingException unregisteredException = mock(FirebaseMessagingException.class);
|
||||
when(unregisteredException.getMessagingErrorCode()).thenReturn(MessagingErrorCode.UNREGISTERED);
|
||||
@@ -111,7 +111,7 @@ class FcmSenderTest {
|
||||
|
||||
@Test
|
||||
void testSendMessageException() {
|
||||
final PushNotification pushNotification = new PushNotification("foo", PushNotification.TokenType.FCM, PushNotification.NotificationType.NOTIFICATION, null, null, null, true);
|
||||
final PushNotification pushNotification = new PushNotification("foo", PushNotification.TokenType.FCM, PushNotification.NotificationType.NOTIFICATION, null, null, null, true, Optional.empty());
|
||||
|
||||
final SettableApiFuture<String> sendFuture = SettableApiFuture.create();
|
||||
sendFuture.setException(new IOException());
|
||||
|
||||
@@ -66,7 +66,7 @@ class PushNotificationManagerTest {
|
||||
.thenReturn(CompletableFuture.completedFuture(new SendPushNotificationResult(true, Optional.empty(), false, Optional.empty())));
|
||||
|
||||
pushNotificationManager.sendNewMessageNotification(account, Device.PRIMARY_ID, urgent);
|
||||
verify(fcmSender).sendNotification(new PushNotification(deviceToken, PushNotification.TokenType.FCM, PushNotification.NotificationType.NOTIFICATION, null, account, device, urgent));
|
||||
verify(fcmSender).sendNotification(new PushNotification(deviceToken, PushNotification.TokenType.FCM, PushNotification.NotificationType.NOTIFICATION, null, account, device, urgent, Optional.empty()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -78,7 +78,7 @@ class PushNotificationManagerTest {
|
||||
.thenReturn(CompletableFuture.completedFuture(new SendPushNotificationResult(true, Optional.empty(), false, Optional.empty())));
|
||||
|
||||
pushNotificationManager.sendRegistrationChallengeNotification(deviceToken, PushNotification.TokenType.APN, challengeToken);
|
||||
verify(apnSender).sendNotification(new PushNotification(deviceToken, PushNotification.TokenType.APN, PushNotification.NotificationType.CHALLENGE, challengeToken, null, null, true));
|
||||
verify(apnSender).sendNotification(new PushNotification(deviceToken, PushNotification.TokenType.APN, PushNotification.NotificationType.CHALLENGE, challengeToken, null, null, true, Optional.empty()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -97,7 +97,7 @@ class PushNotificationManagerTest {
|
||||
.thenReturn(CompletableFuture.completedFuture(new SendPushNotificationResult(true, Optional.empty(), false, Optional.empty())));
|
||||
|
||||
pushNotificationManager.sendRateLimitChallengeNotification(account, challengeToken);
|
||||
verify(apnSender).sendNotification(new PushNotification(deviceToken, PushNotification.TokenType.APN, PushNotification.NotificationType.RATE_LIMIT_CHALLENGE, challengeToken, account, device, true));
|
||||
verify(apnSender).sendNotification(new PushNotification(deviceToken, PushNotification.TokenType.APN, PushNotification.NotificationType.RATE_LIMIT_CHALLENGE, challengeToken, account, device, true, Optional.empty()));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@@ -124,10 +124,10 @@ class PushNotificationManagerTest {
|
||||
|
||||
if (isApn){
|
||||
verify(apnSender).sendNotification(new PushNotification(deviceToken, PushNotification.TokenType.APN,
|
||||
PushNotification.NotificationType.ATTEMPT_LOGIN_NOTIFICATION_HIGH_PRIORITY, "someContext", account, device, true));
|
||||
PushNotification.NotificationType.ATTEMPT_LOGIN_NOTIFICATION_HIGH_PRIORITY, "someContext", account, device, true, Optional.empty()));
|
||||
} else {
|
||||
verify(fcmSender, times(1)).sendNotification(new PushNotification(deviceToken, PushNotification.TokenType.FCM,
|
||||
PushNotification.NotificationType.ATTEMPT_LOGIN_NOTIFICATION_HIGH_PRIORITY, "someContext", account, device, true));
|
||||
PushNotification.NotificationType.ATTEMPT_LOGIN_NOTIFICATION_HIGH_PRIORITY, "someContext", account, device, true, Optional.empty()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,7 +141,7 @@ class PushNotificationManagerTest {
|
||||
when(account.getDevice(Device.PRIMARY_ID)).thenReturn(Optional.of(device));
|
||||
|
||||
final PushNotification pushNotification = new PushNotification(
|
||||
"token", PushNotification.TokenType.FCM, PushNotification.NotificationType.NOTIFICATION, null, account, device, urgent);
|
||||
"token", PushNotification.TokenType.FCM, PushNotification.NotificationType.NOTIFICATION, null, account, device, urgent, Optional.empty());
|
||||
|
||||
when(fcmSender.sendNotification(pushNotification))
|
||||
.thenReturn(CompletableFuture.completedFuture(new SendPushNotificationResult(true, Optional.empty(), false, Optional.empty())));
|
||||
@@ -165,7 +165,7 @@ class PushNotificationManagerTest {
|
||||
when(account.getDevice(Device.PRIMARY_ID)).thenReturn(Optional.of(device));
|
||||
|
||||
final PushNotification pushNotification = new PushNotification(
|
||||
"token", PushNotification.TokenType.APN, PushNotification.NotificationType.NOTIFICATION, null, account, device, urgent);
|
||||
"token", PushNotification.TokenType.APN, PushNotification.NotificationType.NOTIFICATION, null, account, device, urgent, Optional.empty());
|
||||
|
||||
when(apnSender.sendNotification(pushNotification))
|
||||
.thenReturn(CompletableFuture.completedFuture(new SendPushNotificationResult(true, Optional.empty(), false, Optional.empty())));
|
||||
@@ -200,7 +200,7 @@ class PushNotificationManagerTest {
|
||||
when(accountsManager.getByAccountIdentifier(aci)).thenReturn(Optional.of(account));
|
||||
|
||||
final PushNotification pushNotification = new PushNotification(
|
||||
"token", PushNotification.TokenType.FCM, PushNotification.NotificationType.NOTIFICATION, null, account, device, true);
|
||||
"token", PushNotification.TokenType.FCM, PushNotification.NotificationType.NOTIFICATION, null, account, device, true, Optional.empty());
|
||||
|
||||
when(fcmSender.sendNotification(pushNotification))
|
||||
.thenReturn(CompletableFuture.completedFuture(new SendPushNotificationResult(false, Optional.empty(), true, Optional.empty())));
|
||||
@@ -225,7 +225,7 @@ class PushNotificationManagerTest {
|
||||
when(accountsManager.getByAccountIdentifier(aci)).thenReturn(Optional.of(account));
|
||||
|
||||
final PushNotification pushNotification = new PushNotification(
|
||||
"token", PushNotification.TokenType.APN, PushNotification.NotificationType.NOTIFICATION, null, account, device, true);
|
||||
"token", PushNotification.TokenType.APN, PushNotification.NotificationType.NOTIFICATION, null, account, device, true, Optional.empty());
|
||||
|
||||
when(apnSender.sendNotification(pushNotification))
|
||||
.thenReturn(CompletableFuture.completedFuture(new SendPushNotificationResult(false, Optional.empty(), true, Optional.empty())));
|
||||
@@ -256,7 +256,7 @@ class PushNotificationManagerTest {
|
||||
when(accountsManager.getByAccountIdentifier(aci)).thenReturn(Optional.of(account));
|
||||
|
||||
final PushNotification pushNotification = new PushNotification(
|
||||
"token", PushNotification.TokenType.APN, PushNotification.NotificationType.NOTIFICATION, null, account, device, true);
|
||||
"token", PushNotification.TokenType.APN, PushNotification.NotificationType.NOTIFICATION, null, account, device, true, Optional.empty());
|
||||
|
||||
when(apnSender.sendNotification(pushNotification))
|
||||
.thenReturn(CompletableFuture.completedFuture(new SendPushNotificationResult(false, Optional.empty(), true, Optional.of(tokenTimestamp.minusSeconds(60)))));
|
||||
|
||||
@@ -0,0 +1,272 @@
|
||||
/*
|
||||
* Copyright 2025 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.whispersystems.textsecuregcm.workers;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.google.i18n.phonenumbers.PhoneNumberUtil;
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.whispersystems.textsecuregcm.identity.IdentityType;
|
||||
import org.whispersystems.textsecuregcm.storage.Account;
|
||||
import org.whispersystems.textsecuregcm.storage.Device;
|
||||
import org.whispersystems.textsecuregcm.storage.MessagesManager;
|
||||
|
||||
public class IdleWakeupEligibilityCheckerTest {
|
||||
|
||||
private static final Instant CURRENT_TIME = Instant.now();
|
||||
private final Clock clock = Clock.fixed(CURRENT_TIME, ZoneId.systemDefault());
|
||||
|
||||
private MessagesManager messagesManager;
|
||||
private IdleWakeupEligibilityChecker idleChecker;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
messagesManager = mock(MessagesManager.class);
|
||||
idleChecker = new IdleWakeupEligibilityChecker(clock, messagesManager);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void isDeviceEligible(final Account account,
|
||||
final Device device,
|
||||
final boolean mayHaveMessages,
|
||||
final boolean mayHaveUrgentMessages,
|
||||
final boolean expectEligible) {
|
||||
|
||||
when(messagesManager.mayHavePersistedMessages(account.getIdentifier(IdentityType.ACI), device))
|
||||
.thenReturn(CompletableFuture.completedFuture(mayHaveMessages));
|
||||
|
||||
when(messagesManager.mayHaveUrgentPersistedMessages(account.getIdentifier(IdentityType.ACI), device))
|
||||
.thenReturn(CompletableFuture.completedFuture(mayHaveUrgentMessages));
|
||||
|
||||
assertEquals(expectEligible, idleChecker.isDeviceEligible(account, device).join());
|
||||
}
|
||||
|
||||
private static List<Arguments> isDeviceEligible() {
|
||||
final List<Arguments> arguments = new ArrayList<>();
|
||||
|
||||
final Account account = mock(Account.class);
|
||||
when(account.getIdentifier(IdentityType.ACI)).thenReturn(UUID.randomUUID());
|
||||
when(account.getNumber()).thenReturn(PhoneNumberUtil.getInstance().format(
|
||||
PhoneNumberUtil.getInstance().getExampleNumber("US"), PhoneNumberUtil.PhoneNumberFormat.E164));
|
||||
|
||||
{
|
||||
// Long-idle device with push token and messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getApnId()).thenReturn("apns-token");
|
||||
when(device.getLastSeen()).thenReturn(
|
||||
CURRENT_TIME.minus(IdleWakeupEligibilityChecker.MIN_LONG_IDLE_DURATION).toEpochMilli());
|
||||
|
||||
arguments.add(Arguments.of(account, device, true, true, false));
|
||||
}
|
||||
|
||||
{
|
||||
// Long-idle device missing push token, but with messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getLastSeen()).thenReturn(
|
||||
CURRENT_TIME.minus(IdleWakeupEligibilityChecker.MIN_LONG_IDLE_DURATION).toEpochMilli());
|
||||
|
||||
arguments.add(Arguments.of(account, device, true, true, false));
|
||||
}
|
||||
|
||||
{
|
||||
// Long-idle device missing push token and messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getLastSeen()).thenReturn(
|
||||
CURRENT_TIME.minus(IdleWakeupEligibilityChecker.MIN_LONG_IDLE_DURATION).toEpochMilli());
|
||||
|
||||
arguments.add(Arguments.of(account, device, false, false, false));
|
||||
}
|
||||
|
||||
{
|
||||
// Long-idle device with push token, but no messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getLastSeen()).thenReturn(
|
||||
CURRENT_TIME.minus(IdleWakeupEligibilityChecker.MIN_LONG_IDLE_DURATION).toEpochMilli());
|
||||
when(device.getApnId()).thenReturn("apns-token");
|
||||
|
||||
arguments.add(Arguments.of(account, device, false, false, true));
|
||||
}
|
||||
|
||||
{
|
||||
// Short-idle device with push token and urgent messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getApnId()).thenReturn("apns-token");
|
||||
when(device.getLastSeen()).thenReturn(
|
||||
CURRENT_TIME.minus(IdleWakeupEligibilityChecker.MIN_SHORT_IDLE_DURATION).toEpochMilli());
|
||||
|
||||
arguments.add(Arguments.of(account, device, true, true, true));
|
||||
}
|
||||
|
||||
{
|
||||
// Short-idle device with push token and only non-urgent messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getApnId()).thenReturn("apns-token");
|
||||
when(device.getLastSeen()).thenReturn(
|
||||
CURRENT_TIME.minus(IdleWakeupEligibilityChecker.MIN_SHORT_IDLE_DURATION).toEpochMilli());
|
||||
|
||||
arguments.add(Arguments.of(account, device, true, false, false));
|
||||
}
|
||||
|
||||
{
|
||||
// Short-idle device missing push token, but with urgent messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getLastSeen()).thenReturn(
|
||||
CURRENT_TIME.minus(IdleWakeupEligibilityChecker.MIN_SHORT_IDLE_DURATION).toEpochMilli());
|
||||
|
||||
arguments.add(Arguments.of(account, device, true, true, false));
|
||||
}
|
||||
|
||||
{
|
||||
// Short-idle device missing push token and messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getLastSeen()).thenReturn(
|
||||
CURRENT_TIME.minus(IdleWakeupEligibilityChecker.MIN_SHORT_IDLE_DURATION).toEpochMilli());
|
||||
|
||||
arguments.add(Arguments.of(account, device, false, false, false));
|
||||
}
|
||||
|
||||
{
|
||||
// Short-idle device with push token, but no messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getLastSeen()).thenReturn(
|
||||
CURRENT_TIME.minus(IdleWakeupEligibilityChecker.MIN_SHORT_IDLE_DURATION).toEpochMilli());
|
||||
when(device.getApnId()).thenReturn("apns-token");
|
||||
|
||||
arguments.add(Arguments.of(account, device, false, false, false));
|
||||
}
|
||||
|
||||
{
|
||||
// Active device with push token and urgent messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getLastSeen()).thenReturn(CURRENT_TIME.toEpochMilli());
|
||||
when(device.getApnId()).thenReturn("apns-token");
|
||||
|
||||
arguments.add(Arguments.of(account, device, true, true, false));
|
||||
}
|
||||
|
||||
{
|
||||
// Active device missing push token, but with urgent messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getLastSeen()).thenReturn(CURRENT_TIME.toEpochMilli());
|
||||
|
||||
arguments.add(Arguments.of(account, device, true, true, false));
|
||||
}
|
||||
|
||||
{
|
||||
// Active device missing push token and messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getLastSeen()).thenReturn(CURRENT_TIME.toEpochMilli());
|
||||
|
||||
arguments.add(Arguments.of(account, device, false, false, false));
|
||||
}
|
||||
|
||||
{
|
||||
// Active device with push token, but no messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getLastSeen()).thenReturn(CURRENT_TIME.toEpochMilli());
|
||||
when(device.getApnId()).thenReturn("apns-token");
|
||||
|
||||
arguments.add(Arguments.of(account, device, false, false, false));
|
||||
}
|
||||
|
||||
return arguments;
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void isShortIdle(final Duration idleDuration, final boolean expectIdle) {
|
||||
final Instant currentTime = Instant.now();
|
||||
final Clock clock = Clock.fixed(currentTime, ZoneId.systemDefault());
|
||||
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getLastSeen()).thenReturn(currentTime.minus(idleDuration).toEpochMilli());
|
||||
|
||||
assertEquals(expectIdle, IdleWakeupEligibilityChecker.isShortIdle(device, clock));
|
||||
}
|
||||
|
||||
private static List<Arguments> isShortIdle() {
|
||||
return List.of(
|
||||
Arguments.of(IdleWakeupEligibilityChecker.MIN_SHORT_IDLE_DURATION, true),
|
||||
Arguments.of(IdleWakeupEligibilityChecker.MIN_SHORT_IDLE_DURATION.plusMillis(1), true),
|
||||
Arguments.of(IdleWakeupEligibilityChecker.MIN_SHORT_IDLE_DURATION.minusMillis(1), false),
|
||||
Arguments.of(IdleWakeupEligibilityChecker.MAX_SHORT_IDLE_DURATION, false),
|
||||
Arguments.of(IdleWakeupEligibilityChecker.MAX_SHORT_IDLE_DURATION.plusMillis(1), false),
|
||||
Arguments.of(IdleWakeupEligibilityChecker.MAX_SHORT_IDLE_DURATION.minusMillis(1), true)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void isLongIdle(final Duration idleDuration, final boolean expectIdle) {
|
||||
final Instant currentTime = Instant.now();
|
||||
final Clock clock = Clock.fixed(currentTime, ZoneId.systemDefault());
|
||||
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getLastSeen()).thenReturn(currentTime.minus(idleDuration).toEpochMilli());
|
||||
|
||||
assertEquals(expectIdle, IdleWakeupEligibilityChecker.isLongIdle(device, clock));
|
||||
}
|
||||
|
||||
private static List<Arguments> isLongIdle() {
|
||||
return List.of(
|
||||
Arguments.of(IdleWakeupEligibilityChecker.MIN_LONG_IDLE_DURATION, true),
|
||||
Arguments.of(IdleWakeupEligibilityChecker.MIN_LONG_IDLE_DURATION.plusMillis(1), true),
|
||||
Arguments.of(IdleWakeupEligibilityChecker.MIN_LONG_IDLE_DURATION.minusMillis(1), false),
|
||||
Arguments.of(IdleWakeupEligibilityChecker.MAX_LONG_IDLE_DURATION, false),
|
||||
Arguments.of(IdleWakeupEligibilityChecker.MAX_LONG_IDLE_DURATION.plusMillis(1), false),
|
||||
Arguments.of(IdleWakeupEligibilityChecker.MAX_LONG_IDLE_DURATION.minusMillis(1), true)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void hasPushToken(final Device device, final boolean expectHasPushToken) {
|
||||
assertEquals(expectHasPushToken, IdleWakeupEligibilityChecker.hasPushToken(device));
|
||||
}
|
||||
|
||||
private static List<Arguments> hasPushToken() {
|
||||
final List<Arguments> arguments = new ArrayList<>();
|
||||
|
||||
{
|
||||
// No token at all
|
||||
final Device device = mock(Device.class);
|
||||
|
||||
arguments.add(Arguments.of(device, false));
|
||||
}
|
||||
|
||||
{
|
||||
// FCM token
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getGcmId()).thenReturn("fcm-token");
|
||||
|
||||
arguments.add(Arguments.of(device, true));
|
||||
}
|
||||
|
||||
{
|
||||
// APNs token
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getApnId()).thenReturn("apns-token");
|
||||
|
||||
arguments.add(Arguments.of(device, true));
|
||||
}
|
||||
|
||||
return arguments;
|
||||
}
|
||||
}
|
||||
@@ -8,12 +8,9 @@ import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.google.i18n.phonenumbers.PhoneNumberUtil;
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
@@ -21,8 +18,6 @@ import java.util.concurrent.CompletableFuture;
|
||||
import net.sourceforge.argparse4j.inf.Namespace;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.whispersystems.textsecuregcm.identity.IdentityType;
|
||||
import org.whispersystems.textsecuregcm.push.IdleDeviceNotificationScheduler;
|
||||
@@ -124,7 +119,7 @@ class NotifyIdleDevicesCommandTest {
|
||||
when(eligibleDevice.getId()).thenReturn(Device.PRIMARY_ID);
|
||||
when(eligibleDevice.getApnId()).thenReturn("apns-token");
|
||||
when(eligibleDevice.getLastSeen())
|
||||
.thenReturn(CURRENT_TIME.minus(NotifyIdleDevicesCommand.MIN_LONG_IDLE_DURATION).toEpochMilli());
|
||||
.thenReturn(CURRENT_TIME.minus(IdleWakeupEligibilityChecker.MIN_LONG_IDLE_DURATION).toEpochMilli());
|
||||
|
||||
final Device ineligibleDevice = mock(Device.class);
|
||||
when(ineligibleDevice.getId()).thenReturn((byte) (Device.PRIMARY_ID + 1));
|
||||
@@ -147,225 +142,4 @@ class NotifyIdleDevicesCommandTest {
|
||||
|
||||
verify(idleDeviceNotificationScheduler, never()).scheduleNotification(eq(account), eq(ineligibleDevice), any());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void isDeviceEligible(final Account account,
|
||||
final Device device,
|
||||
final boolean mayHaveMessages,
|
||||
final boolean mayHaveUrgentMessages,
|
||||
final boolean expectEligible) {
|
||||
|
||||
when(messagesManager.mayHavePersistedMessages(account.getIdentifier(IdentityType.ACI), device))
|
||||
.thenReturn(CompletableFuture.completedFuture(mayHaveMessages));
|
||||
|
||||
when(messagesManager.mayHaveUrgentPersistedMessages(account.getIdentifier(IdentityType.ACI), device))
|
||||
.thenReturn(CompletableFuture.completedFuture(mayHaveUrgentMessages));
|
||||
|
||||
assertEquals(expectEligible,
|
||||
NotifyIdleDevicesCommand.isDeviceEligible(account, device, messagesManager, Clock.fixed(CURRENT_TIME, ZoneId.systemDefault())).block());
|
||||
}
|
||||
|
||||
private static List<Arguments> isDeviceEligible() {
|
||||
final List<Arguments> arguments = new ArrayList<>();
|
||||
|
||||
final Account account = mock(Account.class);
|
||||
when(account.getIdentifier(IdentityType.ACI)).thenReturn(UUID.randomUUID());
|
||||
when(account.getNumber()).thenReturn(PhoneNumberUtil.getInstance().format(
|
||||
PhoneNumberUtil.getInstance().getExampleNumber("US"), PhoneNumberUtil.PhoneNumberFormat.E164));
|
||||
|
||||
{
|
||||
// Long-idle device with push token and messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getApnId()).thenReturn("apns-token");
|
||||
when(device.getLastSeen()).thenReturn(CURRENT_TIME.minus(NotifyIdleDevicesCommand.MIN_LONG_IDLE_DURATION).toEpochMilli());
|
||||
|
||||
arguments.add(Arguments.of(account, device, true, true, false));
|
||||
}
|
||||
|
||||
{
|
||||
// Long-idle device missing push token, but with messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getLastSeen()).thenReturn(CURRENT_TIME.minus(NotifyIdleDevicesCommand.MIN_LONG_IDLE_DURATION).toEpochMilli());
|
||||
|
||||
arguments.add(Arguments.of(account, device, true, true, false));
|
||||
}
|
||||
|
||||
{
|
||||
// Long-idle device missing push token and messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getLastSeen()).thenReturn(CURRENT_TIME.minus(NotifyIdleDevicesCommand.MIN_LONG_IDLE_DURATION).toEpochMilli());
|
||||
|
||||
arguments.add(Arguments.of(account, device, false, false, false));
|
||||
}
|
||||
|
||||
{
|
||||
// Long-idle device with push token, but no messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getLastSeen()).thenReturn(CURRENT_TIME.minus(NotifyIdleDevicesCommand.MIN_LONG_IDLE_DURATION).toEpochMilli());
|
||||
when(device.getApnId()).thenReturn("apns-token");
|
||||
|
||||
arguments.add(Arguments.of(account, device, false, false, true));
|
||||
}
|
||||
|
||||
{
|
||||
// Short-idle device with push token and urgent messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getApnId()).thenReturn("apns-token");
|
||||
when(device.getLastSeen()).thenReturn(CURRENT_TIME.minus(NotifyIdleDevicesCommand.MIN_SHORT_IDLE_DURATION).toEpochMilli());
|
||||
|
||||
arguments.add(Arguments.of(account, device, true, true, true));
|
||||
}
|
||||
|
||||
{
|
||||
// Short-idle device with push token and only non-urgent messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getApnId()).thenReturn("apns-token");
|
||||
when(device.getLastSeen()).thenReturn(CURRENT_TIME.minus(NotifyIdleDevicesCommand.MIN_SHORT_IDLE_DURATION).toEpochMilli());
|
||||
|
||||
arguments.add(Arguments.of(account, device, true, false, false));
|
||||
}
|
||||
|
||||
{
|
||||
// Short-idle device missing push token, but with urgent messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getLastSeen()).thenReturn(CURRENT_TIME.minus(NotifyIdleDevicesCommand.MIN_SHORT_IDLE_DURATION).toEpochMilli());
|
||||
|
||||
arguments.add(Arguments.of(account, device, true, true, false));
|
||||
}
|
||||
|
||||
{
|
||||
// Short-idle device missing push token and messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getLastSeen()).thenReturn(CURRENT_TIME.minus(NotifyIdleDevicesCommand.MIN_SHORT_IDLE_DURATION).toEpochMilli());
|
||||
|
||||
arguments.add(Arguments.of(account, device, false, false, false));
|
||||
}
|
||||
|
||||
{
|
||||
// Short-idle device with push token, but no messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getLastSeen()).thenReturn(CURRENT_TIME.minus(NotifyIdleDevicesCommand.MIN_SHORT_IDLE_DURATION).toEpochMilli());
|
||||
when(device.getApnId()).thenReturn("apns-token");
|
||||
|
||||
arguments.add(Arguments.of(account, device, false, false, false));
|
||||
}
|
||||
|
||||
{
|
||||
// Active device with push token and urgent messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getLastSeen()).thenReturn(CURRENT_TIME.toEpochMilli());
|
||||
when(device.getApnId()).thenReturn("apns-token");
|
||||
|
||||
arguments.add(Arguments.of(account, device, true, true, false));
|
||||
}
|
||||
|
||||
{
|
||||
// Active device missing push token, but with urgent messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getLastSeen()).thenReturn(CURRENT_TIME.toEpochMilli());
|
||||
|
||||
arguments.add(Arguments.of(account, device, true, true, false));
|
||||
}
|
||||
|
||||
{
|
||||
// Active device missing push token and messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getLastSeen()).thenReturn(CURRENT_TIME.toEpochMilli());
|
||||
|
||||
arguments.add(Arguments.of(account, device, false, false, false));
|
||||
}
|
||||
|
||||
{
|
||||
// Active device with push token, but no messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getLastSeen()).thenReturn(CURRENT_TIME.toEpochMilli());
|
||||
when(device.getApnId()).thenReturn("apns-token");
|
||||
|
||||
arguments.add(Arguments.of(account, device, false, false, false));
|
||||
}
|
||||
|
||||
return arguments;
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void isShortIdle(final Duration idleDuration, final boolean expectIdle) {
|
||||
final Instant currentTime = Instant.now();
|
||||
final Clock clock = Clock.fixed(currentTime, ZoneId.systemDefault());
|
||||
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getLastSeen()).thenReturn(currentTime.minus(idleDuration).toEpochMilli());
|
||||
|
||||
assertEquals(expectIdle, NotifyIdleDevicesCommand.isShortIdle(device, clock));
|
||||
}
|
||||
|
||||
private static List<Arguments> isShortIdle() {
|
||||
return List.of(
|
||||
Arguments.of(NotifyIdleDevicesCommand.MIN_SHORT_IDLE_DURATION, true),
|
||||
Arguments.of(NotifyIdleDevicesCommand.MIN_SHORT_IDLE_DURATION.plusMillis(1), true),
|
||||
Arguments.of(NotifyIdleDevicesCommand.MIN_SHORT_IDLE_DURATION.minusMillis(1), false),
|
||||
Arguments.of(NotifyIdleDevicesCommand.MAX_SHORT_IDLE_DURATION, false),
|
||||
Arguments.of(NotifyIdleDevicesCommand.MAX_SHORT_IDLE_DURATION.plusMillis(1), false),
|
||||
Arguments.of(NotifyIdleDevicesCommand.MAX_SHORT_IDLE_DURATION.minusMillis(1), true)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void isLongIdle(final Duration idleDuration, final boolean expectIdle) {
|
||||
final Instant currentTime = Instant.now();
|
||||
final Clock clock = Clock.fixed(currentTime, ZoneId.systemDefault());
|
||||
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getLastSeen()).thenReturn(currentTime.minus(idleDuration).toEpochMilli());
|
||||
|
||||
assertEquals(expectIdle, NotifyIdleDevicesCommand.isLongIdle(device, clock));
|
||||
}
|
||||
|
||||
private static List<Arguments> isLongIdle() {
|
||||
return List.of(
|
||||
Arguments.of(NotifyIdleDevicesCommand.MIN_LONG_IDLE_DURATION, true),
|
||||
Arguments.of(NotifyIdleDevicesCommand.MIN_LONG_IDLE_DURATION.plusMillis(1), true),
|
||||
Arguments.of(NotifyIdleDevicesCommand.MIN_LONG_IDLE_DURATION.minusMillis(1), false),
|
||||
Arguments.of(NotifyIdleDevicesCommand.MAX_LONG_IDLE_DURATION, false),
|
||||
Arguments.of(NotifyIdleDevicesCommand.MAX_LONG_IDLE_DURATION.plusMillis(1), false),
|
||||
Arguments.of(NotifyIdleDevicesCommand.MAX_LONG_IDLE_DURATION.minusMillis(1), true)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void hasPushToken(final Device device, final boolean expectHasPushToken) {
|
||||
assertEquals(expectHasPushToken, NotifyIdleDevicesCommand.hasPushToken(device));
|
||||
}
|
||||
|
||||
private static List<Arguments> hasPushToken() {
|
||||
final List<Arguments> arguments = new ArrayList<>();
|
||||
|
||||
{
|
||||
// No token at all
|
||||
final Device device = mock(Device.class);
|
||||
|
||||
arguments.add(Arguments.of(device, false));
|
||||
}
|
||||
|
||||
{
|
||||
// FCM token
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getGcmId()).thenReturn("fcm-token");
|
||||
|
||||
arguments.add(Arguments.of(device, true));
|
||||
}
|
||||
|
||||
{
|
||||
// APNs token
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getApnId()).thenReturn("apns-token");
|
||||
|
||||
arguments.add(Arguments.of(device, true));
|
||||
}
|
||||
|
||||
return arguments;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user