forked from ilastik/lazyflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestRequest.py
811 lines (653 loc) · 26.7 KB
/
testRequest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
from __future__ import print_function
from builtins import range
from builtins import object
###############################################################################
# lazyflow: data flow based lazy parallel computation framework
#
# Copyright (C) 2011-2014, the ilastik developers
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the Lesser GNU General Public License
# as published by the Free Software Foundation; either version 2.1
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# See the files LICENSE.lgpl2 and LICENSE.lgpl3 for full text of the
# GNU Lesser General Public License version 2.1 and 3 respectively.
# This information is also available on the ilastik web site at:
# http://ilastik.org/license/
###############################################################################
from lazyflow.request.request import Request, RequestLock, SimpleRequestCondition, RequestPool
import os
import time
import random
import numpy
import gc
import platform
from functools import partial
import unittest
import nose
import psutil
from lazyflow.utility.tracer import traceLogged
import threading
import sys
import logging
handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('%(levelname)s %(name)s %(message)s')
handler.setFormatter(formatter)
# Test
logger = logging.getLogger("tests.testRequestRewrite")
# Test Trace
traceLogger = logging.getLogger("TRACE." + logger.name)
TEST_WITH_SINGLE_THREADED_DEBUG_MODE = False
if TEST_WITH_SINGLE_THREADED_DEBUG_MODE:
Request.reset_thread_pool(0)
class TestRequest(unittest.TestCase):
@traceLogged(traceLogger)
def test_basic(self):
"""
Fire a couple requests and check the answer they give.
"""
def someWork():
time.sleep(0.001)
return "Hello,"
callback_result = ['']
def callback(result):
callback_result[0] = result
def test(s):
req = Request(someWork)
req.notify_finished(callback)
s2 = req.wait()
time.sleep(0.001)
return s2 + s
req = Request( partial(test, s = " World!") )
req.notify_finished(callback)
# Wait for the result
assert req.wait() == "Hello, World!" # Wait for it
assert req.wait() == "Hello, World!" # It's already finished, should be same answer
assert callback_result[0] == "Hello, World!" # From the callback
requests = []
for i in range(10):
req = Request( partial(test, s = "hallo %d" %i) )
requests.append(req)
for r in requests:
r.wait()
@traceLogged(traceLogger)
def test_callWaitDuringCallback(self):
"""
When using request.notify_finished(...) to handle request completions,
the handler should be allowed to call request.wait() on the request that it's handling.
"""
def handler(req, result):
req.wait()
def workFn():
pass
req = Request(workFn)
req.notify_finished( partial(handler, req) )
#req.submit()
req.wait()
@traceLogged(traceLogger)
def test_block_during_calback(self):
"""
It is valid for request finish handlers to fire off and wait for requests.
This tests that feature.
"""
def workload():
time.sleep(0.1)
return 1
total_result = [0]
def handler(result):
req = Request(workload)
total_result[0] = result + req.wait() # Waiting on some other request from WITHIN a request callback
req = Request( workload )
req.notify_finished( handler )
assert req.wait() == 1
assert total_result[0] == 2
@traceLogged(traceLogger)
def test_lotsOfSmallRequests(self):
"""
Fire off some reasonably large random number of nested requests.
Mostly, this test ensures that the requests all complete without a hang.
"""
handlerCounter = [0]
handlerLock = threading.Lock()
def completionHandler( result, req ):
logger.debug( "Handing completion {}".format(result) )
handlerLock.acquire()
handlerCounter[0] += 1
handlerLock.release()
req.calledHandler = True
requestCounter = [0]
requestLock = threading.Lock()
allRequests = []
# This closure randomly chooses to either (a) return immediately or (b) fire off more work
def someWork(depth, force=False, i=-1):
#print 'depth=', depth, 'i=', i
if depth > 0 and (force or random.random() > 0.5):
requests = []
for i in range(10):
req = Request( partial(someWork, depth=depth-1, i=i) )
req.notify_finished( partial(completionHandler, req=req) )
requests.append(req)
allRequests.append(req)
requestLock.acquire()
requestCounter[0] += 1
requestLock.release()
for r in requests:
r.wait()
return requestCounter[0]
req = Request( partial(someWork, depth=4, force=True) )
logger.debug("Waiting for requests...")
req.wait()
logger.debug("root request finished")
# Handler should have been called once for each request we fired
assert handlerCounter[0] == requestCounter[0]
logger.debug("finished testLotsOfSmallRequests")
for r in allRequests:
assert r.finished
logger.debug("waited for all subrequests")
@traceLogged(traceLogger)
def test_cancel_basic(self):
"""
Start a workload and cancel it. Verify that it was actually cancelled before all the work was finished.
"""
if Request.global_thread_pool.num_workers == 0:
raise nose.SkipTest
def workload():
time.sleep(0.1)
return 1
got_cancel = [False]
workcounter = [0]
def big_workload():
try:
requests = []
for i in range(100):
requests.append( Request(workload) )
for r in requests:
workcounter[0] += r.wait()
assert False, "Shouldn't get to this line. This test is designed so that big_workload should be cancelled before it finishes all its work"
for r in requests:
assert not r.cancelled
except Request.CancellationException:
got_cancel[0] = True
except Exception as ex:
import traceback
traceback.print_exc()
raise
completed = [False]
def handle_complete( result ):
completed[0] = True
req = Request( big_workload )
req.notify_finished( handle_complete )
req.submit()
while workcounter[0] == 0:
time.sleep(0.001)
req.cancel()
time.sleep(1)
assert req.cancelled
assert not completed[0]
assert got_cancel[0]
# Make sure this test is functioning properly:
# The cancellation should have occurred in the middle (not before the request even got started)
# If not, then adjust the timing of the cancellation, above.
assert workcounter[0] != 0, "This timing-sensitive test needs to be tweaked."
assert workcounter[0] != 100, "This timing-sensitive test needs to be tweaked."
@traceLogged(traceLogger)
def test_dont_cancel_shared_request(self):
"""
Test that a request isn't cancelled if it has requests pending for it.
"""
if Request.global_thread_pool.num_workers == 0:
raise nose.SkipTest
cancelled_requests = []
def f1():
time.sleep(1)
return "RESULT"
r1 = Request(f1)
r1.notify_cancelled( partial(cancelled_requests.append, 1) )
def f2():
try:
return r1.wait()
except:
cancelled_requests.append(2)
r2 = Request(f2)
def f3():
try:
return r1.wait()
except:
cancelled_requests.append(3)
r3 = Request(f3)
def otherThread():
r2.wait()
t = threading.Thread(target=otherThread)
t.start()
r3.submit()
time.sleep(0.5)
# By now both r2 and r3 are waiting for the result of r1
# Cancelling r3 should not cancel r1.
r3.cancel()
t.join() # Wait for r2 to finish
time.sleep(0.5)
assert r1.started
assert r1.finished
assert not r1.cancelled # Not cancelled, even though we cancelled a request that was waiting for it.
assert 1 not in cancelled_requests
assert r2.started
assert r2.finished
assert not r2.cancelled # Not cancelled.
assert 1 not in cancelled_requests
assert r2.wait() == "RESULT"
assert r3.started
assert r3.finished
assert r3.cancelled # Successfully cancelled.
assert 3 in cancelled_requests
@traceLogged(traceLogger)
def test_early_cancel(self):
"""
If you try to wait for a request after it's already been cancelled, you get a InvalidRequestException.
"""
def f():
pass
req = Request(f)
req.cancel()
try:
req.wait()
except Request.InvalidRequestException:
pass
else:
assert False, "Expected a Request.InvalidRequestException because we're waiting for a request that's already been cancelled."
@traceLogged(traceLogger)
def test_uncancellable(self):
"""
If a request is being waited on by a regular thread, it can't be cancelled.
"""
def workload():
time.sleep(0.1)
return 1
def big_workload():
result = 0
requests = []
for i in range(10):
requests.append( Request(workload) )
for r in requests:
result += r.wait()
return result
req = Request(big_workload)
def attempt_cancel():
time.sleep(1)
req.cancel()
# Start another thread that will try to cancel the request.
# It won't have any effect because we're already waiting for it in a non-request thread.
t = threading.Thread(target=attempt_cancel)
t.start()
result = req.wait()
assert result == 10
t.join()
@traceLogged(traceLogger)
def test_failed_request(self):
"""
A request is "failed" if it throws an exception while executing.
The exception should be forwarded to ALL waiting requests.
"""
def impossible_workload():
raise RuntimeError("Intentional exception.")
req = Request(impossible_workload)
try:
req.wait()
except RuntimeError:
pass
else:
assert False, "Expected an exception from that request, but didn't get it."
@traceLogged(traceLogger)
def test_failed_request2(self):
"""
A request is "failed" if it throws an exception while executing.
The exception should be forwarded to ALL waiting requests, which should re-raise it.
"""
class CustomRuntimeError(RuntimeError):
pass
def impossible_workload():
time.sleep(0.2)
raise CustomRuntimeError("Intentional exception.")
impossible_req = Request(impossible_workload)
def wait_for_impossible():
# This request will fail...
impossible_req.wait()
# Since there are some exception guards in the code we're testing,
# spit something out to stderr just to be sure this error
# isn't getting swallowed accidentally.
sys.stderr.write("ERROR: Shouldn't get here.")
assert False, "Shouldn't get here."
req1 = Request(wait_for_impossible)
req2 = Request(wait_for_impossible)
failed_ids = []
lock = threading.Lock()
def handle_failed_req(req_id, failure_exc, exc_info):
assert isinstance(failure_exc, CustomRuntimeError)
with lock:
failed_ids.append(req_id)
req1.notify_failed( partial(handle_failed_req, 1) )
req2.notify_failed( partial(handle_failed_req, 2) )
try:
req1.submit()
except:
# submit may fail here if in single-threaded debug mode.
assert Request.global_thread_pool.num_workers == 0
try:
req2.submit()
except:
# submit may fail here if in single-threaded debug mode.
assert Request.global_thread_pool.num_workers == 0
try:
req1.wait()
except RuntimeError:
pass
else:
# submit may fail here if in single-threaded debug mode.
if Request.global_thread_pool.num_workers > 0:
assert False, "Expected an exception from that request, but didn't get it."
try:
req2.wait()
except RuntimeError:
pass
else:
# submit may fail here if in single-threaded debug mode.
if Request.global_thread_pool.num_workers > 0:
assert False, "Expected an exception from that request, but didn't get it."
assert 1 in failed_ids
assert 2 in failed_ids
'''
@traceLogged(traceLogger)
def test_old_api_support(self):
"""
For now, the request_rewrite supports the old interface, too.
"""
def someWork(destination=None):
if destination is None:
destination = [""]
time.sleep(0.001)
destination[0] = "Hello,"
return destination
callback_result = [ [] ]
def callback(result):
callback_result[0] = result[0]
def test(s, destination=None,):
req = Request(someWork)
req.onFinish(callback)
s2 = req.wait()[0]
time.sleep(0.001)
if destination is None:
destination = [""]
destination[0] = s2 + s
return destination
req = Request( partial(test, s = " World!") )
preAllocatedResult = [""]
req.writeInto(preAllocatedResult)
req.notify(callback)
# Wait for the result
assert req.wait()[0] == "Hello, World!" # Wait for it
assert callback_result[0] == "Hello, World!" # From the callback
assert preAllocatedResult[0] == req.wait()[0], "This might fail if the request was started BEFORE writeInto() was called"
requests = []
for i in range(10):
req = Request( partial(test, s = "hallo %d" %i) )
requests.append(req)
for r in requests:
r.wait()
'''
@traceLogged(traceLogger)
def test_callbacks_before_wait_returns(self):
"""
If the user adds callbacks to the request via notify_finished() BEFORE the request is submitted,
then wait() should block for the completion of all those callbacks before returning.
Any callbacks added AFTER the request has already been submitted are NOT guaranteed
to be executed before wait() returns, but they will still be executed.
"""
def someQuickWork():
return 42
callback_results = []
def slowCallback(n, result):
time.sleep(0.1)
callback_results.append(n)
req = Request( someQuickWork )
req.notify_finished( partial(slowCallback, 1) )
req.notify_finished( partial(slowCallback, 2) )
req.notify_finished( partial(slowCallback, 3) )
result = req.wait()
assert result == 42
assert callback_results == [1,2,3], "wait() returned before callbacks were complete! Got: {}".format( callback_results )
req.notify_finished( partial(slowCallback, 4) )
req.wait()
assert callback_results == [1,2,3,4], "Callback on already-finished request wasn't executed."
@traceLogged(traceLogger)
def test_request_timeout(self):
"""
Test the timeout feature when calling wait() from a foreign thread.
See wait() for details.
"""
if Request.global_thread_pool.num_workers == 0:
raise nose.SkipTest
def slowWorkload():
time.sleep( 10.0 )
req = Request( slowWorkload )
try:
req.wait(0.5)
except Request.TimeoutException:
pass
else:
assert False, "Expected to get Request.TimeoutException"
@traceLogged(traceLogger)
def testRequestLock(self):
"""
Test the special Request-aware lock.
Launch 99 requests and threads that all must fight over access to the same list.
The list will eventually be 0,1,2...99, and each request will append a single number to the list.
Each request must wait its turn before it can append it's number and finish.
"""
# This test doesn't work if the request system is working in single-threaded 'debug' mode.
# It depends on concurrent execution to make progress. Otherwise it hangs.
if Request.global_thread_pool.num_workers == 0:
raise nose.SkipTest
req_lock = RequestLock()
l = [0]
def append_n(n):
#print "Starting append_{}\n".format(n)
while True:
with req_lock:
if l[-1] == n-1:
#print "***** Appending {}".format(n)
l.append(n)
return
# Create 50 requests
N = 50
reqs = []
for i in range(1,2*N,2):
req = Request( partial(append_n, i) )
reqs.append(req)
# Create 49 threads
thrds = []
for i in range(2,2*N,2):
thrd = threading.Thread( target=partial(append_n, i) )
thrds.append(thrd)
# Submit in reverse order to ensure that no request finishes until they have all been started.
# This proves that the requests really are being suspended.
for req in reversed(reqs):
req.submit()
# Start all the threads
for thrd in reversed(thrds):
thrd.start()
# All requests must finish
for req in reqs:
req.wait()
# All threads should finish
for thrd in thrds:
thrd.join()
assert l == list(range(100)), "Requests and/or threads finished in the wrong order!"
def testRequestLockSemantics(self):
"""
To be used with threading.Condition, RequestLock objects MUST NOT have RLock semantics.
It is important that the RequestLock is NOT re-entrant.
"""
if Request.global_thread_pool.num_workers == 0:
raise nose.SkipTest
with RequestLock() as lock:
assert not lock.acquire(0)
def test_result_discarded(self):
"""
After a request is deleted, its result should be discarded.
"""
import weakref
from functools import partial
def f():
return numpy.zeros( (10,), dtype=numpy.uint8 ) + 1
w = [None]
def onfinish(r, result):
w[0] = weakref.ref(result)
req = Request(f)
req.notify_finished( partial(onfinish, req) )
req.submit()
req.wait()
del req
# The ThreadPool._Worker loop has a local reference (next_task),
# so wait just a tic for the ThreadPool worker to cycle back to the top of its loop (and discard the reference)
time.sleep(0.1)
assert w[0]() is None
def testThreadPoolReset(self):
num_workers = Request.global_thread_pool.num_workers
Request.reset_thread_pool(num_workers=1)
try:
lock = threading.Lock()
def check_for_contention():
assert lock.acquire(False), "Should not be contention for this lock!"
time.sleep(0.1)
lock.release()
reqs = [Request( check_for_contention ) for x in range(10)]
for req in reqs:
req.submit()
for req in reqs:
req.wait()
finally:
# Set it back to what it was
Request.reset_thread_pool(num_workers)
print('done')
class TestRequestExceptions(object):
"""
Check for proper behavior when an exception is generated within a request:
- The worker thread main loop never sees the exception.
- The exception is propagated to ALL threads/requests that were waiting on the failed request.
- If a thread/request calls wait() on a request that has already failed, the exception is raised in the caller.
- Requests have a signal that fires when the request fails due to an exception.
"""
@traceLogged(traceLogger)
def testWorkerThreadLoopProtection(self):
"""
The worker threads should not die due to an exception raised within a request.
"""
for worker in Request.global_thread_pool.workers:
assert worker.is_alive(), "Something is wrong with this test. All workers should be alive."
def always_fails():
raise Exception("This is an intentional exception for this test.")
req = Request(always_fails)
# Must add a default fail handler or else it will log an exception by default.
req.notify_failed(lambda *args: None)
try:
req.submit()
except:
if Request.global_thread_pool.num_workers > 0:
raise
else:
if Request.global_thread_pool.num_workers == 0:
# In the single-threaded debug mode, the exception should be raised within submit()
assert False, "Expected to request to raise an Exception!"
try:
req.wait()
except:
pass
else:
if Request.global_thread_pool.num_workers > 0:
# In the single-threaded debug mode, the exception should be raised within submit()
assert False, "Expected to request to raise an Exception!"
for worker in Request.global_thread_pool.workers:
assert worker.is_alive(), "An exception was propagated to a worker run loop!"
@traceLogged(traceLogger)
def testExceptionPropagation(self):
"""
When an exception is generated in a request, the exception should be propagated to all waiting threads.
Also, the failure signal should fire.
"""
class SpecialException(Exception):
pass
def always_fails():
time.sleep(0.2)
raise SpecialException()
req1 = Request(always_fails)
def wait_for_req1():
req1.wait()
req2 = Request(wait_for_req1)
req3 = Request(wait_for_req1)
signaled_exceptions = []
def failure_handler(ex, exc_info):
signaled_exceptions.append(ex)
req2.notify_failed( failure_handler )
req3.notify_failed( failure_handler )
caught_exceptions = []
def wait_for_request(req):
try:
req.wait()
except SpecialException as ex:
caught_exceptions.append(ex)
except:
raise # Got some other exception than the one we expected
else:
assert "Expected to get an exception. Didn't get one."
th2 = threading.Thread( target=partial( wait_for_request, req2 ) )
th3 = threading.Thread( target=partial( wait_for_request, req3 ) )
th2.start()
th3.start()
th2.join()
th3.join()
assert len(caught_exceptions) == 2, "Expected both requests to catch exceptions."
assert len(signaled_exceptions) == 2, "Expected both requests to signal failure."
assert isinstance( caught_exceptions[0], SpecialException ), "Caught exception was of the wrong type."
assert caught_exceptions[0] == caught_exceptions[1] == signaled_exceptions[0] == signaled_exceptions[1]
# Attempting to wait for a request that has already failed will raise the exception that causes the failure
wait_for_request(req2)
# Subscribing to notify_failed on a request that's already failed should call the failure handler immediately.
req2.notify_failed( failure_handler )
assert len(signaled_exceptions) == 3
class TestRequestPool(object):
"""
See also: There's a separate file for other RequestPool tests...
"""
def testBasic(self):
def work():
time.sleep(0.2)
reqs = []
for _ in range(10):
reqs.append( Request( work ) )
pool = RequestPool()
for req in reqs:
pool.add(req)
pool.wait()
# Should all be done.
for req in reqs:
assert req.finished
if __name__ == "__main__":
# Logging is OFF by default when running from command-line nose, i.e.:
# nosetests thisFile.py)
# but ON by default if running this test directly, i.e.:
# python thisFile.py
logging.getLogger().addHandler( handler )
logger.setLevel(logging.DEBUG)
traceLogger.setLevel(logging.DEBUG)
import sys
import nose
sys.argv.append("--nocapture") # Don't steal stdout. Show it on the console as usual.
sys.argv.append("--nologcapture") # Don't set the logging level to DEBUG. Leave it alone.
ret = nose.run(defaultTest=__file__)
if not ret: sys.exit(1)