99
1010QUEUE_SIZE = 5
1111
12+ def qfull (q ):
13+ return q .maxsize > 0 and q .qsize () == q .maxsize
14+
1215# A thread to run a function that unclogs a blocked Queue.
1316class _TriggerThread (threading .Thread ):
1417 def __init__ (self , fn , args ):
@@ -96,7 +99,7 @@ def _get(self):
9699 return Queue .Queue ._get (self )
97100
98101def FailingQueueTest (q ):
99- if not q . empty ():
102+ if q . qsize ():
100103 raise RuntimeError ("Call this function with an empty queue" )
101104 for i in range (QUEUE_SIZE - 1 ):
102105 q .put (i )
@@ -114,7 +117,7 @@ def FailingQueueTest(q):
114117 except FailingQueueException :
115118 pass
116119 q .put ("last" )
117- verify (q . full ( ), "Queue should be full" )
120+ verify (qfull ( q ), "Queue should be full" )
118121 # Test a failing blocking put
119122 q .fail_next_put = True
120123 try :
@@ -136,34 +139,34 @@ def FailingQueueTest(q):
136139 # Check the Queue isn't damaged.
137140 # put failed, but get succeeded - re-add
138141 q .put ("last" )
139- verify (q . full ( ), "Queue should be full" )
142+ verify (qfull ( q ), "Queue should be full" )
140143 q .get ()
141- verify (not q . full ( ), "Queue should not be full" )
144+ verify (not qfull ( q ), "Queue should not be full" )
142145 q .put ("last" )
143- verify (q . full ( ), "Queue should be full" )
146+ verify (qfull ( q ), "Queue should be full" )
144147 # Test a blocking put
145148 _doBlockingTest ( q .put , ("full" ,), q .get , ())
146149 # Empty it
147150 for i in range (QUEUE_SIZE ):
148151 q .get ()
149- verify (q . empty (), "Queue should be empty" )
152+ verify (not q . qsize (), "Queue should be empty" )
150153 q .put ("first" )
151154 q .fail_next_get = True
152155 try :
153156 q .get ()
154157 raise TestFailed ("The queue didn't fail when it should have" )
155158 except FailingQueueException :
156159 pass
157- verify (not q . empty (), "Queue should not be empty" )
160+ verify (q . qsize (), "Queue should not be empty" )
158161 q .fail_next_get = True
159162 try :
160163 q .get (timeout = 0.1 )
161164 raise TestFailed ("The queue didn't fail when it should have" )
162165 except FailingQueueException :
163166 pass
164- verify (not q . empty (), "Queue should not be empty" )
167+ verify (q . qsize (), "Queue should not be empty" )
165168 q .get ()
166- verify (q . empty (), "Queue should be empty" )
169+ verify (not q . qsize (), "Queue should be empty" )
167170 q .fail_next_get = True
168171 try :
169172 _doExceptionalBlockingTest (q .get , (), q .put , ('empty' ,),
@@ -172,12 +175,12 @@ def FailingQueueTest(q):
172175 except FailingQueueException :
173176 pass
174177 # put succeeded, but get failed.
175- verify (not q . empty (), "Queue should not be empty" )
178+ verify (q . qsize (), "Queue should not be empty" )
176179 q .get ()
177- verify (q . empty (), "Queue should be empty" )
180+ verify (not q . qsize (), "Queue should be empty" )
178181
179182def SimpleQueueTest (q ):
180- if not q . empty ():
183+ if q . qsize ():
181184 raise RuntimeError ("Call this function with an empty queue" )
182185 # I guess we better check things actually queue correctly a little :)
183186 q .put (111 )
@@ -186,10 +189,10 @@ def SimpleQueueTest(q):
186189 "Didn't seem to queue the correct data!" )
187190 for i in range (QUEUE_SIZE - 1 ):
188191 q .put (i )
189- verify (not q . empty (), "Queue should not be empty" )
190- verify (not q . full ( ), "Queue should not be full" )
192+ verify (q . qsize (), "Queue should not be empty" )
193+ verify (not qfull ( q ), "Queue should not be full" )
191194 q .put ("last" )
192- verify (q . full ( ), "Queue should be full" )
195+ verify (qfull ( q ), "Queue should be full" )
193196 try :
194197 q .put ("full" , block = 0 )
195198 raise TestFailed ("Didn't appear to block with a full queue" )
@@ -206,7 +209,7 @@ def SimpleQueueTest(q):
206209 # Empty it
207210 for i in range (QUEUE_SIZE ):
208211 q .get ()
209- verify (q . empty (), "Queue should be empty" )
212+ verify (not q . qsize (), "Queue should be empty" )
210213 try :
211214 q .get (block = 0 )
212215 raise TestFailed ("Didn't appear to block with an empty queue" )
0 commit comments