Пример #1
0
		public virtual void  TestThreadSafety()
		{
			Directory dir = new MockRAMDirectory();
			int n = 150;
			
			IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), IndexWriter.MaxFieldLength.LIMITED);
			for (int i = 0; i < n; i++)
			{
				writer.AddDocument(CreateDocument(i, 3));
			}
			writer.Optimize();
			writer.Close();
			
			TestReopen test = new AnonymousClassTestReopen7(dir, n, this);
			
			System.Collections.IList readers = (System.Collections.IList) System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(new System.Collections.ArrayList()));
			IndexReader firstReader = IndexReader.Open(dir);
			IndexReader reader = firstReader;
			System.Random rnd = NewRandom();
			
			ReaderThread[] threads = new ReaderThread[n];
			System.Collections.Hashtable readersToClose = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable());
			
			for (int i = 0; i < n; i++)
			{
				if (i % 10 == 0)
				{
					IndexReader refreshed = reader.Reopen();
					if (refreshed != reader)
					{
						SupportClass.CollectionsHelper.AddIfNotContains(readersToClose, reader);
					}
					reader = refreshed;
				}
				IndexReader r = reader;
				
				int index = i;
				
				ReaderThreadTask task;
				
				if (i < 20 || (i >= 50 && i < 70) || i > 90)
				{
					task = new AnonymousClassReaderThreadTask(index, r, test, readersToClose, readers, rnd, this);
				}
				else
				{
					task = new AnonymousClassReaderThreadTask1(readers, rnd, this);
				}
				
				threads[i] = new ReaderThread(task);
				threads[i].Start();
			}
			
			lock (this)
			{
				System.Threading.Monitor.Wait(this, 15000);
			}
			
			for (int i = 0; i < n; i++)
			{
				if (threads[i] != null)
				{
					threads[i].StopThread();
				}
			}
			
			for (int i = 0; i < n; i++)
			{
				if (threads[i] != null)
				{
					threads[i].Join();
					if (threads[i].error != null)
					{
						System.String msg = "Error occurred in thread " + threads[i].Name + ":\n" + threads[i].error.Message;
						Assert.Fail(msg);
					}
				}
			}
			
			System.Collections.IEnumerator it = readersToClose.GetEnumerator();
			while (it.MoveNext())
			{
				((IndexReader) ((System.Collections.DictionaryEntry)it.Current).Key).Close();
			}
			
			firstReader.Close();
			reader.Close();
			
			it = readersToClose.GetEnumerator();
			while (it.MoveNext())
			{
                AssertReaderClosed((IndexReader)((System.Collections.DictionaryEntry)it.Current).Key, true, true);
			}
			
			AssertReaderClosed(reader, true, true);
			AssertReaderClosed(firstReader, true, true);
			
			dir.Close();
		}
		public virtual void  TestThreadSafety()
		{
			Directory dir = new RAMDirectory();
			int n = 150;
			
			IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer());
			for (int i = 0; i < n; i++)
			{
				writer.AddDocument(CreateDocument(i, 3));
			}
			writer.Optimize();
			writer.Close();
			
			TestReopen_Renamed_Class test = new AnonymousClassTestReopen7(dir, n, this);
			
			System.Collections.IList readers = (System.Collections.IList) System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(new System.Collections.ArrayList()));
			IndexReader firstReader = IndexReader.Open(dir);
			IndexReader reader = firstReader;
			System.Random rnd = new System.Random();
			
			ReaderThread[] threads = new ReaderThread[n];
			System.Collections.Hashtable readersToClose = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable());
			
			for (int i = 0; i < n; i++)
			{
				if (i % 10 == 0)
				{
					IndexReader refreshed = reader.Reopen();
					if (refreshed != reader)
					{
						if (!readersToClose.Contains(reader))
							readersToClose.Add(reader, reader);
					}
					reader = refreshed;
				}
				IndexReader r = reader;
				
				int index = i;
				
				ReaderThreadTask task;
				
				if (i < 20 || (i >= 50 && i < 70) || i > 90)
				{
					task = new AnonymousClassReaderThreadTask(index, r, test, readersToClose, readers, rnd, this);
				}
				else
				{
					task = new AnonymousClassReaderThreadTask1(readers, rnd, this);
				}
				
				threads[i] = new ReaderThread(task);
				threads[i].Start();
			}
			
			lock (this)
			{
				try
				{
					System.Threading.Monitor.Wait(this, 15000);
				}
				catch (System.Threading.ThreadInterruptedException)
				{
				}
			}
			
			for (int i = 0; i < n; i++)
			{
				if (threads[i] != null)
				{
					threads[i].StopThread();
				}
			}
			
			for (int i = 0; i < n; i++)
			{
				if (threads[i] != null)
				{
					try
					{
						threads[i].Join();
						if (threads[i].exception != null)
						{
							throw threads[i].exception;
						}
					}
					catch (System.Threading.ThreadInterruptedException)
					{
					}
				}
			}
			
			System.Collections.IEnumerator it = readersToClose.Keys.GetEnumerator();
			while (it.MoveNext())
			{
				((IndexReader) it.Current).Close();
			}
			
			firstReader.Close();
			reader.Close();
			
			it = readersToClose.Keys.GetEnumerator();
			while (it.MoveNext())
			{
				AssertReaderClosed((IndexReader) it.Current, true, true);
			}
			
			AssertReaderClosed(reader, true, true);
			AssertReaderClosed(firstReader, true, true);
		}