Don't get stuck in the loop.
If we get an exception from the iterator don't catch it and re-try, this makes our loop safer from getting stuck and never returning.
This commit is contained in:
parent
76bf626795
commit
034fb3dd93
2 changed files with 22 additions and 11 deletions
|
@ -105,8 +105,8 @@ public class License {
|
||||||
if (found == null && uri.startsWith("http://") && uri.toLowerCase().indexOf("creativecommons.org") != -1) {
|
if (found == null && uri.startsWith("http://") && uri.toLowerCase().indexOf("creativecommons.org") != -1) {
|
||||||
final Iterator it = License.lookupLicense.keySet().iterator();
|
final Iterator it = License.lookupLicense.keySet().iterator();
|
||||||
while (it.hasNext() && found == null) {
|
while (it.hasNext() && found == null) {
|
||||||
try {
|
|
||||||
final String key = (String) it.next();
|
final String key = (String) it.next();
|
||||||
|
try {
|
||||||
if (key.startsWith(CC_START)) {
|
if (key.startsWith(CC_START)) {
|
||||||
final String licensePath = key.substring(CC_START.length(), key.length());
|
final String licensePath = key.substring(CC_START.length(), key.length());
|
||||||
final StringTokenizer tok = new StringTokenizer(licensePath, "/");
|
final StringTokenizer tok = new StringTokenizer(licensePath, "/");
|
||||||
|
|
|
@ -24,12 +24,15 @@ public class LicenseTest {
|
||||||
public void testConcurrent() throws InterruptedException {
|
public void testConcurrent() throws InterruptedException {
|
||||||
final AtomicBoolean run = new AtomicBoolean(true);
|
final AtomicBoolean run = new AtomicBoolean(true);
|
||||||
final AtomicLong type = new AtomicLong(0);
|
final AtomicLong type = new AtomicLong(0);
|
||||||
|
// Tracking any problems.
|
||||||
final AtomicBoolean hadProblem = new AtomicBoolean(false);
|
final AtomicBoolean hadProblem = new AtomicBoolean(false);
|
||||||
|
final AtomicBoolean hadException = new AtomicBoolean(false);
|
||||||
|
|
||||||
// This thread keeps on adding new licenses (not very realistic but shows the bug)
|
// This thread keeps on adding new licenses (not very realistic but shows the bug)
|
||||||
Thread addNew = new Thread(){
|
Thread addNew = new Thread(){
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
try {
|
||||||
while(run.get()) {
|
while(run.get()) {
|
||||||
License license = License.findByValue("http://creativecommons.org/licenses/"+
|
License license = License.findByValue("http://creativecommons.org/licenses/"+
|
||||||
type.incrementAndGet()+ "/1");
|
type.incrementAndGet()+ "/1");
|
||||||
|
@ -37,6 +40,9 @@ public class LicenseTest {
|
||||||
hadProblem.set(true);
|
hadProblem.set(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
hadException.set(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -45,6 +51,7 @@ public class LicenseTest {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
Random rnd = new Random();
|
Random rnd = new Random();
|
||||||
|
try {
|
||||||
while(run.get()) {
|
while(run.get()) {
|
||||||
License license = License.findByValue("http://creativecommons.org/licenses/"+
|
License license = License.findByValue("http://creativecommons.org/licenses/"+
|
||||||
rnd.nextInt(type.intValue())+"/1");
|
rnd.nextInt(type.intValue())+"/1");
|
||||||
|
@ -52,6 +59,9 @@ public class LicenseTest {
|
||||||
hadProblem.set(true);
|
hadProblem.set(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
hadException.set(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -66,6 +76,7 @@ public class LicenseTest {
|
||||||
getExisting.join(50);
|
getExisting.join(50);
|
||||||
// Check we didn't have any problems and they have both stopped.
|
// Check we didn't have any problems and they have both stopped.
|
||||||
assertFalse(hadProblem.get());
|
assertFalse(hadProblem.get());
|
||||||
|
assertFalse(hadException.get());
|
||||||
assertFalse(addNew.isAlive());
|
assertFalse(addNew.isAlive());
|
||||||
assertFalse(getExisting.isAlive());
|
assertFalse(getExisting.isAlive());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue