예제 #1
0
        public void AtomicObjectInitialisesWithExpectedNullDefault()
        {
            var o = new AtomicObject <string>();

            Assert.IsNull(o.Value);
            Assert.IsNull((string)o);
        }
예제 #2
0
        public void AtomicObjectToStringReturnsExpectedStringWithSpecifiedValue()
        {
            const string expected = "foo";

            var o = new AtomicObject <string>(expected);

            Assert.AreEqual(expected, o.ToString());
        }
예제 #3
0
        public void AtomicObjectSetsUnderlyingValueMultiThreaded()
        {
            var o = new AtomicObject <string>();

            System.Threading.Tasks.Parallel.For(0, 1000, i => o.Value = i.ToString());

            Assert.IsNotNull(o.Value);
            Assert.IsNotNull((string)o);
        }
예제 #4
0
        public void AtomicObjectInitialisesWithExpectedSpecifiedValue()
        {
            const string expected = "foo";

            var o = new AtomicObject <string>(expected);

            Assert.AreEqual(expected, o.Value);
            Assert.AreEqual(expected, (string)o);
        }
 public ThreadAnonymousInnerClassHelper(BaseTermVectorsFormatTestCase outerInstance, int numDocs, Lucene.Net.Index.BaseTermVectorsFormatTestCase.RandomDocument[] docs, IndexReader reader, AtomicObject <Exception> exception, int i)
 {
     this.OuterInstance = outerInstance;
     this.NumDocs       = numDocs;
     this.Docs          = docs;
     this.Reader        = reader;
     this.ARException   = exception;
     this.i             = i;
 }
예제 #6
0
        public void AtomicObjectCompareAndSetReturnsFalseIfComparisonDoesNotMatch()
        {
            var o = new AtomicObject <string>("bar");

            bool result = o.CompareAndSet("foo", "bar");

            Assert.IsFalse(result);
            Assert.AreEqual("bar", o.Value);
            Assert.AreEqual("bar", (string)o);
        }
예제 #7
0
 public ThreadAnonymousInnerClassHelper(TestIndexWriterWithThreads outerInstance, BaseDirectoryWrapper d, AtomicObject <IndexWriter> writerRef, LineFileDocs docs, int iters, AtomicBoolean failed, ReentrantLock rollbackLock, ReentrantLock commitLock)
 {
     this.OuterInstance = outerInstance;
     this.d             = d;
     this.WriterRef     = writerRef;
     this.Docs          = docs;
     this.Iters         = iters;
     this.Failed        = failed;
     this.RollbackLock  = rollbackLock;
     this.CommitLock    = commitLock;
 }
예제 #8
0
        public void AtomicObjectCompareAndSetReturnsExpectedValuesForFailedMatchesMultiThreaded()
        {
            const int expected = 1000;

            var o = new AtomicObject <string>("foo");

            var values = new ConcurrentCountedSet <bool>();

            System.Threading.Tasks.Parallel.For(0, 1000, i => values.Add(o.CompareAndSet(i % 2 == 0 ? "bar" : "foo", "bar")));

            Assert.AreEqual(expected, values[true] + values[false]);
        }
예제 #9
0
        public void AtomicObjectGetAndSetReturnsOriginalValueMultiThreaded()
        {
            const int expected = 1000;

            ConcurrentCountedSet <string> values = new ConcurrentCountedSet <string>();

            var o = new AtomicObject <string>("foo");

            System.Threading.Tasks.Parallel.For(0, 1000, i => values.Add(o.GetAndSet(i % 2 == 0 ? "bar" : "foo")));

            Assert.AreEqual(expected, values["foo"] + values["bar"]);
        }
