lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nightowl...@apache.org
Subject [lucenenet] 01/12: Fixed TestThreadInterruptDeadlock and TestTwoThreadsInterruptDeadlock
Date Thu, 11 Jul 2019 15:42:56 GMT
This is an automated email from the ASF dual-hosted git repository.

nightowl888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucenenet.git

commit 71d143037a5753edea612c56219288d716b677c2
Author: Simon Svensson <sisve@devhost.se>
AuthorDate: Thu Jan 17 21:10:41 2019 +0100

    Fixed TestThreadInterruptDeadlock and TestTwoThreadsInterruptDeadlock
---
 .../Store/MockDirectoryWrapper.cs                     |  4 ++++
 src/Lucene.Net.Tests/Index/TestIndexWriter.cs         | 10 +---------
 src/Lucene.Net/Index/IndexWriter.cs                   |  5 ++---
 src/Lucene.Net/Support/Threading/ThreadClass.cs       | 19 +++++++++++++++++++
 4 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/src/Lucene.Net.TestFramework/Store/MockDirectoryWrapper.cs b/src/Lucene.Net.TestFramework/Store/MockDirectoryWrapper.cs
index 1994084..c8063a6 100644
--- a/src/Lucene.Net.TestFramework/Store/MockDirectoryWrapper.cs
+++ b/src/Lucene.Net.TestFramework/Store/MockDirectoryWrapper.cs
@@ -580,7 +580,11 @@ namespace Lucene.Net.Store
         {
             if (RandomState.NextBoolean())
             {
+#if NETSTANDARD1_6
                 Thread.Sleep(0);
+#else
+                Thread.Yield();
+#endif
             }
         }
 
diff --git a/src/Lucene.Net.Tests/Index/TestIndexWriter.cs b/src/Lucene.Net.Tests/Index/TestIndexWriter.cs
index 963dc7d..9340d5f 100644
--- a/src/Lucene.Net.Tests/Index/TestIndexWriter.cs
+++ b/src/Lucene.Net.Tests/Index/TestIndexWriter.cs
@@ -1350,7 +1350,7 @@ namespace Lucene.Net.Index
                         Console.WriteLine("TEST: now rollback");
                     }
                     // clear interrupt state:
-                    //Thread.interrupted();
+                    ThreadClass.Interrupted();
                     if (w != null)
                     {
                         try
@@ -1438,10 +1438,6 @@ namespace Lucene.Net.Index
 #endif
 #endif
 
-#if NETCOREAPP2_0
-            fail("LUCENENET TODO: Uncaught exceptions on background thread causing test runner
crash");
-#endif
-
             IndexerThreadInterrupt t = new IndexerThreadInterrupt(this);
             t.SetDaemon(true);
             t.Start();
@@ -1484,10 +1480,6 @@ namespace Lucene.Net.Index
         [Test]
         public virtual void TestTwoThreadsInterruptDeadlock()
         {
-#if NETCOREAPP2_0
-            fail("LUCENENET TODO: Uncaught exceptions on background thread causing test runner
crash");
-#endif
-
             IndexerThreadInterrupt t1 = new IndexerThreadInterrupt(this);
             t1.SetDaemon(true);
             t1.Start();
diff --git a/src/Lucene.Net/Index/IndexWriter.cs b/src/Lucene.Net/Index/IndexWriter.cs
index 9811ee3..53496bd 100644
--- a/src/Lucene.Net/Index/IndexWriter.cs
+++ b/src/Lucene.Net/Index/IndexWriter.cs
@@ -1,4 +1,5 @@
 using Lucene.Net.Support;
+using Lucene.Net.Support.Threading;
 using System;
 using System.Collections.Concurrent;
 using System.Collections.Generic;
@@ -1180,9 +1181,7 @@ namespace Lucene.Net.Index
                     try
                     {
                         // clean up merge scheduler in all cases, although flushing may have
failed:
-                        //interrupted = Thread.Interrupted();
-                        //LUCENE TO-DO
-                        interrupted = false;
+                        interrupted = ThreadClass.Interrupted();
 
                         if (waitForMerges)
                         {
diff --git a/src/Lucene.Net/Support/Threading/ThreadClass.cs b/src/Lucene.Net/Support/Threading/ThreadClass.cs
index cdb3cb2..f959a4a 100644
--- a/src/Lucene.Net/Support/Threading/ThreadClass.cs
+++ b/src/Lucene.Net/Support/Threading/ThreadClass.cs
@@ -300,6 +300,25 @@ namespace Lucene.Net.Support.Threading
             return This;
         }
 
+        /// <summary>
+        /// LUCENENET specific.
+        /// Java has Thread.interrupted() which returns, and clears, the interrupt
+        /// flag of the current thread. .NET has no such method, so we're calling
+        /// Thread.Sleep to provoke the exception which will also clear the flag.
+        /// </summary>
+        /// <returns></returns>
+        internal static bool Interrupted() {
+#if !NETSTANDARD1_6
+            try {
+                Thread.Sleep(0);
+            } catch (ThreadInterruptedException) {
+                return true;
+            }
+#endif
+
+            return false;
+        }
+
         public static bool operator ==(ThreadClass t1, object t2)
         {
             if (((object)t1) == null) return t2 == null;


Mime
View raw message