5
5
6
6
package org .whispersystems .textsecuregcm .storage ;
7
7
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 ;
14
10
import java .util .List ;
15
- import java .util .Optional ;
16
- import java .util .concurrent .atomic .AtomicBoolean ;
17
11
import org .junit .jupiter .api .extension .AfterEachCallback ;
18
12
import org .junit .jupiter .api .extension .BeforeEachCallback ;
19
13
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 ;
23
14
import software .amazon .awssdk .services .dynamodb .DynamoDbAsyncClient ;
24
15
import software .amazon .awssdk .services .dynamodb .DynamoDbClient ;
25
16
import software .amazon .awssdk .services .dynamodb .model .AttributeDefinition ;
29
20
import software .amazon .awssdk .services .dynamodb .model .KeyType ;
30
21
import software .amazon .awssdk .services .dynamodb .model .LocalSecondaryIndex ;
31
22
import software .amazon .awssdk .services .dynamodb .model .ProvisionedThroughput ;
32
- import javax .annotation .Nullable ;
33
23
34
24
public class DynamoDbExtension implements BeforeEachCallback , AfterEachCallback {
35
25
36
- private static final String DEFAULT_LIBRARY_PATH = "target/lib" ;
37
-
38
26
public interface TableSchema {
39
27
String tableName ();
40
28
String hashKeyName ();
@@ -58,36 +46,15 @@ record RawSchema(
58
46
.writeCapacityUnits (20L )
59
47
.build ();
60
48
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 ;
67
50
private final List <TableSchema > schemas ;
68
51
private DynamoDbClient dynamoDB2 ;
69
52
private DynamoDbAsyncClient dynamoAsyncDB2 ;
70
53
71
54
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 );
77
55
this .schemas = List .of (schemas );
78
56
}
79
57
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
-
91
58
@ Override
92
59
public void afterEach (ExtensionContext context ) {
93
60
stopServer ();
@@ -98,17 +65,14 @@ public void afterEach(ExtensionContext context) {
98
65
*/
99
66
public void stopServer () {
100
67
try {
101
- server . stop ();
68
+ embedded . shutdown ();
102
69
} catch (Exception e ) {
103
70
throw new RuntimeException (e );
104
71
}
105
72
}
106
73
107
74
@ Override
108
75
public void beforeEach (ExtensionContext context ) throws Exception {
109
-
110
- startServer ();
111
-
112
76
initializeClient ();
113
77
114
78
createTables ();
@@ -143,35 +107,10 @@ private void createTable(TableSchema schema) {
143
107
getDynamoDbClient ().createTable (createTableRequest );
144
108
}
145
109
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
-
156
110
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 ();
175
114
}
176
115
177
116
public DynamoDbClient getDynamoDbClient () {
0 commit comments