Skip to content

Commit

Permalink
Merge pull request #13 from zopefoundation/clearSynchs
Browse files Browse the repository at this point in the history
added ITransactionManager.registeredSynchs and clearSynchs
  • Loading branch information
jimfulton committed May 5, 2016
2 parents 788f38b + fc5a0be commit e0df9d0
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
10 changes: 10 additions & 0 deletions transaction/_manager.py
Expand Up @@ -95,6 +95,16 @@ def unregisterSynch(self, synch):
"""
self._synchs.remove(synch)

def clearSynchs(self):
""" See ITransactionManager.
"""
self._synchs.clear()

def registeredSynchs(self):
""" See ITransactionManager.
"""
return bool(self._synchs)

def isDoomed(self):
""" See ITransactionManager.
"""
Expand Down
14 changes: 14 additions & 0 deletions transaction/interfaces.py
Expand Up @@ -74,6 +74,20 @@ def unregisterSynch(synch):
life. See ISynchronizer for details.
"""

def clearSynchs():
"""Unregister all registered ISynchronizers.
This exists to support test cleanup/initialization
"""

def registeredSynchs():
"""Determine if any ISynchronizers are registered.
Return true is any are registered, and return False otherwise.
This exists to support test cleanup/initialization
"""

class ITransaction(Interface):
"""Object representing a running transaction.
Expand Down
15 changes: 15 additions & 0 deletions transaction/tests/test__manager.py
Expand Up @@ -105,12 +105,27 @@ def test_unregisterSynch(self):
tm = self._makeOne()
synch1 = DummySynch()
synch2 = DummySynch()
self.assertFalse(tm.registeredSynchs())
tm.registerSynch(synch1)
self.assertTrue(tm.registeredSynchs())
tm.registerSynch(synch2)
self.assertTrue(tm.registeredSynchs())
tm.unregisterSynch(synch1)
self.assertTrue(tm.registeredSynchs())
self.assertEqual(len(tm._synchs), 1)
self.assertFalse(synch1 in tm._synchs)
self.assertTrue(synch2 in tm._synchs)
tm.unregisterSynch(synch2)
self.assertFalse(tm.registeredSynchs())

def test_clearSynchs(self):
tm = self._makeOne()
synch1 = DummySynch()
synch2 = DummySynch()
tm.registerSynch(synch1)
tm.registerSynch(synch2)
tm.clearSynchs()
self.assertEqual(len(tm._synchs), 0)

def test_isDoomed_wo_existing_txn(self):
tm = self._makeOne()
Expand Down
13 changes: 13 additions & 0 deletions transaction/tests/test_weakset.py
Expand Up @@ -48,6 +48,19 @@ def test_remove(self):
w.remove(dummy)
self.assertEqual(dummy in w, False)

def test_clear(self):
from transaction.weakset import WeakSet
w = WeakSet()
dummy = Dummy()
w.add(dummy)
dummy2 = Dummy()
w.add(dummy2)
self.assertEqual(dummy in w, True)
self.assertEqual(dummy2 in w, True)
w.clear()
self.assertEqual(dummy in w, False)
self.assertEqual(dummy2 in w, False)

def test_as_weakref_list(self):
import gc
from transaction.weakset import WeakSet
Expand Down
3 changes: 3 additions & 0 deletions transaction/weakset.py
Expand Up @@ -50,6 +50,9 @@ def add(self, obj):
def remove(self, obj):
del self.data[id(obj)]

def clear(self):
self.data.clear()

# f is a one-argument function. Execute f(elt) for each elt in the
# set. f's return value is ignored.
def map(self, f):
Expand Down

0 comments on commit e0df9d0

Please sign in to comment.