Add isEnrolled(e164, experiment) for pre-registration experiments

This commit is contained in:
Chris Eager
2021-03-11 11:23:56 -06:00
committed by Chris Eager
parent 25f603efc9
commit ad838b4827
2 changed files with 106 additions and 29 deletions

View File

@@ -5,8 +5,7 @@
package org.whispersystems.textsecuregcm.experiment;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -14,53 +13,104 @@ import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Stream;
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.configuration.dynamic.DynamicConfiguration;
import org.whispersystems.textsecuregcm.configuration.dynamic.DynamicExperimentEnrollmentConfiguration;
import org.whispersystems.textsecuregcm.configuration.dynamic.DynamicPreRegistrationExperimentEnrollmentConfiguration;
import org.whispersystems.textsecuregcm.storage.Account;
import org.whispersystems.textsecuregcm.storage.DynamicConfigurationManager;
class ExperimentEnrollmentManagerTest {
private DynamicExperimentEnrollmentConfiguration experimentEnrollmentConfiguration;
private DynamicPreRegistrationExperimentEnrollmentConfiguration preRegistrationExperimentEnrollmentConfiguration;
private ExperimentEnrollmentManager experimentEnrollmentManager;
private Account account;
private static final UUID ACCOUNT_UUID = UUID.randomUUID();
private static final String EXPERIMENT_NAME = "test";
private static final String UUID_EXPERIMENT_NAME = "uuid_test";
private static final String E164 = "+12025551212";
private static final String E164_EXPERIMENT_NAME = "e164_test";
@BeforeEach
void setUp() {
final DynamicConfigurationManager dynamicConfigurationManager = mock(DynamicConfigurationManager.class);
final DynamicConfiguration dynamicConfiguration = mock(DynamicConfiguration.class);
experimentEnrollmentConfiguration = mock(DynamicExperimentEnrollmentConfiguration.class);
experimentEnrollmentManager = new ExperimentEnrollmentManager(dynamicConfigurationManager);
account = mock(Account.class);
experimentEnrollmentConfiguration = mock(DynamicExperimentEnrollmentConfiguration.class);
preRegistrationExperimentEnrollmentConfiguration = mock(
DynamicPreRegistrationExperimentEnrollmentConfiguration.class);
when(dynamicConfigurationManager.getConfiguration()).thenReturn(dynamicConfiguration);
when(dynamicConfiguration.getExperimentEnrollmentConfiguration(EXPERIMENT_NAME))
when(dynamicConfiguration.getExperimentEnrollmentConfiguration(UUID_EXPERIMENT_NAME))
.thenReturn(Optional.of(experimentEnrollmentConfiguration));
when(dynamicConfiguration.getPreRegistrationEnrollmentConfiguration(E164_EXPERIMENT_NAME))
.thenReturn(Optional.of(preRegistrationExperimentEnrollmentConfiguration));
account = mock(Account.class);
when(account.getUuid()).thenReturn(ACCOUNT_UUID);
}
@Test
void testIsEnrolled() {
assertFalse(experimentEnrollmentManager.isEnrolled(account.getUuid(), EXPERIMENT_NAME));
assertFalse(experimentEnrollmentManager.isEnrolled(account.getUuid(), EXPERIMENT_NAME + "-unrelated-experiment"));
void testIsEnrolled_UuidExperiment() {
assertFalse(experimentEnrollmentManager.isEnrolled(account.getUuid(), UUID_EXPERIMENT_NAME));
assertFalse(
experimentEnrollmentManager.isEnrolled(account.getUuid(), UUID_EXPERIMENT_NAME + "-unrelated-experiment"));
when(experimentEnrollmentConfiguration.getEnrolledUuids()).thenReturn(Set.of(ACCOUNT_UUID));
assertTrue(experimentEnrollmentManager.isEnrolled(account.getUuid(), EXPERIMENT_NAME));
assertTrue(experimentEnrollmentManager.isEnrolled(account.getUuid(), UUID_EXPERIMENT_NAME));
when(experimentEnrollmentConfiguration.getEnrolledUuids()).thenReturn(Collections.emptySet());
when(experimentEnrollmentConfiguration.getEnrollmentPercentage()).thenReturn(0);
assertFalse(experimentEnrollmentManager.isEnrolled(account.getUuid(), EXPERIMENT_NAME));
assertFalse(experimentEnrollmentManager.isEnrolled(account.getUuid(), UUID_EXPERIMENT_NAME));
when(experimentEnrollmentConfiguration.getEnrollmentPercentage()).thenReturn(100);
assertTrue(experimentEnrollmentManager.isEnrolled(account.getUuid(), EXPERIMENT_NAME));
assertTrue(experimentEnrollmentManager.isEnrolled(account.getUuid(), UUID_EXPERIMENT_NAME));
}
@ParameterizedTest
@MethodSource
void testIsEnrolled_PreRegistrationExperiment(final String e164, final String experimentName,
final Set<String> enrolledE164s, final Set<String> includedCountryCodes, final Set<String> excludedCountryCodes,
final int enrollmentPercentage,
final boolean expectedEnrolled, final String message) {
when(preRegistrationExperimentEnrollmentConfiguration.getEnrolledE164s()).thenReturn(enrolledE164s);
when(preRegistrationExperimentEnrollmentConfiguration.getEnrollmentPercentage()).thenReturn(enrollmentPercentage);
when(preRegistrationExperimentEnrollmentConfiguration.getIncludedCountryCodes()).thenReturn(includedCountryCodes);
when(preRegistrationExperimentEnrollmentConfiguration.getExcludedCountryCodes()).thenReturn(excludedCountryCodes);
assertEquals(message, expectedEnrolled, experimentEnrollmentManager.isEnrolled(e164, experimentName));
}
static Stream<Arguments> testIsEnrolled_PreRegistrationExperiment() {
return Stream.of(
Arguments.of(E164, E164_EXPERIMENT_NAME, Collections.emptySet(), Collections.emptySet(),
Collections.emptySet(), 0, false, "default configuration expects no enrollment"),
Arguments
.of(E164, E164_EXPERIMENT_NAME + "-unrelated-experiment", Collections.emptySet(), Collections.emptySet(),
Collections.emptySet(), 0, false, "unknown experiment expects no enrollment"),
Arguments.of(E164, E164_EXPERIMENT_NAME, Set.of(E164), Collections.emptySet(),
Collections.emptySet(), 0, true, "explicitly enrolled E164 overrides 0% rollout"),
Arguments.of(E164, E164_EXPERIMENT_NAME, Set.of(E164), Collections.emptySet(),
Set.of("1"), 0, true, "explicitly enrolled E164 overrides excluded country code"),
Arguments.of(E164, E164_EXPERIMENT_NAME, Collections.emptySet(), Set.of("1"),
Collections.emptySet(), 0, true, "included country code overrides 0% rollout"),
Arguments.of(E164, E164_EXPERIMENT_NAME, Collections.emptySet(), Collections.emptySet(),
Set.of("1"), 100, false, "excluded country code overrides 100% rollout"),
Arguments.of(E164, E164_EXPERIMENT_NAME, Collections.emptySet(), Collections.emptySet(),
Collections.emptySet(), 100, true, "enrollment expected for 100% rollout")
);
}
}