cache1.getRoot().getChild(Fqn.fromString("/org/jboss/data")).setResident(true); //so that it won't be counted for eviction
}
}
}
EvictionController ec1 = new EvictionController(cache1);
ec1.startEviction();
int childrenSize = cache1.getRoot().getChild(Fqn.fromString("/org/jboss/data")).getChildren().size();
assert childrenSize == 5000 : "Expected 5000, saw " + childrenSize;
c = UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.REPL_SYNC, true);
additionalConfiguration(c);
final Cache<Object, Object> cache2 = new UnitTestCacheFactory<Object, Object>().createCache(c);
caches.put("evict2", cache2);
Node<Object, Object> parent;// = cache2.getRoot().getChild(Fqn.fromString("/org/jboss/test/data"));
parent = cache2.getRoot().getChild(Fqn.fromString("/org/jboss/data"));
Set children = parent.getChildren();
//4999 because the root of the region will also be counted, as it is not resident
assertTrue("Minimum number of base children transferred", children.size() >= 4999);
// Sleep 2.5 secs so the nodes we are about to create in data won't
// exceed the 4 sec TTL when eviction thread runs
TestingUtil.sleepThread(2500);
class Putter extends Thread
{
Cache<Object, Object> cache = null;
boolean stopped = false;
Exception ex = null;
public void run()
{
int i = 25000;
while (!stopped)
{
try
{
cache.put(Fqn.fromString("/org/jboss/data/" + i), "key", "base" + i);
cache.put(Fqn.fromString("/org/jboss/test/data/" + i), "key", "data" + i);
i++;
}
catch (Exception e)
{
ex = e;
}
}
}
}
Putter p1 = new Putter();
p1.cache = cache1;
p1.start();
Putter p2 = new Putter();
p2.cache = cache2;
p2.start();
Random rnd = new Random();
TestingUtil.sleepThread(rnd.nextInt(200));
int maxCountBase = 0;
int maxCountData = 0;
boolean sawBaseDecrease = false;
boolean sawDataDecrease = false;
long start = System.currentTimeMillis();
Node root = cache2.getRoot();
while ((System.currentTimeMillis() - start) < 10000)
{
parent = root.getChild(Fqn.fromString("/org/jboss/test/data"));
children = parent.getChildren();
if (children != null)
{
int dataCount = children.size();
if (dataCount < maxCountData)
{
System.out.println("data " + dataCount + " < " + maxCountData + " elapsed = " + (System.currentTimeMillis() - start));
sawDataDecrease = true;
}
else
{
maxCountData = dataCount;
}
}
parent = cache2.getRoot().getChild(Fqn.fromString("/org/jboss/data"));
children = parent.getChildren();
if (children != null)
{
int baseCount = children.size();
if (baseCount < maxCountBase)
{
System.out.println("base " + baseCount + " < " + maxCountBase + " elapsed = " + (System.currentTimeMillis() - start));
sawBaseDecrease = true;
}
else
{
maxCountBase = baseCount;
}
}
if (sawDataDecrease && sawBaseDecrease)
{
break;
}
TestingUtil.sleepThread(50);
}
p1.stopped = true;
p2.stopped = true;
p1.join(1000);
p2.join(1000);
assertTrue("Saw data decrease", sawDataDecrease);
assertTrue("Saw base decrease", sawBaseDecrease);
assertNull("No exceptions in p1", p1.ex);
assertNull("No exceptions in p2", p2.ex);
EvictionController ec2 = new EvictionController(cache2);
ec2.startEviction();
parent = cache2.getRoot().getChild(Fqn.fromString("/org/jboss/test/data"));
children = parent.getChildren();
if (children != null)
{
System.out.println(children.size());
assertTrue("Excess children evicted", children.size() <= 5);
}
parent = cache2.getRoot().getChild(Fqn.fromString("/org/jboss/data"));
children = parent.getChildren();
if (children != null)
{
System.out.println(children.size());
assertTrue("Excess children evicted", children.size() <= 25000);
}
// Sleep more to let the eviction thread run again,
// which will evict all data nodes due to their ttl of 4 secs
ec2.evictRegionWithTimeToLive("/org/jboss/test/data");
parent = cache2.getRoot().getChild(Fqn.fromString("/org/jboss/test/data"));
if (parent != null)
{
children = parent.getChildren();