Provides a task scheduler that ensures a maximum concurrency level while running on top of the thread pool. Source: https://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler(v=vs.110).aspx
Inheritance: System.Threading.Tasks.TaskScheduler
        public virtual void TestHugeN()
        {
            TaskScheduler service = new LimitedConcurrencyLevelTaskScheduler(4);

            IndexSearcher[] searchers = new IndexSearcher[] { new IndexSearcher(Reader), new IndexSearcher(Reader, service) };
            Query[] queries = new Query[] { new MatchAllDocsQuery(), new TermQuery(new Term("field", "1")) };
            Sort[] sorts = new Sort[] { null, new Sort(new SortField("field2", SortField.Type_e.STRING)) };
            Filter[] filters = new Filter[] { null, new QueryWrapperFilter(new TermQuery(new Term("field2", "true"))) };
            ScoreDoc[] afters = new ScoreDoc[] { null, new FieldDoc(0, 0f, new object[] { new BytesRef("boo!") }) };

            foreach (IndexSearcher searcher in searchers)
            {
                foreach (ScoreDoc after in afters)
                {
                    foreach (Query query in queries)
                    {
                        foreach (Sort sort in sorts)
                        {
                            foreach (Filter filter in filters)
                            {
                                searcher.Search(query, int.MaxValue);
                                searcher.SearchAfter(after, query, int.MaxValue);
                                searcher.Search(query, filter, int.MaxValue);
                                searcher.SearchAfter(after, query, filter, int.MaxValue);
                                if (sort != null)
                                {
                                    searcher.Search(query, int.MaxValue, sort);
                                    searcher.Search(query, filter, int.MaxValue, sort);
                                    searcher.Search(query, filter, int.MaxValue, sort, true, true);
                                    searcher.Search(query, filter, int.MaxValue, sort, true, false);
                                    searcher.Search(query, filter, int.MaxValue, sort, false, true);
                                    searcher.Search(query, filter, int.MaxValue, sort, false, false);
                                    searcher.SearchAfter(after, query, filter, int.MaxValue, sort);
                                    searcher.SearchAfter(after, query, filter, int.MaxValue, sort, true, true);
                                    searcher.SearchAfter(after, query, filter, int.MaxValue, sort, true, false);
                                    searcher.SearchAfter(after, query, filter, int.MaxValue, sort, false, true);
                                    searcher.SearchAfter(after, query, filter, int.MaxValue, sort, false, false);
                                }
                            }
                        }
                    }
                }
            }

            TestUtil.ShutdownExecutorService(service);
        }
Esempio n. 2
0
 /// <summary>
 /// Create a new searcher over the reader. this searcher might randomly use
 /// threads. if <code>maybeWrap</code> is true, this searcher might wrap the
 /// reader with one that returns null for getSequentialSubReaders. If
 /// <code>wrapWithAssertions</code> is true, this searcher might be an
 /// <seealso cref="AssertingIndexSearcher"/> instance.
 /// </summary>
 /// <param name="similarity">
 /// LUCENENET specific
 /// Removes dependency on <see cref="LuceneTestCase.ClassEnv.Similarity"/>
 /// </param>
 public static IndexSearcher NewSearcher(IndexReader r, bool maybeWrap, bool wrapWithAssertions, Similarity similarity)
 {
     Random random = Random();
     if (Usually())
     {
         if (maybeWrap)
         {
             r = MaybeWrapReader(r);
         }
         // TODO: this whole check is a coverage hack, we should move it to tests for various filterreaders.
         // ultimately whatever you do will be checkIndex'd at the end anyway.
         if (random.Next(500) == 0 && r is AtomicReader)
         {
             // TODO: not useful to check DirectoryReader (redundant with checkindex)
             // but maybe sometimes run this on the other crazy readers maybeWrapReader creates?
             TestUtil.CheckReader(r);
         }
         IndexSearcher ret;
         if (wrapWithAssertions)
         {
             ret = random.NextBoolean() ? new AssertingIndexSearcher(random, r) : new AssertingIndexSearcher(random, r.Context);
         }
         else
         {
             ret = random.NextBoolean() ? new IndexSearcher(r) : new IndexSearcher(r.Context);
         }
         ret.Similarity = similarity;
         return ret;
     }
     else
     {
         int threads = 0;
         TaskScheduler ex;
         if (random.NextBoolean())
         {
         ex = null;
         }
         else
         {
             threads = TestUtil.NextInt(random, 1, 8);
             ex = new LimitedConcurrencyLevelTaskScheduler(threads);
             //ex = new ThreadPoolExecutor(threads, threads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<IThreadRunnable>(), new NamedThreadFactory("LuceneTestCase"));
             // uncomment to intensify LUCENE-3840
             // ex.prestartAllCoreThreads();
         }
         if (ex != null)
         {
             if (VERBOSE)
             {
                 Console.WriteLine("NOTE: newSearcher using ExecutorService with " + threads + " threads");
             }
             //r.AddReaderClosedListener(new ReaderClosedListenerAnonymousInnerClassHelper(ex));
         }
         IndexSearcher ret;
         if (wrapWithAssertions)
         {
             ret = random.NextBoolean() ? new AssertingIndexSearcher(random, r, ex) : new AssertingIndexSearcher(random, r.Context, ex);
         }
         else
         {
             ret = random.NextBoolean() ? new IndexSearcher(r, ex) : new IndexSearcher(r.Context, ex);
         }
         ret.Similarity = similarity;
         return ret;
     }
 }