mirror of
https://github.com/signalapp/Signal-Server
synced 2026-04-21 09:10:35 +01:00
Introduce a job scheduler and experiment for sending notifications to idle devices
This commit is contained in:
@@ -0,0 +1,280 @@
|
||||
package org.whispersystems.textsecuregcm.experiment;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyByte;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.google.i18n.phonenumbers.PhoneNumberUtil;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
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.api.Test;
|
||||
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.push.IdleDeviceNotificationScheduler;
|
||||
import org.whispersystems.textsecuregcm.storage.Account;
|
||||
import org.whispersystems.textsecuregcm.storage.Device;
|
||||
import org.whispersystems.textsecuregcm.storage.MessagesManager;
|
||||
|
||||
class NotifyIdleDevicesWithoutMessagesPushNotificationExperimentTest {
|
||||
|
||||
private MessagesManager messagesManager;
|
||||
private IdleDeviceNotificationScheduler idleDeviceNotificationScheduler;
|
||||
|
||||
private NotifyIdleDevicesWithoutMessagesPushNotificationExperiment experiment;
|
||||
|
||||
private static final Instant CURRENT_TIME = Instant.now();
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
messagesManager = mock(MessagesManager.class);
|
||||
|
||||
idleDeviceNotificationScheduler = mock(IdleDeviceNotificationScheduler.class);
|
||||
when(idleDeviceNotificationScheduler.scheduleNotification(any(), anyByte(), any()))
|
||||
.thenReturn(CompletableFuture.completedFuture(null));
|
||||
|
||||
experiment = new NotifyIdleDevicesWithoutMessagesPushNotificationExperiment(messagesManager,
|
||||
idleDeviceNotificationScheduler);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void isDeviceEligible(final Account account,
|
||||
final Device device,
|
||||
final boolean isDeviceIdle,
|
||||
final boolean mayHaveMessages,
|
||||
final boolean expectEligible) {
|
||||
|
||||
when(messagesManager.mayHaveMessages(account.getIdentifier(IdentityType.ACI), device))
|
||||
.thenReturn(CompletableFuture.completedFuture(mayHaveMessages));
|
||||
|
||||
when(idleDeviceNotificationScheduler.isIdle(device)).thenReturn(isDeviceIdle);
|
||||
|
||||
assertEquals(expectEligible, experiment.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));
|
||||
|
||||
{
|
||||
// Idle device with push token and messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getApnId()).thenReturn("apns-token");
|
||||
|
||||
arguments.add(Arguments.of(account, device, true, true, false));
|
||||
}
|
||||
|
||||
{
|
||||
// Idle device missing push token, but with messages
|
||||
arguments.add(Arguments.of(account, mock(Device.class), true, true, false));
|
||||
}
|
||||
|
||||
{
|
||||
// Idle device missing push token and messages
|
||||
arguments.add(Arguments.of(account, mock(Device.class), true, false, false));
|
||||
}
|
||||
|
||||
{
|
||||
// Idle device with push token, but no messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getApnId()).thenReturn("apns-token");
|
||||
|
||||
arguments.add(Arguments.of(account, device, true, false, true));
|
||||
}
|
||||
|
||||
{
|
||||
// Active device with push token and messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getApnId()).thenReturn("apns-token");
|
||||
|
||||
arguments.add(Arguments.of(account, device, false, true, false));
|
||||
}
|
||||
|
||||
{
|
||||
// Active device missing push token, but with messages
|
||||
arguments.add(Arguments.of(account, mock(Device.class), false, true, false));
|
||||
}
|
||||
|
||||
{
|
||||
// Active device missing push token and messages
|
||||
arguments.add(Arguments.of(account, mock(Device.class), false, false, false));
|
||||
}
|
||||
|
||||
{
|
||||
// Active device with push token, but no messages
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getApnId()).thenReturn("apns-token");
|
||||
|
||||
arguments.add(Arguments.of(account, device, false, false, false));
|
||||
}
|
||||
|
||||
return arguments;
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void hasPushToken(final Device device, final boolean expectHasPushToken) {
|
||||
assertEquals(expectHasPushToken, NotifyIdleDevicesWithoutMessagesPushNotificationExperiment.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));
|
||||
}
|
||||
|
||||
{
|
||||
// APNs VOIP token
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getApnId()).thenReturn("apns-token");
|
||||
when(device.getVoipApnId()).thenReturn("apns-voip-token");
|
||||
|
||||
arguments.add(Arguments.of(device, false));
|
||||
}
|
||||
|
||||
return arguments;
|
||||
}
|
||||
|
||||
@Test
|
||||
void getState() {
|
||||
assertEquals(DeviceLastSeenState.MISSING_DEVICE_STATE, experiment.getState(null, null));
|
||||
assertEquals(DeviceLastSeenState.MISSING_DEVICE_STATE, experiment.getState(mock(Account.class), null));
|
||||
|
||||
final long createdAtMillis = CURRENT_TIME.minus(Duration.ofDays(14)).toEpochMilli();
|
||||
|
||||
{
|
||||
final Device apnsDevice = mock(Device.class);
|
||||
when(apnsDevice.getApnId()).thenReturn("apns-token");
|
||||
when(apnsDevice.getCreated()).thenReturn(createdAtMillis);
|
||||
when(apnsDevice.getLastSeen()).thenReturn(CURRENT_TIME.toEpochMilli());
|
||||
|
||||
assertEquals(
|
||||
new DeviceLastSeenState(true, createdAtMillis, true, CURRENT_TIME.toEpochMilli(), DeviceLastSeenState.PushTokenType.APNS),
|
||||
experiment.getState(mock(Account.class), apnsDevice));
|
||||
}
|
||||
|
||||
{
|
||||
final Device fcmDevice = mock(Device.class);
|
||||
when(fcmDevice.getGcmId()).thenReturn("fcm-token");
|
||||
when(fcmDevice.getCreated()).thenReturn(createdAtMillis);
|
||||
when(fcmDevice.getLastSeen()).thenReturn(CURRENT_TIME.toEpochMilli());
|
||||
|
||||
assertEquals(
|
||||
new DeviceLastSeenState(true, createdAtMillis, true, CURRENT_TIME.toEpochMilli(), DeviceLastSeenState.PushTokenType.FCM),
|
||||
experiment.getState(mock(Account.class), fcmDevice));
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void getPopulation(final boolean inExperimentGroup,
|
||||
final DeviceLastSeenState.PushTokenType tokenType,
|
||||
final NotifyIdleDevicesWithoutMessagesPushNotificationExperiment.Population expectedPopulation) {
|
||||
|
||||
final DeviceLastSeenState state = new DeviceLastSeenState(true, 0, true, 0, tokenType);
|
||||
final PushNotificationExperimentSample<DeviceLastSeenState> sample =
|
||||
new PushNotificationExperimentSample<>(inExperimentGroup, state, state);
|
||||
|
||||
assertEquals(expectedPopulation, NotifyIdleDevicesWithoutMessagesPushNotificationExperiment.getPopulation(sample));
|
||||
}
|
||||
|
||||
private static List<Arguments> getPopulation() {
|
||||
return List.of(
|
||||
Arguments.of(true, DeviceLastSeenState.PushTokenType.APNS,
|
||||
NotifyIdleDevicesWithoutMessagesPushNotificationExperiment.Population.APNS_EXPERIMENT),
|
||||
|
||||
Arguments.of(false, DeviceLastSeenState.PushTokenType.APNS,
|
||||
NotifyIdleDevicesWithoutMessagesPushNotificationExperiment.Population.APNS_CONTROL),
|
||||
|
||||
Arguments.of(true, DeviceLastSeenState.PushTokenType.FCM,
|
||||
NotifyIdleDevicesWithoutMessagesPushNotificationExperiment.Population.FCM_EXPERIMENT),
|
||||
|
||||
Arguments.of(false, DeviceLastSeenState.PushTokenType.FCM,
|
||||
NotifyIdleDevicesWithoutMessagesPushNotificationExperiment.Population.FCM_CONTROL)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void getOutcome(final DeviceLastSeenState initialState,
|
||||
final DeviceLastSeenState finalState,
|
||||
final NotifyIdleDevicesWithoutMessagesPushNotificationExperiment.Outcome expectedOutcome) {
|
||||
|
||||
final PushNotificationExperimentSample<DeviceLastSeenState> sample =
|
||||
new PushNotificationExperimentSample<>(true, initialState, finalState);
|
||||
|
||||
assertEquals(expectedOutcome, NotifyIdleDevicesWithoutMessagesPushNotificationExperiment.getOutcome(sample));
|
||||
}
|
||||
|
||||
private static List<Arguments> getOutcome() {
|
||||
return List.of(
|
||||
// Device no longer exists
|
||||
Arguments.of(
|
||||
new DeviceLastSeenState(true, 0, true, 0, DeviceLastSeenState.PushTokenType.APNS),
|
||||
DeviceLastSeenState.MISSING_DEVICE_STATE,
|
||||
NotifyIdleDevicesWithoutMessagesPushNotificationExperiment.Outcome.DELETED
|
||||
),
|
||||
|
||||
// Device re-registered (i.e. "created" timestamp changed)
|
||||
Arguments.of(
|
||||
new DeviceLastSeenState(true, 0, true, 0, DeviceLastSeenState.PushTokenType.APNS),
|
||||
new DeviceLastSeenState(true, 1, true, 1, DeviceLastSeenState.PushTokenType.APNS),
|
||||
NotifyIdleDevicesWithoutMessagesPushNotificationExperiment.Outcome.DELETED
|
||||
),
|
||||
|
||||
// Device has lost push tokens
|
||||
Arguments.of(
|
||||
new DeviceLastSeenState(true, 0, true, 0, DeviceLastSeenState.PushTokenType.APNS),
|
||||
new DeviceLastSeenState(true, 0, false, 0, DeviceLastSeenState.PushTokenType.APNS),
|
||||
NotifyIdleDevicesWithoutMessagesPushNotificationExperiment.Outcome.UNINSTALLED
|
||||
),
|
||||
|
||||
// Device reactivated
|
||||
Arguments.of(
|
||||
new DeviceLastSeenState(true, 0, true, 0, DeviceLastSeenState.PushTokenType.APNS),
|
||||
new DeviceLastSeenState(true, 0, true, 1, DeviceLastSeenState.PushTokenType.APNS),
|
||||
NotifyIdleDevicesWithoutMessagesPushNotificationExperiment.Outcome.REACTIVATED
|
||||
),
|
||||
|
||||
// No change
|
||||
Arguments.of(
|
||||
new DeviceLastSeenState(true, 0, true, 0, DeviceLastSeenState.PushTokenType.APNS),
|
||||
new DeviceLastSeenState(true, 0, true, 0, DeviceLastSeenState.PushTokenType.APNS),
|
||||
NotifyIdleDevicesWithoutMessagesPushNotificationExperiment.Outcome.UNCHANGED
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
package org.whispersystems.textsecuregcm.push;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.anyByte;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.whispersystems.textsecuregcm.storage.Account;
|
||||
import org.whispersystems.textsecuregcm.storage.AccountsManager;
|
||||
import org.whispersystems.textsecuregcm.storage.Device;
|
||||
import org.whispersystems.textsecuregcm.util.SystemMapper;
|
||||
import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient;
|
||||
|
||||
class IdleDeviceNotificationSchedulerTest {
|
||||
|
||||
private AccountsManager accountsManager;
|
||||
private PushNotificationManager pushNotificationManager;
|
||||
|
||||
private IdleDeviceNotificationScheduler idleDeviceNotificationScheduler;
|
||||
|
||||
private static final Instant CURRENT_TIME = Instant.now();
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
accountsManager = mock(AccountsManager.class);
|
||||
pushNotificationManager = mock(PushNotificationManager.class);
|
||||
|
||||
idleDeviceNotificationScheduler = new IdleDeviceNotificationScheduler(
|
||||
accountsManager,
|
||||
pushNotificationManager,
|
||||
mock(DynamoDbAsyncClient.class),
|
||||
"test-idle-device-notifications",
|
||||
Duration.ofDays(7),
|
||||
Clock.fixed(CURRENT_TIME, ZoneId.systemDefault()));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void processJob(final boolean accountPresent,
|
||||
final boolean devicePresent,
|
||||
final boolean tokenPresent,
|
||||
final Instant deviceLastSeen,
|
||||
final String expectedOutcome) throws JsonProcessingException, NotPushRegisteredException {
|
||||
|
||||
final UUID accountIdentifier = UUID.randomUUID();
|
||||
final byte deviceId = Device.PRIMARY_ID;
|
||||
|
||||
final Device device = mock(Device.class);
|
||||
when(device.getLastSeen()).thenReturn(deviceLastSeen.toEpochMilli());
|
||||
|
||||
final Account account = mock(Account.class);
|
||||
when(account.getDevice(deviceId)).thenReturn(devicePresent ? Optional.of(device) : Optional.empty());
|
||||
|
||||
when(accountsManager.getByAccountIdentifierAsync(accountIdentifier))
|
||||
.thenReturn(CompletableFuture.completedFuture(accountPresent ? Optional.of(account) : Optional.empty()));
|
||||
|
||||
if (tokenPresent) {
|
||||
when(pushNotificationManager.sendNewMessageNotification(any(), anyByte(), anyBoolean()))
|
||||
.thenReturn(CompletableFuture.completedFuture(
|
||||
Optional.of(new SendPushNotificationResult(true, Optional.empty(), false, Optional.empty()))));
|
||||
} else {
|
||||
when(pushNotificationManager.sendNewMessageNotification(any(), anyByte(), anyBoolean()))
|
||||
.thenThrow(NotPushRegisteredException.class);
|
||||
}
|
||||
|
||||
final byte[] jobData = SystemMapper.jsonMapper().writeValueAsBytes(
|
||||
new IdleDeviceNotificationScheduler.AccountAndDeviceIdentifier(accountIdentifier, deviceId));
|
||||
|
||||
assertEquals(expectedOutcome, idleDeviceNotificationScheduler.processJob(jobData).join());
|
||||
}
|
||||
|
||||
private static List<Arguments> processJob() {
|
||||
final Instant idleDeviceLastSeenTimestamp = CURRENT_TIME
|
||||
.minus(IdleDeviceNotificationScheduler.MIN_IDLE_DURATION)
|
||||
.minus(Duration.ofDays(1));
|
||||
|
||||
return List.of(
|
||||
// Account present, device present, device has tokens, device is idle
|
||||
Arguments.of(true, true, true, idleDeviceLastSeenTimestamp, "sent"),
|
||||
|
||||
// Account present, device present, device has tokens, but device is active
|
||||
Arguments.of(true, true, true, CURRENT_TIME, "deviceSeenRecently"),
|
||||
|
||||
// Account present, device present, device is idle, but missing tokens
|
||||
Arguments.of(true, true, false, idleDeviceLastSeenTimestamp, "deviceTokenDeleted"),
|
||||
|
||||
// Account present, but device missing
|
||||
Arguments.of(true, false, true, idleDeviceLastSeenTimestamp, "deviceDeleted"),
|
||||
|
||||
// Account missing
|
||||
Arguments.of(false, true, true, idleDeviceLastSeenTimestamp, "accountDeleted")
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void isIdle() {
|
||||
{
|
||||
final Device idleDevice = mock(Device.class);
|
||||
when(idleDevice.getLastSeen())
|
||||
.thenReturn(CURRENT_TIME.minus(IdleDeviceNotificationScheduler.MIN_IDLE_DURATION).minus(Duration.ofDays(1))
|
||||
.toEpochMilli());
|
||||
|
||||
assertTrue(idleDeviceNotificationScheduler.isIdle(idleDevice));
|
||||
}
|
||||
|
||||
{
|
||||
final Device activeDevice = mock(Device.class);
|
||||
when(activeDevice.getLastSeen()).thenReturn(CURRENT_TIME.toEpochMilli());
|
||||
|
||||
assertFalse(idleDeviceNotificationScheduler.isIdle(activeDevice));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import net.sourceforge.argparse4j.inf.Namespace;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.whispersystems.textsecuregcm.WhisperServerConfiguration;
|
||||
import org.whispersystems.textsecuregcm.experiment.PushNotificationExperiment;
|
||||
import org.whispersystems.textsecuregcm.experiment.PushNotificationExperimentSample;
|
||||
import org.whispersystems.textsecuregcm.experiment.PushNotificationExperimentSamples;
|
||||
@@ -23,6 +24,7 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyByte;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -69,6 +71,7 @@ class FinishPushNotificationExperimentCommandTest {
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null);
|
||||
|
||||
//noinspection unchecked
|
||||
@@ -76,6 +79,13 @@ class FinishPushNotificationExperimentCommandTest {
|
||||
when(experiment.getExperimentName()).thenReturn(EXPERIMENT_NAME);
|
||||
when(experiment.getState(any(), any())).thenReturn("test");
|
||||
|
||||
doAnswer(invocation -> {
|
||||
final Flux<PushNotificationExperimentSample<String>> samples = invocation.getArgument(0);
|
||||
samples.then().block();
|
||||
|
||||
return null;
|
||||
}).when(experiment).analyzeResults(any());
|
||||
|
||||
finishPushNotificationExperimentCommand = new TestFinishPushNotificationExperimentCommand(experiment);
|
||||
}
|
||||
|
||||
|
||||
@@ -63,6 +63,7 @@ class StartPushNotificationExperimentCommandTest {
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user