private ICollector CreateTimedCollector(MyHitCollector hc, long timeAllowed, bool greedy)
        {
            TimeLimitingCollector res = new TimeLimitingCollector(hc, counter, timeAllowed);

            res.IsGreedy = (greedy); // set to true to make sure at least one doc is collected.
            return(res);
        }
        private void DoTestTimeout(bool multiThreaded, bool greedy)
        {
            // setup
            MyHitCollector myHc = new MyHitCollector();

            myHc.SetSlowDown(SLOW_DOWN);
            ICollector 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));
            }
        }
        private void DoTestSearch()
        {
            int totalResults    = 0;
            int totalTLCResults = 0;

            try
            {
                MyHitCollector myHc = new MyHitCollector();
                Search(myHc);
                totalResults = myHc.HitCount();

                myHc = new MyHitCollector();
                long       oneHour     = 3600000;
                ICollector tlCollector = CreateTimedCollector(myHc, oneHour, false);
                Search(tlCollector);
                totalTLCResults = myHc.HitCount();
            }
            catch (Exception e)
            {
                e.printStackTrace();
                assertTrue("Unexpected exception: " + e, false); //==fail
            }
            assertEquals("Wrong number of results!", totalResults, totalTLCResults);
        }
        private void  DoTestSearch()
        {
            int totalResults    = 0;
            int totalTLCResults = 0;

            try
            {
                MyHitCollector myHc = new MyHitCollector(this);
                Search(myHc);
                totalResults = myHc.HitCount();

                myHc = new MyHitCollector(this);
                long      oneHour     = 3600000;
                Collector tlCollector = CreateTimedCollector(myHc, oneHour, false);
                Search(tlCollector);
                totalTLCResults = myHc.HitCount();
            }
            catch (System.Exception e)
            {
                System.Console.Error.WriteLine(e.StackTrace);
                Assert.IsTrue(false, "Unexpected exception: " + e);                 //==fail
            }
            Assert.AreEqual(totalResults, totalTLCResults, "Wrong number of results!");
        }
		private void  DoTestTimeout(bool multiThreaded, bool greedy)
		{
			// setup
			MyHitCollector myHc = new MyHitCollector(this);
			myHc.SetSlowDown(SLOW_DOWN);
			HitCollector tlCollector = CreateTimedCollector(myHc, TIME_ALLOWED, greedy);
			
			// search
			TimeLimitedCollector.TimeExceededException timoutException = null;
			try
			{
				Search(tlCollector);
			}
			catch (TimeLimitedCollector.TimeExceededException x)
			{
				timoutException = x;
			}
			catch (System.Exception e)
			{
				Assert.IsTrue(false, "Unexpected exception: " + e); //==fail
			}
			
			// must get exception
			Assert.IsNotNull(timoutException, "Timeout expected!");
			
			// greediness affect last doc collected
			int exceptionDoc = timoutException.GetLastDocCollected();
			int lastCollected = myHc.GetLastDocCollected();
			Assert.IsTrue(exceptionDoc > 0, "doc collected at timeout must be > 0!");
			if (greedy)
			{
				Assert.IsTrue(exceptionDoc == lastCollected, "greedy=" + greedy + " exceptionDoc=" + exceptionDoc + " != lastCollected=" + lastCollected);
				Assert.IsTrue(myHc.HitCount() > 0, "greedy, but no hits found!");
			}
			else
			{
				Assert.IsTrue(exceptionDoc > lastCollected, "greedy=" + greedy + " exceptionDoc=" + exceptionDoc + " not > lastCollected=" + lastCollected);
			}
			
			// verify that elapsed time at exception is within valid limits
			Assert.AreEqual(timoutException.GetTimeAllowed(), TIME_ALLOWED);
			// a) Not too early
			Assert.IsTrue(timoutException.GetTimeElapsed() > TIME_ALLOWED - TimeLimitedCollector.GetResolution(), "elapsed=" + timoutException.GetTimeElapsed() + " <= (allowed-resolution)=" + (TIME_ALLOWED - TimeLimitedCollector.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.Console.Out.WriteLine("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));
			}
		}
		private HitCollector CreateTimedCollector(MyHitCollector hc, long timeAllowed, bool greedy)
		{
			TimeLimitedCollector res = new TimeLimitedCollector(hc, timeAllowed);
			res.SetGreedy(greedy); // set to true to make sure at least one doc is collected.
			return res;
		}
		private void  DoTestSearch()
		{
			int totalResults = 0;
			int totalTLCResults = 0;
			try
			{
				MyHitCollector myHc = new MyHitCollector(this);
				Search(myHc);
				totalResults = myHc.HitCount();
				
				myHc = new MyHitCollector(this);
				long oneHour = 3600000;
				HitCollector tlCollector = CreateTimedCollector(myHc, oneHour, false);
				Search(tlCollector);
				totalTLCResults = myHc.HitCount();
			}
			catch (System.Exception e)
			{
				System.Console.Error.WriteLine(e.StackTrace);
				Assert.IsTrue(false, "Unexpected exception: " + e); //==fail
			}
			Assert.AreEqual(totalResults, totalTLCResults, "Wrong number of results!");
		}
        private void DoTestSearch()
        {
            int totalResults = 0;
            int totalTLCResults = 0;
            try
            {
                MyHitCollector myHc = new MyHitCollector();
                Search(myHc);
                totalResults = myHc.HitCount();

                myHc = new MyHitCollector();
                long oneHour = 3600000;
                Collector tlCollector = CreateTimedCollector(myHc, oneHour, false);
                Search(tlCollector);
                totalTLCResults = myHc.HitCount();
            }
            catch (Exception e)
            {
                e.printStackTrace();
                assertTrue("Unexpected exception: " + e, false); //==fail
            }
            assertEquals("Wrong number of results!", totalResults, totalTLCResults);
        }