Skip to content

Commit d6f890c

Browse files
authored
Use AmazonDynamoDBLocal instead of DynamoDBProxyServer
1 parent 59e0137 commit d6f890c

File tree

6 files changed

+31
-141
lines changed

6 files changed

+31
-141
lines changed

pom.xml

+1-28
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
<commons-io.version>2.17.0</commons-io.version>
4444
<dropwizard.version>4.0.10</dropwizard.version>
4545
<dropwizard-metrics-datadog.version>1.1.13</dropwizard-metrics-datadog.version>
46-
<!-- can be updated to latest version with Dropwizard 5 (Jetty 12) -->
46+
<!-- can be updated to latest version with Dropwizard 5 (Jetty 12); will then need to disable telemetry -->
4747
<dynamodblocal.version>2.2.1</dynamodblocal.version>
4848
<google-cloud-libraries.version>26.50.0</google-cloud-libraries.version>
4949
<grpc.version>1.61.1</grpc.version> <!-- should be kept in sync with the value from Google libraries-bom -->
@@ -482,33 +482,6 @@
482482
<groupId>org.apache.maven.plugins</groupId>
483483
<artifactId>maven-dependency-plugin</artifactId>
484484
<version>3.7.1</version>
485-
<executions>
486-
<execution>
487-
<id>copy</id>
488-
<phase>test-compile</phase>
489-
<goals>
490-
<goal>copy-dependencies</goal>
491-
</goals>
492-
<configuration>
493-
<includeScope>test</includeScope>
494-
<includeTypes>so,dll,dylib</includeTypes>
495-
<outputDirectory>${project.build.directory}/lib</outputDirectory>
496-
</configuration>
497-
</execution>
498-
</executions>
499-
</plugin>
500-
501-
<plugin>
502-
<groupId>org.apache.maven.plugins</groupId>
503-
<artifactId>maven-surefire-plugin</artifactId>
504-
<configuration>
505-
<systemProperties>
506-
<property>
507-
<name>sqlite4java.library.path</name>
508-
<value>${project.build.directory}/lib</value>
509-
</property>
510-
</systemProperties>
511-
</configuration>
512485
</plugin>
513486

514487
<plugin>

service/pom.xml

-15
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
<properties>
1414
<firebase-admin.version>9.4.1</firebase-admin.version>
1515
<java-uuid-generator.version>5.1.0</java-uuid-generator.version>
16-
<sqlite4java.version>1.0.392</sqlite4java.version>
1716
<google-androidpublisher.version>v3-rev20241016-2.0.0</google-androidpublisher.version>
1817
<storekit.version>3.2.0</storekit.version>
1918
</properties>
@@ -435,13 +434,6 @@
435434
<scope>test</scope>
436435
</dependency>
437436

438-
<dependency>
439-
<groupId>com.almworks.sqlite4java</groupId>
440-
<artifactId>sqlite4java</artifactId>
441-
<version>${sqlite4java.version}</version>
442-
<scope>test</scope>
443-
</dependency>
444-
445437
<dependency>
446438
<groupId>io.projectreactor</groupId>
447439
<artifactId>reactor-core</artifactId>
@@ -480,13 +472,6 @@
480472
<artifactId>DynamoDBLocal</artifactId>
481473
<scope>test</scope>
482474
</dependency>
483-
<dependency>
484-
<groupId>io.github.ganadist.sqlite4java</groupId>
485-
<artifactId>libsqlite4java-osx-aarch64</artifactId>
486-
<version>${sqlite4java.version}</version>
487-
<type>dylib</type>
488-
<scope>test</scope>
489-
</dependency>
490475

491476
<dependency>
492477
<groupId>com.google.auth</groupId>

service/src/test/java/org/whispersystems/textsecuregcm/LocalWhisperServerService.java

