[Zodb-checkins] CVS: ZEO/ZEO/tests - ThreadTests.py:1.1.2.1 testZEO.py:1.16.4.4.2.6

Barry Warsaw barry@wooz.org
Wed, 15 May 2002 01:11:58 -0400


Update of /cvs-repository/ZEO/ZEO/tests
In directory cvs.zope.org:/tmp/cvs-serv5426/ZEO/tests

Modified Files:
      Tag: ZEO2-branch
	testZEO.py 
Added Files:
      Tag: ZEO2-branch
	ThreadTests.py 
Log Message:
Added a new test class/module ThreadTests which exercises some tricky
ZEO threading issues.  Specifically added now is a test of a suspected
ZEO bug that can occur when one thread closes the storage while
another thread is in the middle of a transaction.


=== Added File ZEO/ZEO/tests/ThreadTests.py ===
"""Compromising positions involving threads."""

import threading

from ZODB.Transaction import Transaction
from ZODB.tests.StorageTestBase import zodb_pickle, MinPO

import ZEO.ClientStorage
from ZEO.Exceptions import Disconnected

ZERO = '\0'*8

class DummyDB:
    def invalidate(self, *args):
        pass


class GetsThroughVoteThread(threading.Thread):
    # run the entire test in a thread so that the blocking call for
    # tpc_vote() doesn't hang the test suite.

    def __init__(self, storage, doCloseEvent, threadStartedEvent):
        self.storage = storage
        self.trans = Transaction()
        self.doCloseEvent = doCloseEvent
        self.threadStartedEvent = threadStartedEvent
        self.gotDisconnected = 0
        threading.Thread.__init__(self)

    def run(self):
        self.storage.tpc_begin(self.trans)
        oid = self.storage.new_oid()
        self.storage.store(oid, ZERO, zodb_pickle(MinPO("c")), '', self.trans)
        self.storage.tpc_vote(self.trans)
        self.threadStartedEvent.set()
        self.doCloseEvent.wait(10)
        try:
            self.storage.tpc_finish(self.trans)
        except Disconnected:
            self.gotDisconnected = 1


class ThreadTests:

    # Thread 1 should start a transaction, but not get all the way through it.
    # Main thread should close the connection.  Thread 1 should then get
    # disconnected.

    def checkDisconnectedOnThread2Close(self):
        doCloseEvent = threading.Event()
        threadStartedEvent = threading.Event()
        thread1 = GetsThroughVoteThread(self._storage,
                                        doCloseEvent, threadStartedEvent)
        thread1.start()
        threadStartedEvent.wait(10)
        self._storage.close()
        doCloseEvent.set()
        thread1.join()
        self.assertEqual(thread1.gotDisconnected, 1)


=== ZEO/ZEO/tests/testZEO.py 1.16.4.4.2.5 => 1.16.4.4.2.6 ===
 import zLOG
 
-from ZEO.tests import forker, Cache, CommitLockTests
+from ZEO.tests import forker, Cache, CommitLockTests, ThreadTests
 from ZEO.smac import Disconnected
 
 # Sorry Jim...
@@ -72,6 +72,7 @@
                    MTStorage.MTStorage,
                    ReadOnlyStorage.ReadOnlyStorage,
                    CommitLockTests.CommitLockTests,
+                   ThreadTests.ThreadTests,
                    ):
     """An abstract base class for ZEO tests