|
13 | 13 | import tempfile
|
14 | 14 | import os
|
15 | 15 | import imp
|
| 16 | +import inspect |
16 | 17 |
|
17 | 18 | from twisted.trial.unittest import TestCase
|
18 | 19 | from twisted.internet.defer import succeed, Deferred, fail, CancelledError
|
@@ -641,6 +642,19 @@ class RunInReactorTests(TestCase):
|
641 | 642 | Tests for the run_in_reactor decorator.
|
642 | 643 | """
|
643 | 644 |
|
| 645 | + def test_signature(self): |
| 646 | + """ |
| 647 | + The function decorated with the run_in_reactor decorator has the same |
| 648 | + signature as the original function. |
| 649 | + """ |
| 650 | + c = EventLoop(lambda: FakeReactor(), lambda f, g: None) |
| 651 | + |
| 652 | + def some_name(arg1, arg2, karg1=2, *args, **kw): |
| 653 | + pass |
| 654 | + decorated = c.run_in_reactor(some_name) |
| 655 | + self.assertEqual(inspect.getargspec(some_name), |
| 656 | + inspect.getargspec(decorated)) |
| 657 | + |
644 | 658 | def test_name(self):
|
645 | 659 | """
|
646 | 660 | The function decorated with run_in_reactor has the same name as the
|
@@ -672,6 +686,48 @@ def func(a, b, c):
|
672 | 686 | func(1, 2, c=3)
|
673 | 687 | self.assertEqual(calls, [(1, 2, 3)])
|
674 | 688 |
|
| 689 | + def test_method(self): |
| 690 | + """ |
| 691 | + The function decorated with the wait decorator can be a method. |
| 692 | + """ |
| 693 | + myreactor = FakeReactor() |
| 694 | + c = EventLoop(lambda: myreactor, lambda f, g: None) |
| 695 | + c.no_setup() |
| 696 | + calls = [] |
| 697 | + |
| 698 | + class C(object): |
| 699 | + @c.run_in_reactor |
| 700 | + def func(self, a, b, c): |
| 701 | + calls.append((self, a, b, c)) |
| 702 | + |
| 703 | + o = C() |
| 704 | + o.func(1, 2, c=3) |
| 705 | + self.assertEqual(calls, [(o, 1, 2, 3)]) |
| 706 | + |
| 707 | + def test_classmethod(self): |
| 708 | + """ |
| 709 | + The function decorated with the wait decorator can be a classmethod. |
| 710 | + """ |
| 711 | + myreactor = FakeReactor() |
| 712 | + c = EventLoop(lambda: myreactor, lambda f, g: None) |
| 713 | + c.no_setup() |
| 714 | + calls = [] |
| 715 | + |
| 716 | + class C(object): |
| 717 | + @c.run_in_reactor |
| 718 | + @classmethod |
| 719 | + def func(cls, a, b, c): |
| 720 | + calls.append((cls, a, b, c)) |
| 721 | + |
| 722 | + @classmethod |
| 723 | + @c.run_in_reactor |
| 724 | + def func2(cls, a, b, c): |
| 725 | + calls.append((cls, a, b, c)) |
| 726 | + |
| 727 | + C.func(1, 2, c=3) |
| 728 | + C.func2(1, 2, c=3) |
| 729 | + self.assertEqual(calls, [(C, 1, 2, 3), (C, 1, 2, 3)]) |
| 730 | + |
675 | 731 | def make_wrapped_function(self):
|
676 | 732 | """
|
677 | 733 | Return a function wrapped with run_in_reactor that returns its first
|
@@ -809,6 +865,19 @@ def some_name(argument):
|
809 | 865 |
|
810 | 866 | self.assertEqual(some_name.__name__, "some_name")
|
811 | 867 |
|
| 868 | + def test_signature(self): |
| 869 | + """ |
| 870 | + The function decorated with the wait decorator has the same signature |
| 871 | + as the original function. |
| 872 | + """ |
| 873 | + decorator = self.decorator() |
| 874 | + |
| 875 | + def some_name(arg1, arg2, karg1=2, *args, **kw): |
| 876 | + pass |
| 877 | + decorated = decorator(some_name) |
| 878 | + self.assertEqual(inspect.getargspec(some_name), |
| 879 | + inspect.getargspec(decorated)) |
| 880 | + |
812 | 881 | def test_wrapped_function(self):
|
813 | 882 | """
|
814 | 883 | The function wrapped by the wait decorator can be accessed via the
|
@@ -863,6 +932,28 @@ def func(a, b, c):
|
863 | 932 | func(1, 2, c=3)
|
864 | 933 | self.assertEqual(calls, [(1, 2, 3)])
|
865 | 934 |
|
| 935 | + def test_classmethod(self): |
| 936 | + """ |
| 937 | + The function decorated with the wait decorator can be a classmethod. |
| 938 | + """ |
| 939 | + calls = [] |
| 940 | + decorator = self.decorator() |
| 941 | + |
| 942 | + class C(object): |
| 943 | + @decorator |
| 944 | + @classmethod |
| 945 | + def func(cls, a, b, c): |
| 946 | + calls.append((a, b, c)) |
| 947 | + |
| 948 | + @classmethod |
| 949 | + @decorator |
| 950 | + def func2(cls, a, b, c): |
| 951 | + calls.append((a, b, c)) |
| 952 | + |
| 953 | + C.func(1, 2, c=3) |
| 954 | + C.func2(1, 2, c=3) |
| 955 | + self.assertEqual(calls, [(1, 2, 3), (1, 2, 3)]) |
| 956 | + |
866 | 957 | def test_deferred_success_result(self):
|
867 | 958 | """
|
868 | 959 | If the underlying function returns a Deferred, the wrapper returns a
|
|
0 commit comments