Skip to content

Commit 4b97eff

Browse files
Test fixes
1 parent ce25798 commit 4b97eff

File tree

3 files changed

+162
-147
lines changed

3 files changed

+162
-147
lines changed

sdk/cosmos/azure-cosmos-encryption/src/test/java/com/azure/cosmos/encryption/CosmosNettyLeakDetectorFactory.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import com.azure.cosmos.implementation.Configs;
66
import com.azure.cosmos.implementation.RxDocumentClientImpl;
7-
import com.azure.cosmos.implementation.StackTraceUtil;
87
import io.netty.buffer.PooledByteBufAllocator;
98
import io.netty.util.ResourceLeakDetector;
109
import io.netty.util.ResourceLeakDetectorFactory;
@@ -16,12 +15,14 @@
1615
import org.testng.IInvokedMethod;
1716
import org.testng.IInvokedMethodListener;
1817
import org.testng.ITestClass;
18+
import org.testng.ITestContext;
1919
import org.testng.ITestNGMethod;
2020
import org.testng.ITestResult;
2121

2222
import java.lang.management.BufferPoolMXBean;
2323
import java.lang.management.ManagementFactory;
2424
import java.util.ArrayList;
25+
import java.util.Arrays;
2526
import java.util.HashMap;
2627
import java.util.List;
2728
import java.util.Map;
@@ -84,26 +85,28 @@ public void onAfterClass(ITestClass testClass) {
8485
// -Dtestng.listener.execution.symmetric=true allows, but this is only available
8586
// in TestNG 7.7.1 - which requires Java11
8687
// So, this class simulates this behavior by hooking into IInvokedMethodListener
87-
// If the test class itself does not have @afterClass we execute the logic here
88-
89-
ITestNGMethod[] afterClassMethods = testClass.getAfterClassMethods();
90-
boolean testClassHasAfterClassMethods = afterClassMethods != null && afterClassMethods.length > 0;
91-
if (!testClassHasAfterClassMethods) {
92-
this.onAfterClassCore(testClass);
93-
}
9488
}
9589

