Introduce a DynamoDB-backed remote config store

This commit is contained in:
Jon Chambers
2021-11-29 12:44:37 -05:00
committed by Jon Chambers
parent 4eb7dde1c8
commit 23bc11f3b6
10 changed files with 275 additions and 82 deletions

View File

@@ -1,125 +0,0 @@
/*
* Copyright 2013-2020 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.whispersystems.textsecuregcm.tests.storage;
import static org.assertj.core.api.Assertions.assertThat;
import com.codahale.metrics.Timer;
import com.opentable.db.postgres.embedded.LiquibasePreparer;
import com.opentable.db.postgres.junit5.EmbeddedPostgresExtension;
import com.opentable.db.postgres.junit5.PreparedDbExtension;
import java.util.List;
import java.util.Set;
import org.jdbi.v3.core.Jdbi;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.whispersystems.textsecuregcm.configuration.CircuitBreakerConfiguration;
import org.whispersystems.textsecuregcm.storage.FaultTolerantDatabase;
import org.whispersystems.textsecuregcm.storage.RemoteConfig;
import org.whispersystems.textsecuregcm.storage.RemoteConfigs;
import org.whispersystems.textsecuregcm.tests.util.AuthHelper;
public class RemoteConfigsTest {
@RegisterExtension
static PreparedDbExtension ACCOUNTS_POSTGRES_EXTENSION =
EmbeddedPostgresExtension.preparedDatabase(LiquibasePreparer.forClasspathLocation("accountsdb.xml"));
private RemoteConfigs remoteConfigs;
@BeforeEach
void setUp() {
final FaultTolerantDatabase remoteConfigDatabase = new FaultTolerantDatabase("remote_configs-test", Jdbi.create(ACCOUNTS_POSTGRES_EXTENSION.getTestDatabase()), new CircuitBreakerConfiguration());
remoteConfigDatabase.use(jdbi -> jdbi.useHandle(handle ->
handle.createUpdate("DELETE FROM remote_config").execute()));
this.remoteConfigs = new RemoteConfigs(remoteConfigDatabase);
}
@Test
void testStore() {
remoteConfigs.set(new RemoteConfig("android.stickers", 50, Set.of(AuthHelper.VALID_UUID, AuthHelper.VALID_UUID_TWO), "FALSE", "TRUE", null));
remoteConfigs.set(new RemoteConfig("value.sometimes", 25, Set.of(AuthHelper.VALID_UUID_TWO), "default", "custom", null));
List<RemoteConfig> configs = remoteConfigs.getAll();
assertThat(configs).hasSize(2);
assertThat(configs.get(0).getName()).isEqualTo("android.stickers");
assertThat(configs.get(0).getValue()).isEqualTo("TRUE");
assertThat(configs.get(0).getDefaultValue()).isEqualTo("FALSE");
assertThat(configs.get(0).getPercentage()).isEqualTo(50);
assertThat(configs.get(0).getUuids()).hasSize(2);
assertThat(configs.get(0).getUuids()).contains(AuthHelper.VALID_UUID);
assertThat(configs.get(0).getUuids()).contains(AuthHelper.VALID_UUID_TWO);
assertThat(configs.get(0).getUuids()).doesNotContain(AuthHelper.INVALID_UUID);
assertThat(configs.get(1).getName()).isEqualTo("value.sometimes");
assertThat(configs.get(1).getValue()).isEqualTo("custom");
assertThat(configs.get(1).getDefaultValue()).isEqualTo("default");
assertThat(configs.get(1).getPercentage()).isEqualTo(25);
assertThat(configs.get(1).getUuids()).hasSize(1);
assertThat(configs.get(1).getUuids()).contains(AuthHelper.VALID_UUID_TWO);
assertThat(configs.get(1).getUuids()).doesNotContain(AuthHelper.VALID_UUID);
assertThat(configs.get(1).getUuids()).doesNotContain(AuthHelper.INVALID_UUID);
}
@Test
void testUpdate() {
remoteConfigs.set(new RemoteConfig("android.stickers", 50, Set.of(), "FALSE", "TRUE", null));
remoteConfigs.set(new RemoteConfig("value.sometimes", 22, Set.of(), "def", "!", null));
remoteConfigs.set(new RemoteConfig("ios.stickers", 50, Set.of(AuthHelper.DISABLED_UUID), "FALSE", "TRUE", null));
remoteConfigs.set(new RemoteConfig("ios.stickers", 75, Set.of(), "FALSE", "TRUE", null));
remoteConfigs.set(new RemoteConfig("value.sometimes", 77, Set.of(), "hey", "wut", null));
List<RemoteConfig> configs = remoteConfigs.getAll();
assertThat(configs).hasSize(3);
assertThat(configs.get(0).getName()).isEqualTo("android.stickers");
assertThat(configs.get(0).getPercentage()).isEqualTo(50);
assertThat(configs.get(0).getUuids()).isEmpty();
assertThat(configs.get(0).getDefaultValue()).isEqualTo("FALSE");
assertThat(configs.get(0).getValue()).isEqualTo("TRUE");
assertThat(configs.get(1).getName()).isEqualTo("ios.stickers");
assertThat(configs.get(1).getPercentage()).isEqualTo(75);
assertThat(configs.get(1).getUuids()).isEmpty();
assertThat(configs.get(1).getDefaultValue()).isEqualTo("FALSE");
assertThat(configs.get(1).getValue()).isEqualTo("TRUE");
assertThat(configs.get(2).getName()).isEqualTo("value.sometimes");
assertThat(configs.get(2).getPercentage()).isEqualTo(77);
assertThat(configs.get(2).getUuids()).isEmpty();
assertThat(configs.get(2).getDefaultValue()).isEqualTo("hey");
assertThat(configs.get(2).getValue()).isEqualTo("wut");
}
@Test
void testDelete() {
remoteConfigs.set(new RemoteConfig("android.stickers", 50, Set.of(AuthHelper.VALID_UUID), "FALSE", "TRUE", null));
remoteConfigs.set(new RemoteConfig("ios.stickers", 50, Set.of(), "FALSE", "TRUE", null));
remoteConfigs.set(new RemoteConfig("ios.stickers", 75, Set.of(), "FALSE", "TRUE", null));
remoteConfigs.set(new RemoteConfig("value.always", 100, Set.of(), "never", "always", null));
remoteConfigs.delete("android.stickers");
List<RemoteConfig> configs = remoteConfigs.getAll();
assertThat(configs).hasSize(2);
assertThat(configs.get(0).getName()).isEqualTo("ios.stickers");
assertThat(configs.get(0).getPercentage()).isEqualTo(75);
assertThat(configs.get(0).getDefaultValue()).isEqualTo("FALSE");
assertThat(configs.get(0).getValue()).isEqualTo("TRUE");
assertThat(configs.get(1).getName()).isEqualTo("value.always");
assertThat(configs.get(1).getPercentage()).isEqualTo(100);
assertThat(configs.get(1).getValue()).isEqualTo("always");
assertThat(configs.get(1).getDefaultValue()).isEqualTo("never");
}
}