lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nightowl...@apache.org
Subject [lucenenet] 02/07: Lucene.Net.TestFramework, Lucene.Net.Tests.Index.TestIndexWriterWithThreads: Factored out AtomicObject and replaced with J2N.Threading.Atomic.AtomicReference
Date Sun, 15 Dec 2019 21:19:06 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 94f33e93b405fca3ff5ce069a8d93d91a4caf5ee
Author: Shad Storhaug <shad@shadstorhaug.com>
AuthorDate: Mon Dec 16 00:25:18 2019 +0700

    Lucene.Net.TestFramework, Lucene.Net.Tests.Index.TestIndexWriterWithThreads: Factored
out AtomicObject and replaced with J2N.Threading.Atomic.AtomicReference
---
 .../Index/BaseStoredFieldsFormatTestCase.cs        |  7 +--
 .../Index/BaseTermVectorsFormatTestCase.cs         |  7 +--
 .../Lucene.Net.TestFramework.csproj                |  1 +
 .../Index/TestIndexWriterWithThreads.cs            | 18 ++++---
 src/Lucene.Net/Support/AtomicObject.cs             | 59 ----------------------
 5 files changed, 19 insertions(+), 73 deletions(-)

diff --git a/src/Lucene.Net.TestFramework/Index/BaseStoredFieldsFormatTestCase.cs b/src/Lucene.Net.TestFramework/Index/BaseStoredFieldsFormatTestCase.cs
index 616b0cb..8423aed 100644
--- a/src/Lucene.Net.TestFramework/Index/BaseStoredFieldsFormatTestCase.cs
+++ b/src/Lucene.Net.TestFramework/Index/BaseStoredFieldsFormatTestCase.cs
@@ -13,6 +13,7 @@ using System.Text;
 using Console = Lucene.Net.Support.SystemConsole;
 using Assert = Lucene.Net.TestFramework.Assert;
 using Lucene.Net.TestFramework;
+using J2N.Threading.Atomic;
 
 #if TESTFRAMEWORK_MSTEST
 using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute;
@@ -533,7 +534,7 @@ namespace Lucene.Net.Index
                     }
                     iw.Commit();
 
