/usr/lib/python2.7/dist-packages/crochet/tests/test_api.py is in python-crochet 1.4.0-0ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 | """
Tests for the crochet APIs.
"""
from __future__ import absolute_import
import threading
import subprocess
import time
import gc
import sys
import weakref
import tempfile
import os
import imp
from twisted.trial.unittest import TestCase
from twisted.internet.defer import succeed, Deferred, fail, CancelledError
from twisted.python.failure import Failure
from twisted.python import threadable
from twisted.python.runtime import platform
if platform.type == "posix":
try:
from twisted.internet.process import reapAllProcesses
except (SyntaxError, ImportError):
if sys.version_info < (3, 3, 0):
raise
else:
# Process support is still not ported to Python 3 on some versions
# of Twisted.
reapAllProcesses = None
else:
# waitpid() is only necessary on POSIX:
reapAllProcesses = None
from crochet._eventloop import (EventLoop, EventualResult, TimeoutError,
ResultRegistry, ReactorStopped)
from test_setup import FakeReactor
from crochet import (_main, setup, in_reactor, retrieve_result, _store, no_setup,
run_in_reactor, wait_for_reactor, wait_for)
from crochet.tests import crochet_directory
class ResultRegistryTests(TestCase):
"""
Tests for ResultRegistry.
"""
def test_stopped_registered(self):
"""
ResultRegistery.stop() fires registered EventualResult with
ReactorStopped.
"""
registry = ResultRegistry(FakeReactor())
er = EventualResult(None, None)
registry.register(er)
registry.stop()
self.assertRaises(ReactorStopped, er.wait, timeout=0)
def test_stopped_new_registration(self):
"""
After ResultRegistery.stop() is called subsequent register() calls
raise ReactorStopped.
"""
registry = ResultRegistry(FakeReactor())
er = EventualResult(None, None)
registry.stop()
self.assertRaises(ReactorStopped, registry.register, er)
def test_stopped_already_have_result(self):
"""
ResultRegistery.stop() has no impact on registered EventualResult
which already have a result.
"""
registry = ResultRegistry(FakeReactor())
er = EventualResult(succeed(123), None)
registry.register(er)
registry.stop()
self.assertEqual(er.wait(), 123)
self.assertEqual(er.wait(), 123)
self.assertEqual(er.wait(), 123)
def test_weakref(self):
"""
Registering an EventualResult with a ResultRegistry does not prevent
it from being garbage collected.
"""
registry = ResultRegistry(FakeReactor())
er = EventualResult(None, None)
registry.register(er)
ref = weakref.ref(er)
del er
gc.collect()
self.assertIdentical(ref(), None)
def test_runs_with_lock(self):
"""
All code in ResultRegistry.stop() and register() is protected by a
lock.
"""
self.assertTrue(ResultRegistry.stop.synchronized)
self.assertTrue(ResultRegistry.register.synchronized)
class EventualResultTests(TestCase):
"""
Tests for EventualResult.
"""
def setUp(self):
self.patch(threadable, "isInIOThread", lambda: False)
def test_success_result(self):
"""
wait() returns the value the Deferred fired with.
"""
dr = EventualResult(succeed(123), None)
self.assertEqual(dr.wait(), 123)
def test_later_success_result(self):
"""
wait() returns the value the Deferred fired with, in the case where
the Deferred is fired after wait() is called.
"""
d = Deferred()
def fireSoon():
import time; time.sleep(0.01)
d.callback(345)
threading.Thread(target=fireSoon).start()
dr = EventualResult(d, None)
self.assertEqual(dr.wait(), 345)
def test_success_result_twice(self):
"""
A second call to wait() returns same value as the first call.
"""
dr = EventualResult(succeed(123), None)
self.assertEqual(dr.wait(), 123)
self.assertEqual(dr.wait(), 123)
def test_failure_result(self):
"""
wait() raises the exception the Deferred fired with.
"""
dr = EventualResult(fail(RuntimeError()), None)
self.assertRaises(RuntimeError, dr.wait)
def test_later_failure_result(self):
"""
wait() raises the exception the Deferred fired with, in the case
where the Deferred is fired after wait() is called.
"""
d = Deferred()
def fireSoon():
time.sleep(0.01)
d.errback(RuntimeError())
threading.Thread(target=fireSoon).start()
dr = EventualResult(d, None)
self.assertRaises(RuntimeError, dr.wait)
def test_failure_result_twice(self):
"""
A second call to wait() raises same value as the first call.
"""
dr = EventualResult(fail(ZeroDivisionError()), None)
self.assertRaises(ZeroDivisionError, dr.wait)
self.assertRaises(ZeroDivisionError, dr.wait)
def test_timeout(self):
"""
If no result is available, wait(timeout) will throw a TimeoutError.
"""
start = time.time()
dr = EventualResult(Deferred(), None)
self.assertRaises(TimeoutError, dr.wait, timeout=0.03)
self.assertTrue(abs(time.time() - start - 0.03) < 0.005)
def test_timeout_twice(self):
"""
If no result is available, a second call to wait(timeout) will also
result in a TimeoutError exception.
"""
dr = EventualResult(Deferred(), None)
self.assertRaises(TimeoutError, dr.wait, timeout=0.01)
self.assertRaises(TimeoutError, dr.wait, timeout=0.01)
def test_timeout_then_result(self):
"""
If a result becomes available after a timeout, a second call to
wait() will return it.
"""
d = Deferred()
dr = EventualResult(d, None)
self.assertRaises(TimeoutError, dr.wait, timeout=0.01)
d.callback(u"value")
self.assertEqual(dr.wait(), u"value")
self.assertEqual(dr.wait(), u"value")
def test_reactor_thread_disallowed(self):
"""
wait() cannot be called from the reactor thread.
"""
self.patch(threadable, "isInIOThread", lambda: True)
d = Deferred()
dr = EventualResult(d, None)
self.assertRaises(RuntimeError, dr.wait, 0)
def test_cancel(self):
"""
cancel() cancels the wrapped Deferred, running cancellation in the
event loop thread.
"""
reactor = FakeReactor()
cancelled = []
def error(f):
cancelled.append(reactor.in_call_from_thread)
cancelled.append(f)
d = Deferred().addErrback(error)
dr = EventualResult(d, _reactor=reactor)
dr.cancel()
self.assertTrue(cancelled[0])
self.assertIsInstance(cancelled[1].value, CancelledError)
def test_stash(self):
"""
EventualResult.stash() stores the object in the global ResultStore.
"""
dr = EventualResult(Deferred(), None)
uid = dr.stash()
self.assertIdentical(dr, _store.retrieve(uid))
def test_original_failure(self):
"""
original_failure() returns the underlying Failure of the Deferred
wrapped by the EventualResult.
"""
try:
1/0
except:
f = Failure()
dr = EventualResult(fail(f), None)
self.assertIdentical(dr.original_failure(), f)
def test_original_failure_no_result(self):
"""
If there is no result yet, original_failure() returns None.
"""
dr = EventualResult(Deferred(), None)
self.assertIdentical(dr.original_failure(), None)
def test_original_failure_not_error(self):
"""
If the result is not an error, original_failure() returns None.
"""
dr = EventualResult(succeed(3), None)
self.assertIdentical(dr.original_failure(), None)
def test_error_logged_no_wait(self):
"""
If the result is an error and wait() was never called, the error will
be logged once the EventualResult is garbage-collected.
"""
dr = EventualResult(fail(ZeroDivisionError()), None)
del dr
gc.collect()
excs = self.flushLoggedErrors(ZeroDivisionError)
self.assertEqual(len(excs), 1)
def test_error_logged_wait_timeout(self):
"""
If the result is an error and wait() was called but timed out, the
error will be logged once the EventualResult is garbage-collected.
"""
d = Deferred()
dr = EventualResult(d, None)
try:
dr.wait(0)
except TimeoutError:
pass
d.errback(ZeroDivisionError())
del dr
if sys.version_info[0] == 2:
sys.exc_clear()
gc.collect()
excs = self.flushLoggedErrors(ZeroDivisionError)
self.assertEqual(len(excs), 1)
def test_error_after_gc_logged(self):
"""
If the result is an error that occurs after all user references to the
EventualResult are lost, the error is still logged.
"""
d = Deferred()
dr = EventualResult(d, None)
del dr
d.errback(ZeroDivisionError())
gc.collect()
excs = self.flushLoggedErrors(ZeroDivisionError)
self.assertEqual(len(excs), 1)
def test_control_c_is_possible(self):
"""
If you're wait()ing on an EventualResult in main thread, make sure the
KeyboardInterrupt happens in timely manner.
"""
program = """\
import os, threading, signal, time, sys
import crochet
crochet.setup()
from twisted.internet.defer import Deferred
if sys.platform.startswith('win'):
signal.signal(signal.SIGBREAK, signal.default_int_handler)
sig_int=signal.CTRL_BREAK_EVENT
sig_kill=signal.SIGTERM
else:
sig_int=signal.SIGINT
sig_kill=signal.SIGKILL
def interrupt():
time.sleep(0.1) # Make sure we've hit wait()
os.kill(os.getpid(), sig_int)
time.sleep(1)
# Still running, test shall fail...
os.kill(os.getpid(), sig_kill)
t = threading.Thread(target=interrupt)
t.setDaemon(True)
t.start()
d = Deferred()
e = crochet.EventualResult(d, None)
try:
# Queue.get() has special non-interruptible behavior if not given timeout,
# so don't give timeout here.
e.wait()
except KeyboardInterrupt:
sys.exit(23)
"""
kw = { 'cwd': crochet_directory }
# on Windows the only way to interrupt a subprocess reliably is to
# create a new process group:
# http://docs.python.org/2/library/subprocess.html#subprocess.CREATE_NEW_PROCESS_GROUP
if platform.type.startswith('win'):
kw['creationflags'] = subprocess.CREATE_NEW_PROCESS_GROUP
process = subprocess.Popen([sys.executable, "-c", program], **kw)
self.assertEqual(process.wait(), 23)
def test_connect_deferred(self):
"""
If an EventualResult is created with None,
EventualResult._connect_deferred can be called later to register a
Deferred as the one it is wrapping.
"""
er = EventualResult(None, None)
self.assertRaises(TimeoutError, er.wait, 0)
d = Deferred()
er._connect_deferred(d)
self.assertRaises(TimeoutError, er.wait, 0)
d.callback(123)
self.assertEqual(er.wait(), 123)
def test_reactor_stop_unblocks_EventualResult(self):
"""
Any EventualResult.wait() calls still waiting when the reactor has
stopped will get a ReactorStopped exception.
"""
program = """\
import os, threading, signal, time, sys
from twisted.internet.defer import Deferred
from twisted.internet import reactor
import crochet
crochet.setup()
@crochet.run_in_reactor
def run():
reactor.callLater(0.1, reactor.stop)
return Deferred()
er = run()
try:
er.wait(timeout=10)
except crochet.ReactorStopped:
sys.exit(23)
"""
process = subprocess.Popen([sys.executable, "-c", program],
cwd=crochet_directory)
self.assertEqual(process.wait(), 23)
def test_reactor_stop_unblocks_EventualResult_in_threadpool(self):
"""
Any EventualResult.wait() calls still waiting when the reactor has
stopped will get a ReactorStopped exception, even if it is running in
Twisted's thread pool.
"""
program = """\
import os, threading, signal, time, sys
from twisted.internet.defer import Deferred
from twisted.internet import reactor
import crochet
crochet.setup()
@crochet.run_in_reactor
def run():
reactor.callLater(0.1, reactor.stop)
return Deferred()
result = [13]
def inthread():
er = run()
try:
er.wait(timeout=10)
except crochet.ReactorStopped:
result[0] = 23
reactor.callInThread(inthread)
time.sleep(1)
sys.exit(result[0])
"""
process = subprocess.Popen([sys.executable, "-c", program],
cwd=crochet_directory)
self.assertEqual(process.wait(), 23)
def test_immediate_cancel(self):
"""
Immediately cancelling the result of @run_in_reactor function will
still cancel the Deferred.
"""
# This depends on the way reactor runs callFromThread calls, so need
# real functional test.
program = """\
import os, threading, signal, time, sys
from twisted.internet.defer import Deferred, CancelledError
import crochet
crochet.setup()
@crochet.run_in_reactor
def run():
return Deferred()
er = run()
er.cancel()
try:
er.wait(1)
except CancelledError:
sys.exit(23)
else:
sys.exit(3)
"""
process = subprocess.Popen([sys.executable, "-c", program],
cwd=crochet_directory,)
self.assertEqual(process.wait(), 23)
def test_noWaitingDuringImport(self):
"""
EventualResult.wait() raises an exception if called while a module is
being imported.
This prevents the imports from taking a long time, preventing other
imports from running in other threads. It also prevents deadlocks,
which can happen if the code being waited on also tries to import
something.
"""
if sys.version_info[0] > 2:
from unittest import SkipTest
raise SkipTest("This test is too fragile (and insufficient) on "
"Python 3 - see "
"https://github.com/itamarst/crochet/issues/43")
directory = tempfile.mktemp()
os.mkdir(directory)
sys.path.append(directory)
self.addCleanup(sys.path.remove, directory)
with open(os.path.join(directory, "shouldbeunimportable.py"), "w") as f:
f.write("""\
from crochet import EventualResult
from twisted.internet.defer import Deferred
EventualResult(Deferred(), None).wait(1.0)
""")
self.assertRaises(RuntimeError, __import__, "shouldbeunimportable")
def test_waiting_during_different_thread_importing(self):
"""
EventualResult.wait() should work if called while a module is
being imported in a different thread. See
EventualResultTests.test_noWaitingDuringImport for the explanation of
what should happen if an import is happening in the current thread.
"""
test_complete = threading.Event()
lock_held = threading.Event()
er = EventualResult(succeed(123), None)
def other_thread():
imp.acquire_lock()
lock_held.set()
test_complete.wait()
imp.release_lock()
t = threading.Thread(target=other_thread)
t.start()
lock_held.wait()
# While the imp lock is held by the other thread, we can't
# allow exceptions/assertions to happen because trial will
# try to do an import causing a deadlock instead of a
# failure. We collect all assertion pairs (result, expected),
# wait for the import lock to be released, and then check our
# assertions at the end of the test.
assertions = []
# we want to run .wait while the other thread has the lock acquired
assertions.append((imp.lock_held(), True))
try:
assertions.append((er.wait(), 123))
finally:
test_complete.set()
assertions.append((imp.lock_held(), True))
test_complete.set()
t.join()
[self.assertEqual(result, expected) for result, expected in assertions]
class InReactorTests(TestCase):
"""
Tests for the deprecated in_reactor decorator.
"""
def test_name(self):
"""
The function decorated with in_reactor has the same name as the
original function.
"""
c = EventLoop(lambda: FakeReactor(), lambda f, g: None)
@c.in_reactor
def some_name(reactor):
pass
self.assertEqual(some_name.__name__, "some_name")
def test_in_reactor_thread(self):
"""
The function decorated with in_reactor is run in the reactor
thread, and takes the reactor as its first argument.
"""
myreactor = FakeReactor()
c = EventLoop(lambda: myreactor, lambda f, g: None)
c.no_setup()
calls = []
@c.in_reactor
def func(reactor, a, b, c):
self.assertIdentical(reactor, myreactor)
self.assertTrue(reactor.in_call_from_thread)
calls.append((a, b, c))
func(1, 2, c=3)
self.assertEqual(calls, [(1, 2, 3)])
def test_run_in_reactor_wrapper(self):
"""
in_reactor is implemented on top of run_in_reactor.
"""
wrapped = [False]
def fake_run_in_reactor(function):
def wrapper(*args, **kwargs):
wrapped[0] = True
result = function(*args, **kwargs)
wrapped[0] = False
return result
return wrapper
myreactor = FakeReactor()
c = EventLoop(lambda: myreactor, lambda f, g: None)
c.no_setup()
c.run_in_reactor = fake_run_in_reactor
@c.in_reactor
def func(reactor):
self.assertTrue(wrapped[0])
return 17
result = func()
self.assertFalse(wrapped[0])
self.assertEqual(result, 17)
class RunInReactorTests(TestCase):
"""
Tests for the run_in_reactor decorator.
"""
def test_name(self):
"""
The function decorated with run_in_reactor has the same name as the
original function.
"""
c = EventLoop(lambda: FakeReactor(), lambda f, g: None)
@c.run_in_reactor
def some_name():
pass
self.assertEqual(some_name.__name__, "some_name")
def test_run_in_reactor_thread(self):
"""
The function decorated with run_in_reactor is run in the reactor
thread.
"""
myreactor = FakeReactor()
c = EventLoop(lambda: myreactor, lambda f, g: None)
c.no_setup()
calls = []
@c.run_in_reactor
def func(a, b, c):
self.assertTrue(myreactor.in_call_from_thread)
calls.append((a, b, c))
func(1, 2, c=3)
self.assertEqual(calls, [(1, 2, 3)])
def make_wrapped_function(self):
"""
Return a function wrapped with run_in_reactor that returns its first
argument.
"""
myreactor = FakeReactor()
c = EventLoop(lambda: myreactor, lambda f, g: None)
c.no_setup()
@c.run_in_reactor
def passthrough(argument):
return argument
return passthrough
def test_deferred_success_result(self):
"""
If the underlying function returns a Deferred, the wrapper returns a
EventualResult hooked up to the Deferred.
"""
passthrough = self.make_wrapped_function()
result = passthrough(succeed(123))
self.assertIsInstance(result, EventualResult)
self.assertEqual(result.wait(), 123)
def test_deferred_failure_result(self):
"""
If the underlying function returns a Deferred, the wrapper returns a
EventualResult hooked up to the Deferred that can deal with failures
as well.
"""
passthrough = self.make_wrapped_function()
result = passthrough(fail(ZeroDivisionError()))
self.assertIsInstance(result, EventualResult)
self.assertRaises(ZeroDivisionError, result.wait)
def test_regular_result(self):
"""
If the underlying function returns a non-Deferred, the wrapper returns
a EventualResult hooked up to a Deferred wrapping the result.
"""
passthrough = self.make_wrapped_function()
result = passthrough(123)
self.assertIsInstance(result, EventualResult)
self.assertEqual(result.wait(), 123)
def test_exception_result(self):
"""
If the underlying function throws an exception, the wrapper returns a
EventualResult hooked up to a Deferred wrapping the exception.
"""
myreactor = FakeReactor()
c = EventLoop(lambda: myreactor, lambda f, g: None)
c.no_setup()
@c.run_in_reactor
def raiser():
1/0
result = raiser()
self.assertIsInstance(result, EventualResult)
self.assertRaises(ZeroDivisionError, result.wait)
def test_registry(self):
"""
@run_in_reactor registers the EventualResult in the ResultRegistry.
"""
myreactor = FakeReactor()
c = EventLoop(lambda: myreactor, lambda f, g: None)
c.no_setup()
@c.run_in_reactor
def run():
return
result = run()
self.assertIn(result, c._registry._results)
def test_wrapped_function(self):
"""
The function wrapped by @run_in_reactor can be accessed via the
`wrapped_function` attribute.
"""
c = EventLoop(lambda: None, lambda f, g: None)
def func():
pass
wrapper = c.run_in_reactor(func)
self.assertIdentical(wrapper.wrapped_function, func)
class WaitTestsMixin(object):
"""
Tests mixin for the wait_for_reactor/wait_for decorators.
"""
def setUp(self):
self.reactor = FakeReactor()
self.eventloop = EventLoop(lambda: self.reactor, lambda f, g: None)
self.eventloop.no_setup()
def decorator(self):
"""
Return a callable that decorates a function, using the decorator being
tested.
"""
raise NotImplementedError()
def make_wrapped_function(self):
"""
Return a function wrapped with the decorator being tested that returns
its first argument, or raises it if it's an exception.
"""
decorator = self.decorator()
@decorator
def passthrough(argument):
if isinstance(argument, Exception):
raise argument
return argument
return passthrough
def test_name(self):
"""
The function decorated with the wait decorator has the same name as the
original function.
"""
decorator = self.decorator()
@decorator
def some_name(argument):
pass
self.assertEqual(some_name.__name__, "some_name")
def test_wrapped_function(self):
"""
The function wrapped by the wait decorator can be accessed via the
`wrapped_function` attribute.
"""
decorator = self.decorator()
def func():
pass
wrapper = decorator(func)
self.assertIdentical(wrapper.wrapped_function, func)
def test_reactor_thread_disallowed(self):
"""
Functions decorated with the wait decorator cannot be called from the
reactor thread.
"""
self.patch(threadable, "isInIOThread", lambda: True)
f = self.make_wrapped_function()
self.assertRaises(RuntimeError, f, None)
def test_wait_for_reactor_thread(self):
"""
The function decorated with the wait decorator is run in the reactor
thread.
"""
in_call_from_thread = []
decorator = self.decorator()
@decorator
def func():
in_call_from_thread.append(self.reactor.in_call_from_thread)
in_call_from_thread.append(self.reactor.in_call_from_thread)
func()
in_call_from_thread.append(self.reactor.in_call_from_thread)
self.assertEqual(in_call_from_thread, [False, True, False])
def test_arguments(self):
"""
The function decorated with wait decorator gets all arguments passed
to the wrapper.
"""
calls = []
decorator = self.decorator()
@decorator
def func(a, b, c):
calls.append((a, b, c))
func(1, 2, c=3)
self.assertEqual(calls, [(1, 2, 3)])
def test_deferred_success_result(self):
"""
If the underlying function returns a Deferred, the wrapper returns a
the Deferred's result.
"""
passthrough = self.make_wrapped_function()
result = passthrough(succeed(123))
self.assertEqual(result, 123)
def test_deferred_failure_result(self):
"""
If the underlying function returns a Deferred with an errback, the
wrapper throws an exception.
"""
passthrough = self.make_wrapped_function()
self.assertRaises(
ZeroDivisionError, passthrough, fail(ZeroDivisionError()))
def test_regular_result(self):
"""
If the underlying function returns a non-Deferred, the wrapper returns
that result.
"""
passthrough = self.make_wrapped_function()
result = passthrough(123)
self.assertEqual(result, 123)
def test_exception_result(self):
"""
If the underlying function throws an exception, the wrapper raises
that exception.
"""
raiser = self.make_wrapped_function()
self.assertRaises(ZeroDivisionError, raiser, ZeroDivisionError())
def test_control_c_is_possible(self):
"""
A call to a decorated function responds to a Ctrl-C (i.e. with a
KeyboardInterrupt) in a timely manner.
"""
program = """\
import os, threading, signal, time, sys
import crochet
crochet.setup()
from twisted.internet.defer import Deferred
if sys.platform.startswith('win'):
signal.signal(signal.SIGBREAK, signal.default_int_handler)
sig_int=signal.CTRL_BREAK_EVENT
sig_kill=signal.SIGTERM
else:
sig_int=signal.SIGINT
sig_kill=signal.SIGKILL
def interrupt():
time.sleep(0.1) # Make sure we've hit wait()
os.kill(os.getpid(), sig_int)
time.sleep(1)
# Still running, test shall fail...
os.kill(os.getpid(), sig_kill)
t = threading.Thread(target=interrupt)
t.setDaemon(True)
t.start()
@crochet.%s
def wait():
return Deferred()
try:
wait()
except KeyboardInterrupt:
sys.exit(23)
""" % (self.DECORATOR_CALL,)
kw = { 'cwd': crochet_directory }
if platform.type.startswith('win'):
kw['creationflags'] = subprocess.CREATE_NEW_PROCESS_GROUP
process = subprocess.Popen([sys.executable, "-c", program], **kw)
self.assertEqual(process.wait(), 23)
def test_reactor_stop_unblocks(self):
"""
Any @wait_for_reactor-decorated calls still waiting when the reactor
has stopped will get a ReactorStopped exception.
"""
program = """\
import os, threading, signal, time, sys
from twisted.internet.defer import Deferred
from twisted.internet import reactor
import crochet
crochet.setup()
@crochet.%s
def run():
reactor.callLater(0.1, reactor.stop)
return Deferred()
try:
er = run()
except crochet.ReactorStopped:
sys.exit(23)
""" % (self.DECORATOR_CALL,)
process = subprocess.Popen([sys.executable, "-c", program],
cwd=crochet_directory)
self.assertEqual(process.wait(), 23)
class WaitForReactorTests(WaitTestsMixin, TestCase):
"""
Tests for the wait_for_reactor decorator.
"""
DECORATOR_CALL = "wait_for_reactor"
def decorator(self):
return self.eventloop.wait_for_reactor
class WaitForTests(WaitTestsMixin, TestCase):
"""
Tests for the wait_for_reactor decorator.
"""
DECORATOR_CALL = "wait_for(timeout=5)"
def decorator(self):
return lambda func: self.eventloop.wait_for(timeout=5)(func)
def test_timeoutRaises(self):
"""
If a function wrapped with wait_for hits the timeout, it raises
TimeoutError.
"""
@self.eventloop.wait_for(timeout=0.5)
def times_out():
return Deferred().addErrback(lambda f: f.trap(CancelledError))
start = time.time()
self.assertRaises(TimeoutError, times_out)
self.assertTrue(abs(time.time() - start - 0.5) < 0.1)
def test_timeoutCancels(self):
"""
If a function wrapped with wait_for hits the timeout, it cancels
the underlying Deferred.
"""
result = Deferred()
error = []
result.addErrback(error.append)
@self.eventloop.wait_for(timeout=0.0)
def times_out():
return result
self.assertRaises(TimeoutError, times_out)
self.assertIsInstance(error[0].value, CancelledError)
class PublicAPITests(TestCase):
"""
Tests for the public API.
"""
def test_no_sideeffects(self):
"""
Creating an EventLoop object, as is done in crochet.__init__, does not
call any methods on the objects it is created with.
"""
c = EventLoop(lambda: None, lambda f, g: 1/0, lambda *args: 1/0,
watchdog_thread=object(), reapAllProcesses=lambda: 1/0)
del c
def test_eventloop_api(self):
"""
An EventLoop object configured with the real reactor and
_shutdown.register is exposed via its public methods.
"""
from twisted.python.log import startLoggingWithObserver
from crochet import _shutdown
self.assertIsInstance(_main, EventLoop)
self.assertEqual(_main.setup, setup)
self.assertEqual(_main.no_setup, no_setup)
self.assertEqual(_main.in_reactor, in_reactor)
self.assertEqual(_main.run_in_reactor, run_in_reactor)
self.assertEqual(_main.wait_for_reactor, wait_for_reactor)
self.assertEqual(_main.wait_for, wait_for)
self.assertIdentical(_main._atexit_register, _shutdown.register)
self.assertIdentical(_main._startLoggingWithObserver,
startLoggingWithObserver)
self.assertIdentical(_main._watchdog_thread, _shutdown._watchdog)
def test_eventloop_api_reactor(self):
"""
The publicly exposed EventLoop will, when setup, use the global reactor.
"""
from twisted.internet import reactor
_main.no_setup()
self.assertIdentical(_main._reactor, reactor)
def test_retrieve_result(self):
"""
retrieve_result() calls retrieve() on the global ResultStore.
"""
dr = EventualResult(Deferred(), None)
uid = dr.stash()
self.assertIdentical(dr, retrieve_result(uid))
def test_reapAllProcesses(self):
"""
An EventLoop object configured with the real reapAllProcesses on POSIX
plaforms.
"""
self.assertIdentical(_main._reapAllProcesses, reapAllProcesses)
if platform.type != "posix":
test_reapAllProcesses.skip = "Only relevant on POSIX platforms"
if reapAllProcesses is None:
test_reapAllProcesses.skip = "Twisted does not yet support processes"
|