示例#1
0
    def test_getattr(self, stats_config):
        lc = LazyClient(**stats_config)

        with pytest.raises(AttributeError) as exc_info:
            lc.missing_method()

        assert exc_info.match(
            "'LazyClient' object has no attribute 'missing_method'")
示例#2
0
    def test_timer_contextmanager_enabled(self, stats_client_cls,
                                          stats_config):
        lc = LazyClient(**stats_config)

        with lc.timer('stat', rate=2) as timer:
            pass

        assert timer is lc.client.timer.return_value.__enter__.return_value
        assert lc.client.timer.call_args_list == [call('stat', rate=2)]
示例#3
0
    def test_timer_contextmanager_disabled(self, stats_client_cls,
                                           stats_config):
        stats_config['enabled'] = False
        lc = LazyClient(**stats_config)

        with lc.timer('stat', rate=2) as timer:
            pass

        assert isinstance(timer, Mock)
        assert lc.client.timer.call_args_list == []
示例#4
0
    def test_pipeline_contextmanager_disabled(self, stats_client_cls,
                                              stats_config):
        stats_config['enabled'] = False
        lc = LazyClient(**stats_config)

        with lc.pipeline() as pipe:
            pipe.incr('foo')
            pipe.send()

        assert isinstance(pipe, Mock)
        assert lc.client.pipeline.mock_calls == []
示例#5
0
    def test_pipeline_contextmanager_enabled(self, stats_client_cls,
                                             stats_config):
        lc = LazyClient(**stats_config)

        with lc.pipeline() as pipe:
            pipe.incr('foo')
            pipe.send()

        assert pipe is lc.client.pipeline.return_value.__enter__.return_value
        assert lc.client.pipeline.mock_calls == [
            call(),
            call().__enter__(),
            call().__enter__().incr('foo'),
            call().__enter__().send(),
            call().__exit__(None, None, None)
        ]
示例#6
0
    def test_passthrough_disabled(self, method, stats_config):
        stats_config['enabled'] = False
        lc = LazyClient(**stats_config)

        delegate = getattr(lc, method)

        # make the call
        delegate(1, 2, 3, 'Darth Vader', obi_wan='Kenobi')

        assert getattr(lc.client, method).call_args_list == []
示例#7
0
    def test_passthrough_methods(self, method, stats_client_cls, stats_config):
        lc = LazyClient(**stats_config)

        delegate = getattr(lc, method)

        # make the call
        delegate(1, 2, 3, 'Darth Vader', obi_wan='Kenobi')

        # verify it's a passthrough
        assert getattr(lc.client, method).call_args_list == [
            call(1, 2, 3, 'Darth Vader', obi_wan='Kenobi')
        ]
示例#8
0
    def test_init(self, stats_client_cls, stats_config):
        lc = LazyClient(**stats_config)

        assert lc.config == dict(
            host='statsd.host',
            port=1234,
            prefix='statsd.prefix',
            maxudpsize=1024,
        )
        assert lc.enabled
        assert lc._client is None
        assert stats_client_cls.call_count == 0
示例#9
0
    def test_init(self, stats_client_cls, stats_client_cls_tcp, stats_config):
        lc = LazyClient(**stats_config)

        assert lc.config == dict(
            host='tcp.statsd.host',
            port=4321,
            prefix='tcp.statsd.prefix',
            timeout=5,
        )
        assert lc.enabled
        assert lc._client is None
        assert stats_client_cls.call_count == 0
        assert stats_client_cls_tcp.call_count == 0
示例#10
0
    def test_client(self, stats_client_cls, stats_config):
        lc = LazyClient(**stats_config)

        client = lc.client  # this is a property

        assert stats_client_cls.call_args_list == [
            call(
                host='statsd.host',
                port=1234,
                prefix='statsd.prefix',
                maxudpsize=1024,
            )
        ]
        assert client == stats_client_cls.return_value
示例#11
0
    def test_client_udp(self, stats_client_cls, stats_client_cls_tcp,
                        stats_config, proto):
        lc = LazyClient(protocol=proto, **stats_config)

        client = lc.client  # this is a property

        assert lc.protocol is Protocols.udp

        assert stats_client_cls.call_args_list == [
            call(
                host='statsd.host',
                port=1234,
                prefix='statsd.prefix',
                maxudpsize=1024,
            )
        ]
        assert client == stats_client_cls.return_value
        assert stats_client_cls_tcp.call_count == 0
示例#12
0
    def test_client(self, stats_client_cls, stats_client_cls_tcp, stats_config,
                    proto):
        del stats_config['protocol']
        lc = LazyClient(protocol=proto, **stats_config)

        client = lc.client  # this is a property

        assert lc.protocol is Protocols.tcp

        assert stats_client_cls_tcp.call_args_list == [
            call(
                host='tcp.statsd.host',
                port=4321,
                prefix='tcp.statsd.prefix',
                timeout=5,
            )
        ]
        assert client == stats_client_cls_tcp.return_value
        assert stats_client_cls.call_count == 0
示例#13
0
    def test_getattr(self, stats_client_cls, stats_config):
        lc = LazyClient(**stats_config)

        with pytest.raises(AttributeError):
            lc.missing_method()
示例#14
0
    def test_init_invalid_protocol(self, stats_client_cls,
                                   stats_client_cls_tcp, stats_config, proto):
        with pytest.raises(ValueError) as err:
            LazyClient(protocol=proto, **stats_config)

        assert err.match('Invalid protocol')