[Zodb-checkins] CVS: StandaloneZODB/ZEO/tests - TestThread.py:1.1.2.1 CommitLockTests.py:1.1.2.6

Jeremy Hylton jeremy@zope.com
Fri, 9 Aug 2002 16:09:03 -0400


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

Modified Files:
      Tag: ZEO2-branch
	CommitLockTests.py 
Added Files:
      Tag: ZEO2-branch
	TestThread.py 
Log Message:
Add TestThread class that causes test to fail if it doesn't exit, but
doesn't cause the tests to hang on exit with unstopped threads.

Use TestThread in CommitLockTests.


=== Added File StandaloneZODB/ZEO/tests/TestThread.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
"""A Thread base class for use with unittest."""

from cStringIO import StringIO
import threading
import traceback

class TestThread(threading.Thread):
    __super_init = threading.Thread.__init__
    __super_run = threading.Thread.run

    def __init__(self, testcase, group=None, target=None, name=None,
                 args=(), kwargs={}, verbose=None):
        self.__super_init(group, target, name, args, kwargs, verbose)
        self.setDaemon(1)
        self._testcase = testcase

    def run(self):
        try:
            self.testrun()
        except Exception, err:
            s = StringIO()
            traceback.print_exc(file=s)
            self._testcase.fail("Exception in thread %s:\n%s\n" %
                                (self, s.getvalue()))

    def cleanup(self, timeout=15):
        self.join(timeout)
        if self.isAlive():
            self._testcase.fail("Thread did not finish: %s" % self)


=== StandaloneZODB/ZEO/tests/CommitLockTests.py 1.1.2.5 => 1.1.2.6 ===
--- StandaloneZODB/ZEO/tests/CommitLockTests.py:1.1.2.5	Tue May 21 18:28:57 2002
+++ StandaloneZODB/ZEO/tests/CommitLockTests.py	Fri Aug  9 16:09:03 2002
@@ -17,6 +17,7 @@
 
 from ZODB.Transaction import Transaction
 from ZODB.tests.StorageTestBase import zodb_pickle, MinPO
+from ZEO.tests.TestThread import TestThread
 
 import ZEO.ClientStorage
 from ZEO.Exceptions import Disconnected
@@ -27,30 +28,32 @@
     def invalidate(self, *args):
         pass
 
-class WorkerThread(threading.Thread):
+class WorkerThread(TestThread):
 
     # 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, trans, method="tpc_finish"):
+    def __init__(self, testcase, storage, trans, method="tpc_finish"):
         self.storage = storage
         self.trans = trans
         self.method = method
-        threading.Thread.__init__(self)
+        TestThread.__init__(self, testcase)
 
-    def run(self):
+    def testrun(self):
         try:
             self.storage.tpc_begin(self.trans)
             oid = self.storage.new_oid()
-            self.storage.store(oid, ZERO, zodb_pickle(MinPO("c")), '', self.trans)
+            self.storage.store(oid, ZERO, zodb_pickle(MinPO("c")), '',
+                               self.trans)
             oid = self.storage.new_oid()
-            self.storage.store(oid, ZERO, zodb_pickle(MinPO("c")), '', self.trans)
+            self.storage.store(oid, ZERO, zodb_pickle(MinPO("c")), '',
+                               self.trans)
             self.storage.tpc_vote(self.trans)
             if self.method == "tpc_finish":
                 self.storage.tpc_finish(self.trans)
             else:
                 self.storage.tpc_abort(self.trans)
-        except Disconnected:
+        except Disconnected, err:
             pass
 
 class CommitLockTests:
@@ -94,7 +97,6 @@
 
     def _cleanup(self):
         for store, trans in self._storages:
-            store.tpc_abort(trans)
             store.close()
         self._storages = []
 
@@ -104,6 +106,7 @@
 
         # Start on transaction normally.
         t = Transaction()
+        oid = self._storage.new_oid()
         self._storage.tpc_begin(t)
 
         # Start a second transaction on a different connection without
@@ -119,7 +122,6 @@
             else:
                 self._storages.append((storage2, t2))
 
-        oid = self._storage.new_oid()
         self._storage.store(oid, ZERO, zodb_pickle(MinPO(1)), '', t)
         self._storage.tpc_vote(t)
         if method_name == "tpc_finish":
@@ -148,13 +150,13 @@
 
     def _dosetup2(self, storage, trans, tid):
         self._threads = []
-        t = WorkerThread(storage, trans)
+        t = WorkerThread(self, storage, trans)
         self._threads.append(t)
         t.start()
 
     def _dowork2(self, method_name):
         for t in self._threads:
-            t.join()
+            t.cleanup()
 
     def _duplicate_client(self):
         "Open another ClientStorage to the same server."