public void testTimeLimitingCollector() throws Exception { Directory dir = TestUtil.getBookIndexDirectory(); IndexSearcher searcher = new IndexSearcher(dir); Query q = new MatchAllDocsQuery(); int numAllBooks = TestUtil.hitCount(searcher, q); TopScoreDocCollector topDocs = TopScoreDocCollector.create(10, false); Collector collector = new TimeLimitingCollector(topDocs, // #A 1000); // #A try { searcher.search(q, collector); assertEquals(numAllBooks, topDocs.getTotalHits()); // #B } catch (TimeExceededException tee) { // #C LOGGER.info("Too much time taken."); // #C } // #C searcher.close(); dir.close(); }
private void doTestTimeout(boolean multiThreaded, boolean greedy) { // setup MyHitCollector myHc = new MyHitCollector(); myHc.setSlowDown(SLOW_DOWN); Collector tlCollector = createTimedCollector(myHc, TIME_ALLOWED, greedy); // search TimeExceededException timoutException = null; try { search(tlCollector); } catch (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.getLastDocCollected(); int lastCollected = myHc.getLastDocCollected(); 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.getTimeAllowed(), TIME_ALLOWED); // a) Not too early assertTrue ( "elapsed="+timoutException.getTimeElapsed()+" <= (allowed-resolution)="+(TIME_ALLOWED-counterThread.getResolution()), timoutException.getTimeElapsed() > TIME_ALLOWED-counterThread.getResolution()); // 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.getTimeElapsed() > maxTime(multiThreaded)) { System.out.println("Informative: timeout exceeded (no action required: most probably just " + " because the test machine is slower than usual): " + "lastDoc="+exceptionDoc+ " ,&& allowed="+timoutException.getTimeAllowed() + " ,&& elapsed="+timoutException.getTimeElapsed() + " >= " + maxTimeStr(multiThreaded)); } }