mirror of
https://github.com/corda/corda.git
synced 2025-01-21 03:55:00 +00:00
Fix for unit test failure when threads are created too slowly.
This commit is contained in:
parent
f7341732fc
commit
f4f4b8a26b
@ -5,7 +5,7 @@ public class AtomicIntegerConcurrentTest {
|
|||||||
final int threadCount,
|
final int threadCount,
|
||||||
final int iterationsPerThread) {
|
final int iterationsPerThread) {
|
||||||
// we assume a 1ms delay per thread to try to get them all to start at the same time
|
// we assume a 1ms delay per thread to try to get them all to start at the same time
|
||||||
final long startTime = System.currentTimeMillis() + threadCount;
|
final long startTime = System.currentTimeMillis() + threadCount + 10;
|
||||||
final AtomicInteger result = new AtomicInteger();
|
final AtomicInteger result = new AtomicInteger();
|
||||||
final AtomicInteger threadDoneCount = new AtomicInteger();
|
final AtomicInteger threadDoneCount = new AtomicInteger();
|
||||||
|
|
||||||
@ -15,6 +15,7 @@ public class AtomicIntegerConcurrentTest {
|
|||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
doOperation();
|
doOperation();
|
||||||
|
waitTillReady();
|
||||||
} finally {
|
} finally {
|
||||||
synchronized (threadDoneCount) {
|
synchronized (threadDoneCount) {
|
||||||
threadDoneCount.incrementAndGet();
|
threadDoneCount.incrementAndGet();
|
||||||
@ -24,15 +25,19 @@ public class AtomicIntegerConcurrentTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void doOperation() {
|
private void waitTillReady() {
|
||||||
long sleepTime = System.currentTimeMillis() - startTime;
|
long sleepTime = System.currentTimeMillis() - startTime;
|
||||||
|
if (sleepTime > 0) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(sleepTime);
|
Thread.sleep(sleepTime);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
// let thread exit
|
// let thread exit
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void doOperation() {
|
||||||
boolean flip = true;
|
boolean flip = true;
|
||||||
for (int i = 0; i < iterationsPerThread; i++) {
|
for (int i = 0; i < iterationsPerThread; i++) {
|
||||||
if (flip) {
|
if (flip) {
|
||||||
|
@ -5,7 +5,7 @@ public class AtomicLongConcurrentTest {
|
|||||||
final int threadCount,
|
final int threadCount,
|
||||||
final int iterationsPerThread) {
|
final int iterationsPerThread) {
|
||||||
// we assume a 1ms delay per thread to try to get them all to start at the same time
|
// we assume a 1ms delay per thread to try to get them all to start at the same time
|
||||||
final long startTime = System.currentTimeMillis() + threadCount;
|
final long startTime = System.currentTimeMillis() + threadCount + 10;
|
||||||
final AtomicLong result = new AtomicLong();
|
final AtomicLong result = new AtomicLong();
|
||||||
final AtomicLong threadDoneCount = new AtomicLong();
|
final AtomicLong threadDoneCount = new AtomicLong();
|
||||||
|
|
||||||
@ -15,6 +15,7 @@ public class AtomicLongConcurrentTest {
|
|||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
doOperation();
|
doOperation();
|
||||||
|
waitTillReady();
|
||||||
} finally {
|
} finally {
|
||||||
synchronized (threadDoneCount) {
|
synchronized (threadDoneCount) {
|
||||||
threadDoneCount.incrementAndGet();
|
threadDoneCount.incrementAndGet();
|
||||||
@ -24,15 +25,19 @@ public class AtomicLongConcurrentTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void doOperation() {
|
private void waitTillReady() {
|
||||||
long sleepTime = System.currentTimeMillis() - startTime;
|
long sleepTime = System.currentTimeMillis() - startTime;
|
||||||
|
if (sleepTime > 0) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(sleepTime);
|
Thread.sleep(sleepTime);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
// let thread exit
|
// let thread exit
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void doOperation() {
|
||||||
boolean flip = true;
|
boolean flip = true;
|
||||||
for (int i = 0; i < iterationsPerThread; i++) {
|
for (int i = 0; i < iterationsPerThread; i++) {
|
||||||
if (flip) {
|
if (flip) {
|
||||||
|
@ -5,7 +5,7 @@ public class AtomicReferenceConcurrentTest {
|
|||||||
private static void runTest(final int threadCount,
|
private static void runTest(final int threadCount,
|
||||||
final int iterationsPerThread) {
|
final int iterationsPerThread) {
|
||||||
// we assume a 1ms delay per thread to try to get them all to start at the same time
|
// we assume a 1ms delay per thread to try to get them all to start at the same time
|
||||||
final long startTime = System.currentTimeMillis() + threadCount;
|
final long startTime = System.currentTimeMillis() + threadCount + 10;
|
||||||
final AtomicReference<Integer> result = new AtomicReference<Integer>(0);
|
final AtomicReference<Integer> result = new AtomicReference<Integer>(0);
|
||||||
final AtomicInteger threadDoneCount = new AtomicInteger(0);
|
final AtomicInteger threadDoneCount = new AtomicInteger(0);
|
||||||
|
|
||||||
@ -15,6 +15,7 @@ public class AtomicReferenceConcurrentTest {
|
|||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
doOperation();
|
doOperation();
|
||||||
|
waitTillReady();
|
||||||
} finally {
|
} finally {
|
||||||
synchronized (threadDoneCount) {
|
synchronized (threadDoneCount) {
|
||||||
threadDoneCount.incrementAndGet();
|
threadDoneCount.incrementAndGet();
|
||||||
@ -24,15 +25,19 @@ public class AtomicReferenceConcurrentTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void doOperation() {
|
private void waitTillReady() {
|
||||||
long sleepTime = System.currentTimeMillis() - startTime;
|
long sleepTime = System.currentTimeMillis() - startTime;
|
||||||
|
if (sleepTime > 0) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(sleepTime);
|
Thread.sleep(sleepTime);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
// let thread exit
|
// let thread exit
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void doOperation() {
|
||||||
for (int i = 0; i < iterationsPerThread; i++) {
|
for (int i = 0; i < iterationsPerThread; i++) {
|
||||||
Integer current = result.get();
|
Integer current = result.get();
|
||||||
while (! result.compareAndSet(current, current + 1)) {
|
while (! result.compareAndSet(current, current + 1)) {
|
||||||
@ -63,6 +68,5 @@ public class AtomicReferenceConcurrentTest {
|
|||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
runTest(10, 100);
|
runTest(10, 100);
|
||||||
runTest(10, 100);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user