You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by sy...@apache.org on 2016/10/02 14:35:24 UTC

[06/50] [abbrv] lucenenet git commit: Completed port of Suggest.Analyzing.AnalyzingInfixSuggesterTest.TestRandomNRT()

Completed port of Suggest.Analyzing.AnalyzingInfixSuggesterTest.TestRandomNRT()


Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/e539eee1
Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/e539eee1
Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/e539eee1

Branch: refs/heads/master
Commit: e539eee153e90499416605ecf660d367a02c280c
Parents: 4898450
Author: Shad Storhaug <sh...@shadstorhaug.com>
Authored: Thu Sep 15 03:07:50 2016 +0700
Committer: Shad Storhaug <sh...@shadstorhaug.com>
Committed: Sun Oct 2 17:44:04 2016 +0700

----------------------------------------------------------------------
 .../Analyzing/AnalyzingInfixSuggesterTest.cs    | 148 ++++++++++---------
 1 file changed, 78 insertions(+), 70 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/e539eee1/src/Lucene.Net.Tests.Suggest/Suggest/Analyzing/AnalyzingInfixSuggesterTest.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests.Suggest/Suggest/Analyzing/AnalyzingInfixSuggesterTest.cs b/src/Lucene.Net.Tests.Suggest/Suggest/Analyzing/AnalyzingInfixSuggesterTest.cs
index dc12951..a4f3830 100644
--- a/src/Lucene.Net.Tests.Suggest/Suggest/Analyzing/AnalyzingInfixSuggesterTest.cs
+++ b/src/Lucene.Net.Tests.Suggest/Suggest/Analyzing/AnalyzingInfixSuggesterTest.cs
@@ -2,6 +2,7 @@
 using Lucene.Net.Analysis.Core;
 using Lucene.Net.Analysis.Tokenattributes;
 using Lucene.Net.Analysis.Util;
+using Lucene.Net.Support;
 using Lucene.Net.Util;
 using NUnit.Framework;
 using System;
@@ -9,6 +10,9 @@ using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using System.Text;
+using System.Text.RegularExpressions;
+using System.Threading;
+using System.Threading.Tasks;
 
 namespace Lucene.Net.Search.Suggest.Analyzing
 {
@@ -528,46 +532,63 @@ namespace Lucene.Net.Search.Suggest.Analyzing
             internal int index;
         }
 
-        //private static class LookupThread : Thread
-        //{
-        //    private readonly AnalyzingInfixSuggester suggester;
-        //    private volatile boolean stop;
-
-        //public LookupThread(AnalyzingInfixSuggester suggester)
-        //{
-        //    this.suggester = suggester;
-        //}
-
-        //public void finish()
-        //{
-        //    stop = true;
-        //      this.join();
-        //}
-
-        //@Override
-        //    public void run()
-        //{
-        //    while (stop == false)
-        //    {
-        //        String query = randomText();
-        //        int topN = TestUtil.nextInt(random(), 1, 100);
-        //        boolean allTermsRequired = random().nextBoolean();
-        //        boolean doHilite = random().nextBoolean();
-        //        // We don't verify the results; just doing
-        //        // simultaneous lookups while adding/updating to
-        //        // see if there are any thread hazards:
-        //        try
-        //        {
-        //            suggester.lookup(TestUtil.stringToCharSequence(query, random()),
-        //                             topN, allTermsRequired, doHilite);
-        //        }
-        //        catch (IOException ioe)
-        //        {
-        //            throw new RuntimeException(ioe);
-        //        }
-        //    }
-        //}
-        //  }
+        private class LookupThread : ThreadClass
+        {
+            private readonly AnalyzingInfixSuggesterTest outerInstance;
+
+            private readonly AnalyzingInfixSuggester suggester;
+            private readonly AtomicBoolean stop;
+            private Exception[] error;
+
+            public LookupThread(AnalyzingInfixSuggesterTest outerInstance, AnalyzingInfixSuggester suggester, AtomicBoolean stop, Exception[] error)
+            {
+                this.outerInstance = outerInstance;
+                this.suggester = suggester;
+                this.stop = stop;
+                this.error = error;
+            }
+
+            public override void Run()
+            {
+                Priority += 1;
+                while (!stop.Get())
+                {
+                    string query = RandomText();
+                    int topN = TestUtil.NextInt(Random(), 1, 100);
+                    bool allTermsRequired = Random().nextBoolean();
+                    bool doHilite = Random().nextBoolean();
+                    // We don't verify the results; just doing
+                    // simultaneous lookups while adding/updating to
+                    // see if there are any thread hazards:
+                    try
+                    {
+                        suggester.DoLookup(TestUtil.StringToCharSequence(query, Random()).ToString(),
+                                         topN, allTermsRequired, doHilite);
+                        Thread.Sleep(10);// don't starve refresh()'s CPU, which sleeps every 50 bytes for 1 ms
+                    }
+                    catch (Exception e)
+                    {
+                        error[0] = e;
+                        stop.Set(true);
+                    }
+                }
+            }
+        }
+
+        /// <summary>
+        /// Grab the stack trace into a string since the exception was thrown in a thread and we want the assert 
+        /// outside the thread to show the stack trace in case of failure.   
+        /// </summary>
+        private string stackTraceStr(Exception error)
+        {
+            if (error == null)
+            {
+                return "";
+            }
+
+            error.printStackTrace();
+            return error.StackTrace;
+        }
 
         internal class TestRandomNRTComparator : IComparer<Input>
         {
@@ -588,7 +609,6 @@ namespace Lucene.Net.Search.Suggest.Analyzing
             }
         }
 
