Пример #1
0
    def test_enabling_remote_client_registers_component(self):
        component_manager = ComponentManager()
        component_manager.clear_component_registry()

        config = ConfigManager()
        config.set(REMOTE_CLIENT_ENABLED, True)

        with ExtensionTestHarness(RemoteClientExtension, config=config):
            self.assertIn('RemoteClient', component_manager.COMPONENT_REGISTRY)
 def setUp(self):
     component_manager = ComponentManager()
     component_manager.clear_component_registry()
     self.registry = component_manager.COMPONENT_REGISTRY
     self.builder = DependencyGraphBuilder(self.registry)
Пример #3
0
class TestComponentsOnHandlers(unittest.TestCase):
    def load_components(self):
        @component(scope='message')
        class MessageScoped(object):
            def __init__(self):
                self.calls = 0

            def __call__(self, *args, **kwargs):
                self.calls += 1
                return self.calls

        @component(scope='dispatcher')
        class DispatcherScoped(object):
            def __init__(self):
                self.calls = 0

            def __call__(self, *args, **kwargs):
                self.calls += 1
                return self.calls

        @component(scope='session')
        class SessionScoped(object):
            def __init__(self):
                self.calls = 0

            def __call__(self, *args, **kwargs):
                self.calls += 1
                return self.calls

    def setUp(self):
        self.component_manager = ComponentManager()
        self.component_manager.clear_component_registry()
        self.load_components()

    def test_sequential_dispatcher_requires_component(self):
        messagebus = create_messagebus()
        comp_counts = Queue()

        @requires(comp='MessageScoped')
        def handle_message(message, comp):
            comp_counts.put(comp())

        dispatcher = SequentialDispatcher(messagebus, handle_message)
        try:
            dispatcher.register([defined_message])
            messagebus.trigger_event(defined_message,
                                     defined_endpoint,
                                     data='data1')
            self.assertEqual(comp_counts.get(timeout=1), 1)
        finally:
            dispatcher.destroy()

    def test_concurrent_dispatcher_requires_component(self):
        messagebus = create_messagebus()
        comp_counts = Queue()

        @requires(comp='MessageScoped')
        def handle_message(message, comp):
            comp_counts.put(comp())

        dispatcher = ConcurrentDispatcher(messagebus, handle_message)
        try:
            dispatcher.register([defined_message])
            messagebus.trigger_event(defined_message,
                                     defined_endpoint,
                                     data='data1')
            self.assertEqual(comp_counts.get(timeout=1), 1)
        finally:
            dispatcher.destroy()

    def test_callback_dispatcher_requires_component(self):
        messagebus = create_messagebus()
        comp_counts = Queue()

        @requires(comp='MessageScoped')
        def handle_message(message, comp):
            comp_counts.put(comp())

        dispatcher = CallbackDispatcher(messagebus, handle_message)
        try:
            dispatcher.register([defined_message])
            messagebus.trigger_event(defined_message,
                                     defined_endpoint,
                                     data='data1')
            self.assertEqual(comp_counts.get(timeout=1), 1)
        finally:
            dispatcher.destroy()

    def test_multiple_calls_give_different_message_scoped_components(self):
        messagebus = create_messagebus()
        comp_counts = Queue()

        @requires(comp='MessageScoped')
        def handle_message(message, comp):
            comp_counts.put(comp())

        dispatcher = CallbackDispatcher(messagebus, handle_message)
        try:
            dispatcher.register([defined_message])
            messagebus.trigger_event(defined_message,
                                     defined_endpoint,
                                     data='data1')
            messagebus.trigger_event(defined_message,
                                     defined_endpoint,
                                     data='data2')
            self.assertEqual(comp_counts.get(timeout=1), 1)
            self.assertEqual(comp_counts.get(timeout=1), 1)
        finally:
            dispatcher.destroy()

    def test_multiple_calls_give_same_dispatcher_scoped_component(self):
        messagebus = create_messagebus()
        comp_counts_a = Queue()
        comp_counts_b = Queue()

        @requires(comp='DispatcherScoped')
        def handle_message_a(message, comp):
            comp_counts_a.put(comp())

        @requires(comp='DispatcherScoped')
        def handle_message_b(message, comp):
            comp_counts_b.put(comp())

        dispatcher_a = CallbackDispatcher(messagebus, handle_message_a)
        dispatcher_b = CallbackDispatcher(messagebus, handle_message_b)
        try:
            try:
                dispatcher_a.register([defined_message])
                dispatcher_b.register([defined_message])
                messagebus.trigger_event(defined_message,
                                         defined_endpoint,
                                         data='data1')
                messagebus.trigger_event(defined_message,
                                         defined_endpoint,
                                         data='data2')
                self.assertEqual(comp_counts_a.get(timeout=1), 1)
                self.assertEqual(comp_counts_a.get(timeout=1), 2)
                self.assertEqual(comp_counts_b.get(timeout=1), 1)
                self.assertEqual(comp_counts_b.get(timeout=1), 2)
            finally:
                dispatcher_a.destroy()
                messagebus.trigger_event(defined_message,
                                         defined_endpoint,
                                         data='data2')
                assert comp_counts_a.empty()
                self.assertEqual(comp_counts_b.get(timeout=1), 3)
        finally:
            dispatcher_b.destroy()

    def test_multiple_calls_give_same_session_scoped_component(self):
        messagebus = create_messagebus()
        comp_counts = Queue()

        @requires(comp='SessionScoped')
        def handle_message(message, comp):
            comp_counts.put(comp())

        dispatcher = CallbackDispatcher(messagebus, handle_message)
        try:
            dispatcher.register([defined_message])
            messagebus.trigger_event(defined_message,
                                     defined_endpoint,
                                     data='data1')
            messagebus.trigger_event(defined_message,
                                     defined_endpoint,
                                     data='data2')
            self.assertEqual(comp_counts.get(timeout=1), 1)
            self.assertEqual(comp_counts.get(timeout=1), 2)
        finally:
            dispatcher.destroy()

    def test_message_entity_is_used_to_fixate_entities(self):
        messagebus = create_messagebus()
        calls = Queue()

        @component(name='A')
        class a1():
            entity = 'a1'

        @component(name='A')
        class a2():
            entity = 'a2'

        @requires(a='A')
        def handle_message(message, a):
            calls.put(a.entity)

        dispatcher = CallbackDispatcher(messagebus, handle_message)
        try:
            dispatcher.register([defined_message], entities=['a1', 'a2'])
            messagebus.trigger_event(defined_message,
                                     defined_endpoint,
                                     entity='a2',
                                     data='data1')
            self.assertEqual(calls.get(timeout=1), 'a2')
            messagebus.trigger_event(defined_message,
                                     defined_endpoint,
                                     entity='a1',
                                     data='data2')
            self.assertEqual(calls.get(timeout=1), 'a1')
        finally:
            dispatcher.destroy()

    def test_combination_of_fixated_and_not_fixated_entities(self):
        messagebus = create_messagebus()
        calls = Queue()

        @component(name='A')
        class a1():
            entity = 'a1'

        @component(name='A', can=['2'])
        class a2():
            entity = 'a2'

        @requires(a='A')
        @requires(a2='A', can=['2'], fixate_entities=False)
        def handle_message(message, a, a2):
            calls.put(a.entity)
            calls.put(a2.entity)

        dispatcher = CallbackDispatcher(messagebus, handle_message)
        try:
            dispatcher.register([defined_message], entities=['a1', 'a2'])
            messagebus.trigger_event(defined_message,
                                     defined_endpoint,
                                     entity='a2',
                                     data='data1')
            self.assertEqual(calls.get(timeout=1), 'a2')
            self.assertEqual(calls.get(timeout=1), 'a2')
            messagebus.trigger_event(defined_message,
                                     defined_endpoint,
                                     entity='a1',
                                     data='data2')
            self.assertEqual(calls.get(timeout=1), 'a1')
            self.assertEqual(calls.get(timeout=1), 'a2')
        finally:
            dispatcher.destroy()
