PojoCache[] caches = new PojoCache[count + 1];
caches[0] = cacheA;
// Create a semaphore and take all its tickets
Semaphore semaphore = new Semaphore(count);
for (int i = 0; i < count; i++) {
semaphore.acquire();
}
// Create stressor threads that will block on the semaphore
for (int i = 0; i < count; i++)
{
stressors[i] = new CacheStressor(semaphore, names[i], sync);
caches[i + 1] = stressors[i].getTreeCache();
stressors[i].getTreeCache().put("/" + names[i], null);
}
for (int i = 0; i < count; i++)
{
stressors[i].start();
// Give each one a chance to stabilize
TestingUtil.sleepThread(100);
}
// Make sure everyone's views are in sync
TestingUtil.blockUntilViewsReceived(caches, 60000);
// Repeat the basic test two times in order to involve inactivation
for (int x = 0; x < 2; x++)
{
// if (x > 0)
// {
// Reset things by inactivating the region
// and enabling the stressors
for (int i = 0; i < count; i++)
{
cacheA.inactivateRegion("/" + names[i]);
log.info("TEST: Run " + x + "-- /" + names[i] + " inactivated on A");
stressors[i].startPuts();
}
// }
// Release the semaphore to allow the threads to start work
semaphore.release(count);
// Sleep to ensure the threads get all the semaphore tickets
// and to ensure puts are actively in progress
TestingUtil.sleepThread(300);
// Activate cacheA
for (int i = 0; i < count; i++)
{
log.info("TEST: Activating /" + names[i] + " on A");
cacheA.activateRegion("/" + names[i]);
// Stop the stressor so we don't pollute cacheA's state
// with too many messages sent after activation -- we want
// to compare transferred state with the sender
stressors[i].stopPuts();
log.info("TEST: Run " + x + "-- /" + names[i] + " activated on A");
// Reacquire one semaphore ticket
boolean acquired = semaphore.attempt(60000);
if (!acquired)
fail("failed to acquire semaphore " + names[i]);
log.info("TEST: Run " + x + "-- acquired semaphore from " + names[i]);
// Pause to allow other work to proceed