Sto scrivendo unit test per AtomicInteger e AtomicBoolean. Saranno utilizzati come test di riferimento per testare emulazioni di queste classi in ogg-c, per l'uso in progetti tradotti.Come testare l'atomicità AtomicBoolean?
Il test AtomicInteger funzionava bene Credo, fondamentalmente eseguendo un numero prevedibile di incremento, decremento, aggiungere e sottrarre operazioni in un gran numero di cicli for, ognuno dei quali esegue nel proprio filo (e molti fili per tipo di operazione) . Le operazioni effettive iniziano simultaneamente con CountDownLatch.
Dopo aver eseguito tutti i thread, si asserisce confrontando il numero intero atomico con il valore intero previsto in base al numero di thread, alle iterazioni per thread e all'aumento/diminuzione previsto per iterazione. Questo test passa.
Ma come testare AtomicBoolean? Le operazioni di base sono get e set in modo tale che chiamare molte volte in molti thread e aspettarsi che il risultato finale sia vero o falso non sembra avere senso. La direzione che sto pensando è di usare due AtomicBooleans che dovrebbero avere sempre valori opposti. In questo modo:
@Test
public void testAtomicity() throws Exception {
// ==== SETUP ====
final AtomicBoolean booleanA = new AtomicBoolean(true);
final AtomicBoolean booleanB = new AtomicBoolean(false);
final int threadCount = 50;
final int iterationsPerThread = 5000;
final CountDownLatch startSignalLatch = new CountDownLatch(1);
final CountDownLatch threadsFinishedLatch = new CountDownLatch(threadCount);
final AtomicBoolean assertFailed = new AtomicBoolean(false);
// ==== EXECUTE: start all threads ====
for (int i = 0; i < threadCount; i++) {
// ==== Create the thread =====
AtomicOperationsThread thread;
thread = new AtomicOperationsThread("Thread #" + i, booleanA, booleanB, startSignalLatch, threadsFinishedLatch, iterationsPerThread, assertFailed);
System.out.println("Creating Thread #" + i);
// ==== Start the thread (each thread will wait until the startSignalLatch is triggered) =====
thread.start();
}
startSignalLatch.countDown();
// ==== VERIFY: that the AtomicInteger has the expected value after all threads have finished ====
final boolean allThreadsFinished;
allThreadsFinished = threadsFinishedLatch.await(60, TimeUnit.SECONDS);
assertTrue("Not all threads have finished before reaching the timeout", allThreadsFinished);
assertFalse(assertFailed.get());
}
private static class AtomicOperationsThread extends Thread {
// ##### Instance variables #####
private final CountDownLatch startSignalLatch;
private final CountDownLatch threadsFinishedLatch;
private final int iterations;
private final AtomicBoolean booleanA, booleanB;
private final AtomicBoolean assertFailed;
// ##### Constructor #####
private AtomicOperationsThread(final String name, final AtomicBoolean booleanA, final AtomicBoolean booleanB, final CountDownLatch startSignalLatch, final CountDownLatch threadsFinishedLatch, final int iterations, final AtomicBoolean assertFailed) {
super(name);
this.booleanA = booleanA;
this.booleanB = booleanB;
this.startSignalLatch = startSignalLatch;
this.threadsFinishedLatch = threadsFinishedLatch;
this.iterations = iterations;
this.assertFailed = assertFailed;
}
// ##### Thread implementation #####
@Override
public void run() {
super.run();
// ==== Wait for the signal to start (so all threads are executed simultaneously) =====
try {
System.out.println(this.getName() + " has started. Awaiting startSignal.");
startSignalLatch.await(); /* Awaiting start signal */
} catch (InterruptedException e) {
throw new RuntimeException("The startSignalLatch got interrupted.", e);
}
// ==== Perform the atomic operations =====
for (int i = 0; i < iterations; i++) {
final boolean booleanAChanged;
booleanAChanged = booleanA.compareAndSet(!booleanB.get(), booleanB.getAndSet(booleanA.get())); /* Set A to the current value of B if A is currently the opposite of B, then set B to the current value of A */
if (!booleanAChanged){
assertFailed.set(true);
System.out.println("Assert failed in thread: " + this.getName());
}
}
// ==== Mark this thread as finished =====
threadsFinishedLatch.countDown();
}
}
Questo funziona con un thread ma non riesce con più. Immagino che questo sia dovuto al fatto che booleanAChanged = booleanA.compareAndSet(!booleanB.get(), booleanB.getAndSet(booleanA.get()));
non è un'operazione atomica.
Qualche suggerimento?
Sono sicuro che hai - ma assicuratevi di pensare a tutto quando si verifica questo tipo di basso livello di concorrenza vale a dire l'hardware è in esecuzione, il sistema operativo, la JVM - si può tutte le cose ad effetto come questo e significa che i test che passano sempre in un contesto falliscono in un altro. – selig
Grazie per il tuo feedback. Presumo che tu ti stia riferendo al test AtomicInteger, giusto? Mi piacerebbe essere sicuro che non ci siano falsi positivi; Puoi indicare possibili miglioramenti per questo approccio e/o dare esempi di come qualcosa può andare storto? –