PageRenderTime 35ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/_tests/test_thread.py

http://django-search-lucene.googlecode.com/
Python | 127 lines | 67 code | 27 blank | 33 comment | 16 complexity | 9157ef86abe9b875df31acb657f728c0 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2005,2006,2007,2008 Spike^ekipS <spikeekips@gmail.com>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. import sys, sqlite3, time, threading, datetime, random, unittest
  18. class PyLuceneThreadTestCase (unittest.TestCase):
  19. def setUp (self) :
  20. self.from_model = models_tests.doc.objects
  21. self.from_indexed = sys.MODELS_REGISTERED.get("tests.doc")[0].objects
  22. def update_document_without_save (self, mainThread=False, ) :
  23. if not mainThread :
  24. pylucene.initialize_vm()
  25. self.documents = list(self.from_model.all())
  26. random.shuffle(self.documents)
  27. for o in self.documents :
  28. __title = str(o.pk) + " : " + o.title + str(random.random() * 1000)
  29. __summary = o.summary + str(random.random() * 1000)
  30. o.title = __title
  31. o.summary = __summary
  32. sys.INDEX_MANAGER.execute("index_update", o)
  33. o_n = self.from_indexed.get(pk=o.pk)
  34. self.assertEqual(o.title, o_n.title, )
  35. self.assertEqual(o.summary, o_n.summary, )
  36. def update_document_using_save (self, mainThread=False, ) :
  37. """
  38. In SQLite, there will be a thread-lock problems.
  39. """
  40. if not mainThread :
  41. pylucene.initialize_vm()
  42. self.documents = list(self.from_model.all())
  43. random.shuffle(self.documents)
  44. for o in self.documents :
  45. __title = str(o.pk) + " : " + o.title + str(random.random() * 1000)
  46. __summary = o.summary + str(random.random() * 1000)
  47. o.title = __title
  48. o.summary = __summary
  49. try :
  50. o.save()
  51. except ObjectDoesNotExist :
  52. return
  53. except sqlite3.OperationalError:
  54. return
  55. except Exception, e :
  56. print "[EE] Save error,", e
  57. raise
  58. else :
  59. try :
  60. o_n = self.from_indexed.get(pk=o.pk)
  61. except ObjectDoesNotExist :
  62. pass
  63. except Exception, e :
  64. print "[EE] objects_search,", e
  65. def test_thread(self):
  66. threads = []
  67. for i in xrange(30) :
  68. threads.append(threading.Thread(target=self.update_document_without_save))
  69. [thread.start() for thread in threads]
  70. [thread.join() for thread in threads]
  71. if __name__ == "__main__" :
  72. from django.conf import settings
  73. settings.SEARCH_STORAGE_PATH = settings.SEARCH_STORAGE_PATH + "_test"
  74. settings.SEARCH_STORAGE_TYPE = "fs"
  75. settings.DEBUG = False
  76. from django.contrib.webdesign.lorem_ipsum import words, paragraphs
  77. from django.db.models import ObjectDoesNotExist
  78. import pylucene, core
  79. import models as models_tests
  80. models_tests.add()
  81. models_tests.cleanup_index()
  82. models_tests.cleanup_docs()
  83. models_tests.insert_docs(10)
  84. unittest.main(testRunner=models_tests.SearcherTestRunner(verbosity=2))
  85. sys.exit()
  86. """
  87. Description
  88. -----------
  89. ChangeLog
  90. ---------
  91. Usage
  92. -----
  93. """
  94. __author__ = "Spike^ekipS <spikeekips@gmail.com>"