Skip to content

Commit fa1e151

Browse files
chore(java): shut down local DDB in test (#2176)
1 parent 6b54985 commit fa1e151

3 files changed

Lines changed: 50 additions & 11 deletions

File tree

DynamoDbEncryption/runtimes/java/src/test/sdkv1/com/amazonaws/services/dynamodbv2/datamodeling/TransformerHolisticIT.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import java.util.stream.Collectors;
5656
import javax.crypto.SecretKey;
5757
import javax.crypto.spec.SecretKeySpec;
58+
import org.testng.annotations.AfterTest;
5859
import org.testng.annotations.BeforeTest;
5960
import org.testng.annotations.DataProvider;
6061
import org.testng.annotations.Test;
@@ -326,6 +327,14 @@ public void setUp() {
326327
createCiphertextTables(client);
327328
}
328329

330+
@AfterTest
331+
public void tearDownDDBLocal() {
332+
if (client != null) {
333+
client.shutdown();
334+
client = null;
335+
}
336+
}
337+
329338
@Test(dataProvider = "getDecryptTestVectors")
330339
public void decryptTestVector(Scenario scenario) throws IOException {
331340
client = DynamoDBEmbedded.create().amazonDynamoDB();

DynamoDbEncryption/runtimes/java/src/test/sdkv1/com/amazonaws/services/dynamodbv2/datamodeling/encryption/providers/CachingMostRecentProviderTests.java

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import java.util.Map;
2727
import javax.crypto.SecretKey;
2828
import javax.crypto.spec.SecretKeySpec;
29+
import org.testng.annotations.AfterClass;
30+
import org.testng.annotations.AfterMethod;
31+
import org.testng.annotations.BeforeClass;
2932
import org.testng.annotations.BeforeMethod;
3033
import org.testng.annotations.Test;
3134

@@ -47,30 +50,48 @@ public class CachingMostRecentProviderTests {
4750
private static final DynamoDBEncryptor ENCRYPTOR =
4851
DynamoDBEncryptor.getInstance(BASE_PROVIDER);
4952

50-
private AmazonDynamoDB client;
51-
private Map<String, Integer> methodCalls;
53+
private static AmazonDynamoDB rawClient;
54+
private static AmazonDynamoDB instrumentedClient;
55+
private static Map<String, Integer> methodCalls;
5256
private ProviderStore store;
5357
private EncryptionContext ctx;
5458

59+
// DDB Local client is created once per class since DynamoDBEmbedded.create() is expensive.
60+
// Table is created/deleted per method to ensure test isolation, as tests assert
61+
// exact DynamoDB call counts and expect a clean table with no leftover data.
62+
@BeforeClass
63+
public static void setupClient() {
64+
methodCalls = new HashMap<String, Integer>();
65+
rawClient = DynamoDBEmbedded.create().amazonDynamoDB();
66+
instrumentedClient =
67+
instrument(rawClient, AmazonDynamoDB.class, methodCalls);
68+
}
69+
70+
@AfterClass
71+
public static void tearDownDDBLocal() {
72+
if (rawClient != null) {
73+
rawClient.shutdown();
74+
rawClient = null;
75+
}
76+
}
77+
5578
@BeforeMethod
5679
public void setup() {
57-
methodCalls = new HashMap<String, Integer>();
58-
client =
59-
instrument(
60-
DynamoDBEmbedded.create().amazonDynamoDB(),
61-
AmazonDynamoDB.class,
62-
methodCalls
63-
);
6480
MetaStore.createTable(
65-
client,
81+
instrumentedClient,
6682
TABLE_NAME,
6783
new ProvisionedThroughput(1L, 1L)
6884
);
69-
store = new MetaStore(client, TABLE_NAME, ENCRYPTOR);
85+
store = new MetaStore(instrumentedClient, TABLE_NAME, ENCRYPTOR);
7086
ctx = new EncryptionContext.Builder().build();
7187
methodCalls.clear();
7288
}
7389

90+
@AfterMethod
91+
public void tearDown() {
92+
rawClient.deleteTable(TABLE_NAME);
93+
}
94+
7495
@Test
7596
public void testConstructors() {
7697
final CachingMostRecentProvider prov = new CachingMostRecentProvider(

DynamoDbEncryption/runtimes/java/src/test/sdkv1/com/amazonaws/services/dynamodbv2/mapper/integration/DynamoDBTestBase.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Collection;
2222
import java.util.HashSet;
2323
import java.util.Set;
24+
import org.testng.annotations.AfterClass;
2425

2526
public class DynamoDBTestBase {
2627

@@ -30,6 +31,14 @@ public static void setUpTestBase() {
3031
dynamo = DynamoDBEmbedded.create().amazonDynamoDB();
3132
}
3233

34+
@AfterClass
35+
public static void tearDownDDBLocal() {
36+
if (dynamo != null) {
37+
dynamo.shutdown();
38+
dynamo = null;
39+
}
40+
}
41+
3342
public static AmazonDynamoDB getClient() {
3443
if (dynamo == null) {
3544
setUpTestBase();

0 commit comments

Comments
 (0)