1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import qm
21 import Queue
22 from threading import *
23 import sys
24 import types
25
26
27
28
29
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
46 """Construct a new 'CommandThread'.
47
48 'target' -- The 'Target' that owns this thread."""
49
50 Thread.__init__(self, None, None, None)
51
52
53 self.__target = target
54
55
56
57 self.__command_queue = Queue.Queue(0)
58
59
61 """Execute the thread."""
62
63 try:
64
65
66 while 1:
67
68 command = self.__command_queue.get()
69
70
71
72 if isinstance(command, types.StringType):
73 assert command == "Stop"
74 self._Trace("Received stop command")
75 self._Stop()
76 break
77
78
79 method, desc, context = command
80 assert method == "_RunTest"
81
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
87
88
89 exc_info = sys.exc_info()
90 sys.stderr.write(qm.common.format_exception(exc_info))
91 assert 0
92
93
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
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
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
158