Пример #1
0
        public void IndexWriterUsage_GettingSafe()
        {
            var timer = new System.Timers.Timer(1000.0);

            timer.Elapsed += new System.Timers.ElapsedEventHandler(IndexWriterUsage_GettingSafe_TimerElapsed);
            timer.Start();

            IndexWriterFrame safeFrame = null;

            try
            {
                var fastWriterUsage = IndexWriterUsageAccessor.Instance as FastIndexWriterUsage;
                Assert.IsNotNull(fastWriterUsage, "IndexWriterUsageAccessor.Instance is not FastIndexWriterUsage");

                var indexWriter        = LuceneManager._writer;
                var fastWriterUsageAcc = new IndexWriterUsageAccessor(fastWriterUsage);

                var counter = 50;
                while (fastWriterUsageAcc.RefCount > 0 && counter-- > 0)
                {
                    System.Threading.Thread.Sleep(100);
                }

                var refCount = fastWriterUsageAcc.RefCount;
                Assert.IsTrue(refCount == 0, String.Format("refCount is {0}, expected: 0", refCount));

                _fastWriterFrames = new List <IndexWriterFrame>();
                _fastWriterFrames.Add(IndexWriterFrame.Get(false));
                _fastWriterFrames.Add(IndexWriterFrame.Get(false));
                _fastWriterFrames.Add(IndexWriterFrame.Get(false));
                _fastWriterFrames.Add(IndexWriterFrame.Get(false));
                safeFrame = IndexWriterFrame.Get(true);

                refCount = fastWriterUsageAcc.RefCount;
                Assert.IsTrue(refCount == 0, String.Format("refCount is {0}, expected: 0", refCount));
            }
            finally
            {
                if (safeFrame != null)
                {
                    safeFrame.Dispose();
                }

                timer.Stop();
                timer.Elapsed -= new System.Timers.ElapsedEventHandler(IndexWriterUsage_GettingSafe_TimerElapsed);
                timer.Dispose();
            }
        }
Пример #2
0
        public void IndexWriterUsage_UseFast()
        {
            var fastWriterUsage = IndexWriterUsageAccessor.Instance as FastIndexWriterUsage;

            Assert.IsNotNull(fastWriterUsage, "IndexWriterUsageAccessor.Instance is not FastIndexWriterUsage");

            var indexWriter        = LuceneManager._writer;
            var fastWriterUsageAcc = new IndexWriterUsageAccessor(fastWriterUsage);

            var counter = 50;

            while (fastWriterUsageAcc.RefCount > 0 && counter-- > 0)
            {
                System.Threading.Thread.Sleep(100);
            }

            var refCount = fastWriterUsageAcc.RefCount;

            Assert.IsTrue(refCount == 0, String.Format("refCount is {0}, expected: 0", refCount));

            using (var wrf1 = IndexWriterFrame.Get(false))
            {
                refCount = fastWriterUsageAcc.RefCount;
                Assert.IsTrue(refCount == 1, String.Format("refCount is {0}, expected: 1", refCount));
                Assert.ReferenceEquals(indexWriter == wrf1.IndexWriter, "IndexWriter has changed #1");
                using (var wrf2 = IndexWriterFrame.Get(false))
                {
                    refCount = fastWriterUsageAcc.RefCount;
                    Assert.IsTrue(refCount == 2, String.Format("refCount is {0}, expected: 2", refCount));
                    Assert.ReferenceEquals(indexWriter == wrf1.IndexWriter, "IndexWriter has changed #2");
                }
                refCount = fastWriterUsageAcc.RefCount;
                Assert.IsTrue(refCount == 1, String.Format("refCount is {0}, expected: 1", refCount));
            }
            refCount = fastWriterUsageAcc.RefCount;
            Assert.IsTrue(refCount == 0, String.Format("refCount is {0}, expected: 0", refCount));
        }