Skip to content

Commit 30a87f1

Browse files
committed
Fixed HttpLoadGenerator logic
- requestCounter was increased even when nothing else happened in the iteration - countRunning changed to Semaphore Signed-off-by: David Matějček <[email protected]>
1 parent 36703e8 commit 30a87f1

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

appserver/tests/application/src/test/java/org/glassfish/main/test/app/monitoring/AppClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@ private HttpURLConnection openHttpGetConnection(String context) {
8585
try {
8686
HttpURLConnection connection = GlassFishTestEnvironment.openConnection(port, context);
8787
connection.setRequestMethod("GET");
88+
connection.setUseCaches(false);
8889
connection.setConnectTimeout(100);
8990
connection.setReadTimeout(requestTimeout);
91+
connection.connect();
9092
return connection;
9193
} catch (IOException e) {
9294
throw new IllegalStateException("Failed to open connection to " + context, e);

appserver/tests/application/src/test/java/org/glassfish/main/test/app/monitoring/HttpLoadGenerator.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
import java.util.concurrent.ConcurrentLinkedDeque;
2323
import java.util.concurrent.ExecutorService;
2424
import java.util.concurrent.Executors;
25+
import java.util.concurrent.Semaphore;
2526
import java.util.concurrent.TimeUnit;
2627
import java.util.concurrent.atomic.AtomicInteger;
2728

2829
import org.glassfish.main.test.app.monitoring.ThreadPoolMonitoringTest.Action;
2930

3031
import static java.lang.System.Logger.Level.DEBUG;
3132
import static java.lang.System.Logger.Level.INFO;
32-
import static java.lang.System.Logger.Level.TRACE;
3333
import static org.junit.jupiter.api.Assertions.assertTrue;
3434

3535
class HttpLoadGenerator extends Thread implements AutoCloseable {
@@ -66,23 +66,27 @@ class HttpLoadGenerator extends Thread implements AutoCloseable {
6666

6767
@Override
6868
public void run() {
69-
final AtomicInteger countRunning = new AtomicInteger(0);
69+
final Semaphore countRunning = new Semaphore(maxParallel);
7070
while (!isInterrupted()) {
71-
if (requestCounter.getAndIncrement() > maxRequests) {
71+
if (requestCounter.get() == maxRequests) {
7272
LOG.log(INFO, "Already produced {0} requests, stopping the executor.", maxRequests);
7373
executor.shutdown();
7474
return;
7575
}
76-
if (countRunning.get() == maxParallel) {
77-
LOG.log(TRACE, "Waiting...");
78-
Thread.onSpinWait();
79-
continue;
76+
try {
77+
countRunning.acquire();
78+
} catch (InterruptedException e) {
79+
interrupt();
8080
}
81-
LOG.log(DEBUG, () -> "Running: " + countRunning + ". Starting another...");
82-
countRunning.incrementAndGet();
81+
LOG.log(DEBUG,
82+
() -> "Running: " + (maxParallel - countRunning.availablePermits()) + ". Starting another...");
83+
requestCounter.incrementAndGet();
8384
executor.submit(() -> {
84-
action.doAction();
85-
countRunning.decrementAndGet();
85+
try {
86+
action.doAction();
87+
} finally {
88+
countRunning.release();
89+
}
8690
});
8791
}
8892
}

appserver/tests/application/src/test/java/org/glassfish/main/test/app/monitoring/ThreadPoolMonitoringTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,10 @@ void testDualListenerHugeAmountOfFastRequests() throws Exception {
197197

198198
generator1.close();
199199
generatorTest.close();
200-
waitForThreadsBusyCount(HTTP_POOL_1_PORT, 0);
201-
waitForThreadsBusyCount(HTTP_POOL_TEST_PORT, 0);
202200
waitForTaskCountStoppedChanging(HTTP_POOL_1_PORT);
203201
waitForTaskCountStoppedChanging(HTTP_POOL_TEST_PORT);
204-
// FIXME: Even those waits don't ensure that task count will not increase.
205-
// Reproduced on GHA, more probable on Windows machine - task count changed by a number of currentThreadCount.
206-
Thread.sleep(1000L);
202+
waitForThreadsBusyCount(HTTP_POOL_1_PORT, 0);
203+
waitForThreadsBusyCount(HTTP_POOL_TEST_PORT, 0);
207204
final ThreadPoolMetrics metrics1 = getThreadPoolMetrics(HTTP_POOL_1);
208205
final ThreadPoolMetrics metricsTest = getThreadPoolMetrics(HTTP_POOL_TEST);
209206
assertAll(

0 commit comments

Comments
 (0)