Package qm :: Package test :: Module command_thread
[hide private]
[frames] | no frames]

Source Code for Module qm.test.command_thread

  1  ######################################################################## 
  2  # 
  3  # File:   command_thread.py 
  4  # Author: Mark Mitchell 
  5  # Date:   01/02/2002 
  6  # 
  7  # Contents: 
  8  #   CommandThread 
  9  # 
 10  # Copyright (c) 2002 by CodeSourcery, LLC.  All rights reserved.  
 11  # 
 12  # For license terms see the file COPYING. 
 13  # 
 14  ######################################################################## 
 15   
 16  ######################################################################## 
 17  # Imports 
 18  ######################################################################## 
 19   
 20  import qm 
 21  import Queue 
 22  from   threading import * 
 23  import sys 
 24  import types 
 25   
 26  ######################################################################## 
 27  # Classes 
 28  ######################################################################## 
 29   
30 -class CommandThread(Thread):
31 """A 'CommandThread' is a thread that executes commands. 32 33 The commands are written to a 'Queue' by a controlling thread. 34 The 'CommandThread' extracts the commands and dispatches them to 35 derived class methods that process them. This class is used as a 36 base class for thread classes used by some targets. 37 38 The commands are written to the 'Queue' as Python objects. The 39 normal commands have the form '(method, descriptor, context)' 40 where 'method' is a string. At present, the only value used for 41 'method' is '_RunTest'. In that case 'descriptor' is a test 42 descriptor and 'context' is a 'Context'. The 'Stop' command is 43 provided as a simple string, not a tuple.""" 44
45 - def __init__(self, target):
46 """Construct a new 'CommandThread'. 47 48 'target' -- The 'Target' that owns this thread.""" 49 50 Thread.__init__(self, None, None, None) 51 52 # Remember the target. 53 self.__target = target 54 55 # Create the queue to which the controlling thread will 56 # write commands. 57 self.__command_queue = Queue.Queue(0)
58 59
60 - def run(self):
61 """Execute the thread.""" 62 63 try: 64 # Process commands from the queue, until the "quit" 65 # command is received. 66 while 1: 67 # Read the command. 68 command = self.__command_queue.get() 69 70 # If the command is just a string, it should be 71 # the 'Stop' command. 72 if isinstance(command, types.StringType): 73 assert command == "Stop" 74 self._Trace("Received stop command") 75 self._Stop() 76 break 77 78 # Decompose command. 79 method, desc, context = command 80 assert method == "_RunTest" 81 # Run it. 82 self._Trace("About to run test " + desc.GetId()) 83 self._RunTest(desc, context) 84 self._Trace("Finished running test " + desc.GetId()) 85 except: 86 # Exceptions should not occur in the above loop. However, 87 # in the event that one does occur it is easier to debug 88 # QMTest is the exception is written out. 89 exc_info = sys.exc_info() 90 sys.stderr.write(qm.common.format_exception(exc_info)) 91 assert 0
92 93
94 - def GetTarget(self):
95 """Return the 'Target' associated with this thread. 96 97 returns -- The 'Target' with which this thread is associated. 98 99 Derived classes must not override this method.""" 100 101 return self.__target
102 103
104 - def RunTest(self, descriptor, context):
105 """Run the test given by 'descriptor'. 106 107 'descriptor' -- The 'TestDescriptor' for the test to be run. 108 109 'context' -- The 'Context' in which to run the test. 110 111 This method is called by the controlling thread. 112 113 Derived classes must not override this method.""" 114 115 self.__command_queue.put(("_RunTest", descriptor, context))
116 117
118 - def Stop(self):
119 """Stop the thread. 120 121 Derived classes must not override this method.""" 122 123 self.__command_queue.put("Stop")
124 125
126 - def _RunTest(self, descriptor, context):
127 """Run the test given by 'descriptor'. 128 129 'descriptor' -- The 'TestDescriptor' for the test to be run. 130 131 'context' -- The 'Context' in which to run the test. 132 133 Derived classes must override this method.""" 134 135 raise NotImplementedError
136 137
138 - def _Stop(self):
139 """Stop the thread. 140 141 This method is called in the thread after 'Stop' is called 142 from the controlling thread. Derived classes can use this 143 method to release resources before the thread is destroyed. 144 145 Derived classes may override this method.""" 146 147 pass
148 149
150 - def _Trace(self, message):
151 """Write a trace 'message'. 152 153 'message' -- A string to be output as a trace message.""" 154 155 if __debug__: 156 tracer = qm.test.cmdline.get_qmtest().GetTracer() 157 tracer.Write(message, "command_thread")
158