Showing failure problem with with concurrent access.
This results in one thread getting stuck in a loop.
This commit is contained in:
parent
0a824b70a7
commit
a52d3d5f71
2 changed files with 79 additions and 0 deletions
|
@ -128,6 +128,13 @@ public class License {
|
|||
return found;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is just useful for testing to allow clearing of the looked up licenses.
|
||||
*/
|
||||
static void clear() {
|
||||
lookupLicense.clear();
|
||||
}
|
||||
|
||||
public Behaviour[] getPermits() {
|
||||
return permits;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,72 @@
|
|||
package org.rometools.feed.module.cc.types;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
/**
|
||||
* @author Matthew Buckett
|
||||
*/
|
||||
public class LicenseTest {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
// As the looked up licenses are held statically we need to clear this between tests.
|
||||
License.clear();
|
||||
}
|
||||
|
||||
@Test(timeout = 500)
|
||||
public void testConcurrent() throws InterruptedException {
|
||||
final AtomicBoolean run = new AtomicBoolean(true);
|
||||
final AtomicLong type = new AtomicLong(0);
|
||||
final AtomicBoolean hadProblem = new AtomicBoolean(false);
|
||||
|
||||
// This thread keeps on adding new licenses (not very realistic but shows the bug)
|
||||
Thread addNew = new Thread(){
|
||||
@Override
|
||||
public void run() {
|
||||
while(run.get()) {
|
||||
License license = License.findByValue("http://creativecommons.org/licenses/"+
|
||||
type.incrementAndGet()+ "/1");
|
||||
if (license == null) {
|
||||
hadProblem.set(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// This thread attempts to get ones we know have already been put in.
|
||||
Thread getExisting = new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
Random rnd = new Random();
|
||||
while(run.get()) {
|
||||
License license = License.findByValue("http://creativecommons.org/licenses/"+
|
||||
rnd.nextInt(type.intValue())+"/1");
|
||||
if (license == null) {
|
||||
hadProblem.set(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
addNew.start();
|
||||
getExisting.start();
|
||||
// Let them do some stuff.
|
||||
Thread.sleep(100);
|
||||
// Get them to both stop.
|
||||
run.set(false);
|
||||
// Allow them a little time to stop.
|
||||
addNew.join(20);
|
||||
getExisting.join(20);
|
||||
// Check we didn't have any problems and they have both stopped.
|
||||
assertFalse(hadProblem.get());
|
||||
assertFalse(addNew.isAlive());
|
||||
assertFalse(getExisting.isAlive());
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue