Birden çok iş parçacığı aracılığıyla erişildiğinde java.util.concurrent.ConcurrentLinkedQueue sınamak çalışıyorum. Aşağıda, iki eşzamanlı iş parçacığında çalışmak için RepeatedTest kullanarak yaptığım Junit testim var. Sorularım: ConcurrentLinkedQueue üzerinde eşzamanlılığı test etmek için RepeatedTest'i kullanmak doğru mu? Kaynak kodu aşağıda belirtilmiştir.Eşzamanlılık sınamak için Junit
Teşekkür
import java.util.concurrent.ConcurrentLinkedQueue;
import junit.extensions.ActiveTestSuite;
import junit.extensions.RepeatedTest;
import junit.extensions.TestSetup;
import junit.framework.TestCase;
public class TestNonBlockingConcurrentQueue extends TestCase{
private static ConcurrentLinkedQueue clq;
public void testPut() throws Exception {
int messageCounter = 0;
for(;messageCounter <10000; messageCounter++){
clq.offer(messageCounter);
}
assertEquals(clq.size(), messageCounter);
}
public void testGet() throws Exception {
while(!clq.isEmpty()){
clq.poll();
}
assertEquals("Size should be zero", clq.size(), 0);
}
public static junit.framework.Test suite() {
ActiveTestSuite ats = new ActiveTestSuite();
TestSetup setup = new TestSetup(ats) {
protected void setUp() throws Exception {
System.out.println("Creating ConcurrentLinkedQueue..");
clq = new ConcurrentLinkedQueue();
}
protected void tearDown() throws Exception {
clq = null;
}
};
ats.addTest(new RepeatedTest(new TestNonBlockingConcurrentQueue("testPut"), 2));
ats.addTest(new RepeatedTest(new TestNonBlockingConcurrentQueue("testGet"), 2));
return setup;
}
public TestNonBlockingConcurrentQueue(String testName){
super(testName);
}
Bu, eşzamanlı olmayan sorunlar için de geçerlidir. 'Edsger W. Dijkstra' alıntılamak için: * Test varlığını gösterir, hataların olmadığını gösterir. – whiskeysierra
@Willi, kesinlikle! Bu sadece eşzamanlılık problemlerinin tespit edilmesinin zorlaştığı yönündedir, bence. – Bruno
Elbette, eşzamanlılık sorunları için testler yazabilirsiniz. Hataların olmadığını kanıtlayan testler yazamazsınız, ancak yine de belirli hataların varlığını test edebilirsiniz. Ancak bu testler yazmak karmaşık olabilir. –