Пример #4
0
class TestTestRunnerComponents(unittest.TestCase):
    class Verifier(object):
        def __init__(self):
            self.data = []

    def load_components(self):
        @component()
        @requires(verifier='Verifier')
        class MyComponent(object):
            def __init__(self, verifier):
                self.verifier = verifier

            def __enter__(self):
                self.verifier.data.append('enter')
                return self

            def __exit__(self, exc_type, exc_val, exc_tb):
                self.verifier.data.append('exit')

            def __call__(self, *args, **kwargs):
                self.verifier.data.append('call')

        @component()
        @requires(verifier='Verifier')
        class MyEnterFailComponent(object):
            def __init__(self, verifier):
                self.verifier = verifier

            def __enter__(self):
                self.verifier.data.append('enter')
                raise (Exception('enter fails'))

            def __exit__(self, exc_type, exc_val, exc_tb):
                self.verifier.data.append('exit')

            def __call__(self, *args, **kwargs):
                self.verifier.data.append('call')

        @component()
        @requires(verifier='Verifier')
        class MyExitFailComponent(object):
            def __init__(self, verifier):
                self.verifier = verifier

            def __enter__(self):
                self.verifier.data.append('enter')
                return self

            def __exit__(self, exc_type, exc_val, exc_tb):
                self.verifier.data.append('exit')
                raise (Exception('exit fails'))

            def __call__(self, *args, **kwargs):
                self.verifier.data.append('call')

    def setUp(self):
        self.component_manager = ComponentManager()
        self.component_manager.clear_component_registry()
        component(scope='session')(TestTestRunnerComponents.Verifier)
        self.load_components()
        self.verifier = TestTestRunnerComponents.Verifier()
        self.parent_scope = Scope('session', None)
        self.parent_scope.register_instance(
            _InstanceId(TestTestRunnerComponents.Verifier, ()), self.verifier)
        self.run_queue = []
        self.messagebus = Mock()

        def send_request(message_id,
                         endpoint_id=None,
                         entities=None,
                         data=None):
            futures = Mock()
            if message_id == SCHEDULE_NEXT_TEST:
                future = Mock()
                if self.run_queue:
                    future.result.return_value = self.run_queue.pop(0)
                else:
                    future.result.return_value = None

                futures.wait.return_value = [future]
            return futures

        self.messagebus.send_request.side_effect = send_request
        self.tr = TestRunner(self.messagebus,
                             Factory(self.component_manager),
                             'suite-name',
                             parent_scope=self.parent_scope)

    def test_enters_and_exit_called_on_component_with_test_scope(self):
        @requires(comp='MyComponent', scope='test')
        def my_test_case(comp):
            comp()

        self.run_queue.append(TestCaseDefinition(my_test_case))
        self.tr.run()
        assert 'enter' in self.verifier.data
        assert 'call' in self.verifier.data
        assert 'exit' in self.verifier.data

    def test_enters_and_exit_called_on_component_with_module_scope(self):
        @requires(comp='MyComponent', scope='module')
        def my_test_case(comp):
            comp()

        self.run_queue.append(TestCaseDefinition(my_test_case))
        self.tr.run()
        assert 'enter' in self.verifier.data
        assert 'call' in self.verifier.data
        assert 'exit' in self.verifier.data

    def test_enters_and_exit_called_on_component_with_class_scope(self):
        class TestMyTestCase(object):
            @requires(comp='MyComponent', scope='class')
            def my_test_case(self, comp):
                comp()

        self.run_queue.append(TestCaseDefinition(
            TestMyTestCase().my_test_case))
        self.tr.run()
        assert 'enter' in self.verifier.data
        assert 'call' in self.verifier.data
        assert 'exit' in self.verifier.data

    def test_enters_and_exit_called_on_component_with_runner_scope(self):
        @requires(comp='MyComponent', scope='module')
        def my_test_case(comp):
            comp()

        self.run_queue.append(TestCaseDefinition(my_test_case))
        self.tr.run()
        assert 'enter' in self.verifier.data
        assert 'call' in self.verifier.data
        assert 'exit' in self.verifier.data

    def test_enter_and_exit_called_once_on_class_scope_component_for_test_cases_in_same_class(
            self):
        class TestMyTestCase(object):
            @requires(comp='MyComponent', scope='class')
            def my_test_case1(self, comp):
                comp()

            @requires(comp='MyComponent', scope='class')
            def my_test_case2(self, comp):
                comp()

        test_case_class = TestMyTestCase()
        self.run_queue.extend([
            TestCaseDefinition(test_case_class.my_test_case1),
            TestCaseDefinition(test_case_class.my_test_case2)
        ])
        self.tr.run()
        assert self.verifier.data.count('enter') == 1
        assert self.verifier.data.count('call') == 2
        assert self.verifier.data.count('exit') == 1

    def test_enter_and_exit_called_once_on_module_scope_component_for_test_cases_in_same_module(
            self):
        @requires(comp='MyComponent', scope='module')
        def my_test_case1(comp):
            comp()

        @requires(comp='MyComponent', scope='module')
        def my_test_case2(comp):
            comp()

        self.run_queue.extend([
            TestCaseDefinition(my_test_case1),
            TestCaseDefinition(my_test_case2)
        ])
        self.tr.run()
        assert self.verifier.data.count('enter') == 1
        assert self.verifier.data.count('call') == 2
        assert self.verifier.data.count('exit') == 1

    def test_enter_and_exit_called_once_on_runner_scope_component(self):
        @requires(comp='MyComponent', scope='runner')
        def my_test_case1(comp):
            comp()

        @requires(comp='MyComponent', scope='runner')
        def my_test_case2(comp):
            comp()

        self.run_queue.extend([
            TestCaseDefinition(my_test_case1),
            TestCaseDefinition(my_test_case2)
        ])
        self.tr.run()
        assert self.verifier.data.count('enter') == 1
        assert self.verifier.data.count('call') == 2
        assert self.verifier.data.count('exit') == 1

    def test_enter_fails_sets_test_case_verdict_to_error_and_exit_is_called(
            self):
        @requires(comp='MyEnterFailComponent', scope='runner')
        def my_test_case1(comp):
            comp()

        self.run_queue.extend([TestCaseDefinition(my_test_case1)])
        self.tr.run()
        assert self.tr.run_history[0].verdict == Verdict.ERROR
        assert 'enter' in self.verifier.data
        assert 'call' not in self.verifier.data
        assert 'exit' in self.verifier.data

    def test_enter_fails_sets_test_case_verdict_to_error_and_continues_to_next_test_case(
            self):
        @requires(comp='MyEnterFailComponent', scope='runner')
        def my_test_case1(comp):
            comp()

        @requires(comp='MyComponent', scope='runner')
        def my_test_case2(comp):
            comp()

        self.run_queue.extend([
            TestCaseDefinition(my_test_case1),
            TestCaseDefinition(my_test_case2)
        ])
        self.tr.run()
        assert self.tr.run_history[0].verdict == Verdict.ERROR
        assert self.tr.run_history[1].verdict == Verdict.PASSED

    def test_exit_fails_on_test_scope_aborts_test_run(self):
        @requires(comp='MyExitFailComponent', scope='test')
        def my_test_case1(comp):
            comp()

        @requires(comp='MyComponent', scope='runner')
        def my_test_case2(comp):
            comp()

        self.run_queue.extend([
            TestCaseDefinition(my_test_case1),
            TestCaseDefinition(my_test_case2)
        ])
        self.tr.run()
        assert self.tr.run_history[0].verdict == Verdict.PASSED
        assert self.tr.run_history[1].verdict == Verdict.SKIPPED

    def test_exit_fails_on_non_test_scope_aborts_test_run(self):
        class MyTestCase1(object):
            @requires(comp='MyExitFailComponent', scope='class')
            def my_test_case1(self, comp):
                comp()

        class MyTestCase2(object):
            @requires(comp='MyComponent', scope='class')
            def my_test_case2(self, comp):
                comp()

        self.run_queue.extend([
            TestCaseDefinition(MyTestCase1().my_test_case1),
            TestCaseDefinition(MyTestCase2().my_test_case2)
        ])
        self.tr.run()
        assert self.tr.run_history[0].verdict == Verdict.PASSED
        assert self.tr.run_history[1].verdict == Verdict.SKIPPED