code: def test_timelimit():
      schema = fields.Schema(text=fields.TEXT)
      ix = RamStorage().create_index(schema)
      w = ix.writer()
      for _ in xrange(50):
          w.add_document(text=u("alfa"))
      w.commit()
  
      import time
      from whoosh import collectors, matching
  
      class SlowMatcher(matching.WrappingMatcher):
          def next(self):
              time.sleep(0.02)
              self.child.next()
  
      class SlowQuery(query.WrappingQuery):
          def matcher(self, searcher, context=None):
              return SlowMatcher(self.child.matcher(searcher, context))
  
      with ix.searcher() as s:
          oq = query.Term("text", u("alfa"))
          sq = SlowQuery(oq)
  
          col = collectors.TimeLimitCollector(
              s.collector(limit=None), timelimit=0.1
          )
          with pytest.raises(searching.TimeLimit):
              s.search_with_collector(sq, col)
  
          col = collectors.TimeLimitCollector(
              s.collector(limit=40), timelimit=0.1
          )
          with pytest.raises(collectors.TimeLimit):
              s.search_with_collector(sq, col)
  
          col = collectors.TimeLimitCollector(
              s.collector(limit=None), timelimit=0.25
          )
          try:
              s.search_with_collector(sq, col)
              assert False  # Shouldn't get here
          except collectors.TimeLimit:
              r = col.results()
              assert r.scored_length() > 0
  
          col = collectors.TimeLimitCollector(
              s.collector(limit=None), timelimit=0.5
          )
          s.search_with_collector(oq, col)
          assert col.results().runtime < 0.5
  
file_path: whoosh/tests/test_collector.py
imports: from __future__ import with_statement
  import pytest
  from whoosh import collectors, fields, query, searching
  from whoosh.compat import u, xrange
  from whoosh.filedb.filestore import RamStorage
  from whoosh.util.testing import TempIndex
label_desc: Use image node_id 3 for calling a global function with example usage: test_timelimit() and returns: SlowMatcher