9690
@Override
97-
public void afterInvocation(IInvokedMethod method, ITestResult result) {
91+
public void afterInvocation(IInvokedMethod method, ITestResult result, ITestContext ctx) {
9892
ITestClass testClass = (ITestClass)result.getTestClass();
9993
ITestNGMethod[] afterClassMethods = testClass.getAfterClassMethods();
10094
boolean testClassHasAfterClassMethods = afterClassMethods != null && afterClassMethods.length > 0;
10195

102-
if (testClassHasAfterClassMethods
96+
boolean isImplementedAfterClassMethod = testClassHasAfterClassMethods
10397
&& method.isConfigurationMethod()
104-
&& method.getTestMethod().isAfterClassConfiguration()) {
98+
&& method.getTestMethod().isAfterClassConfiguration();
99+
100+
ITestNGMethod[] testMethods = ctx.getAllTestMethods();
101+
102+
boolean isLastTestMethodOnTestClassWithoutAfterClassMethod = !testClassHasAfterClassMethods
103+
&& method.isTestMethod()
104+
&& method.getTestMethod().isTest()
105+
&& method.getTestMethod().getEnabled()
106+
&& testMethods.length > 0
107+
&& method.getTestMethod() == testMethods[testMethods.length - 1];
105108

106-
// <-- This point is guaranteed to be AFTER the class’s @AfterClass ran if any existed
109+
if (isImplementedAfterClassMethod || isLastTestMethodOnTestClassWithoutAfterClassMethod) {
107110
this.onAfterClassCore(testClass);
108111
}
109112
}

sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/CosmosClientBuilderTest.java

Lines changed: 132 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,11 @@ public void validateBadPreferredRegions1() {
6161

6262
@Test(groups = "unit")
6363
public void validateBadPreferredRegions2() {
64-
try {
65-
CosmosAsyncClient client = new CosmosClientBuilder()
64+
try (CosmosAsyncClient client = new CosmosClientBuilder()
6665
.key(TestConfigurations.MASTER_KEY)
6766
.endpoint(hostName)
6867
.preferredRegions(Arrays.asList(" "))
69-
.buildAsyncClient();
68+
.buildAsyncClient()) {
7069
client.close();
7170
} catch (Exception e) {
7271
assertThat(e).isInstanceOf(RuntimeException.class);
@@ -92,9 +91,11 @@ public void validateApiTypePresent() {
9291
ImplementationBridgeHelpers.CosmosClientBuilderHelper.getCosmosClientBuilderAccessor();
9392
accessor.setCosmosClientApiType(cosmosClientBuilder, apiType);
9493

95-
RxDocumentClientImpl documentClient =
96-
(RxDocumentClientImpl) ReflectionUtils.getAsyncDocumentClient(cosmosClientBuilder.buildAsyncClient());
97-
assertThat(ReflectionUtils.getApiType(documentClient)).isEqualTo(apiType);
94+
try (CosmosAsyncClient cosmosClient = cosmosClientBuilder.buildAsyncClient()) {
95+
RxDocumentClientImpl documentClient =
96+
(RxDocumentClientImpl) ReflectionUtils.getAsyncDocumentClient(cosmosClient);
97+
assertThat(ReflectionUtils.getApiType(documentClient)).isEqualTo(apiType);
98+
}
9899
}
99100

100101
@Test(groups = "emulator", dataProvider = "regionScopedSessionContainerConfigs")
@@ -111,23 +112,24 @@ public void validateSessionTokenCapturingForAccountDefaultConsistency(boolean sh
111112
.key(TestConfigurations.MASTER_KEY)
112113
.userAgentSuffix("custom-direct-client");
113114

114-
CosmosAsyncClient client = cosmosClientBuilder.buildAsyncClient();
115-
RxDocumentClientImpl documentClient =
116-
(RxDocumentClientImpl) ReflectionUtils.getAsyncDocumentClient(client);
115+
try (CosmosAsyncClient client = cosmosClientBuilder.buildAsyncClient()) {
116+
RxDocumentClientImpl documentClient =
117+
(RxDocumentClientImpl) ReflectionUtils.getAsyncDocumentClient(client);
117118

118-
if (documentClient.getDefaultConsistencyLevelOfAccount() != ConsistencyLevel.SESSION) {
119-
throw new SkipException("This test is only applicable when default account-level consistency is Session.");
120-
}
119+
if (documentClient.getDefaultConsistencyLevelOfAccount() != ConsistencyLevel.SESSION) {
120+
throw new SkipException("This test is only applicable when default account-level consistency is Session.");
121+
}
121122

122-
ISessionContainer sessionContainer = documentClient.getSession();
123+
ISessionContainer sessionContainer = documentClient.getSession();
123124

124-
if (System.getProperty("COSMOS.SESSION_CAPTURING_TYPE") != null && System.getProperty("COSMOS.SESSION_CAPTURING_TYPE").equals("REGION_SCOPED")) {
125-
assertThat(sessionContainer instanceof RegionScopedSessionContainer).isTrue();
126-
} else {
127-
assertThat(sessionContainer instanceof SessionContainer).isTrue();
128-
}
125+
if (System.getProperty("COSMOS.SESSION_CAPTURING_TYPE") != null && System.getProperty("COSMOS.SESSION_CAPTURING_TYPE").equals("REGION_SCOPED")) {
126+
assertThat(sessionContainer instanceof RegionScopedSessionContainer).isTrue();
127+
} else {
128+
assertThat(sessionContainer instanceof SessionContainer).isTrue();
129+
}
129130

130-
assertThat(sessionContainer.getDisableSessionCapturing()).isEqualTo(false);
131+
assertThat(sessionContainer.getDisableSessionCapturing()).isEqualTo(false);
132+
}
131133
} finally {
132134
System.clearProperty("COSMOS.SESSION_CAPTURING_TYPE");
133135
}
@@ -144,126 +146,133 @@ public void validateSessionTokenCapturingForAccountDefaultConsistencyWithEnvVari
144146
.key(TestConfigurations.MASTER_KEY)
145147
.userAgentSuffix("custom-direct-client");
146148

147-
CosmosAsyncClient client = cosmosClientBuilder.buildAsyncClient();
148-
RxDocumentClientImpl documentClient =
149-
(RxDocumentClientImpl) ReflectionUtils.getAsyncDocumentClient(client);
149+
try (CosmosAsyncClient client = cosmosClientBuilder.buildAsyncClient()) {
150+
RxDocumentClientImpl documentClient =
151+
(RxDocumentClientImpl) ReflectionUtils.getAsyncDocumentClient(client);
150152

151-
if (documentClient.getDefaultConsistencyLevelOfAccount() != ConsistencyLevel.SESSION) {
152-
throw new SkipException("This test is only applicable when default account-level consistency is Session.");
153-
}
153+
if (documentClient.getDefaultConsistencyLevelOfAccount() != ConsistencyLevel.SESSION) {
154+
throw new SkipException("This test is only applicable when default account-level consistency is Session.");
155+
}
154156

155-
ISessionContainer sessionContainer = documentClient.getSession();
157+
ISessionContainer sessionContainer = documentClient.getSession();
156158

157-
if (System.getenv("COSMOS.SESSION_CAPTURING_TYPE") != null && System.getenv("COSMOS.SESSION_CAPTURING_TYPE").equals("REGION_SCOPED")) {
158-
assertThat(sessionContainer instanceof RegionScopedSessionContainer).isTrue();
159-
} else {
160-
assertThat(sessionContainer instanceof SessionContainer).isTrue();
161-
}
159+
if (System.getenv("COSMOS.SESSION_CAPTURING_TYPE") != null && System.getenv("COSMOS.SESSION_CAPTURING_TYPE").equals("REGION_SCOPED")) {
160+
assertThat(sessionContainer instanceof RegionScopedSessionContainer).isTrue();
161+
} else {
162+
assertThat(sessionContainer instanceof SessionContainer).isTrue();
163+
}
162164

163-
assertThat(sessionContainer.getDisableSessionCapturing()).isEqualTo(false);
165+
assertThat(sessionContainer.getDisableSessionCapturing()).isEqualTo(false);
166+
}
164167
} finally {
165168
System.clearProperty("COSMOS.SESSION_CAPTURING_TYPE");
166169
}
167170
}
168171

169172
@Test(groups = "emulator")
170173
public void validateContainerCreationInterceptor() {
171-
CosmosClient clientWithoutInterceptor = new CosmosClientBuilder()
174+
try (CosmosClient clientWithoutInterceptor = new CosmosClientBuilder()
172175
.endpoint(TestConfigurations.HOST)
173176
.key(TestConfigurations.MASTER_KEY)
174177
.userAgentSuffix("noInterceptor")
175-
.buildClient();
176-
177-
ConcurrentMap<CacheKey, List<?>> queryCache = new ConcurrentHashMap<>();
178-
179-
CosmosClient clientWithInterceptor = new CosmosClientBuilder()
180-
.endpoint(TestConfigurations.HOST)
181-
.key(TestConfigurations.MASTER_KEY)
182-
.userAgentSuffix("withInterceptor")
183-
.containerCreationInterceptor(originalContainer -> new CacheAndValidateQueriesContainer(originalContainer, queryCache))
184-
.buildClient();
185-
186-
CosmosAsyncClient asyncClientWithInterceptor = new CosmosClientBuilder()
187-
.endpoint(TestConfigurations.HOST)
188-
.key(TestConfigurations.MASTER_KEY)
189-
.userAgentSuffix("withInterceptor")
190-
.containerCreationInterceptor(originalContainer -> new CacheAndValidateQueriesContainer(originalContainer, queryCache))
191-
.buildAsyncClient();
192-
193-
CosmosContainer normalContainer = clientWithoutInterceptor
194-
.getDatabase("TestDB")
195-
.getContainer("TestContainer");
196-
assertThat(normalContainer).isNotNull();
197-
assertThat(normalContainer.getClass()).isEqualTo(CosmosContainer.class);
198-
assertThat(normalContainer.asyncContainer.getClass()).isEqualTo(CosmosAsyncContainer.class);
199-
200-
CosmosContainer customSyncContainer = clientWithInterceptor
201-
.getDatabase("TestDB")
202-
.getContainer("TestContainer");
203-
assertThat(customSyncContainer).isNotNull();
204-
assertThat(customSyncContainer.getClass()).isEqualTo(CosmosContainer.class);
205-
assertThat(customSyncContainer.asyncContainer.getClass()).isEqualTo(CacheAndValidateQueriesContainer.class);
206-
207-
CosmosAsyncContainer customAsyncContainer = asyncClientWithInterceptor
208-
.getDatabase("TestDB")
209-
.getContainer("TestContainer");
210-
assertThat(customAsyncContainer).isNotNull();
211-
assertThat(customAsyncContainer.getClass()).isEqualTo(CacheAndValidateQueriesContainer.class);
212-
213-
try {
214-
customSyncContainer.queryItems("SELECT * from c", null, ObjectNode.class);
215-
fail("Unparameterized query should throw");
216-
} catch (IllegalStateException expectedError) {}
217-
218-
try {
219-
customAsyncContainer.queryItems("SELECT * from c", null, ObjectNode.class);
220-
fail("Unparameterized query should throw");
221-
} catch (IllegalStateException expectedError) {}
222-
223-
try {
224-
customAsyncContainer.queryItems("SELECT * from c", ObjectNode.class);
225-
fail("Unparameterized query should throw");
226-
} catch (IllegalStateException expectedError) {}
227-
228-
SqlQuerySpec querySpec = new SqlQuerySpec().setQueryText("SELECT * from c");
229-
assertThat(queryCache).size().isEqualTo(0);
230-
231-
try {
232-
List<ObjectNode> items = customSyncContainer
233-
.queryItems(querySpec, null, ObjectNode.class)
234-
.stream().collect(Collectors.toList());
235-
fail("Not yet cached - the query above should always throw");
236-
} catch (CosmosException cosmosException) {
237-
// Container does not exist - when not cached should fail
238-
assertThat(cosmosException.getStatusCode()).isEqualTo(404);
239-
assertThat(cosmosException.getSubStatusCode()).isEqualTo(1003);
240-
}
241-
242-
queryCache.putIfAbsent(new CacheKey(ObjectNode.class.getCanonicalName(), querySpec), new ArrayList<>());
243-
assertThat(queryCache).size().isEqualTo(1);
244-
245-
// Validate that CacheKey equality check works
246-
queryCache.putIfAbsent(new CacheKey(ObjectNode.class.getCanonicalName(), querySpec), new ArrayList<>());
247-
assertThat(queryCache).size().isEqualTo(1);
248-
249-
// Validate that form cache the results can be served
250-
List<ObjectNode> items = customSyncContainer
251-
.queryItems(querySpec, null, ObjectNode.class)
252-
.stream().collect(Collectors.toList());
178+
.buildClient()) {
253179

254-
querySpec = new SqlQuerySpec().setQueryText("SELECT * from c");
255-
CosmosPagedFlux<ObjectNode> cachedPagedFlux = customAsyncContainer
256-
.queryItems(querySpec, null, ObjectNode.class);
257-
assertThat(cachedPagedFlux.getClass().getName()).startsWith("com.azure.cosmos.util.CosmosPagedFluxStaticListImpl");
180+
ConcurrentMap<CacheKey, List<?>> queryCache = new ConcurrentHashMap<>();
258181

259-
// Validate that uncached query form async Container also fails with 404 due to non-existing Container
260-
querySpec = new SqlQuerySpec().setQueryText("SELECT * from r");
261-
try {
262-
CosmosPagedFlux<ObjectNode> uncachedPagedFlux = customAsyncContainer
263-
.queryItems(querySpec, null, ObjectNode.class);
264-
} catch (CosmosException cosmosException) {
265-
assertThat(cosmosException.getStatusCode()).isEqualTo(404);
266-
assertThat(cosmosException.getSubStatusCode()).isEqualTo(1003);
182+
try (CosmosClient clientWithInterceptor = new CosmosClientBuilder()
183+
.endpoint(TestConfigurations.HOST)
184+
.key(TestConfigurations.MASTER_KEY)
185+
.userAgentSuffix("withInterceptor")
186+
.containerCreationInterceptor(originalContainer -> new CacheAndValidateQueriesContainer(originalContainer, queryCache))
187+
.buildClient()) {
188+
189+
try (CosmosAsyncClient asyncClientWithInterceptor = new CosmosClientBuilder()
190+
.endpoint(TestConfigurations.HOST)
191+
.key(TestConfigurations.MASTER_KEY)
192+
.userAgentSuffix("withInterceptor")
193+
.containerCreationInterceptor(originalContainer -> new CacheAndValidateQueriesContainer(originalContainer, queryCache))
194+
.buildAsyncClient()) {
195+
196+
CosmosContainer normalContainer = clientWithoutInterceptor
197+
.getDatabase("TestDB")
198+
.getContainer("TestContainer");
199+
assertThat(normalContainer).isNotNull();
200+
assertThat(normalContainer.getClass()).isEqualTo(CosmosContainer.class);
201+
assertThat(normalContainer.asyncContainer.getClass()).isEqualTo(CosmosAsyncContainer.class);
202+
203+
CosmosContainer customSyncContainer = clientWithInterceptor
204+
.getDatabase("TestDB")
205+
.getContainer("TestContainer");
206+
assertThat(customSyncContainer).isNotNull();
207+
assertThat(customSyncContainer.getClass()).isEqualTo(CosmosContainer.class);
208+
assertThat(customSyncContainer.asyncContainer.getClass()).isEqualTo(CacheAndValidateQueriesContainer.class);
209+
210+
CosmosAsyncContainer customAsyncContainer = asyncClientWithInterceptor
211+
.getDatabase("TestDB")
212+
.getContainer("TestContainer");
213+
assertThat(customAsyncContainer).isNotNull();
214+
assertThat(customAsyncContainer.getClass()).isEqualTo(CacheAndValidateQueriesContainer.class);
215+
216+
try {
217+
customSyncContainer.queryItems("SELECT * from c", null, ObjectNode.class);
218+
fail("Unparameterized query should throw");
219+
} catch (IllegalStateException expectedError) {
220+
}
221+
222+
try {
223+
customAsyncContainer.queryItems("SELECT * from c", null, ObjectNode.class);
224+
fail("Unparameterized query should throw");
225+
} catch (IllegalStateException expectedError) {
226+
}
227+
228+
try {
229+
customAsyncContainer.queryItems("SELECT * from c", ObjectNode.class);
230+
fail("Unparameterized query should throw");
231+
} catch (IllegalStateException expectedError) {
232+
}
233+
234+
SqlQuerySpec querySpec = new SqlQuerySpec().setQueryText("SELECT * from c");
235+
assertThat(queryCache).size().isEqualTo(0);
236+
237+
try {
238+
List<ObjectNode> items = customSyncContainer
239+
.queryItems(querySpec, null, ObjectNode.class)
240+
.stream().collect(Collectors.toList());
241+
fail("Not yet cached - the query above should always throw");
242+
} catch (CosmosException cosmosException) {
243+
// Container does not exist - when not cached should fail
244+
assertThat(cosmosException.getStatusCode()).isEqualTo(404);
245+
assertThat(cosmosException.getSubStatusCode()).isEqualTo(1003);
246+
}
247+
248+
queryCache.putIfAbsent(new CacheKey(ObjectNode.class.getCanonicalName(), querySpec), new ArrayList<>());
249+
assertThat(queryCache).size().isEqualTo(1);
250+
251+
// Validate that CacheKey equality check works
252+
queryCache.putIfAbsent(new CacheKey(ObjectNode.class.getCanonicalName(), querySpec), new ArrayList<>());
253+
assertThat(queryCache).size().isEqualTo(1);
254+
255+
// Validate that form cache the results can be served
256+
List<ObjectNode> items = customSyncContainer
257+
.queryItems(querySpec, null, ObjectNode.class)
258+
.stream().collect(Collectors.toList());
259+
260+
querySpec = new SqlQuerySpec().setQueryText("SELECT * from c");
261+
CosmosPagedFlux<ObjectNode> cachedPagedFlux = customAsyncContainer
262+
.queryItems(querySpec, null, ObjectNode.class);
263+
assertThat(cachedPagedFlux.getClass().getName()).startsWith("com.azure.cosmos.util.CosmosPagedFluxStaticListImpl");
264+
265+
// Validate that uncached query form async Container also fails with 404 due to non-existing Container
266+
querySpec = new SqlQuerySpec().setQueryText("SELECT * from r");
267+
try {
268+
CosmosPagedFlux<ObjectNode> uncachedPagedFlux = customAsyncContainer
269+
.queryItems(querySpec, null, ObjectNode.class);
270+
} catch (CosmosException cosmosException) {
271+
assertThat(cosmosException.getStatusCode()).isEqualTo(404);
272+
assertThat(cosmosException.getSubStatusCode()).isEqualTo(1003);
273+
}
274+
}
275+
}
267276
}
268277
}
269278

0 commit comments

Comments
 (0)