-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ public static void main(String[] args) throws Exception {
2626

2727
System.setProperty("secrets.bundle.filename",
2828
Resources.getResource("config/test-secrets-bundle.yml").getPath());
29-
System.setProperty("sqlite.dir", "service/target/lib");
30-
System.setProperty("aws.region", "local-test-region");
3129

3230
final String config = Optional.ofNullable(System.getenv(SIGNAL_SERVER_CONFIG_ENV_VAR))
3331
.orElse(Resources.getResource("config/test.yml").getPath());

service/src/test/java/org/whispersystems/textsecuregcm/WhisperServerServiceTest.java

+22-26
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ class WhisperServerServiceTest {
4545
static {
4646
System.setProperty("secrets.bundle.filename",
4747
Resources.getResource("config/test-secrets-bundle.yml").getPath());
48-
// needed for AppConfigDataClient initialization
49-
System.setProperty("aws.region", "local-test-region");
5048
}
5149

5250
private static final DropwizardAppExtension<WhisperServerConfiguration> EXTENSION = new DropwizardAppExtension<>(
@@ -57,7 +55,6 @@ class WhisperServerServiceTest {
5755
@AfterAll
5856
static void teardown() {
5957
System.clearProperty("secrets.bundle.filename");
60-
System.clearProperty("aws.region");
6158
}
6259

6360
@BeforeEach
@@ -140,36 +137,35 @@ void dynamoDb() {
140137
final AwsCredentialsProvider awsCredentialsProvider = EXTENSION.getConfiguration().getAwsCredentialsConfiguration()
141138
.build();
142139

143-
try (DynamoDbClient dynamoDbClient = EXTENSION.getConfiguration().getDynamoDbClientConfiguration()
144-
.buildSyncClient(awsCredentialsProvider, new NoopAwsSdkMetricPublisher())) {
140+
final DynamoDbClient dynamoDbClient = EXTENSION.getConfiguration().getDynamoDbClientConfiguration()
141+
.buildSyncClient(awsCredentialsProvider, new NoopAwsSdkMetricPublisher());
145142

146-
final DynamoDbExtension.TableSchema numbers = DynamoDbExtensionSchema.Tables.NUMBERS;
147-
final AttributeValue numberAV = AttributeValues.s("+12125550001");
143+
final DynamoDbExtension.TableSchema numbers = DynamoDbExtensionSchema.Tables.NUMBERS;
144+
final AttributeValue numberAV = AttributeValues.s("+12125550001");
148145

149-
final GetItemResponse notFoundResponse = dynamoDbClient.getItem(GetItemRequest.builder()
150-
.tableName(numbers.tableName())
151-
.key(Map.of(numbers.hashKeyName(), numberAV))
152-
.build());
146+
final GetItemResponse notFoundResponse = dynamoDbClient.getItem(GetItemRequest.builder()
147+
.tableName(numbers.tableName())
148+
.key(Map.of(numbers.hashKeyName(), numberAV))
149+
.build());
153150

154-
assertFalse(notFoundResponse.hasItem());
151+
assertFalse(notFoundResponse.hasItem());
155152

156-
dynamoDbClient.putItem(PutItemRequest.builder()
157-
.tableName(numbers.tableName())
158-
.item(Map.of(numbers.hashKeyName(), numberAV))
159-
.build());
153+
dynamoDbClient.putItem(PutItemRequest.builder()
154+
.tableName(numbers.tableName())
155+
.item(Map.of(numbers.hashKeyName(), numberAV))
156+
.build());
160157

161-
final GetItemResponse foundResponse = dynamoDbClient.getItem(GetItemRequest.builder()
162-
.tableName(numbers.tableName())
163-
.key(Map.of(numbers.hashKeyName(), numberAV))
164-
.build());
158+
final GetItemResponse foundResponse = dynamoDbClient.getItem(GetItemRequest.builder()
159+
.tableName(numbers.tableName())
160+
.key(Map.of(numbers.hashKeyName(), numberAV))
161+
.build());
165162

166-
assertTrue(foundResponse.hasItem());
163+
assertTrue(foundResponse.hasItem());
167164

168-
dynamoDbClient.deleteItem(DeleteItemRequest.builder()
169-
.tableName(numbers.tableName())
170-
.key(Map.of(numbers.hashKeyName(), numberAV))
171-
.build());
172-
}
165+
dynamoDbClient.deleteItem(DeleteItemRequest.builder()
166+
.tableName(numbers.tableName())
167+
.key(Map.of(numbers.hashKeyName(), numberAV))
168+
.build());
173169
}
174170

175171
}

service/src/test/java/org/whispersystems/textsecuregcm/configuration/LocalDynamoDbFactory.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
@JsonTypeName("local")
1717
public class LocalDynamoDbFactory implements DynamoDbClientFactory {
1818

19-
private static final DynamoDbExtension EXTENSION = new DynamoDbExtension(System.getProperty("sqlite.dir"),
20-
DynamoDbExtensionSchema.Tables.values());
19+
private static final DynamoDbExtension EXTENSION = new DynamoDbExtension(DynamoDbExtensionSchema.Tables.values());
2120

2221
static {
2322
try {

service/src/test/java/org/whispersystems/textsecuregcm/storage/DynamoDbExtension.java

+7-68
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,12 @@
55

66
package org.whispersystems.textsecuregcm.storage;
77

8-
import com.almworks.sqlite4java.SQLite;
9-
import com.amazonaws.services.dynamodbv2.local.main.ServerRunner;
10-
import com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer;
11-
import java.net.ServerSocket;
12-
import java.net.URI;
13-
import java.time.Duration;
8+
import com.amazonaws.services.dynamodbv2.local.embedded.DynamoDBEmbedded;
9+
import com.amazonaws.services.dynamodbv2.local.shared.access.AmazonDynamoDBLocal;
1410
import java.util.List;
15-
import java.util.Optional;
16-
import java.util.concurrent.atomic.AtomicBoolean;
1711
import org.junit.jupiter.api.extension.AfterEachCallback;
1812
import org.junit.jupiter.api.extension.BeforeEachCallback;
1913
import org.junit.jupiter.api.extension.ExtensionContext;
20-
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
21-
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
22-
import software.amazon.awssdk.regions.Region;
2314
import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient;
2415
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
2516
import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition;
@@ -29,12 +20,9 @@
2920
import software.amazon.awssdk.services.dynamodb.model.KeyType;
3021
import software.amazon.awssdk.services.dynamodb.model.LocalSecondaryIndex;
3122
import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput;
32-
import javax.annotation.Nullable;
3323

3424
public class DynamoDbExtension implements BeforeEachCallback, AfterEachCallback {
3525

36-
private static final String DEFAULT_LIBRARY_PATH = "target/lib";
37-
3826
public interface TableSchema {
3927
String tableName();
4028
String hashKeyName();
@@ -58,36 +46,15 @@ record RawSchema(
5846
.writeCapacityUnits(20L)
5947
.build();
6048

61-
private static final AtomicBoolean libraryLoaded = new AtomicBoolean();
62-
63-
private DynamoDBProxyServer server;
64-
private int port;
65-
66-
private final String libraryPath;
49+
private AmazonDynamoDBLocal embedded;
6750
private final List<TableSchema> schemas;
6851
private DynamoDbClient dynamoDB2;
6952
private DynamoDbAsyncClient dynamoAsyncDB2;
7053

7154
public DynamoDbExtension(TableSchema... schemas) {
72-
this(DEFAULT_LIBRARY_PATH, schemas);
73-
}
74-
75-
public DynamoDbExtension(@Nullable final String libraryPath, TableSchema... schemas) {
76-
this.libraryPath = Optional.ofNullable(libraryPath).orElse(DEFAULT_LIBRARY_PATH);
7755
this.schemas = List.of(schemas);
7856
}
7957

80-
private void loadLibrary() {
81-
// to avoid noise in the logs from “library already loaded” warnings, we make sure we only set it once
82-
if (libraryLoaded.get()) {
83-
return;
84-
}
85-
if (libraryLoaded.compareAndSet(false, true)) {
86-
// if you see a library failed to load error, you need to run mvn test-compile at least once first
87-
SQLite.setLibraryPath(this.libraryPath);
88-
}
89-
}
90-
9158
@Override
9259
public void afterEach(ExtensionContext context) {
9360
stopServer();
@@ -98,17 +65,14 @@ public void afterEach(ExtensionContext context) {
9865
*/
9966
public void stopServer() {
10067
try {
101-
server.stop();
68+
embedded.shutdown();
10269
} catch (Exception e) {
10370
throw new RuntimeException(e);
10471
}
10572
}
10673

10774
@Override
10875
public void beforeEach(ExtensionContext context) throws Exception {
109-
110-
startServer();
111-
11276
initializeClient();
11377

11478
createTables();
@@ -143,35 +107,10 @@ private void createTable(TableSchema schema) {
143107
getDynamoDbClient().createTable(createTableRequest);
144108
}
145109

146-
private void startServer() throws Exception {
147-
loadLibrary();
148-
try (ServerSocket serverSocket = new ServerSocket(0)) {
149-
port = serverSocket.getLocalPort();
150-
}
151-
server = ServerRunner.createServerFromCommandLineArgs(
152-
new String[]{"-disableTelemetry", "-inMemory", "-port", String.valueOf(port)});
153-
server.start();
154-
}
155-
156110
private void initializeClient() {
157-
dynamoDB2 = DynamoDbClient.builder()
158-
.endpointOverride(URI.create("http://localhost:" + port))
159-
.region(Region.of("local-test-region"))
160-
.credentialsProvider(StaticCredentialsProvider.create(
161-
AwsBasicCredentials.create("accessKey", "secretKey")))
162-
.overrideConfiguration(builder ->
163-
builder.apiCallTimeout(Duration.ofSeconds(1))
164-
.apiCallAttemptTimeout(Duration.ofSeconds(1)))
165-
.build();
166-
dynamoAsyncDB2 = DynamoDbAsyncClient.builder()
167-
.endpointOverride(URI.create("http://localhost:" + port))
168-
.region(Region.of("local-test-region"))
169-
.credentialsProvider(StaticCredentialsProvider.create(
170-
AwsBasicCredentials.create("accessKey", "secretKey")))
171-
.overrideConfiguration(builder ->
172-
builder.apiCallTimeout(Duration.ofSeconds(1))
173-
.apiCallAttemptTimeout(Duration.ofSeconds(1)))
174-
.build();
111+
embedded = DynamoDBEmbedded.create();
112+
dynamoDB2 = embedded.dynamoDbClient();
113+
dynamoAsyncDB2 = embedded.dynamoDbAsyncClient();
175114
}
176115

177116
public DynamoDbClient getDynamoDbClient() {

0 commit comments

Comments
 (0)