Skip to content

Commit 83652e0

Browse files
authored
Fix unitTest task not running the test source set (#2074)
* Fix unitTest task not running the test source set Signed-off-by: Logan Kennedy <kennedylogan22@gmail.com> * Add src/test/java to the java21 source set Signed-off-by: Logan Kennedy <kennedylogan22@gmail.com> * Also add the test source set output to unitTest testClassesDirs Signed-off-by: Logan Kennedy <kennedylogan22@gmail.com> * Drop srcDir in favor of testClassesDirs to avoid duplicate classes Signed-off-by: Logan Kennedy <kennedylogan22@gmail.com> * Fix timing-flaky BulkIngester flush tests Signed-off-by: Logan Kennedy <kennedylogan22@gmail.com> --------- Signed-off-by: Logan Kennedy <kennedylogan22@gmail.com>
1 parent a34b558 commit 83652e0

3 files changed

Lines changed: 23 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
2525
- Add transparent gRPC transport with HybridTransport (bulk over gRPC, REST fallback), translation layer, TLS, basic auth, AWS SigV4, and JWT support ([#2062](https://github.com/opensearch-project/opensearch-java/pull/2062))
2626

2727
### Fixed
28+
- Fix `unitTest` task not running the tests in the `test` source set ([#2074](https://github.com/opensearch-project/opensearch-java/pull/2074))
2829

2930
## [Unreleased 3.x]
3031
### Added

java-client/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ if (runtimeJavaVersion >= JavaVersion.VERSION_21) {
419419
}
420420

421421
tasks.named<Test>("unitTest") {
422-
testClassesDirs += java21.output.classesDirs
422+
testClassesDirs += java21.output.classesDirs + sourceSets.test.get().output.classesDirs
423423
classpath = sourceSets["java21"].runtimeClasspath
424424
}
425425
}

java-client/src/test/java/org/opensearch/client/opensearch/_helpers/bulk/BulkIngesterTest.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,21 @@ public void sizeLimitTest() throws Exception {
223223
assertEquals(5, ingester.requestCount());
224224
}
225225

226+
/**
227+
* Waits for the periodic flusher to have emitted {@code expected} requests. Tests that add operations
228+
* spaced apart in time cannot rely on sleeping longer than the flush interval: under load two operations
229+
* can land in the same flush window, which coalesces them into a single request.
230+
*/
231+
private static void awaitRequestCount(BulkIngester<?> ingester, long expected) throws InterruptedException {
232+
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10);
233+
while (ingester.requestCount() < expected) {
234+
if (System.nanoTime() - deadline > 0) {
235+
fail("Timed out waiting for " + expected + " requests, got " + ingester.requestCount());
236+
}
237+
Thread.sleep(5);
238+
}
239+
}
240+
226241
@Test
227242
public void periodicFlushTest() throws Exception {
228243
TestTransport transport = new TestTransport();
@@ -237,11 +252,11 @@ public void periodicFlushTest() throws Exception {
237252
.maxConcurrentRequests(Integer.MAX_VALUE - 1)
238253
);
239254

240-
// Add an operation every 100 ms to give time
241-
// to the flushing timer to kick in.
255+
// Add an operation at a time, waiting for the flushing timer to kick in
256+
// before adding the next one so that each gets its own request.
242257
for (int i = 0; i < 10; i++) {
243258
ingester.add(operation);
244-
Thread.sleep(100);
259+
awaitRequestCount(ingester, i + 1);
245260
}
246261

247262
ingester.close();
@@ -299,11 +314,11 @@ public void afterBulk(long executionId, BulkRequest request, List<Void> contexts
299314
.listener(listener)
300315
);
301316

302-
// Add an operation every 100 ms to give time
303-
// to the flushing timer to kick in.
317+
// Add an operation at a time, waiting for the flushing timer to kick in
318+
// before adding the next one so that each gets its own request.
304319
for (int i = 0; i < 10; i++) {
305320
ingester.add(operation);
306-
Thread.sleep(100);
321+
awaitRequestCount(ingester, i + 1);
307322
}
308323

309324
ingester.close();

0 commit comments

Comments
 (0)