private void DoTestTimeout(bool multiThreaded, bool greedy)
{
// setup
MyHitCollector myHc = new MyHitCollector();
myHc.SetSlowDown(SLOW_DOWN);
Collector tlCollector = CreateTimedCollector(myHc, TIME_ALLOWED, greedy);
// search
TimeLimitingCollector.TimeExceededException timoutException = null;
try
{
Search(tlCollector);
}
catch (TimeLimitingCollector.TimeExceededException x)
{
timoutException = x;
}
catch (Exception e)
{
assertTrue("Unexpected exception: " + e, false); //==fail
}
// must get exception
assertNotNull("Timeout expected!", timoutException);
// greediness affect last doc collected
int exceptionDoc = timoutException.LastDocCollected;
int lastCollected = myHc.LastDocCollected;
assertTrue("doc collected at timeout must be > 0!", exceptionDoc > 0);
if (greedy)
{
assertTrue("greedy=" + greedy + " exceptionDoc=" + exceptionDoc + " != lastCollected=" + lastCollected, exceptionDoc == lastCollected);
assertTrue("greedy, but no hits found!", myHc.HitCount() > 0);
}
else
{
assertTrue("greedy=" + greedy + " exceptionDoc=" + exceptionDoc + " not > lastCollected=" + lastCollected, exceptionDoc > lastCollected);
}
// verify that elapsed time at exception is within valid limits
assertEquals(timoutException.TimeAllowed, TIME_ALLOWED);
// a) Not too early
assertTrue("elapsed=" + timoutException.TimeElapsed + " <= (allowed-resolution)=" + (TIME_ALLOWED - counterThread.Resolution),
timoutException.TimeElapsed > TIME_ALLOWED - counterThread.Resolution);
// b) Not too late.
// This part is problematic in a busy test system, so we just print a warning.
// We already verified that a timeout occurred, we just can't be picky about how long it took.
if (timoutException.TimeElapsed > MaxTime(multiThreaded))
{
Console.WriteLine("Informative: timeout exceeded (no action required: most probably just " +
" because the test machine is slower than usual): " +
"lastDoc=" + exceptionDoc +
" ,&& allowed=" + timoutException.TimeAllowed +
" ,&& elapsed=" + timoutException.TimeElapsed +
" >= " + MaxTimeStr(multiThreaded));
}
}