예제 #10
0
        // [Test, Timeout(300000)] // LUCENENET NOTE: For now, we are overriding this test in every subclass to pull it into the right context for the subclass
        public virtual void TestConcurrentReads()
        {
            Directory         dir    = NewDirectory();
            IndexWriterConfig iwConf = NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random()));

            iwConf.SetMaxBufferedDocs(RandomInts.NextIntBetween(Random(), 2, 30));
            RandomIndexWriter iw = new RandomIndexWriter(Random(), dir, iwConf);

            // make sure the readers are properly cloned
            Document doc   = new Document();
            Field    field = new StringField("fld", "", Field.Store.YES);

            doc.Add(field);
            int numDocs = AtLeast(1000);

            for (int i = 0; i < numDocs; ++i)
            {
                field.SetStringValue("" + i);
                iw.AddDocument(doc);
            }
            iw.Commit();

            DirectoryReader          rd              = DirectoryReader.Open(dir);
            IndexSearcher            searcher        = new IndexSearcher(rd);
            int                      concurrentReads = AtLeast(5);
            int                      readsPerThread  = AtLeast(50);
            IList <ThreadClass>      readThreads     = new List <ThreadClass>();
            AtomicObject <Exception> ex              = new AtomicObject <Exception>();

            for (int i = 0; i < concurrentReads; ++i)
            {
                readThreads.Add(new ThreadAnonymousInnerClassHelper(numDocs, rd, searcher, readsPerThread, ex, i));
            }
            foreach (ThreadClass thread in readThreads)
            {
                thread.Start();
            }
            foreach (ThreadClass thread in readThreads)
            {
                thread.Join();
            }
            rd.Dispose();
            if (ex.Value != null)
            {
                throw ex.Value;
            }

            iw.Dispose();
            dir.Dispose();
        }
예제 #11
0
        public void AtomicObjectSetsUnderlyingValue()
        {
            const string expected = "999";

            var o = new AtomicObject <string>();

            for (int i = 0; i < 1000; i++)
            {
                o.Value = i.ToString();
            }

            Assert.AreEqual(expected, o.Value);
            Assert.AreEqual(expected, (string)o);
        }
예제 #12
0
        public void AtomicObjectCompareAndSetReturnsExpectedValuesForFailedMatches()
        {
            const int expected = 1000;

            var o = new AtomicObject <string>("foo");

            var values = new ConcurrentCountedSet <bool>();

            for (int i = 0; i < 1000; i++)
            {
                values.Add(o.CompareAndSet(i % 2 == 0 ? "bar" : "foo", "bar"));
            }

            Assert.AreEqual(expected, values[true] + values[false]);
        }
예제 #13
0
            public ThreadAnonymousInnerClassHelper(int numDocs, DirectoryReader rd, IndexSearcher searcher, int readsPerThread, AtomicObject <Exception> ex, int i)
            {
                this.NumDocs        = numDocs;
                this.Rd             = rd;
                this.Searcher       = searcher;
                this.ReadsPerThread = readsPerThread;
                this.Ex             = ex;
                this.i = i;

                queries = new int[ReadsPerThread];
                for (int j = 0; j < queries.Length; ++j)
                {
                    queries[j] = Random().Next(NumDocs);
                }
            }
예제 #14
0
        public void AtomicObjectGetAndSetReturnsOriginalValue()
        {
            const int expected = 1000;

            ConcurrentCountedSet <string> values = new ConcurrentCountedSet <string>();

            var o = new AtomicObject <string>("foo");

            for (int i = 0; i < 1000; i++)
            {
                values.Add(o.GetAndSet(i % 2 == 0 ? "bar" : "foo"));
            }

            Assert.AreEqual(expected, values["foo"] + values["bar"]);
        }
