|
3 | 3 | package com.azure.cosmos;
|
4 | 4 |
|
5 | 5 | import com.azure.cosmos.implementation.ApiType;
|
| 6 | +import com.azure.cosmos.implementation.ISessionContainer; |
6 | 7 | import com.azure.cosmos.implementation.ImplementationBridgeHelpers;
|
| 8 | +import com.azure.cosmos.implementation.RegionScopedSessionContainer; |
7 | 9 | import com.azure.cosmos.implementation.RxDocumentClientImpl;
|
8 | 10 | import com.azure.cosmos.implementation.SessionContainer;
|
9 | 11 | import com.azure.cosmos.implementation.TestConfigurations;
|
10 | 12 | import com.azure.cosmos.implementation.directconnectivity.ReflectionUtils;
|
11 | 13 | import com.azure.cosmos.models.CosmosClientTelemetryConfig;
|
12 | 14 | import org.testng.SkipException;
|
| 15 | +import org.testng.annotations.DataProvider; |
13 | 16 | import org.testng.annotations.Test;
|
14 | 17 |
|
15 | 18 | import java.net.URISyntaxException;
|
|
20 | 23 | public class CosmosClientBuilderTest {
|
21 | 24 | String hostName = "https://sample-account.documents.azure.com:443/";
|
22 | 25 |
|
| 26 | + @DataProvider(name = "regionScopedSessionContainerConfigs") |
| 27 | + public Object[] regionScopedSessionContainerConfigs() { |
| 28 | + return new Object[] {false, true}; |
| 29 | + } |
| 30 | + |
23 | 31 | @Test(groups = "unit")
|
24 | 32 | public void validateBadPreferredRegions1() {
|
25 | 33 | try {
|
@@ -192,22 +200,72 @@ public void validateApiTypePresent() {
|
192 | 200 | assertThat(ReflectionUtils.getApiType(documentClient)).isEqualTo(apiType);
|
193 | 201 | }
|
194 | 202 |
|
195 |
| - @Test(groups = "emulator") |
196 |
| - public void validateSessionTokenCapturingForAccountDefaultConsistency() { |
197 |
| - CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder() |
198 |
| - .endpoint(TestConfigurations.HOST) |
199 |
| - .key(TestConfigurations.MASTER_KEY) |
200 |
| - .userAgentSuffix("custom-direct-client"); |
| 203 | + @Test(groups = "emulator", dataProvider = "regionScopedSessionContainerConfigs") |
| 204 | + public void validateSessionTokenCapturingForAccountDefaultConsistency(boolean shouldRegionScopedSessionContainerEnabled) { |
201 | 205 |
|
202 |
| - CosmosAsyncClient client = cosmosClientBuilder.buildAsyncClient(); |
203 |
| - RxDocumentClientImpl documentClient = |
204 |
| - (RxDocumentClientImpl) ReflectionUtils.getAsyncDocumentClient(client); |
| 206 | + try { |
| 207 | + |
| 208 | + if (shouldRegionScopedSessionContainerEnabled) { |
| 209 | + System.setProperty("COSMOS.SESSION_CAPTURING_TYPE", "REGION_SCOPED"); |
| 210 | + } |
| 211 | + |
| 212 | + CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder() |
| 213 | + .endpoint(TestConfigurations.HOST) |
| 214 | + .key(TestConfigurations.MASTER_KEY) |
| 215 | + .userAgentSuffix("custom-direct-client"); |
| 216 | + |
| 217 | + CosmosAsyncClient client = cosmosClientBuilder.buildAsyncClient(); |
| 218 | + RxDocumentClientImpl documentClient = |
| 219 | + (RxDocumentClientImpl) ReflectionUtils.getAsyncDocumentClient(client); |
| 220 | + |
| 221 | + if (documentClient.getDefaultConsistencyLevelOfAccount() != ConsistencyLevel.SESSION) { |
| 222 | + throw new SkipException("This test is only applicable when default account-level consistency is Session."); |
| 223 | + } |
205 | 224 |
|
206 |
| - if (documentClient.getDefaultConsistencyLevelOfAccount() != ConsistencyLevel.SESSION) { |
207 |
| - throw new SkipException("This test is only applicable when default account-level consistency is Session."); |
| 225 | + ISessionContainer sessionContainer = documentClient.getSession(); |
| 226 | + |
| 227 | + if (System.getProperty("COSMOS.SESSION_CAPTURING_TYPE") != null && System.getProperty("COSMOS.SESSION_CAPTURING_TYPE").equals("REGION_SCOPED")) { |
| 228 | + assertThat(sessionContainer instanceof RegionScopedSessionContainer).isTrue(); |
| 229 | + } else { |
| 230 | + assertThat(sessionContainer instanceof SessionContainer).isTrue(); |
| 231 | + } |
| 232 | + |
| 233 | + assertThat(sessionContainer.getDisableSessionCapturing()).isEqualTo(false); |
| 234 | + } finally { |
| 235 | + System.clearProperty("COSMOS.SESSION_CAPTURING_TYPE"); |
208 | 236 | }
|
| 237 | + } |
| 238 | + |
| 239 | + // set env variable to COSMOS.SESSION_CAPTURING_TYPE to REGION_SCOPED to test all possible assertions |
| 240 | + @Test(groups = "unit", enabled = false) |
| 241 | + public void validateSessionTokenCapturingForAccountDefaultConsistencyWithEnvVariable() { |
209 | 242 |
|
210 |
| - SessionContainer sessionContainer = (SessionContainer)documentClient.getSession(); |
211 |
| - assertThat(sessionContainer.getDisableSessionCapturing()).isEqualTo(false); |
| 243 | + try { |
| 244 | + |
| 245 | + CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder() |
| 246 | + .endpoint(TestConfigurations.HOST) |
| 247 | + .key(TestConfigurations.MASTER_KEY) |
| 248 | + .userAgentSuffix("custom-direct-client"); |
| 249 | + |
| 250 | + CosmosAsyncClient client = cosmosClientBuilder.buildAsyncClient(); |
| 251 | + RxDocumentClientImpl documentClient = |
| 252 | + (RxDocumentClientImpl) ReflectionUtils.getAsyncDocumentClient(client); |
| 253 | + |
| 254 | + if (documentClient.getDefaultConsistencyLevelOfAccount() != ConsistencyLevel.SESSION) { |
| 255 | + throw new SkipException("This test is only applicable when default account-level consistency is Session."); |
| 256 | + } |
| 257 | + |
| 258 | + ISessionContainer sessionContainer = documentClient.getSession(); |
| 259 | + |
| 260 | + if (System.getenv("COSMOS.SESSION_CAPTURING_TYPE") != null && System.getenv("COSMOS.SESSION_CAPTURING_TYPE").equals("REGION_SCOPED")) { |
| 261 | + assertThat(sessionContainer instanceof RegionScopedSessionContainer).isTrue(); |
| 262 | + } else { |
| 263 | + assertThat(sessionContainer instanceof SessionContainer).isTrue(); |
| 264 | + } |
| 265 | + |
| 266 | + assertThat(sessionContainer.getDisableSessionCapturing()).isEqualTo(false); |
| 267 | + } finally { |
| 268 | + System.clearProperty("COSMOS.SESSION_CAPTURING_TYPE"); |
| 269 | + } |
212 | 270 | }
|
213 | 271 | }
|
0 commit comments