lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nightowl...@apache.org
Subject [lucenenet] 03/03: Lucene.Net.TestFramework: Implemented dispose pattern where applicable
Date Tue, 16 Jul 2019 16:39:02 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 472cc20d185e2d9f660553287fd3f98c96771f2c
Author: Shad Storhaug <shad@shadstorhaug.com>
AuthorDate: Tue Jul 16 08:44:12 2019 +0700

    Lucene.Net.TestFramework: Implemented dispose pattern where applicable
---
 .../Index/RandomIndexWriter.cs                     | 28 ++++++++++++++++------
 .../Randomized/RandomizedContext.cs                |  7 ++++++
 .../Randomized/Randomness.cs                       | 13 ++++++++--
 .../Randomized/SingleThreadedRandom.cs             |  5 ++--
 .../Randomized/ThreadGroup.cs                      | 14 +++++++++--
 src/Lucene.Net.TestFramework/Util/LineFileDocs.cs  | 19 +++++++++++----
 6 files changed, 69 insertions(+), 17 deletions(-)

diff --git a/src/Lucene.Net.TestFramework/Index/RandomIndexWriter.cs b/src/Lucene.Net.TestFramework/Index/RandomIndexWriter.cs
index 3a0c0ea..c471f84 100644
--- a/src/Lucene.Net.TestFramework/Index/RandomIndexWriter.cs
+++ b/src/Lucene.Net.TestFramework/Index/RandomIndexWriter.cs
@@ -507,18 +507,32 @@ namespace Lucene.Net.Index
             }
         }
 
+        // LUCENENET specific: Implemented dispose pattern
+
         /// <summary>
-        /// Close this writer. </summary>
-        /// <seealso cref= IndexWriter#close() </seealso>
+        /// Dispose this writer. </summary>
+        /// <seealso cref="IndexWriter.Dispose()"/>
         public void Dispose()
         {
-            // if someone isn't using getReader() API, we want to be sure to
-            // forceMerge since presumably they might open a reader on the dir.
-            if (GetReaderCalled == false && r.Next(8) == 2)
+            Dispose(true);
+            GC.SuppressFinalize(this);
+        }
+
+        /// <summary>
+        /// Dispose this writer. </summary>
+        /// <seealso cref="IndexWriter.Dispose(bool)"/>
+        protected virtual void Dispose(bool disposing)
+        {
+            if (disposing)
             {
-                DoRandomForceMerge();
+                // if someone isn't using getReader() API, we want to be sure to
+                // forceMerge since presumably they might open a reader on the dir.
+                if (GetReaderCalled == false && r.Next(8) == 2)
+                {
+                    DoRandomForceMerge();
+                }
+                w.Dispose();
             }
-            w.Dispose();
         }
 
         /// <summary>
diff --git a/src/Lucene.Net.TestFramework/Randomized/RandomizedContext.cs b/src/Lucene.Net.TestFramework/Randomized/RandomizedContext.cs
index e853f6a..fb1650d 100644
--- a/src/Lucene.Net.TestFramework/Randomized/RandomizedContext.cs
+++ b/src/Lucene.Net.TestFramework/Randomized/RandomizedContext.cs
@@ -167,8 +167,15 @@ namespace Lucene.Net.Randomized
             }
         }
 
+        // LUCENENET specific: Implemented dispose pattern
         public void Dispose()
         {
+            Dispose(true);
+            GC.SuppressFinalize(this);
+        }
+
+        protected virtual void Dispose(bool disposing)
+        {
         }
     }
 }
\ No newline at end of file
diff --git a/src/Lucene.Net.TestFramework/Randomized/Randomness.cs b/src/Lucene.Net.TestFramework/Randomized/Randomness.cs
index 8f4efd5..0f13322 100644
--- a/src/Lucene.Net.TestFramework/Randomized/Randomness.cs
+++ b/src/Lucene.Net.TestFramework/Randomized/Randomness.cs
@@ -87,10 +87,19 @@ namespace Lucene.Net.Randomized
             return result;
         }
 
+        // LUCENENET specific: Implemented dispose pattern
         public void Dispose()
         {
-            if (this.SingleThreadedRandom != null)
-                this.SingleThreadedRandom.Dispose();
+            Dispose(true);
+            GC.SuppressFinalize(this);
+        }
+
+        protected virtual void Dispose(bool disposing)
+        {
+            if (disposing)
+            {
+                SingleThreadedRandom?.Dispose();
+            }
         }
     }
 }
\ No newline at end of file
diff --git a/src/Lucene.Net.TestFramework/Randomized/SingleThreadedRandom.cs b/src/Lucene.Net.TestFramework/Randomized/SingleThreadedRandom.cs
index 09878f7..e4580f2 100644
--- a/src/Lucene.Net.TestFramework/Randomized/SingleThreadedRandom.cs
+++ b/src/Lucene.Net.TestFramework/Randomized/SingleThreadedRandom.cs
@@ -107,14 +107,15 @@ namespace Lucene.Net.Randomized
             }
         }
 
+        // LUCENENET specific: Implemented dispose pattern
+
         public void Dispose()
         {
             this.Dispose(true);
-
             GC.SuppressFinalize(this);
         }
 
-        protected void Dispose(bool disposing)
+        protected virtual void Dispose(bool disposing)
         {
             if (!this.isDisposed)
             {
diff --git a/src/Lucene.Net.TestFramework/Randomized/ThreadGroup.cs b/src/Lucene.Net.TestFramework/Randomized/ThreadGroup.cs
index e39999e..ca1255b 100644
--- a/src/Lucene.Net.TestFramework/Randomized/ThreadGroup.cs
+++ b/src/Lucene.Net.TestFramework/Randomized/ThreadGroup.cs
@@ -121,11 +121,21 @@ namespace Lucene.Net.Randomized
             return this.threads.GetEnumerator();
         }
 
+        // LUCENENET specific: Implemented dispose pattern
         public void Dispose()
         {
-            lock (GroupLock)
+            Dispose(true);
+            GC.SuppressFinalize(this);
+        }
+
+        protected virtual void Dispose(bool disposing)
+        {
+            if (disposing)
             {
-                Groups.Remove(this);
+                lock (GroupLock)
+                {
+                    Groups.Remove(this);
+                }
             }
         }
     }
diff --git a/src/Lucene.Net.TestFramework/Util/LineFileDocs.cs b/src/Lucene.Net.TestFramework/Util/LineFileDocs.cs
index 1a361ba..9cc2826 100644
--- a/src/Lucene.Net.TestFramework/Util/LineFileDocs.cs
+++ b/src/Lucene.Net.TestFramework/Util/LineFileDocs.cs
@@ -71,12 +71,23 @@ namespace Lucene.Net.Util
 
         public void Dispose()
         {
-            lock (this)
+            Dispose(true);
+            GC.SuppressFinalize(this);
+        }
+
+        // LUCENENET specific: Implemented dispose pattern
+        protected virtual void Dispose(bool disposing)
+        {
+            if (disposing)
             {
-                if (Reader != null)
+                lock (this)
                 {
-                    Reader.Dispose();
-                    Reader = null;
+                    ThreadDocs?.Dispose();
+                    if (Reader != null)
+                    {
+                        Reader.Dispose();
+                        Reader = null;
+                    }
                 }
             }
         }


Mime
View raw message