privatestaticfinalint TASK_COUNT = 10000; privatestaticfinalint THREAD_COUNT = 20; /* Each test may need several retries before a concurrent failure is seen. In the past, for a *knownbug,between5and10retriesweresufficient.UseRETRIEStoconfigurehowmany *iterationstoretryforeachtestscenario.However,toavoidthetestrunningfortoolong, *forexamplewithgcstress,setacapdurationinMAX_RETRIES_DURATION.Withthisatleastone
* iteration would run, but there could be fewer retries if each of them takes too long. */ privatestaticfinalint RETRIES = 50; // b/235431387: timeout reduced from 1 minute privatestaticfinal Duration MAX_RETRIES_DURATION = Duration.ofSeconds(15);
privatestaticvoid testConcurrentProcessing(RunnerFactory factory, String testName) throws Throwable { final Duration startTs = Duration.ofNanos(System.nanoTime()); final Duration endTs = startTs.plus(MAX_RETRIES_DURATION); for (int i = 0; i < RETRIES; ++i) {
concurrentProcessingTestIteration(factory, i, testName);
Duration now = Duration.ofNanos(System.nanoTime()); if (0 < now.compareTo(endTs)) { break;
}
}
}
privatestaticvoid concurrentProcessingTestIteration(
RunnerFactory factory, int iteration, String testName) throws Throwable { final TestTask[] tasks = new TestTask[TASK_COUNT]; final AtomicInteger result = new AtomicInteger();
for (int i = 0; i < TASK_COUNT; ++i) {
tasks[i] = new TestTask(Integer.valueOf(i + 1), result::addAndGet);
}
Thread[] threads = newThread[THREAD_COUNT]; for (int i = 0; i < THREAD_COUNT; ++i) {
threads[i] = factory.createRunner(tasks);
}
for (int i = 0; i < THREAD_COUNT; ++i) {
threads[i].start();
}
for (int i = 0; i < THREAD_COUNT; ++i) {
threads[i].join();
}
check(result.get(),
TASK_COUNT * (TASK_COUNT + 1) / 2,
testName + " test result not as expected",
iteration);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.