resolve concurrency problem
darcs-hash:20070708202230-86b55-b468cbaac24636efc20aea6e39eaa573db17e24e.gz
This commit is contained in:
@@ -64,3 +64,12 @@ def get_accounts_root_filter(self, stanza, lang_class, node=None):
|
||||
return account.get_accounts(stanza.get_from().bare())
|
||||
else:
|
||||
return None
|
||||
|
||||
def replace_handlers(handlers, old_handler_type, new_handler):
|
||||
"""
|
||||
Replace handlers of type `old_handler_type` in `handlers` by `new_handler`
|
||||
"""
|
||||
for handler_group in handlers:
|
||||
for i in xrange(len(handler_group)):
|
||||
if isinstance(handler_group[i], old_handler_type):
|
||||
handler_group[i] = new_handler
|
||||
|
||||
@@ -17,16 +17,16 @@ def db_connect():
|
||||
Create a new connection to the DataBase (SQLObject use connection
|
||||
pool) associated to the current thread.
|
||||
"""
|
||||
if not jcl.model.db_connected:
|
||||
#if not jcl.model.db_connected:
|
||||
jcl.model.hub.threadConnection = \
|
||||
connectionForURI(db_connection_str)
|
||||
# account.hub.threadConnection.debug = True
|
||||
jcl.model.db_connected = True
|
||||
#jcl.model.db_connected = True
|
||||
|
||||
def db_disconnect():
|
||||
"""
|
||||
Delete connection associated to the current thread.
|
||||
"""
|
||||
if jcl.model.db_connected:
|
||||
del jcl.model.hub.threadConnection
|
||||
jcl.model.db_connected = False
|
||||
#if jcl.model.db_connected:
|
||||
#del jcl.model.hub.threadConnection
|
||||
#jcl.model.db_connected = False
|
||||
|
||||
@@ -2,11 +2,68 @@
|
||||
__revision__ = ""
|
||||
|
||||
import unittest
|
||||
import threading
|
||||
import os
|
||||
import sys
|
||||
|
||||
from jcl.model.tests import account
|
||||
from sqlobject import SQLObject
|
||||
from sqlobject.col import StringCol
|
||||
from sqlobject.dbconnection import TheURIOpener
|
||||
|
||||
import jcl.model as model
|
||||
|
||||
if sys.platform == "win32":
|
||||
DB_PATH = "/c|/temp/jcl_test.db"
|
||||
else:
|
||||
DB_PATH = "/tmp/jcl_test.db"
|
||||
DB_URL = DB_PATH# + "?debug=1&debugThreading=1"
|
||||
|
||||
class MockSQLObject(SQLObject):
|
||||
_connection = model.hub
|
||||
string_attr = StringCol()
|
||||
|
||||
class ModelModule_TestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
if os.path.exists(DB_PATH):
|
||||
os.unlink(DB_PATH)
|
||||
model.db_connection_str = 'sqlite://' + DB_URL
|
||||
model.db_connect()
|
||||
MockSQLObject.createTable(ifNotExists=True)
|
||||
model.db_disconnect()
|
||||
|
||||
def tearDown(self):
|
||||
model.db_connect()
|
||||
MockSQLObject.dropTable(ifExists=True)
|
||||
del TheURIOpener.cachedURIs['sqlite://' + DB_URL]
|
||||
model.hub.threadConnection.close()
|
||||
model.db_disconnect()
|
||||
if os.path.exists(DB_PATH):
|
||||
os.unlink(DB_PATH)
|
||||
|
||||
def test_multiple_db_connection(self):
|
||||
def create_account_thread():
|
||||
model.db_connect()
|
||||
obj21 = MockSQLObject(string_attr="obj21")
|
||||
obj22 = MockSQLObject(string_attr="obj22")
|
||||
model.db_disconnect()
|
||||
|
||||
model.db_connect()
|
||||
timer_thread = threading.Thread(target=create_account_thread,
|
||||
name="CreateAccountThread")
|
||||
timer_thread.start()
|
||||
obj11 = MockSQLObject(string_attr="obj11")
|
||||
obj12 = MockSQLObject(string_attr="obj12")
|
||||
timer_thread.join(2)
|
||||
threads = threading.enumerate()
|
||||
self.assertEquals(len(threads), 1)
|
||||
objs = MockSQLObject.select()
|
||||
self.assertEquals(objs.count(), 4)
|
||||
model.db_disconnect()
|
||||
|
||||
def suite():
|
||||
suite = unittest.TestSuite()
|
||||
suite.addTest(unittest.makeSuite(ModelModule_TestCase, 'test'))
|
||||
from jcl.model.tests import account
|
||||
suite.addTest(account.suite())
|
||||
return suite
|
||||
|
||||
|
||||
@@ -277,10 +277,10 @@ class PresenceAccount_TestCase(InheritableAccount_TestCase):
|
||||
self.account_class.possibles_actions)
|
||||
|
||||
def test_possibles_actions(self):
|
||||
for (field_name, \
|
||||
field_type, \
|
||||
possibles_actions, \
|
||||
post_func, \
|
||||
for (field_name,
|
||||
field_type,
|
||||
possibles_actions,
|
||||
post_func,
|
||||
default_func) in self.account_class.get_register_fields()[1:]:
|
||||
if possibles_actions is not None:
|
||||
for possible_action in possibles_actions:
|
||||
|
||||
Reference in New Issue
Block a user