Skip to content

Commit 25d15b6

Browse files
Copilotxinlian12
andcommitted
Migrate ChangeFeedTest to rx.TestSuiteBase and add internal API methods
Co-authored-by: xinlian12 <64233642+xinlian12@users.noreply.github.com>
1 parent 21e019a commit 25d15b6

2 files changed

Lines changed: 95 additions & 4 deletions

File tree

sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/rx/ChangeFeedTest.java

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

55
import com.azure.cosmos.BridgeInternal;
66
import com.azure.cosmos.ConnectionMode;
7+
import com.azure.cosmos.ConsistencyLevel;
78
import com.azure.cosmos.CosmosDiagnostics;
89
import com.azure.cosmos.implementation.AsyncDocumentClient;
910
import com.azure.cosmos.implementation.ClientSideRequestStatistics;
@@ -15,7 +16,6 @@
1516
import com.azure.cosmos.implementation.RequestOptions;
1617
import com.azure.cosmos.implementation.Resource;
1718
import com.azure.cosmos.implementation.ResourceResponse;
18-
import com.azure.cosmos.implementation.TestSuiteBase;
1919
import com.azure.cosmos.implementation.TestUtils;
2020
import com.azure.cosmos.implementation.Utils;
2121
import com.azure.cosmos.implementation.feedranges.FeedRangeEpkImpl;
@@ -58,7 +58,6 @@
5858
import static java.lang.annotation.ElementType.METHOD;
5959
import static org.assertj.core.api.Assertions.assertThat;
6060

61-
//TODO: change to use external TestSuiteBase
6261
public class ChangeFeedTest extends TestSuiteBase {
6362

6463
private static final int SETUP_TIMEOUT = 40000;
@@ -93,7 +92,7 @@ static protected DocumentCollection getCollectionDefinition(boolean enableFullFi
9392
}
9493

9594
public ChangeFeedTest() {
96-
super(createGatewayRxDocumentClient());
95+
super(createInternalGatewayRxDocumentClient(ConsistencyLevel.SESSION, false, null, true));
9796
subscriberValidationTimeout = TIMEOUT;
9897
}
9998

@@ -575,7 +574,7 @@ void populateDocumentsInternal(Method method, boolean enableFullFidelity) {
575574
public void before_ChangeFeedTest() {
576575
// set up the client
577576
client = clientBuilder().build();
578-
createdDatabase = SHARED_DATABASE;
577+
createdDatabase = SHARED_DATABASE_FOR_INTERNAL_CLIENT;
579578
}
580579

581580
@AfterClass(groups = { "query", "emulator" }, timeOut = SHUTDOWN_TIMEOUT, alwaysRun = true)

sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/rx/TestSuiteBase.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2080,4 +2080,96 @@ protected static void truncateCollection(DocumentCollection collection) {
20802080
}
20812081
}
20822082

2083+
/**
2084+
* Deletes a document using AsyncDocumentClient.
2085+
* @param client the AsyncDocumentClient
2086+
* @param documentLink the document link
2087+
* @param partitionKey the partition key
2088+
* @param etag the etag (can be null)
2089+
*/
2090+
public static void deleteDocument(AsyncDocumentClient client, String documentLink, PartitionKey partitionKey, String etag) {
2091+
RequestOptions options = new RequestOptions();
2092+
options.setPartitionKey(partitionKey);
2093+
if (etag != null) {
2094+
options.setIfMatchETag(etag);
2095+
}
2096+
try {
2097+
client.deleteDocument(documentLink, options).block();
2098+
} catch (Exception e) {
2099+
// Ignore ResourceNotFoundException if document was already deleted, or other errors during cleanup
2100+
}
2101+
}
2102+
2103+
/**
2104+
* Bulk inserts documents using AsyncDocumentClient.
2105+
* @param client the AsyncDocumentClient
2106+
* @param collectionLink the collection link
2107+
* @param documents the documents to insert
2108+
* @param <T> the document type
2109+
*/
2110+
public static <T extends Resource> void bulkInsert(AsyncDocumentClient client, String collectionLink, List<T> documents) {
2111+
bulkInsert(client, collectionLink, documents, DEFAULT_BULK_INSERT_CONCURRENCY_LEVEL);
2112+
}
2113+
2114+
/**
2115+
* Bulk inserts documents using AsyncDocumentClient with concurrency level.
2116+
* @param client the AsyncDocumentClient
2117+
* @param collectionLink the collection link
2118+
* @param documents the documents to insert
2119+
* @param concurrencyLevel the concurrency level
2120+
* @param <T> the document type
2121+
*/
2122+
public static <T extends Resource> void bulkInsert(AsyncDocumentClient client, String collectionLink, List<T> documents, int concurrencyLevel) {
2123+
Flux.fromIterable(documents)
2124+
.flatMap(doc -> client.createDocument(collectionLink, doc, new RequestOptions(), false), concurrencyLevel)
2125+
.collectList()
2126+
.block();
2127+
}
2128+
2129+
/**
2130+
* Truncates a DocumentCollection using AsyncDocumentClient.
2131+
* @param client the AsyncDocumentClient
2132+
* @param collection the DocumentCollection
2133+
*/
2134+
protected static void truncateCollection(AsyncDocumentClient client, DocumentCollection collection) {
2135+
logger.info("Truncating DocumentCollection {} ...", collection.getId());
2136+
2137+
String collectionLink = collection.getSelfLink();
2138+
if (collectionLink == null) {
2139+
collectionLink = TestUtils.getCollectionNameLink(
2140+
collection.getAltLink().split("/")[1], // dbs/{dbId}/colls/{collId} -> dbId
2141+
collection.getId());
2142+
}
2143+
2144+
CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
2145+
options.setMaxDegreeOfParallelism(-1);
2146+
2147+
QueryFeedOperationState dummyState = TestUtils.createDummyQueryFeedOperationState(
2148+
ResourceType.Document,
2149+
OperationType.Query,
2150+
options,
2151+
client);
2152+
2153+
try {
2154+
String query = "SELECT * FROM c";
2155+
final String finalCollectionLink = collectionLink;
2156+
List<Document> docs = client.queryDocuments(finalCollectionLink, query, dummyState, Document.class)
2157+
.flatMap(page -> Flux.fromIterable(page.getResults()))
2158+
.collectList()
2159+
.block();
2160+
2161+
if (docs != null) {
2162+
for (Document doc : docs) {
2163+
try {
2164+
client.deleteDocument(doc.getSelfLink(), new RequestOptions()).block();
2165+
} catch (Exception e) {
2166+
// Ignore ResourceNotFoundException if document was already deleted, or other errors during cleanup
2167+
}
2168+
}
2169+
}
2170+
} finally {
2171+
safeClose(dummyState);
2172+
}
2173+
}
2174+
20832175
}

0 commit comments

Comments
 (0)