PyLucene 3.4.0-1 import
[pylucene.git] / test / test_PyLuceneThread.py
1 # ====================================================================
2 #   Licensed under the Apache License, Version 2.0 (the "License");
3 #   you may not use this file except in compliance with the License.
4 #   You may obtain a copy of the License at
5 #
6 #       http://www.apache.org/licenses/LICENSE-2.0
7 #
8 #   Unless required by applicable law or agreed to in writing, software
9 #   distributed under the License is distributed on an "AS IS" BASIS,
10 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 #   See the License for the specific language governing permissions and
12 #   limitations under the License.
13 # ====================================================================
14
15 import time, threading
16 from unittest import TestCase, main
17 from lucene import *
18
19
20 class PyLuceneThreadTestCase(TestCase):
21     """
22     Test using threads in PyLucene with python threads
23     """
24
25     def setUp(self):
26
27         self.classLoader = Thread.currentThread().getContextClassLoader()
28
29         self.directory = RAMDirectory()
30         writer = IndexWriter(self.directory,
31                              StandardAnalyzer(Version.LUCENE_CURRENT), True,
32                              IndexWriter.MaxFieldLength.LIMITED)
33
34         doc1 = Document()
35         doc2 = Document()
36         doc3 = Document()
37         doc4 = Document()
38         doc1.add(Field("field", "one",
39                        Field.Store.YES, Field.Index.ANALYZED))
40         doc2.add(Field("field", "two",
41                        Field.Store.YES, Field.Index.ANALYZED))
42         doc3.add(Field("field", "three",
43                        Field.Store.YES, Field.Index.ANALYZED))
44         doc4.add(Field("field", "one",
45                        Field.Store.YES, Field.Index.ANALYZED))
46
47         writer.addDocument(doc1)
48         writer.addDocument(doc2)
49         writer.addDocument(doc3)
50         writer.addDocument(doc4)
51         writer.optimize()
52         writer.close()
53
54         self.testData = [('one',2), ('two',1), ('three', 1), ('five', 0)] * 500
55         self.lock = threading.Lock()
56         self.totalQueries = 0
57
58
59     def tearDown(self):
60
61         self.directory.close()
62
63
64     def testWithMainThread(self):
65         """ warm up test for runSearch in main thread """
66
67         self.runSearch(2000, True)
68
69
70     def testWithPyLuceneThread(self):
71         """ Run 5 threads with 2000 queries each """
72
73         threads = []
74         for i in xrange(5):
75             threads.append(threading.Thread(target=self.runSearch,
76                                             args=(2000,)))
77
78         for thread in threads:
79             thread.start()
80
81         for thread in threads:
82             thread.join()
83
84         # we survived!
85
86         # and all queries have ran successfully
87         self.assertEqual(10000, self.totalQueries)
88
89
90     def runSearch(self, runCount, mainThread=False):
91         """ search for runCount number of times """
92
93         # problem: if there are any assertion errors in the child
94         #   thread, the calling thread is not notified and may still
95         #   consider the test case pass. We are using self.totalQueries
96         #   to double check that work has actually been done.
97
98         if not mainThread:
99             getVMEnv().attachCurrentThread()
100         time.sleep(0.5)
101
102         searcher = IndexSearcher(self.directory, True)
103         try:
104             self.query = PhraseQuery()
105             for word, count in self.testData[0:runCount]:
106                 query = TermQuery(Term("field", word))
107                 topDocs = searcher.search(query, 50)
108                 self.assertEqual(topDocs.totalHits, count)
109
110                 self.lock.acquire()
111                 self.totalQueries += 1
112                 self.lock.release()
113         finally:
114             searcher.close()
115
116
117 if __name__ == "__main__":
118     import sys, lucene
119     lucene.initVM()
120     if '-loop' in sys.argv:
121         sys.argv.remove('-loop')
122         while True:
123             try:
124                 main()
125             except:
126                 pass
127     else:
128          main()