Exemple #1
0
 def setUp(self):
     self.reactor = FakeReactor()
     self.client = FakeKazooClient()
     self.wrapper = client._RunCallbacksInReactorThreadWrapper(self.reactor,
                                                               self.client)
     self.received_event = None
Exemple #2
0
class _RunCallbacksInReactorThreadTests(SynchronousTestCase):
    def setUp(self):
        self.reactor = FakeReactor()
        self.client = FakeKazooClient()
        self.wrapper = client._RunCallbacksInReactorThreadWrapper(self.reactor,
                                                                  self.client)
        self.received_event = None

    def test_attrs(self):
        """
        All of the attributes passed to the wrapper are available.
        """
        for self_attr, wrapper_attr in [("reactor", "_reactor"),
                                        ("client", "_client")]:
            self.assertIdentical(getattr(self, self_attr),
                                 getattr(self.wrapper, wrapper_attr))

    def _assert_in_reactor_thread(self, event):
        """
        Asserts that we would be called in the reactor thread, by checking
        that this function's caller is :meth:`FakeReactor.callFromThread`.
        """
        self.assertEqual(self.reactor.context.getContext('virtual-thread'),
                         'reactor')
        self.received_event = event

    def test_add_listener(self):
        """
        Tests that a listener is added to the underlying client, after
        being wrapped in such a way that it would be executed in the
        reactor thread.
        """
        self.wrapper.add_listener(self._assert_in_reactor_thread)
        event = object()
        internal_listener, = self.client.listeners
        internal_listener(event)
        self.assertIdentical(self.received_event, event)

    def test_remove_listener(self):
        """Removing a listener works."""
        listener = lambda state: state
        self.wrapper.add_listener(listener)
        self.assertEqual(len(self.client.listeners), 1)
        self.wrapper.remove_listener(listener)
        self.assertEqual(len(self.client.listeners), 0)

    def test_remove_nonexistent_listener(self):
        """
        Attempting to remove a listener that was never added raises an
        exception.
        """
        self.assertRaises(KeyError, self.wrapper.remove_listener, object())

    def test_regular_method(self):
        """
        Regular methods (methods that do not have a watch function) can be
        accessed through the wrapper.
        """
        self.assertIdentical(self.wrapper.close.im_func,
                             self.client.close.im_func)

    def test_watch_function_method(self):
        """
        Methods that have a watch function will get called with a wrapped
        watch function that calls the original watch function in the
        reactor thread.
        """
        self.wrapper.get("abc", watch=self._assert_in_reactor_thread)
        event = object()
        self.client.watch(event)
        self.assertIdentical(self.received_event, event)

    def test_optional_watch_functions(self):
        """
        Methods that take an optional watch function still work when the watch
        function is not provided.
        """
        self.wrapper.get("abc")
        self.assertEqual(self.client.watch, None)
Exemple #3
0
 def setUp(self):
     self.reactor = FakeReactor()
     self.client = FakeKazooClient()
     self.wrapper = client._RunCallbacksInReactorThreadWrapper(
         self.reactor, self.client)
     self.received_event = None
Exemple #4
0
 def setUp(self):
     self.reactor = FakeReactor()
     self.pool = FakeThreadPool()
     self.client = FakeKazooClient()
     self.tx_client = TxKazooClient(self.reactor, self.pool, self.client)
Exemple #5
0
class _RunCallbacksInReactorThreadTests(SynchronousTestCase):
    def setUp(self):
        self.reactor = FakeReactor()
        self.client = FakeKazooClient()
        self.wrapper = client._RunCallbacksInReactorThreadWrapper(
            self.reactor, self.client)
        self.received_event = None

    def test_attrs(self):
        """
        All of the attributes passed to the wrapper are available.
        """
        for self_attr, wrapper_attr in [("reactor", "_reactor"),
                                        ("client", "_client")]:
            self.assertIdentical(getattr(self, self_attr),
                                 getattr(self.wrapper, wrapper_attr))

    def _assert_in_reactor_thread(self, event):
        """
        Asserts that we would be called in the reactor thread, by checking
        that this function's caller is :meth:`FakeReactor.callFromThread`.
        """
        self.assertEqual(self.reactor.context.getContext('virtual-thread'),
                         'reactor')
        self.received_event = event

    def test_add_listener(self):
        """
        Tests that a listener is added to the underlying client, after
        being wrapped in such a way that it would be executed in the
        reactor thread.
        """
        self.wrapper.add_listener(self._assert_in_reactor_thread)
        event = object()
        internal_listener, = self.client.listeners
        internal_listener(event)
        self.assertIdentical(self.received_event, event)

    def test_remove_listener(self):
        """Removing a listener works."""
        def listener(state):
            return state

        self.wrapper.add_listener(listener)
        self.assertEqual(len(self.client.listeners), 1)
        self.wrapper.remove_listener(listener)
        self.assertEqual(len(self.client.listeners), 0)

    def test_remove_nonexistent_listener(self):
        """
        Attempting to remove a listener that was never added raises an
        exception.
        """
        self.assertRaises(KeyError, self.wrapper.remove_listener, object())

    def test_regular_method(self):
        """
        Regular methods (methods that do not have a watch function) can be
        accessed through the wrapper.
        """
        self.assertIdentical(self.wrapper.close.__func__,
                             self.client.close.__func__)

    def test_watch_function_method(self):
        """
        Methods that have a watch function will get called with a wrapped
        watch function that calls the original watch function in the
        reactor thread.
        """
        self.wrapper.get("abc", watch=self._assert_in_reactor_thread)
        event = object()
        self.client.watch(event)
        self.assertIdentical(self.received_event, event)

    def test_optional_watch_functions(self):
        """
        Methods that take an optional watch function still work when the watch
        function is not provided.
        """
        self.wrapper.get("abc")
        self.assertEqual(self.client.watch, None)