forked from voldemort/voldemort
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Substantial refactoring of (Queued)KeyedResourcePool tests.
- factored out a common base class in which all of the nested helper classes are defined - separated out the various types of tests into files: - simple/basic tests - contention tests that spawn threads to generate contention - specific race condition test - The specific race condition test for KeyedResourcePool shows that google issue 276 is resolved: http://code.google.com/p/project-voldemort/issues/detail?id=276
- Loading branch information
Showing
6 changed files
with
496 additions
and
482 deletions.
There are no files selected for viewing
This file contains 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
139 changes: 139 additions & 0 deletions
139
test/unit/voldemort/utils/pool/KeyedResourcePoolContentionTest.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* Copyright 2012 LinkedIn, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package voldemort.utils.pool; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
|
||
import java.util.Random; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class KeyedResourcePoolContentionTest extends KeyedResourcePoolBaseTest { | ||
|
||
protected static int POOL_SIZE = 5; | ||
protected static long TIMEOUT_MS = 500; | ||
|
||
@Before | ||
public void setUp() { | ||
factory = new TestResourceFactory(); | ||
config = new ResourcePoolConfig().setMaxPoolSize(POOL_SIZE) | ||
.setTimeout(TIMEOUT_MS, TimeUnit.MILLISECONDS); | ||
this.pool = new KeyedResourcePool<String, TestResource>(factory, config); | ||
} | ||
|
||
// This method was helpful when developing contendForResources | ||
public void printStats(String key) { | ||
System.err.println(""); | ||
System.err.println("getCreated: " + this.factory.getCreated()); | ||
System.err.println("getDestroyed: " + this.factory.getDestroyed()); | ||
System.err.println("getTotalResourceCount(key): " + this.pool.getTotalResourceCount(key)); | ||
System.err.println("getTotalResourceCount(): " + this.pool.getTotalResourceCount()); | ||
System.err.println("getCheckedInResourcesCount(key): " | ||
+ this.pool.getCheckedInResourcesCount(key)); | ||
System.err.println("getCheckedInResourceCount(): " + this.pool.getCheckedInResourceCount()); | ||
} | ||
|
||
@Test | ||
public void contendForResources() throws Exception { | ||
int numCheckers = POOL_SIZE * 2; | ||
int numChecks = 10 * 1000; | ||
String key = "Key"; | ||
float invalidationRate = (float) 0.25; | ||
CountDownLatch waitForThreads = new CountDownLatch(numCheckers); | ||
CountDownLatch waitForCheckers = new CountDownLatch(numCheckers); | ||
for(int i = 0; i < numCheckers; ++i) { | ||
new Thread(new Checkers(waitForThreads, | ||
waitForCheckers, | ||
key, | ||
numChecks, | ||
invalidationRate)).start(); | ||
} | ||
|
||
try { | ||
waitForCheckers.await(); | ||
assertEquals(this.pool.getCheckedInResourceCount(), this.pool.getTotalResourceCount()); | ||
} catch(InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
|
||
public class Checkers implements Runnable { | ||
|
||
private final CountDownLatch startSignal; | ||
private final CountDownLatch doneSignal; | ||
|
||
private final String key; | ||
private final int checks; | ||
|
||
private Random random; | ||
private float invalidationRate; | ||
|
||
Checkers(CountDownLatch startSignal, | ||
CountDownLatch doneSignal, | ||
String key, | ||
int checks, | ||
float invalidationRate) { | ||
this.startSignal = startSignal; | ||
this.doneSignal = doneSignal; | ||
|
||
this.key = key; | ||
this.checks = checks; | ||
|
||
this.random = new Random(); | ||
this.invalidationRate = invalidationRate; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
startSignal.countDown(); | ||
try { | ||
startSignal.await(); | ||
} catch(InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
try { | ||
TestResource tr = null; | ||
for(int i = 0; i < checks; ++i) { | ||
tr = pool.checkout(key); | ||
assertTrue(tr.isValid()); | ||
|
||
// Invalid some resources (except on last checkin) | ||
float f = random.nextFloat(); | ||
if(f < invalidationRate && i != checks - 1) { | ||
tr.invalidate(); | ||
} | ||
Thread.yield(); | ||
|
||
pool.checkin(key, tr); | ||
Thread.yield(); | ||
|
||
// if(i % 1000 == 0) { printStats(key); } | ||
} | ||
} catch(Exception e) { | ||
System.err.println(e.toString()); | ||
fail(e.toString()); | ||
} | ||
doneSignal.countDown(); | ||
} | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
test/unit/voldemort/utils/pool/KeyedResourcePoolRaceTest.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2012 LinkedIn, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package voldemort.utils.pool; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class KeyedResourcePoolRaceTest extends KeyedResourcePoolBaseTest { | ||
|
||
protected static final int POOL_SIZE = 100; | ||
protected static final long TIMEOUT_MS = 500; | ||
protected static final long NUM_TESTS = 250; | ||
|
||
@Before | ||
public void setUp() { | ||
factory = new TestResourceFactory(); | ||
config = new ResourcePoolConfig().setMaxPoolSize(POOL_SIZE) | ||
.setTimeout(TIMEOUT_MS, TimeUnit.MILLISECONDS); | ||
this.pool = new KeyedResourcePool<String, TestResource>(factory, config); | ||
} | ||
|
||
// See http://code.google.com/p/project-voldemort/issues/detail?id=276 | ||
@Test | ||
public void testAttemptGrow() { | ||
ExecutorService service = Executors.newFixedThreadPool(POOL_SIZE); | ||
for(int i = 0; i < NUM_TESTS; i++) { | ||
final CountDownLatch checkouts = new CountDownLatch(POOL_SIZE); | ||
List<Callable<Boolean>> tasks = new ArrayList<Callable<Boolean>>(POOL_SIZE); | ||
for(int t = 0; t < POOL_SIZE; t++) { | ||
tasks.add(new Callable<Boolean>() { | ||
|
||
@Override | ||
public Boolean call() throws Exception { | ||
try { | ||
TestResource resource = pool.checkout("a"); | ||
checkouts.countDown(); | ||
checkouts.await(); | ||
resource.invalidate(); | ||
pool.checkin("a", resource); | ||
return true; | ||
} catch(Exception e) { | ||
checkouts.countDown(); | ||
throw e; | ||
} | ||
} | ||
}); | ||
} | ||
try { | ||
List<Future<Boolean>> futures = service.invokeAll(tasks); | ||
for(Future<Boolean> future: futures) { | ||
assertTrue(future.get()); | ||
} | ||
} catch(Exception e) { | ||
fail("Unexpected exception - " + e.getMessage()); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.