-                    AtomicObject<Exception> ex = new AtomicObject<Exception>();
+                    AtomicReference<Exception> ex = new AtomicReference<Exception>();
                     using (DirectoryReader rd = DirectoryReader.Open(dir))
                     {
                         IndexSearcher searcher = new IndexSearcher(rd);
@@ -569,11 +570,11 @@ namespace Lucene.Net.Index
             private readonly DirectoryReader rd;
             private readonly IndexSearcher searcher;
             private int readsPerThread;
-            private AtomicObject<Exception> ex;
+            private AtomicReference<Exception> ex;
             private int i;
             private readonly int[] queries;
 
-            public ThreadAnonymousInnerClassHelper(int numDocs, DirectoryReader rd, IndexSearcher
searcher, int readsPerThread, AtomicObject<Exception> ex, int i)
+            public ThreadAnonymousInnerClassHelper(int numDocs, DirectoryReader rd, IndexSearcher
searcher, int readsPerThread, AtomicReference<Exception> ex, int i)
             {
                 this.numDocs = numDocs;
                 this.rd = rd;
diff --git a/src/Lucene.Net.TestFramework/Index/BaseTermVectorsFormatTestCase.cs b/src/Lucene.Net.TestFramework/Index/BaseTermVectorsFormatTestCase.cs
index c7d752a..efc8fa9 100644
--- a/src/Lucene.Net.TestFramework/Index/BaseTermVectorsFormatTestCase.cs
+++ b/src/Lucene.Net.TestFramework/Index/BaseTermVectorsFormatTestCase.cs
@@ -1,3 +1,4 @@
+using J2N.Threading.Atomic;
 using Lucene.Net.Analysis.TokenAttributes;
 using Lucene.Net.Codecs;
 using Lucene.Net.Documents;
@@ -928,7 +929,7 @@ namespace Lucene.Net.Index
                 {
                     docs[i] = docFactory.NewDocument(TestUtil.NextInt32(Random, 1, 3), AtLeast(10),
options);
                 }
-                AtomicObject<Exception> exception = new AtomicObject<Exception>();
+                AtomicReference<Exception> exception = new AtomicReference<Exception>();
                 using (Directory dir = NewDirectory())
                 using (RandomIndexWriter writer = new RandomIndexWriter(
 #if FEATURE_INSTANCE_TESTDATA_INITIALIZATION
@@ -974,10 +975,10 @@ namespace Lucene.Net.Index
             private int numDocs;
             private RandomDocument[] docs;
             private IndexReader reader;
-            private AtomicObject<Exception> exception;
+            private AtomicReference<Exception> exception;
             private int i;
 
-            public ThreadAnonymousInnerClassHelper(BaseTermVectorsFormatTestCase outerInstance,
int numDocs, RandomDocument[] docs, IndexReader reader, AtomicObject<Exception> exception,
int i)
+            public ThreadAnonymousInnerClassHelper(BaseTermVectorsFormatTestCase outerInstance,
int numDocs, RandomDocument[] docs, IndexReader reader, AtomicReference<Exception> exception,
int i)
             {
                 this.outerInstance = outerInstance;
                 this.numDocs = numDocs;
diff --git a/src/Lucene.Net.TestFramework/Lucene.Net.TestFramework.csproj b/src/Lucene.Net.TestFramework/Lucene.Net.TestFramework.csproj
index eb65fa4..a137af3 100644
--- a/src/Lucene.Net.TestFramework/Lucene.Net.TestFramework.csproj
+++ b/src/Lucene.Net.TestFramework/Lucene.Net.TestFramework.csproj
@@ -54,6 +54,7 @@
   </ItemGroup>
 
   <ItemGroup>
+    <PackageReference Include="J2N" Version="$(J2NPackageVersion)" />
     <PackageReference Include="NUnit" Version="$(NUnitPackageVersion)" />
   </ItemGroup>
 
diff --git a/src/Lucene.Net.Tests/Index/TestIndexWriterWithThreads.cs b/src/Lucene.Net.Tests/Index/TestIndexWriterWithThreads.cs
index 0ed4afa..41dcb0f 100644
--- a/src/Lucene.Net.Tests/Index/TestIndexWriterWithThreads.cs
+++ b/src/Lucene.Net.Tests/Index/TestIndexWriterWithThreads.cs
@@ -1,3 +1,4 @@
+using J2N.Threading.Atomic;
 using Lucene.Net.Attributes;
 using Lucene.Net.Documents;
 using Lucene.Net.Randomized.Generators;
@@ -9,6 +10,7 @@ using NUnit.Framework;
 using System;
 using System.IO;
 using System.Threading;
+using AtomicBoolean = J2N.Threading.Atomic.AtomicBoolean;
 using Console = Lucene.Net.Support.SystemConsole;
 
 namespace Lucene.Net.Index
@@ -674,13 +676,13 @@ namespace Lucene.Net.Index
 
             MockAnalyzer analyzer = new MockAnalyzer(Random);
             analyzer.MaxTokenLength = TestUtil.NextInt32(Random, 1, IndexWriter.MAX_TERM_LENGTH);
-            AtomicObject<IndexWriter> writerRef =
-                new AtomicObject<IndexWriter>(new IndexWriter(d, NewIndexWriterConfig(TEST_VERSION_CURRENT,
analyzer)));
+            AtomicReference<IndexWriter> writerRef =
+                new AtomicReference<IndexWriter>(new IndexWriter(d, NewIndexWriterConfig(TEST_VERSION_CURRENT,
analyzer)));
 
             LineFileDocs docs = new LineFileDocs(Random);
             ThreadClass[] threads = new ThreadClass[threadCount];
             int iters = AtLeast(100);
-            AtomicBoolean failed = new AtomicBoolean();
+            AtomicBoolean failed = new J2N.Threading.Atomic.AtomicBoolean();
             ReentrantLock rollbackLock = new ReentrantLock();
             ReentrantLock commitLock = new ReentrantLock();
             for (int threadID = 0; threadID < threadCount; threadID++)
@@ -701,7 +703,7 @@ namespace Lucene.Net.Index
                 }
             }
 
-            Assert.IsTrue(!failed.Get());
+            Assert.IsTrue(!failed.Value);
             writerRef.Value.Dispose();
             d.Dispose();
         }
@@ -711,14 +713,14 @@ namespace Lucene.Net.Index
             private readonly TestIndexWriterWithThreads OuterInstance;
 
             private BaseDirectoryWrapper d;
-            private AtomicObject<IndexWriter> WriterRef;
+            private AtomicReference<IndexWriter> WriterRef;
             private LineFileDocs Docs;
             private int Iters;
             private AtomicBoolean Failed;
             private ReentrantLock RollbackLock;
             private ReentrantLock CommitLock;
 
-            public ThreadAnonymousInnerClassHelper(TestIndexWriterWithThreads outerInstance,
BaseDirectoryWrapper d, AtomicObject<IndexWriter> writerRef, LineFileDocs docs, int
iters, AtomicBoolean failed, ReentrantLock rollbackLock, ReentrantLock commitLock)
+            public ThreadAnonymousInnerClassHelper(TestIndexWriterWithThreads outerInstance,
BaseDirectoryWrapper d, AtomicReference<IndexWriter> writerRef, LineFileDocs docs, int
iters, AtomicBoolean failed, ReentrantLock rollbackLock, ReentrantLock commitLock)
             {
                 this.OuterInstance = outerInstance;
                 this.d = d;
@@ -732,7 +734,7 @@ namespace Lucene.Net.Index
 
             public override void Run()
             {
-                for (int iter = 0; iter < Iters && !Failed.Get(); iter++)
+                for (int iter = 0; iter < Iters && !Failed.Value; iter++)
                 {
                     //final int x = Random().nextInt(5);
                     int x = Random.Next(3);
@@ -820,7 +822,7 @@ namespace Lucene.Net.Index
                     }
                     catch (Exception t)
                     {
-                        Failed.Set(true);
+                        Failed.Value = (true);
                         throw new Exception(t.Message, t);
                     }
                 }
diff --git a/src/Lucene.Net/Support/AtomicObject.cs b/src/Lucene.Net/Support/AtomicObject.cs
deleted file mode 100644
index 84c8c0d..0000000
--- a/src/Lucene.Net/Support/AtomicObject.cs
+++ /dev/null
@@ -1,59 +0,0 @@
-´╗┐using System;
-using System.Threading;
-
-namespace Lucene.Net.Support
-{
-    /*
-	 * Licensed to the Apache Software Foundation (ASF) under one or more
-	 * contributor license agreements.  See the NOTICE file distributed with
-	 * this work for additional information regarding copyright ownership.
-	 * The ASF licenses this file to You under the Apache License, Version 2.0
-	 * (the "License"); you may not use this file except in compliance with
-	 * the License.  You may obtain a copy of the License at
-	 *
-	 *     http://www.apache.org/licenses/LICENSE-2.0
-	 *
-	 * Unless required by applicable law or agreed to in writing, software
-	 * distributed under the License is distributed on an "AS IS" BASIS,
-	 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-	 * See the License for the specific language governing permissions and
-	 * limitations under the License.
-	 */
-
-#if FEATURE_SERIALIZABLE
-    [Serializable]
-#endif
-    public class AtomicObject<T> where T : class
-    {
-        private T _value;
-        private ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
-
-        public AtomicObject() : this(default(T))
-        { }
-
-        public AtomicObject(T initial)
-        {
-            _value = initial;
-        }
-
-        public T Value
-        {
-            get
-            {
-                try
-                {
-                    _lock.EnterReadLock();
-                    return _value;
-                }
-                finally
-                {
-                    _lock.ExitReadLock();
-                }
-            }
-            set
-            {
-                Interlocked.Exchange(ref _value, value);
-            }
-        }
-    }
-}


Mime
View raw message