예제 #15
0
        public virtual void TestRollbackAndCommitWithThreads()
        {
            BaseDirectoryWrapper d = NewDirectory();

            if (d is MockDirectoryWrapper)
            {
                ((MockDirectoryWrapper)d).PreventDoubleWrite = false;
            }

            int threadCount = TestUtil.NextInt32(Random, 2, 6);

            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)));

            LineFileDocs docs = new LineFileDocs(Random);

            ThreadClass[] threads      = new ThreadClass[threadCount];
            int           iters        = AtLeast(100);
            AtomicBoolean failed       = new AtomicBoolean();
            ReentrantLock rollbackLock = new ReentrantLock();
            ReentrantLock commitLock   = new ReentrantLock();

            for (int threadID = 0; threadID < threadCount; threadID++)
            {
                threads[threadID] = new ThreadAnonymousInnerClassHelper(this, d, writerRef, docs, iters, failed, rollbackLock, commitLock);
                threads[threadID].Start();
            }

            for (int threadID = 0; threadID < threadCount; threadID++)
            {
                try
                {
                    threads[threadID].Join();
                }
                catch (Exception e)
                {
                    Console.WriteLine("EXCEPTION in ThreadAnonymousInnerClassHelper: " + Environment.NewLine + e);
                }
            }

            Assert.IsTrue(!failed.Get());
            writerRef.Value.Dispose();
            d.Dispose();
        }
        // run random tests from different threads to make sure the per-thread clones
        // don't share mutable data
        public virtual void TestClone()
        {
            RandomDocumentFactory docFactory = new RandomDocumentFactory(this, 5, 20);
            int numDocs = AtLeast(100);

            foreach (Options options in ValidOptions())
            {
                RandomDocument[] docs = new RandomDocument[numDocs];
                for (int i = 0; i < numDocs; ++i)
                {
                    docs[i] = docFactory.NewDocument(TestUtil.NextInt32(Random, 1, 3), AtLeast(10), options);
                }
                AtomicObject <Exception> exception = new AtomicObject <Exception>();
                using (Directory dir = NewDirectory())
                    using (RandomIndexWriter writer = new RandomIndexWriter(Random, dir, ClassEnvRule.similarity, ClassEnvRule.timeZone))
                    {
                        for (int i = 0; i < numDocs; ++i)
                        {
                            writer.AddDocument(AddId(docs[i].ToDocument(), "" + i));
                        }
                        using (IndexReader reader = writer.GetReader())
                        {
                            for (int i = 0; i < numDocs; ++i)
                            {
                                int docID = DocID(reader, "" + i);
                                AssertEquals(docs[i], reader.GetTermVectors(docID));
                            }

                            ThreadClass[] threads = new ThreadClass[2];
                            for (int i = 0; i < threads.Length; ++i)
                            {
                                threads[i] = new ThreadAnonymousInnerClassHelper(this, numDocs, docs, reader, exception, i);
                            }
                            foreach (ThreadClass thread in threads)
                            {
                                thread.Start();
                            }
                            foreach (ThreadClass thread in threads)
                            {
                                thread.Join();
                            }
                        } // reader.Dispose();
                    }// writer.Dispose();, dir.Dispose();
                Assert.IsNull(exception.Value, "One thread threw an exception");
            }
        }
 public ThreadAnonymousInnerClassHelper(BaseTermVectorsFormatTestCase outerInstance, int numDocs, Lucene.Net.Index.BaseTermVectorsFormatTestCase.RandomDocument[] docs, IndexReader reader, AtomicObject<Exception> exception, int i)
 {
     this.OuterInstance = outerInstance;
     this.NumDocs = numDocs;
     this.Docs = docs;
     this.Reader = reader;
     this.ARException = exception;
     this.i = i;
 }
        // [Test] // LUCENENET NOTE: For now, we are overriding this test in every subclass to pull it into the right context for the subclass
        // run random tests from different threads to make sure the per-thread clones
        // don't share mutable data
        public virtual void TestClone()
        {
            RandomDocumentFactory docFactory = new RandomDocumentFactory(this, 5, 20);
            int numDocs = AtLeast(100);
            foreach (Options options in ValidOptions())
            {
                RandomDocument[] docs = new RandomDocument[numDocs];
                for (int i = 0; i < numDocs; ++i)
                {
                    docs[i] = docFactory.NewDocument(TestUtil.NextInt(Random(), 1, 3), AtLeast(10), options);
                }
                Directory dir = NewDirectory();
                RandomIndexWriter writer = new RandomIndexWriter(Random(), dir, ClassEnvRule.Similarity, ClassEnvRule.TimeZone);
                for (int i = 0; i < numDocs; ++i)
                {
                    writer.AddDocument(AddId(docs[i].ToDocument(), "" + i));
                }
                IndexReader reader = writer.Reader;
                for (int i = 0; i < numDocs; ++i)
                {
                    int docID = DocID(reader, "" + i);
                    AssertEquals(docs[i], reader.GetTermVectors(docID));
                }

                AtomicObject<Exception> exception = new AtomicObject<Exception>();
                ThreadClass[] threads = new ThreadClass[2];
                for (int i = 0; i < threads.Length; ++i)
                {
                    threads[i] = new ThreadAnonymousInnerClassHelper(this, numDocs, docs, reader, exception, i);
                }
                foreach (ThreadClass thread in threads)
                {
                    thread.Start();
                }
                foreach (ThreadClass thread in threads)
                {
                    thread.Join();
                }
                reader.Dispose();
                writer.Dispose();
                dir.Dispose();
                Assert.IsNull(exception.Value, "One thread threw an exception");
            }
        }
 public ThreadAnonymousInnerClassHelper(TestIndexWriterWithThreads outerInstance, BaseDirectoryWrapper d, AtomicObject<IndexWriter> writerRef, LineFileDocs docs, int iters, AtomicBoolean failed, ReentrantLock rollbackLock, ReentrantLock commitLock)
 {
     this.OuterInstance = outerInstance;
     this.d = d;
     this.WriterRef = writerRef;
     this.Docs = docs;
     this.Iters = iters;
     this.Failed = failed;
     this.RollbackLock = rollbackLock;
     this.CommitLock = commitLock;
 }
        public virtual void TestRollbackAndCommitWithThreads()
        {
            BaseDirectoryWrapper d = NewDirectory();
            if (d is MockDirectoryWrapper)
            {
                ((MockDirectoryWrapper)d).PreventDoubleWrite = false;
            }

            int threadCount = TestUtil.NextInt(Random(), 2, 6);

            MockAnalyzer analyzer = new MockAnalyzer(Random());
            analyzer.MaxTokenLength = TestUtil.NextInt(Random(), 1, IndexWriter.MAX_TERM_LENGTH);
            AtomicObject<IndexWriter> writerRef =
                new AtomicObject<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();
            ReentrantLock rollbackLock = new ReentrantLock();
            ReentrantLock commitLock = new ReentrantLock();
            for (int threadID = 0; threadID < threadCount; threadID++)
            {
                threads[threadID] = new ThreadAnonymousInnerClassHelper(this, d, writerRef, docs, iters, failed, rollbackLock, commitLock);
                threads[threadID].Start();
            }

            for (int threadID = 0; threadID < threadCount; threadID++)
            {
                threads[threadID].Join();
            }

            Assert.IsTrue(!failed.Get());
            writerRef.Value.Dispose();
            d.Dispose();
        }
            public ThreadAnonymousInnerClassHelper(int numDocs, DirectoryReader rd, IndexSearcher searcher, int readsPerThread, AtomicObject<Exception> ex, int i)
            {
                this.NumDocs = numDocs;
                this.Rd = rd;
                this.Searcher = searcher;
                this.ReadsPerThread = readsPerThread;
                this.Ex = ex;
                this.i = i;

                queries = new int[ReadsPerThread];
                for (int j = 0; j < queries.Length; ++j)
                {
                    queries[j] = Random().Next(NumDocs);
                }
            }
        // [Test, Timeout(300000)] // LUCENENET NOTE: For now, we are overriding this test in every subclass to pull it into the right context for the subclass
        public virtual void TestConcurrentReads()
        {
            Directory dir = NewDirectory();
            IndexWriterConfig iwConf = NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random()));
            iwConf.SetMaxBufferedDocs(RandomInts.NextIntBetween(Random(), 2, 30));
            RandomIndexWriter iw = new RandomIndexWriter(Random(), dir, iwConf);

            // make sure the readers are properly cloned
            Document doc = new Document();
            Field field = new StringField("fld", "", Field.Store.YES);
            doc.Add(field);
            int numDocs = AtLeast(1000);
            for (int i = 0; i < numDocs; ++i)
            {
                field.StringValue = "" + i;
                iw.AddDocument(doc);
            }
            iw.Commit();

            DirectoryReader rd = DirectoryReader.Open(dir);
            IndexSearcher searcher = new IndexSearcher(rd);
            int concurrentReads = AtLeast(5);
            int readsPerThread = AtLeast(50);
            IList<ThreadClass> readThreads = new List<ThreadClass>();
            AtomicObject<Exception> ex = new AtomicObject<Exception>();
            for (int i = 0; i < concurrentReads; ++i)
            {
                readThreads.Add(new ThreadAnonymousInnerClassHelper(numDocs, rd, searcher, readsPerThread, ex, i));
            }
            foreach (ThreadClass thread in readThreads)
            {
                thread.Start();
            }
            foreach (ThreadClass thread in readThreads)
            {
                thread.Join();
            }
            rd.Dispose();
            if (ex.Value != null)
            {
                throw ex.Value;
            }

            iw.Dispose();
            dir.Dispose();
        }
예제 #23
0
        public void AtomicObjectToStringReturnsExpectedStringWithDefault()
        {
            var o = new AtomicObject <string>();

            Assert.AreEqual(string.Empty, o.ToString());
        }