[Python-checkins] python/dist/src/Lib/test test_socket.py,1.37,1.38

gvanrossum@users.sourceforge.net gvanrossum@users.sourceforge.net
Tue, 18 Jun 2002 11:35:17 -0700


Update of /cvsroot/python/python/dist/src/Lib/test
In directory usw-pr-cvs1:/tmp/cvs-serv5996

Modified Files:
	test_socket.py 
Log Message:
Michael fixed the race conditions and removed the sleeps.
This is his SF patch 569697.  I renamed main() to test_main() again so
that this is run as part of the standard test suite.


Index: test_socket.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_socket.py,v
retrieving revision 1.37
retrieving revision 1.38
diff -C2 -d -r1.37 -r1.38
*** test_socket.py	13 Jun 2002 20:24:17 -0000	1.37
--- test_socket.py	18 Jun 2002 18:35:13 -0000	1.38
***************
*** 38,41 ****
--- 38,77 ----
  
  class ThreadableTest:
+     """Threadable Test class
+ 
+     The ThreadableTest class makes it easy to create a threaded
+     client/server pair from an existing unit test. To create a
+     new threaded class from an existing unit test, use multiple
+     inheritance:
+ 
+         class NewClass (OldClass, ThreadableTest):
+             pass
+ 
+     This class defines two new fixture functions with obvious
+     purposes for overriding:
+ 
+         clientSetUp ()
+         clientTearDown ()
+ 
+     Any new test functions within the class must then define
+     tests in pairs, where the test name is preceeded with a
+     '_' to indicate the client portion of the test. Ex:
+ 
+         def testFoo(self):
+             # Server portion
+ 
+         def _testFoo(self):
+             # Client portion
+ 
+     Any exceptions raised by the clients during their tests
+     are caught and transferred to the main thread to alert
+     the testing framework.
+ 
+     Note, the server setup function cannot call any blocking
+     functions that rely on the client thread during setup,
+     unless serverExplicityReady() is called just before
+     the blocking call (such as in setting up a client/server
+     connection and performing the accept() in setUp().
+     """
  
      def __init__(self):
***************
*** 46,51 ****
          self.tearDown = self._tearDown
  
      def _setUp(self):
!         self.ready = threading.Event()
          self.done = threading.Event()
          self.queue = Queue.Queue(1)
--- 82,95 ----
          self.tearDown = self._tearDown
  
+     def serverExplicitReady(self):
+         """This method allows the server to explicitly indicate that
+         it wants the client thread to proceed. This is useful if the
+         server is about to execute a blocking routine that is
+         dependent upon the client thread during its setup routine."""
+         self.server_ready.set()
+ 
      def _setUp(self):
!         self.server_ready = threading.Event()
!         self.client_ready = threading.Event()
          self.done = threading.Event()
          self.queue = Queue.Queue(1)
***************
*** 60,64 ****
  
          self.__setUp()
!         self.ready.wait()
  
      def _tearDown(self):
--- 104,110 ----
  
          self.__setUp()
!         if not self.server_ready.isSet():
!             self.server_ready.set()
!         self.client_ready.wait()
  
      def _tearDown(self):
***************
*** 71,75 ****
  
      def clientRun(self, test_func):
!         self.ready.set()
          self.clientSetUp()
          if not callable(test_func):
--- 117,122 ----
  
      def clientRun(self, test_func):
!         self.server_ready.wait()
!         self.client_ready.set()
          self.clientSetUp()
          if not callable(test_func):
***************
*** 118,121 ****
--- 165,171 ----
      def setUp(self):
          ThreadedTCPSocketTest.setUp(self)
+         # Indicate explicitly we're ready for the client thread to
+         # proceed and then perform the blocking call to accept
+         self.serverExplicitReady()
          conn, addr = self.serv.accept()
          self.cli_conn = conn
***************
*** 370,374 ****
  
      def _testRecvFrom(self):
-         time.sleep(1) # Give server a chance to set up
          self.cli.sendto(MSG, 0, (HOST, PORT))
  
--- 420,423 ----
***************
*** 408,417 ****
  
      def _testAccept(self):
-         time.sleep(1)
          self.cli.connect((HOST, PORT))
  
      def testConnect(self):
          """Testing non-blocking connect."""
-         time.sleep(1)
          conn, addr = self.serv.accept()
  
--- 457,464 ----
***************
*** 439,443 ****
      def _testRecv(self):
          self.cli.connect((HOST, PORT))
-         time.sleep(1)
          self.cli.send(MSG)
  
--- 486,489 ----
***************
*** 499,503 ****
          self.cli_file.flush()
  
! def main():
      suite = unittest.TestSuite()
      suite.addTest(unittest.makeSuite(GeneralModuleTests))
--- 545,549 ----
          self.cli_file.flush()
  
! def test_main():
      suite = unittest.TestSuite()
      suite.addTest(unittest.makeSuite(GeneralModuleTests))
***************
*** 509,511 ****
  
  if __name__ == "__main__":
!     main()
--- 555,557 ----
  
  if __name__ == "__main__":
!     test_main()