You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2020/06/30 21:58:54 UTC

[lucenenet] 07/09: Added a global Lucene.Net.Diagnostics.Debugging.AssertsEnabled static property that can be used to toggle "asserts" on and off in the release build, similar to how it works in Java. The setting can be injected by end users with the "assert" system property (which is a boolean). (#301)

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 2b8277412025acf7d9a8954507a76ae80e218fda
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Tue Jun 30 16:40:51 2020 +0700

    Added a global Lucene.Net.Diagnostics.Debugging.AssertsEnabled static property that can be used to toggle "asserts" on and off in the release build, similar to how it works in Java. The setting can be injected by end users with the "assert" system property (which is a boolean). (#301)
---
 .../Support/Diagnostics/Debug.cs                   |  4 +-
 .../Support/Util/LuceneTestFrameworkInitializer.cs |  6 +++
 src/Lucene.Net/Index/DocumentsWriterPerThread.cs   |  6 ++-
 .../Index/FreqProxTermsWriterPerField.cs           |  6 ++-
 src/Lucene.Net/Index/IndexWriter.cs                | 44 +++++++++++-----------
 src/Lucene.Net/Index/StoredFieldsProcessor.cs      |  9 +++--
 src/Lucene.Net/Index/TermVectorsConsumer.cs        |  6 ++-
 .../Index/TermVectorsConsumerPerField.cs           |  9 +++--
 src/Lucene.Net/Support/Diagnostics/Debugging.cs    | 20 ++++++++++
 9 files changed, 74 insertions(+), 36 deletions(-)

diff --git a/src/Lucene.Net.TestFramework/Support/Diagnostics/Debug.cs b/src/Lucene.Net.TestFramework/Support/Diagnostics/Debug.cs
index 9257d43..e8d9a9d 100644
--- a/src/Lucene.Net.TestFramework/Support/Diagnostics/Debug.cs
+++ b/src/Lucene.Net.TestFramework/Support/Diagnostics/Debug.cs
@@ -28,7 +28,7 @@
         /// <param name="condition">The conditional expression to evaluate. If the condition is <c>true</c>, no exception is thrown.</param>
         public static void Assert(bool condition)
         {
-            if (!condition)
+            if (Debugging.AssertsEnabled && !condition)
                 throw new AssertionException();
         }
 
@@ -39,7 +39,7 @@
         /// <param name="message">The message to use </param>
         public static void Assert(bool condition, string message)
         {
-            if (!condition)
+            if (Debugging.AssertsEnabled && !condition)
                 throw new AssertionException(message);
         }
     }
diff --git a/src/Lucene.Net.TestFramework/Support/Util/LuceneTestFrameworkInitializer.cs b/src/Lucene.Net.TestFramework/Support/Util/LuceneTestFrameworkInitializer.cs
index 2e3ae58..8fb70b0 100644
--- a/src/Lucene.Net.TestFramework/Support/Util/LuceneTestFrameworkInitializer.cs
+++ b/src/Lucene.Net.TestFramework/Support/Util/LuceneTestFrameworkInitializer.cs
@@ -201,6 +201,12 @@ namespace Lucene.Net.Util
                 DocValuesFormat.SetDocValuesFormatFactory(DocValuesFormatFactory);
                 PostingsFormat.SetPostingsFormatFactory(PostingsFormatFactory);
 
+                // Enable "asserts" for tests. In Java, these were actual asserts,
+                // but in .NET we simply mock this as a boolean static setting that can be
+                // toggled on and off, even in release mode. Note this must be done after
+                // the ConfigurationFactory is set.
+                Lucene.Net.Diagnostics.Debugging.AssertsEnabled = SystemProperties.GetPropertyAsBoolean("assert", true);
+
                 IncrementInitalizationCount(); // For testing
 
                 return new object(); // Placeholder to indicate our initializer has been run already
diff --git a/src/Lucene.Net/Index/DocumentsWriterPerThread.cs b/src/Lucene.Net/Index/DocumentsWriterPerThread.cs
index 13244fc..aba54f9 100644
--- a/src/Lucene.Net/Index/DocumentsWriterPerThread.cs
+++ b/src/Lucene.Net/Index/DocumentsWriterPerThread.cs
@@ -276,7 +276,8 @@ namespace Lucene.Net.Index
 
         public virtual void UpdateDocument(IEnumerable<IIndexableField> doc, Analyzer analyzer, Term delTerm)
         {
-            Debug.Assert(TestPoint("DocumentsWriterPerThread addDocument start"));
+            // LUCENENET: .NET doesn't support asserts in release mode
+            if (Lucene.Net.Diagnostics.Debugging.AssertsEnabled) TestPoint("DocumentsWriterPerThread addDocument start");
             Debug.Assert(deleteQueue != null);
             docState.doc = doc;
             docState.analyzer = analyzer;
@@ -332,7 +333,8 @@ namespace Lucene.Net.Index
 
         public virtual int UpdateDocuments(IEnumerable<IEnumerable<IIndexableField>> docs, Analyzer analyzer, Term delTerm)
         {
-            Debug.Assert(TestPoint("DocumentsWriterPerThread addDocuments start"));
+            // LUCENENET: .NET doesn't support asserts in release mode
+            if (Lucene.Net.Diagnostics.Debugging.AssertsEnabled) TestPoint("DocumentsWriterPerThread addDocuments start");
             Debug.Assert(deleteQueue != null);
             docState.analyzer = analyzer;
             if (INFO_VERBOSE && infoStream.IsEnabled("DWPT"))
diff --git a/src/Lucene.Net/Index/FreqProxTermsWriterPerField.cs b/src/Lucene.Net/Index/FreqProxTermsWriterPerField.cs
index be3a2d6..7bf54a2 100644
--- a/src/Lucene.Net/Index/FreqProxTermsWriterPerField.cs
+++ b/src/Lucene.Net/Index/FreqProxTermsWriterPerField.cs
@@ -199,7 +199,8 @@ namespace Lucene.Net.Index
         {
             // First time we're seeing this term since the last
             // flush
-            Debug.Assert(docState.TestPoint("FreqProxTermsWriterPerField.newTerm start"));
+            // LUCENENET: .NET doesn't support asserts in release mode
+            if (Lucene.Net.Diagnostics.Debugging.AssertsEnabled) docState.TestPoint("FreqProxTermsWriterPerField.newTerm start");
 
             FreqProxPostingsArray postings = (FreqProxPostingsArray)termsHashPerField.postingsArray;
             postings.lastDocIDs[termID] = docState.docID;
@@ -230,7 +231,8 @@ namespace Lucene.Net.Index
 
         internal override void AddTerm(int termID)
         {
-            Debug.Assert(docState.TestPoint("FreqProxTermsWriterPerField.addTerm start"));
+            // LUCENENET: .NET doesn't support asserts in release mode
+            if (Lucene.Net.Diagnostics.Debugging.AssertsEnabled) docState.TestPoint("FreqProxTermsWriterPerField.addTerm start");
 
             FreqProxPostingsArray postings = (FreqProxPostingsArray)termsHashPerField.postingsArray;
 
diff --git a/src/Lucene.Net/Index/IndexWriter.cs b/src/Lucene.Net/Index/IndexWriter.cs
index 51409e1..4d4d516 100644
--- a/src/Lucene.Net/Index/IndexWriter.cs
+++ b/src/Lucene.Net/Index/IndexWriter.cs
@@ -2573,8 +2573,8 @@ namespace Lucene.Net.Index
                         infoStream.Message("IW", "rollback: infos=" + SegString(segmentInfos.Segments));
                     }
 
-                    var tpResult = TestPoint("rollback before checkpoint");
-                    Debug.Assert(tpResult);
+                    // LUCENENET: .NET doesn't support asserts in release mode
+                    if (Lucene.Net.Diagnostics.Debugging.AssertsEnabled) TestPoint("rollback before checkpoint");
 
                     // Ask deleter to locate unreferenced files & remove
                     // them:
@@ -3541,8 +3541,8 @@ namespace Lucene.Net.Index
                 }
 
                 DoBeforeFlush();
-                var tpResult = TestPoint("startDoFlush");
-                Debug.Assert(tpResult);
+                // LUCENENET: .NET doesn't support asserts in release mode
+                if (Lucene.Net.Diagnostics.Debugging.AssertsEnabled) TestPoint("startDoFlush");
                 SegmentInfos toCommit = null;
                 bool anySegmentsFlushed = false;
 
@@ -3858,8 +3858,8 @@ namespace Lucene.Net.Index
             }
 
             DoBeforeFlush();
-            var tpResult = TestPoint("startDoFlush");
-            Debug.Assert(tpResult);
+            // LUCENENET: .NET doesn't support asserts in release mode
+            if (Lucene.Net.Diagnostics.Debugging.AssertsEnabled) TestPoint("startDoFlush");
             bool success = false;
             try
             {
@@ -4101,8 +4101,8 @@ namespace Lucene.Net.Index
         {
             lock (this)
             {
-                var tpResult = TestPoint("startCommitMergeDeletes");
-                Debug.Assert(tpResult);
+                // LUCENENET: .NET doesn't support asserts in release mode
+                if (Lucene.Net.Diagnostics.Debugging.AssertsEnabled) TestPoint("startCommitMergeDeletes");
 
                 IList<SegmentCommitInfo> sourceSegments = merge.Segments;
 
@@ -4338,8 +4338,8 @@ namespace Lucene.Net.Index
         {
             lock (this)
             {
-                var tpResult = TestPoint("startCommitMerge");
-                Debug.Assert(tpResult);
+                // LUCENENET: .NET doesn't support asserts in release mode
+                if (Lucene.Net.Diagnostics.Debugging.AssertsEnabled) TestPoint("startCommitMerge");
 
                 if (hitOOM)
                 {
@@ -4759,8 +4759,8 @@ namespace Lucene.Net.Index
         {
             lock (this)
             {
-                var testPointResult = TestPoint("startMergeInit");
-                Debug.Assert(testPointResult);
+                // LUCENENET: .NET doesn't support asserts in release mode
+                if (Lucene.Net.Diagnostics.Debugging.AssertsEnabled) TestPoint("startMergeInit");
 
                 Debug.Assert(merge.registerDone);
                 Debug.Assert(merge.MaxNumSegments == -1 || merge.MaxNumSegments > 0);
@@ -5446,8 +5446,8 @@ namespace Lucene.Net.Index
         /// </summary>
         private void StartCommit(SegmentInfos toSync)
         {
-            var tpResult = TestPoint("startStartCommit");
-            Debug.Assert(tpResult);
+            // LUCENENET: .NET doesn't support asserts in release mode
+            if (Lucene.Net.Diagnostics.Debugging.AssertsEnabled) TestPoint("startStartCommit");
             Debug.Assert(pendingCommit == null);
 
             if (hitOOM)
@@ -5485,15 +5485,15 @@ namespace Lucene.Net.Index
                     Debug.Assert(FilesExist(toSync));
                 }
 
-                tpResult = TestPoint("midStartCommit");
-                Debug.Assert(tpResult);
+                // LUCENENET: .NET doesn't support asserts in release mode
+                if (Lucene.Net.Diagnostics.Debugging.AssertsEnabled) TestPoint("midStartCommit");
 
                 bool pendingCommitSet = false;
 
                 try
                 {
-                    tpResult = TestPoint("midStartCommit2");
-                    Debug.Assert(tpResult);
+                    // LUCENENET: .NET doesn't support asserts in release mode
+                    if (Lucene.Net.Diagnostics.Debugging.AssertsEnabled) TestPoint("midStartCommit2");
 
                     lock (this)
                     {
@@ -5536,8 +5536,8 @@ namespace Lucene.Net.Index
                         infoStream.Message("IW", "done all syncs: " + string.Format(J2N.Text.StringFormatter.InvariantCulture, "{0}", filesToSync));
                     }
 
-                    tpResult = TestPoint("midStartCommitSuccess");
-                    Debug.Assert(tpResult);
+                    // LUCENENET: .NET doesn't support asserts in release mode
+                    if (Lucene.Net.Diagnostics.Debugging.AssertsEnabled) TestPoint("midStartCommitSuccess");
                 }
                 finally
                 {
@@ -5567,8 +5567,8 @@ namespace Lucene.Net.Index
             {
                 HandleOOM(oom, "startCommit");
             }
-            tpResult = TestPoint("finishStartCommit");
-            Debug.Assert(tpResult);
+            // LUCENENET: .NET doesn't support asserts in release mode
+            if (Lucene.Net.Diagnostics.Debugging.AssertsEnabled) TestPoint("finishStartCommit");
         }
 
         /// <summary>
diff --git a/src/Lucene.Net/Index/StoredFieldsProcessor.cs b/src/Lucene.Net/Index/StoredFieldsProcessor.cs
index 57eabd4..aac5b12 100644
--- a/src/Lucene.Net/Index/StoredFieldsProcessor.cs
+++ b/src/Lucene.Net/Index/StoredFieldsProcessor.cs
@@ -139,7 +139,8 @@ namespace Lucene.Net.Index
         [MethodImpl(MethodImplOptions.NoInlining)]
         internal override void FinishDocument()
         {
-            Debug.Assert(docWriter.TestPoint("StoredFieldsWriter.finishDocument start"));
+            // LUCENENET: .NET doesn't support asserts in release mode
+            if (Lucene.Net.Diagnostics.Debugging.AssertsEnabled) docWriter.TestPoint("StoredFieldsWriter.finishDocument start");
 
             InitFieldsWriter(IOContext.DEFAULT);
             Fill(docState.docID);
@@ -156,7 +157,8 @@ namespace Lucene.Net.Index
             }
 
             Reset();
-            Debug.Assert(docWriter.TestPoint("StoredFieldsWriter.finishDocument end"));
+            // LUCENENET: .NET doesn't support asserts in release mode
+            if (Lucene.Net.Diagnostics.Debugging.AssertsEnabled) docWriter.TestPoint("StoredFieldsWriter.finishDocument end");
         }
 
         public override void AddField(int docID, IIndexableField field, FieldInfo fieldInfo)
@@ -179,7 +181,8 @@ namespace Lucene.Net.Index
                 fieldInfos[numStoredFields] = fieldInfo;
                 numStoredFields++;
 
-                Debug.Assert(docState.TestPoint("StoredFieldsWriterPerThread.processFields.writeField"));
+                // LUCENENET: .NET doesn't support asserts in release mode
+                if (Lucene.Net.Diagnostics.Debugging.AssertsEnabled) docState.TestPoint("StoredFieldsWriterPerThread.processFields.writeField");
             }
         }
     }
diff --git a/src/Lucene.Net/Index/TermVectorsConsumer.cs b/src/Lucene.Net/Index/TermVectorsConsumer.cs
index 504ba38..492c90a 100644
--- a/src/Lucene.Net/Index/TermVectorsConsumer.cs
+++ b/src/Lucene.Net/Index/TermVectorsConsumer.cs
@@ -114,7 +114,8 @@ namespace Lucene.Net.Index
         [MethodImpl(MethodImplOptions.NoInlining)]
         internal override void FinishDocument(TermsHash termsHash)
         {
-            Debug.Assert(docWriter.TestPoint("TermVectorsTermsWriter.finishDocument start"));
+            // LUCENENET: .NET doesn't support asserts in release mode
+            if (Lucene.Net.Diagnostics.Debugging.AssertsEnabled) docWriter.TestPoint("TermVectorsTermsWriter.finishDocument start");
 
             if (!hasVectors)
             {
@@ -139,7 +140,8 @@ namespace Lucene.Net.Index
 
             termsHash.Reset();
             Reset();
-            Debug.Assert(docWriter.TestPoint("TermVectorsTermsWriter.finishDocument end"));
+            // LUCENENET: .NET doesn't support asserts in release mode
+            if (Lucene.Net.Diagnostics.Debugging.AssertsEnabled) docWriter.TestPoint("TermVectorsTermsWriter.finishDocument end");
         }
 
         [MethodImpl(MethodImplOptions.NoInlining)]
diff --git a/src/Lucene.Net/Index/TermVectorsConsumerPerField.cs b/src/Lucene.Net/Index/TermVectorsConsumerPerField.cs
index 2e9bec0..6dfd8c2 100644
--- a/src/Lucene.Net/Index/TermVectorsConsumerPerField.cs
+++ b/src/Lucene.Net/Index/TermVectorsConsumerPerField.cs
@@ -172,7 +172,8 @@ namespace Lucene.Net.Index
         [MethodImpl(MethodImplOptions.NoInlining)]
         internal void FinishDocument()
         {
-            Debug.Assert(docState.TestPoint("TermVectorsTermsWriterPerField.finish start"));
+            // LUCENENET: .NET doesn't support asserts in release mode
+            if (Lucene.Net.Diagnostics.Debugging.AssertsEnabled) docState.TestPoint("TermVectorsTermsWriterPerField.finish start");
 
             int numPostings = termsHashPerField.bytesHash.Count;
 
@@ -301,7 +302,8 @@ namespace Lucene.Net.Index
 
         internal override void NewTerm(int termID)
         {
-            Debug.Assert(docState.TestPoint("TermVectorsTermsWriterPerField.newTerm start"));
+            // LUCENENET: .NET doesn't support asserts in release mode
+            if (Lucene.Net.Diagnostics.Debugging.AssertsEnabled) docState.TestPoint("TermVectorsTermsWriterPerField.newTerm start");
             TermVectorsPostingsArray postings = (TermVectorsPostingsArray)termsHashPerField.postingsArray;
 
             postings.freqs[termID] = 1;
@@ -313,7 +315,8 @@ namespace Lucene.Net.Index
 
         internal override void AddTerm(int termID)
         {
-            Debug.Assert(docState.TestPoint("TermVectorsTermsWriterPerField.addTerm start"));
+            // LUCENENET: .NET doesn't support asserts in release mode
+            if (Lucene.Net.Diagnostics.Debugging.AssertsEnabled) docState.TestPoint("TermVectorsTermsWriterPerField.addTerm start");
             TermVectorsPostingsArray postings = (TermVectorsPostingsArray)termsHashPerField.postingsArray;
 
             postings.freqs[termID]++;
diff --git a/src/Lucene.Net/Support/Diagnostics/Debugging.cs b/src/Lucene.Net/Support/Diagnostics/Debugging.cs
new file mode 100644
index 0000000..0a02efc
--- /dev/null
+++ b/src/Lucene.Net/Support/Diagnostics/Debugging.cs
@@ -0,0 +1,20 @@
+using Lucene.Net.Util;
+
+namespace Lucene.Net.Diagnostics
+{
+    // LUCENENET: This can only be named Debug if we merge it with the Debug
+    // class from Lucene.Net.TestFramework because it is in the same namespace.
+    // But that class is dependent upon AssertionException, which is only for testing.
+    internal static class Debugging
+    {
+        /// <summary>
+        /// Allows toggling "assertions" on/off even in release builds. The default is <c>false</c>.
+        /// <para/>
+        /// This allows loggers and testing frameworks to enable test point messages ("TP")
+        /// from <see cref="Index.IndexWriter"/>, <see cref="Index.DocumentsWriterPerThread"/>,
+        /// <see cref="Index.FreqProxTermsWriterPerField"/>, <see cref="Index.StoredFieldsProcessor"/>,
+        /// <see cref="Index.TermVectorsConsumer"/>, and <see cref="Index.TermVectorsConsumerPerField"/>.
+        /// </summary>
+        public static bool AssertsEnabled { get; set; } = SystemProperties.GetPropertyAsBoolean("assert", false);
+    }
+}