MCPcopy Create free account
hub / github.com/ActiveState/code / test

Function test

recipes/Python/576418_Multithreaded_FIFO_Gate/recipe-576418.py:138–287  ·  view source on GitHub ↗

Test code

()

Source from the content-addressed store, hash-verified

136
137
138def test():
139 """ Test code """
140
141 import random
142 import Queue
143
144 enterlog = Queue.Queue(0)
145 exitlog = Queue.Queue(0)
146
147 def enter(index):
148 enterlog.put(index)
149
150 def exit(index):
151 exitlog.put(index)
152
153
154 class OwnerThread(threading.Thread):
155 """ Owner thread class for gate demo """
156
157 def __init__(self, gate):
158 self.gate = gate
159 threading.Thread.__init__(self, None, 'Owner thread')
160
161 def run(self):
162 # Close the gate
163 print 'Closing gate...'
164 ret = self.gate.close()
165 if ret==0:
166 print 'Gate closed successfully'
167
168 print 'Gate count=>',self.gate.get_count()
169
170 # Open gate after sleeping some time
171 time.sleep(5)
172 if ret==0:
173 print 'Opening gate'
174 self.gate.open()
175 else:
176 print 'Gate closing not successful'
177
178
179 class SampleThread(threading.Thread):
180 """ Sample thread class for gate demo """
181
182 def __init__(self, index, gate):
183 self.gate = gate
184 self.index = index
185 threading.Thread.__init__(self, None, 'Thread %d' % self.index, None)
186
187 def run(self):
188
189 # Sleep randomly
190 time.sleep(random.choice(range(1,10)))
191 # Mark entry to gate
192 enter(self.index)
193 self.gate.enter()
194 # Mark exit out of gate
195 exit(self.index)

Callers 1

recipe-576418.pyFile · 0.70

Calls 4

getMethod · 0.95
test1Function · 0.70
test2Function · 0.70
emptyMethod · 0.45

Tested by

no test coverage detected