From a566440b97260588b3898d4dc3fc15ec61b2c028 Mon Sep 17 00:00:00 2001 From: David Rousselie Date: Sun, 8 Jul 2007 22:22:30 +0200 Subject: [PATCH] resolve concurrency problem darcs-hash:20070708202230-86b55-b468cbaac24636efc20aea6e39eaa573db17e24e.gz --- src/jcl/jabber/__init__.py | 9 +++++ src/jcl/model/__init__.py | 16 ++++----- src/jcl/model/tests/__init__.py | 59 ++++++++++++++++++++++++++++++++- src/jcl/model/tests/account.py | 8 ++--- 4 files changed, 79 insertions(+), 13 deletions(-) diff --git a/src/jcl/jabber/__init__.py b/src/jcl/jabber/__init__.py index 15b9ea1..da6d678 100644 --- a/src/jcl/jabber/__init__.py +++ b/src/jcl/jabber/__init__.py @@ -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 diff --git a/src/jcl/model/__init__.py b/src/jcl/model/__init__.py index e121431..b3557ae 100644 --- a/src/jcl/model/__init__.py +++ b/src/jcl/model/__init__.py @@ -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: - jcl.model.hub.threadConnection = \ - connectionForURI(db_connection_str) - # account.hub.threadConnection.debug = True - jcl.model.db_connected = True + #if not jcl.model.db_connected: + jcl.model.hub.threadConnection = \ + connectionForURI(db_connection_str) + # account.hub.threadConnection.debug = 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 diff --git a/src/jcl/model/tests/__init__.py b/src/jcl/model/tests/__init__.py index a6ad454..f5bf7a5 100644 --- a/src/jcl/model/tests/__init__.py +++ b/src/jcl/model/tests/__init__.py @@ -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 diff --git a/src/jcl/model/tests/account.py b/src/jcl/model/tests/account.py index 9b20056..baeda3d 100644 --- a/src/jcl/model/tests/account.py +++ b/src/jcl/model/tests/account.py @@ -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: