Exemple #1
0
def test_publisher_call(NoisyPublisher, ListenerContainer, Message,
                        publisher_config_file_name):
    """Test the publisher being called properly."""
    pub = Mock()
    NoisyPublisher.return_value = pub
    try:
        try:
            dispatcher = Dispatcher(publisher_config_file_name,
                                    publish_port=40000,
                                    publish_nameservers=["asd"])
            msg = Mock(data={
                'uri': 'original_path',
                'platform_name': 'platform'
            })
            destinations = [['url1', 'params1', 'target2'],
                            ['url2', 'params2', 'target3']]
            success = {'target2': False, 'target3': True}
            dispatcher._publish(msg, destinations, success)
            dispatcher.publisher.send.assert_called_once()
            # The message topic has been composed and uri has been replaced
            msg_call = call('/topic/platform', 'file', {
                'uri': 'url2',
                'platform_name': 'platform'
            })
            assert msg_call in Message.mock_calls
        finally:
            if dispatcher is not None:
                dispatcher.close()
                dispatcher.publisher.stop.assert_called()
    finally:
        os.remove(publisher_config_file_name)
def test_publisher(NoisyPublisher, ListenerContainer, Message):
    """Test the publisher is initialized."""
    pub = Mock()
    NoisyPublisher.return_value = pub
    try:
        with NamedTemporaryFile('w', delete=False) as config_file:
            config_file_name = config_file.name
            config_file.write(test_yaml_pub)
            config_file.flush()
            config_file.close()
            try:
                dp = Dispatcher(config_file_name)
                assert dp.publisher is None
                NoisyPublisher.assert_not_called()
            finally:
                if dp is not None:
                    dp.close()
            try:
                dp = Dispatcher(config_file_name, publish_nameservers=["asd"])
                assert dp.publisher is None
                NoisyPublisher.assert_not_called()
            finally:
                if dp is not None:
                    dp.close()
            try:
                dp = Dispatcher(config_file_name, publish_port=0)
                init_call = call("dispatcher", port=0, nameservers=None)
                assert init_call in NoisyPublisher.mock_calls
            finally:
                if dp is not None:
                    dp.close()
                dp.publisher.stop.assert_called()
            try:
                dp = Dispatcher(config_file_name, publish_port=40000)
                init_call = call("dispatcher", port=40000, nameservers=None)
                assert init_call in NoisyPublisher.mock_calls
            finally:
                if dp is not None:
                    dp.close()
                assert len(dp.publisher.stop.mock_calls) == 2
            try:
                dp = Dispatcher(config_file_name,
                                publish_port=40000,
                                publish_nameservers=["asd"])

                assert dp.publisher is pub
                init_call = call("dispatcher", port=40000, nameservers=["asd"])
                assert init_call in NoisyPublisher.mock_calls

                msg = Mock(data={
                    'uri': 'original_path',
                    'platform_name': 'platform'
                })
                destinations = [['url1', 'params1', 'target2'],
                                ['url2', 'params2', 'target3']]
                success = {'target2': False, 'target3': True}
                dp._publish(msg, destinations, success)
                dp.publisher.send.assert_called_once()
                # The message topic has been composed and uri has been replaced
                msg_call = call('/topic/platform', 'file', {
                    'uri': 'url2',
                    'platform_name': 'platform'
                })
                assert msg_call in Message.mock_calls
            finally:
                if dp is not None:
                    dp.close()
                    dp.publisher.stop.assert_called()
    finally:
        os.remove(config_file_name)