-        /* LUCENENET TODO: Compile issues
         [Test]
         public void TestRandomNRT()
         {
@@ -605,8 +625,11 @@ namespace Lucene.Net.Search.Suggest.Analyzing
             // Initial suggester built with nothing:
             suggester.Build(new InputArrayIterator(new Input[0]));
 
-            LookupThread lookupThread = new LookupThread(suggester);
-            lookupThread.start();
+            var stop = new AtomicBoolean(false);
+            Exception[] error = new Exception[] { null };
+
+            LookupThread lookupThread = new LookupThread(this, suggester, stop, error);
+            lookupThread.Start();
 
             int iters = AtLeast(1000);
             int visibleUpto = 0;
@@ -693,11 +716,14 @@ namespace Lucene.Net.Search.Suggest.Analyzing
                     {
                         Console.WriteLine("TEST: now close/reopen suggester");
                     }
-                    lookupThread.finish();
+                    //lookupThread.Finish();
+                    stop.Set(true);
+                    lookupThread.Join();
+                    Assert.Null(error[0], "Unexpcted exception at retry : \n" + stackTraceStr(error[0]));
                     suggester.Dispose();
                     suggester = new AnalyzingInfixSuggester(TEST_VERSION_CURRENT, NewFSDirectory(tempDir), a, a, minPrefixChars);
-                    lookupThread = new LookupThread(suggester);
-                    lookupThread.start();
+                    lookupThread = new LookupThread(this, suggester, stop, error);
+                    lookupThread.Start();
 
                     visibleUpto = inputs.size();
                     foreach (Update update in pendingUpdates)
@@ -711,7 +737,7 @@ namespace Lucene.Net.Search.Suggest.Analyzing
 
                 if (visibleUpto > 0)
                 {
-                    String query = RandomText();
+                    string query = RandomText();
                     bool lastPrefix = Random().nextInt(5) != 1;
                     if (lastPrefix == false)
                     {
@@ -791,26 +817,6 @@ namespace Lucene.Net.Search.Suggest.Analyzing
 
                     expected.Sort(new TestRandomNRTComparator());
 
-                    //        Collections.sort(expected,
-                    //                         new Comparator<Input>() {
-                    //                           @Override
-                    //                           public int compare(Input a, Input b)
-                    //{
-                    //    if (a.v > b.v)
-                    //    {
-                    //        return -1;
-                    //    }
-                    //    else if (a.v < b.v)
-                    //    {
-                    //        return 1;
-                    //    }
-                    //    else
-                    //    {
-                    //        return 0;
-                    //    }
-                    //}
-                    //                         });
-
                     if (expected.Any())
                     {
 
@@ -853,10 +859,12 @@ namespace Lucene.Net.Search.Suggest.Analyzing
                 }
             }
 
-            lookupThread.finish();
+            //lookupThread.finish();
+            stop.Set(true);
+            lookupThread.Join();
+            Assert.Null(error[0], "Unexpcted exception at retry : \n" + stackTraceStr(error[0]));
             suggester.Dispose();
         }
-        */
 
         private static string Hilite(bool lastPrefix, string[] inputTerms, string[] queryTerms)
         {
@@ -893,7 +901,7 @@ namespace Lucene.Net.Search.Suggest.Analyzing
                         b.Append("<b>");
                         b.Append(queryTerm);
                         b.Append("</b>");
-                        b.Append(inputTerm.Substring(queryTerm.Length, inputTerm.Length));
+                        b.Append(inputTerm.Substring(queryTerm.Length, inputTerm.Length - queryTerm.Length));
                         matched = true;
                         break;
                     }