Skip to content

Commit 1046b1d

Browse files
Cleaning-up dummy QueryFeedRangeState properly
1 parent bca461f commit 1046b1d

17 files changed

+595
-389
lines changed

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

Lines changed: 118 additions & 72 deletions
Large diffs are not rendered by default.

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

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.azure.cosmos.implementation.Configs;
1010
import com.azure.cosmos.models.CosmosContainerResponse;
1111
import com.azure.cosmos.rx.TestSuiteBase;
12+
import org.testng.ITestContext;
1213
import org.testng.annotations.AfterClass;
1314
import org.testng.annotations.BeforeClass;
1415
import org.testng.annotations.Factory;
@@ -42,70 +43,74 @@ public void afterClass() {
4243

4344
@Test(groups = { "fast", "emulator" }, timeOut = TIMEOUT)
4445
public void userAgentSuffixWithoutSpecialCharacter() {
45-
CosmosClient clientWithUserAgentSuffix = getClientBuilder()
46+
try (CosmosClient clientWithUserAgentSuffix = getClientBuilder()
4647
.userAgentSuffix("TestUserAgent")
47-
.buildClient();
48+
.buildClient()) {
4849

49-
CosmosContainerResponse response =
50-
clientWithUserAgentSuffix.getDatabase(this.databaseName).getContainer(this.containerName).read();
50+
CosmosContainerResponse response =
51+
clientWithUserAgentSuffix.getDatabase(this.databaseName).getContainer(this.containerName).read();
5152

52-
assertThat(response).isNotNull();
53-
assertThat(response.getStatusCode()).isEqualTo(200);
54-
assertThat(response.getProperties()).isNotNull();
55-
assertThat(response.getProperties().getId()).isEqualTo(this.containerName);
56-
assertThat(response.getDiagnostics()).isNotNull();
57-
validateUserAgentSuffix(response.getDiagnostics().getUserAgent(), "TestUserAgent");
53+
assertThat(response).isNotNull();
54+
assertThat(response.getStatusCode()).isEqualTo(200);
55+
assertThat(response.getProperties()).isNotNull();
56+
assertThat(response.getProperties().getId()).isEqualTo(this.containerName);
57+
assertThat(response.getDiagnostics()).isNotNull();
58+
validateUserAgentSuffix(response.getDiagnostics().getUserAgent(), "TestUserAgent");
59+
}
5860
}
5961

6062
@Test(groups = { "fast", "emulator" }, timeOut = TIMEOUT)
6163
public void userAgentSuffixWithSpecialCharacter() {
62-
CosmosClient clientWithUserAgentSuffix = getClientBuilder()
64+
try (CosmosClient clientWithUserAgentSuffix = getClientBuilder()
6365
.userAgentSuffix("TéstUserAgent's")
64-
.buildClient();
66+
.buildClient()) {
6567

66-
CosmosContainerResponse response =
67-
clientWithUserAgentSuffix.getDatabase(this.databaseName).getContainer(this.containerName).read();
68+
CosmosContainerResponse response =
69+
clientWithUserAgentSuffix.getDatabase(this.databaseName).getContainer(this.containerName).read();
6870

69-
assertThat(response).isNotNull();
70-
assertThat(response.getStatusCode()).isEqualTo(200);
71-
assertThat(response.getProperties()).isNotNull();
72-
assertThat(response.getProperties().getId()).isEqualTo(this.containerName);
73-
assertThat(response.getDiagnostics()).isNotNull();
74-
validateUserAgentSuffix(response.getDiagnostics().getUserAgent(), "TestUserAgent's");
71+
assertThat(response).isNotNull();
72+
assertThat(response.getStatusCode()).isEqualTo(200);
73+
assertThat(response.getProperties()).isNotNull();
74+
assertThat(response.getProperties().getId()).isEqualTo(this.containerName);
75+
assertThat(response.getDiagnostics()).isNotNull();
76+
validateUserAgentSuffix(response.getDiagnostics().getUserAgent(), "TestUserAgent's");
77+
}
7578
}
7679

7780
@Test(groups = { "fast", "emulator" }, timeOut = TIMEOUT)
7881
public void userAgentSuffixWithUnicodeCharacter() {
79-
CosmosClient clientWithUserAgentSuffix = getClientBuilder()
82+
try (CosmosClient clientWithUserAgentSuffix = getClientBuilder()
8083
.userAgentSuffix("UnicodeChar鱀InUserAgent")
81-
.buildClient();
84+
.buildClient()) {
8285

83-
CosmosContainerResponse response =
84-
clientWithUserAgentSuffix.getDatabase(this.databaseName).getContainer(this.containerName).read();
86+
CosmosContainerResponse response =
87+
clientWithUserAgentSuffix.getDatabase(this.databaseName).getContainer(this.containerName).read();
8588

86-
assertThat(response).isNotNull();
87-
assertThat(response.getStatusCode()).isEqualTo(200);
88-
assertThat(response.getProperties()).isNotNull();
89-
assertThat(response.getProperties().getId()).isEqualTo(this.containerName);
90-
assertThat(response.getDiagnostics()).isNotNull();
91-
validateUserAgentSuffix(response.getDiagnostics().getUserAgent(), "UnicodeChar_InUserAgent");
89+
assertThat(response).isNotNull();
90+
assertThat(response.getStatusCode()).isEqualTo(200);
91+
assertThat(response.getProperties()).isNotNull();
92+
assertThat(response.getProperties().getId()).isEqualTo(this.containerName);
93+
assertThat(response.getDiagnostics()).isNotNull();
94+
validateUserAgentSuffix(response.getDiagnostics().getUserAgent(), "UnicodeChar_InUserAgent");
95+
}
9296
}
9397

9498
@Test(groups = { "fast", "emulator" }, timeOut = TIMEOUT)
9599
public void userAgentSuffixWithWhitespaceAndAsciiSpecialChars() {
96-
CosmosClient clientWithUserAgentSuffix = getClientBuilder()
100+
try (CosmosClient clientWithUserAgentSuffix = getClientBuilder()
97101
.userAgentSuffix("UserAgent with space$%_^()*&")
98-
.buildClient();
102+
.buildClient()) {
99103

100-
CosmosContainerResponse response =
101-
clientWithUserAgentSuffix.getDatabase(this.databaseName).getContainer(this.containerName).read();
104+
CosmosContainerResponse response =
105+
clientWithUserAgentSuffix.getDatabase(this.databaseName).getContainer(this.containerName).read();
102106

103-
assertThat(response).isNotNull();
104-
assertThat(response.getStatusCode()).isEqualTo(200);
105-
assertThat(response.getProperties()).isNotNull();
106-
assertThat(response.getProperties().getId()).isEqualTo(this.containerName);
107-
assertThat(response.getDiagnostics()).isNotNull();
108-
validateUserAgentSuffix(response.getDiagnostics().getUserAgent(), "UserAgent with space$%_^()*&");
107+
assertThat(response).isNotNull();
108+
assertThat(response.getStatusCode()).isEqualTo(200);
109+
assertThat(response.getProperties()).isNotNull();
110+
assertThat(response.getProperties().getId()).isEqualTo(this.containerName);
111+
assertThat(response.getDiagnostics()).isNotNull();
112+
validateUserAgentSuffix(response.getDiagnostics().getUserAgent(), "UserAgent with space$%_^()*&");
113+
}
109114
}
110115

111116
private void validateUserAgentSuffix(String actualUserAgent, String expectedUserAgentSuffix) {

sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/ConsistencyTests2.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ public void validateNoChargeOnFailedSessionRead() throws Exception {
233233
new CosmosClientTelemetryConfig()
234234
.sendClientTelemetryToService(ClientTelemetry.DEFAULT_CLIENT_TELEMETRY_ENABLED))
235235
.build();
236+
QueryFeedOperationState dummyState = null;
236237
try {
237238
// CREATE collection
238239
DocumentCollection parentResource = writeClient.createCollection(createdDatabase.getSelfLink(),
@@ -250,7 +251,7 @@ public void validateNoChargeOnFailedSessionRead() throws Exception {
250251
cosmosQueryRequestOptions.setPartitionKey(new PartitionKey(PartitionKeyInternal.Empty.toJson()));
251252
cosmosQueryRequestOptions.setSessionToken(token);
252253

253-
QueryFeedOperationState dummyState = TestUtils.createDummyQueryFeedOperationState(
254+
dummyState = TestUtils.createDummyQueryFeedOperationState(
254255
ResourceType.Document,
255256
OperationType.ReadFeed,
256257
cosmosQueryRequestOptions,
@@ -262,6 +263,7 @@ public void validateNoChargeOnFailedSessionRead() throws Exception {
262263
parentResource.getSelfLink(), dummyState, Document.class);
263264
validateQueryFailure(feedObservable, validator);
264265
} finally {
266+
safeClose(dummyState);
265267
safeClose(writeClient);
266268
safeClose(readSecondaryClient);
267269
}

sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/DocumentQuerySpyWireContentTest.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,22 +95,28 @@ public void queryWithContinuationTokenLimit(CosmosQueryRequestOptions options, S
9595

9696
client.clearCapturedRequests();
9797

98-
Flux<FeedResponse<Document>> queryObservable = client
98+
QueryFeedOperationState dummyState = TestUtils
99+
.createDummyQueryFeedOperationState(ResourceType.Document, OperationType.Query, options, client);
100+
try {
101+
Flux<FeedResponse<Document>> queryObservable = client
99102
.queryDocuments(
100103
collectionLink,
101104
query,
102-
TestUtils.createDummyQueryFeedOperationState(ResourceType.Document, OperationType.Query, options, client),
105+
dummyState,
103106
Document.class);
104107

105-
List<Document> results = queryObservable.flatMap(p -> Flux.fromIterable(p.getResults()))
106-
.collectList().block();
108+
List<Document> results = queryObservable.flatMap(p -> Flux.fromIterable(p.getResults()))
109+
.collectList().block();
107110

108-
assertThat(results.size()).describedAs("total results").isGreaterThanOrEqualTo(1);
111+
assertThat(results.size()).describedAs("total results").isGreaterThanOrEqualTo(1);
109112

110-
List<HttpRequest> requests = client.getCapturedRequests();
113+
List<HttpRequest> requests = client.getCapturedRequests();
111114

112-
for(HttpRequest req: requests) {
113-
validateRequestHasContinuationTokenLimit(req, options.getResponseContinuationTokenLimitInKb());
115+
for (HttpRequest req : requests) {
116+
validateRequestHasContinuationTokenLimit(req, options.getResponseContinuationTokenLimitInKb());
117+
}
118+
} finally {
119+
safeClose(dummyState);
114120
}
115121
}
116122

@@ -171,14 +177,18 @@ public void before_DocumentQuerySpyWireContentTest() throws Exception {
171177
options,
172178
client
173179
);
180+
try {
174181

175-
// do the query once to ensure the collection is cached.
176-
client.queryDocuments(getMultiPartitionCollectionLink(), "select * from root", state, Document.class)
177-
.then().block();
182+
// do the query once to ensure the collection is cached.
183+
client.queryDocuments(getMultiPartitionCollectionLink(), "select * from root", state, Document.class)
184+
.then().block();
178185

179-
// do the query once to ensure the collection is cached.
180-
client.queryDocuments(getSinglePartitionCollectionLink(), "select * from root", state, Document.class)
181-
.then().block();
186+
// do the query once to ensure the collection is cached.
187+
client.queryDocuments(getSinglePartitionCollectionLink(), "select * from root", state, Document.class)
188+
.then().block();
189+
} finally {
190+
safeClose(state);
191+
}
182192
}
183193

184194
@AfterClass(groups = { "fast" }, timeOut = SHUTDOWN_TIMEOUT, alwaysRun = true)

sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/RequestHeadersSpyWireTest.java

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,21 @@ public void queryWithMaxIntegratedCacheStaleness(CosmosQueryRequestOptions optio
137137

138138
client.clearCapturedRequests();
139139

140-
client.queryDocuments(
141-
collectionLink,
142-
query,
143-
TestUtils.createDummyQueryFeedOperationState(ResourceType.Document, OperationType.Query, options, client),
144-
Document.class).blockLast();
145-
146-
List<HttpRequest> requests = client.getCapturedRequests();
147-
for (HttpRequest httpRequest : requests) {
148-
validateRequestHasDedicatedGatewayHeaders(httpRequest, options.getDedicatedGatewayRequestOptions());
140+
QueryFeedOperationState dummyState = TestUtils
141+
.createDummyQueryFeedOperationState(ResourceType.Document, OperationType.Query, options, client);
142+
try {
143+
client.queryDocuments(
144+
collectionLink,
145+
query,
146+
dummyState,
147+
Document.class).blockLast();
148+
149+
List<HttpRequest> requests = client.getCapturedRequests();
150+
for (HttpRequest httpRequest : requests) {
151+
validateRequestHasDedicatedGatewayHeaders(httpRequest, options.getDedicatedGatewayRequestOptions());
152+
}
153+
} finally {
154+
safeClose(dummyState);
149155
}
150156
}
151157

@@ -168,11 +174,16 @@ public void queryWithMaxIntegratedCacheStalenessInNanoseconds() {
168174
client
169175
);
170176

171-
assertThatThrownBy(() -> client
172-
.queryDocuments(collectionLink, query, state, Document.class)
173-
.blockLast())
174-
.isInstanceOf(IllegalArgumentException.class)
175-
.hasMessage("MaxIntegratedCacheStaleness granularity is milliseconds");
177+
try {
178+
179+
assertThatThrownBy(() -> client
180+
.queryDocuments(collectionLink, query, state, Document.class)
181+
.blockLast())
182+
.isInstanceOf(IllegalArgumentException.class)
183+
.hasMessage("MaxIntegratedCacheStaleness granularity is milliseconds");
184+
} finally {
185+
safeClose(state);
186+
}
176187
}
177188

178189
@Test(groups = { "fast" }, timeOut = TIMEOUT)
@@ -192,13 +203,17 @@ public void queryWithMaxIntegratedCacheStalenessInNegative() {
192203
client
193204
);
194205

195-
client.clearCapturedRequests();
206+
try {
207+
client.clearCapturedRequests();
196208

197-
assertThatThrownBy(() -> client
198-
.queryDocuments(collectionLink, query, state, Document.class)
199-
.blockLast())
200-
.isInstanceOf(IllegalArgumentException.class)
201-
.hasMessage("MaxIntegratedCacheStaleness duration cannot be negative");
209+
assertThatThrownBy(() -> client
210+
.queryDocuments(collectionLink, query, state, Document.class)
211+
.blockLast())
212+
.isInstanceOf(IllegalArgumentException.class)
213+
.hasMessage("MaxIntegratedCacheStaleness duration cannot be negative");
214+
} finally {
215+
safeClose(state);
216+
}
202217
}
203218

204219
@Test(dataProvider = "maxIntegratedCacheStalenessDurationProviderItemOptions", groups = { "fast" }, timeOut =

0 commit comments

Comments
 (0)