Created
May 9, 2013 06:02
Unit test to support the following issue report:
https://github.com/bennidi/mbassador/issues/30
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.assertTrue; | |
import java.util.concurrent.CountDownLatch; | |
import net.engio.mbassy.common.StrongConcurrentSet; | |
import org.junit.Test; | |
public class TestConcurrency { | |
/** | |
* In this test HashMap will cross capacity threshold multiple times in | |
* different directions which will trigger rehashing. Because rehashing | |
* requires modification of Entry class for all hash map entries some keys | |
* may temporarily disappear from the map. | |
* <p> | |
* For more information please take a look at transfer method in HashMap. | |
*/ | |
@Test | |
public void testConcurrentAddRemove() { | |
final CountDownLatch latch = new CountDownLatch(1); | |
final StrongConcurrentSet<Integer> set = new StrongConcurrentSet<Integer>(); | |
final int numItems = 8; | |
// Adding elements that will not be t | |
for (int i = 0; i < numItems; i++) { | |
set.add(i); | |
} | |
// Adds and removes items >= numItems | |
Thread updatingThread = new Thread() { | |
public void run() { | |
try { | |
latch.await(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
System.out.println("Concurrent updates are running"); | |
for (int k = 0; k < 10000; k++) { | |
for (int i = numItems; i < 129; i++) { | |
set.add(i); | |
} | |
for (int i = numItems; i < 129; i++) { | |
set.remove(i); | |
} | |
} | |
}; | |
}; | |
updatingThread.start(); | |
// Making sure that all items are present in the set | |
System.out.println("Concurrent updates are paused"); | |
for (int j = 0; j < numItems; j++) { | |
assertTrue("Couldn't find " + j, set.contains(j)); | |
} | |
System.out.println("Starting concurrent updates"); | |
latch.countDown(); | |
for (int i = 0; i < 100000; i++) { | |
for (int j = 0; j < numItems; j++) { | |
// Since elements from 0 to numItems are never removed, | |
// set.contains(j) should always return true | |
if (!set.contains(j)) { | |
System.out.println("Couldn't find " + j + " on attempt " + i); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment