Skip to content

Commit

Permalink
Merge pull request #363 from mattbennett/cover-tests-too
Browse files Browse the repository at this point in the history
require 100% coverage of test files
  • Loading branch information
mattbennett committed Oct 14, 2016
2 parents 84746ac + 75900db commit 5ace953
Show file tree
Hide file tree
Showing 21 changed files with 109 additions and 173 deletions.
3 changes: 3 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
concurrency = eventlet
parallel = true
data_file = /tmp/.coverage
source =
nameko
test

[report]
show_missing = true
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pylint:
pylint --rcfile=pylintrc nameko -E

test_lib:
py.test test --cov=$(CURDIR)/nameko --cov-config=$(CURDIR)/.coveragerc
py.test test --cov --cov-config=$(CURDIR)/.coveragerc

test_examples:
py.test docs/examples/test --cov=docs/examples
Expand Down
4 changes: 1 addition & 3 deletions test/broken_sample.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import non_existent

non_existent # pyflakes
import non_existent # noqa: F401
4 changes: 1 addition & 3 deletions test/standalone/test_rpc_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class ExampleService(object):

def callback(self):
# to be patched out with mock
pass
pass # pragma: no cover

@rpc
def method(self, arg):
Expand Down Expand Up @@ -362,8 +362,6 @@ def test_recover_from_keyboardinterrupt(
container.stop() # but make sure call doesn't complete

with ServiceRpcProxy('foobar', rabbit_config) as proxy:
def call():
return proxy.spam(ham=0)

with patch('kombu.connection.Connection.drain_events') as drain_events:
drain_events.side_effect = KeyboardInterrupt('killing from test')
Expand Down
16 changes: 6 additions & 10 deletions test/test_broker.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import uuid

import eventlet
from eventlet.event import Event
from mock import Mock, call, patch, ANY
import pytest
import uuid
from eventlet.event import Event
from mock import ANY, Mock, call, patch

from nameko.events import event_handler
from nameko.exceptions import RpcConnectionError
from nameko.rpc import rpc, RpcProxy
from nameko.rpc import RpcProxy, rpc
from nameko.standalone.events import event_dispatcher
from nameko.standalone.rpc import ServiceRpcProxy
from nameko.testing.services import entrypoint_hook, dummy
from nameko.testing.services import dummy, entrypoint_hook
from nameko.testing.utils import (
assert_stops_raising, get_rabbit_connections, reset_rabbit_connections)


disconnect_now = Event()
disconnected = Event()
method_called = Mock()
Expand Down Expand Up @@ -71,10 +71,6 @@ class ProxyService(object):

example_rpc = RpcProxy('exampleservice')

@dummy
def echo(self, arg):
return self.example_rpc.echo(arg)

@dummy
def entrypoint(self, arg):
return self.example_rpc.method(arg)
Expand Down
9 changes: 2 additions & 7 deletions test/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,6 @@ def ham(self):
def egg(self):
raise egg_error

@foobar
def wait(self):
while True:
sleep()


@pytest.fixture
def container():
Expand All @@ -127,7 +122,7 @@ def logger():


def test_collects_extensions(container):
assert len(container.extensions) == 4
assert len(container.extensions) == 3
assert container.extensions == (
CallCollectingEntrypoint.instances |
CallCollectingDependencyProvider.instances)
Expand Down Expand Up @@ -602,7 +597,7 @@ class Service(object):

@dummy
def method(self):
pass
pass # pragma: no cover

return Service

Expand Down
2 changes: 1 addition & 1 deletion test/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SecondService(object):

@rpc
def task(self):
return 'task_result'
return 'task_result' # pragma: no cover


def test_error_in_worker(container_factory, rabbit_config):
Expand Down
96 changes: 35 additions & 61 deletions test/test_events.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import itertools
from collections import defaultdict

import eventlet
import pytest
from mock import Mock, create_autospec, patch

Expand All @@ -10,6 +10,7 @@
EventHandlerConfigurationError, event_handler)
from nameko.messaging import QueueConsumer
from nameko.standalone.events import event_dispatcher as standalone_dispatcher
from nameko.testing.services import entrypoint_waiter
from nameko.testing.utils import DummyProvider

EVENTS_TIMEOUT = 5
Expand Down Expand Up @@ -278,11 +279,7 @@ def service_factory(prefix, base):
e.g. ``service_factory("foo", ServicePoolHandler)`` returns a type
called ``FooServicePoolHandler`` that inherits from ``ServicePoolHandler``,
and ``FooServicePoolHandler.name`` is ``"foo"``.
If prefix is falsy, return the base without modification.
"""
if not prefix:
return base
name = prefix.title() + base.__name__
cls = type(name, (base,), {'name': prefix})
return cls
Expand Down Expand Up @@ -319,17 +316,6 @@ def make(base, prefixes):
return make


def test_event_handler_event_type():

@event_handler('foo', 'bar')
def foo(self):
pass

@event_handler('foo', 'my_event')
def bar(self):
pass


def test_service_pooled_events(rabbit_manager, rabbit_config,
start_containers):
vhost = rabbit_config['vhost']
Expand All @@ -350,9 +336,7 @@ def test_service_pooled_events(rabbit_manager, rabbit_config,
properties=dict(content_type='application/json'))

# a total of two events should be received
with eventlet.timeout.Timeout(EVENTS_TIMEOUT):
while len(events) < 2:
eventlet.sleep()
assert len(events) == 2

# exactly one instance of each service should have been created
# each should have received an event
Expand Down Expand Up @@ -385,9 +369,7 @@ def test_service_pooled_events_multiple_handlers(
properties=dict(content_type='application/json'))

# each handler (3 of them) of the two services should have received the evt
with eventlet.timeout.Timeout(EVENTS_TIMEOUT):
while len(events) < 2:
eventlet.sleep()
assert len(events) == 2

# two worker instances would have been created to deal with the handling
assert len(services['double']) == 2
Expand All @@ -409,9 +391,7 @@ def test_singleton_events(rabbit_manager, rabbit_config, start_containers):
properties=dict(content_type='application/json'))

# exactly one event should have been received
with eventlet.timeout.Timeout(EVENTS_TIMEOUT):
while len(events) < 1:
eventlet.sleep()
assert len(events) == 1

# one lucky handler should have received the event
assert len(services) == 1
Expand Down Expand Up @@ -440,9 +420,7 @@ def test_broadcast_events(rabbit_manager, rabbit_config, start_containers):
properties=dict(content_type='application/json'))

# a total of three events should be received
with eventlet.timeout.Timeout(EVENTS_TIMEOUT):
while len(events) < 3:
eventlet.sleep()
assert len(events) == 3

# all three handlers should receive the event, but they're only of two
# different types
Expand All @@ -467,22 +445,29 @@ def test_broadcast_events(rabbit_manager, rabbit_config, start_containers):

def test_requeue_on_error(rabbit_manager, rabbit_config, start_containers):
vhost = rabbit_config['vhost']
start_containers(RequeueingHandler, ('requeue',))
(container,) = start_containers(RequeueingHandler, ('requeue',))

# the queue should been created and have one consumer
queue = rabbit_manager.get_queue(
vhost, "evt-srcservice-eventtype--requeue.handle")
assert len(queue['consumer_details']) == 1

exchange_name = "srcservice.events"
rabbit_manager.publish(vhost, exchange_name, 'eventtype', '"msg"',
properties=dict(content_type='application/json'))
counter = itertools.count()

def entrypoint_fired_twice(worker_ctx, res, exc_info):
return next(counter) > 1

with entrypoint_waiter(
container, 'handle', callback=entrypoint_fired_twice
):
rabbit_manager.publish(
vhost, "srcservice.events", 'eventtype', '"msg"',
properties=dict(content_type='application/json')
)

# the event will be received multiple times as it gets requeued and then
# consumed again
with eventlet.timeout.Timeout(EVENTS_TIMEOUT):
while len(events) < 2:
eventlet.sleep()
assert len(events) > 1

# multiple instances of the service should have been instantiated
assert len(services['requeue']) > 1
Expand All @@ -492,7 +477,9 @@ def test_requeue_on_error(rabbit_manager, rabbit_config, start_containers):
assert service.events == ["msg"]


def test_reliable_delivery(rabbit_manager, rabbit_config, start_containers):
def test_reliable_delivery(
rabbit_manager, rabbit_config, start_containers, container_factory
):
""" Events sent to queues declared by ``reliable_delivery`` handlers
should be received even if no service was listening when they were
dispatched.
Expand All @@ -512,9 +499,6 @@ def test_reliable_delivery(rabbit_manager, rabbit_config, start_containers):
properties=dict(content_type='application/json'))

# wait for the event to be received
with eventlet.timeout.Timeout(EVENTS_TIMEOUT):
while len(events) == 0:
eventlet.sleep()
assert events == ["msg_1"]

# stop container, check queue still exists, without consumers
Expand All @@ -533,17 +517,17 @@ def test_reliable_delivery(rabbit_manager, rabbit_config, start_containers):
assert ['"msg_2"'] == [msg['payload'] for msg in messages]

# start another container
(container,) = start_containers(ServicePoolHandler, ('service-pool',))
class ServicePool(ServicePoolHandler):
name = "service-pool"

# wait for the service to collect the pending event
with eventlet.timeout.Timeout(EVENTS_TIMEOUT):
while len(events) < 2:
eventlet.sleep()
container = container_factory(ServicePool, rabbit_config)
with entrypoint_waiter(container, 'handle'):
container.start()

# check the new service to collects the pending event
assert len(events) == 2
assert events == ["msg_1", "msg_2"]

container.stop()


def test_unreliable_delivery(rabbit_manager, rabbit_config, start_containers):
""" Events sent to queues declared by non- ``reliable_delivery`` handlers
Expand All @@ -567,10 +551,7 @@ def test_unreliable_delivery(rabbit_manager, rabbit_config, start_containers):
rabbit_manager.publish(vhost, exchange_name, 'eventtype', '"msg_1"',
properties=dict(content_type='application/json'))

# wait for it to arrive in both services
with eventlet.timeout.Timeout(EVENTS_TIMEOUT):
while len(events) < 2:
eventlet.sleep()
# verify it arrived in both services
assert events == ["msg_1", "msg_1"]

# test that the unreliable service received it
Expand All @@ -597,11 +578,6 @@ def test_unreliable_delivery(rabbit_manager, rabbit_config, start_containers):
rabbit_manager.publish(vhost, exchange_name, 'eventtype', '"msg_3"',
properties=dict(content_type='application/json'))

# wait for it to arrive in both services
with eventlet.timeout.Timeout(EVENTS_TIMEOUT):
while len(events) < 5:
eventlet.sleep()

# verify that the "unreliable" handler didn't receive the message sent
# while it wasn't running
assert len(services['unreliable']) == 2
Expand All @@ -612,16 +588,14 @@ def test_unreliable_delivery(rabbit_manager, rabbit_config, start_containers):
def test_custom_event_handler(rabbit_manager, rabbit_config, start_containers):
"""Uses a custom handler subclass for the event_handler entrypoint"""

start_containers(CustomHandler, ('custom-events',))
(container,) = start_containers(CustomHandler, ('custom-events',))

payload = {'custom': 'data'}
dispatch = standalone_dispatcher(rabbit_config)
dispatch('srcservice', "eventtype", payload)

# wait for it to arrive
with eventlet.timeout.Timeout(EVENTS_TIMEOUT):
while len(CustomEventHandler._calls) < 1:
eventlet.sleep()
with entrypoint_waiter(container, 'handle'):
dispatch('srcservice', "eventtype", payload)

assert CustomEventHandler._calls[0].payload == payload


Expand Down
11 changes: 6 additions & 5 deletions test/test_extensions.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# coding: utf-8

from mock import Mock
import pytest
from mock import Mock

from nameko.extensions import (
Extension, Entrypoint, DependencyProvider,
is_dependency, is_entrypoint, is_extension)
DependencyProvider, Entrypoint, Extension, is_dependency, is_entrypoint,
is_extension)
from nameko.testing.services import entrypoint_hook
from nameko.testing.utils import get_extension

Expand All @@ -21,6 +21,7 @@ class SimpleDependencyProvider(DependencyProvider):
class SimpleEntrypoint(Entrypoint):
pass


simple = SimpleEntrypoint.decorator


Expand All @@ -31,11 +32,11 @@ class Service(object):

@simple
def meth1(self):
pass
pass # pragma: no cover

@simple
def meth2(self):
pass
pass # pragma: no cover


def test_entrypoint_uniqueness(container_factory):
Expand Down

0 comments on commit 5ace953

Please sign in to comment.