示例#1
0
def test_subscribe_decorator():
    subscriber = Mock()

    @subscribe('bar')
    def dummy(*args, **kwargs):
        subscriber(*args, **kwargs)

    hub.publish('bar', ['a', 'b', 'c'])
    subscriber.assert_called_once_with(['a', 'b', 'c'])
    hub.remove_subscriber(subscriber, 'bar')
示例#2
0
def test_sub_key():
    subscriber = Mock()
    hub.add_subscriber(subscriber, 'foo')
    hub.publish('foo:bar', 1, 2, 3)
    subscriber.assert_called_once_with(1, 2, 3)
    hub.remove_subscriber(subscriber, 'foo')

    subscriber = Mock()
    hub.add_subscriber(subscriber, 'foo:bar:bang')
    hub.publish('foo:bar', 1, 2, 3)
    assert subscriber.call_count == 0
    hub.remove_subscriber(subscriber, 'foo')
示例#3
0
def test_two_subscribers():
    subscriber_1 = Mock()
    subscriber_2 = Mock()
    hub.add_subscriber(subscriber_1, 'foo')
    hub.add_subscriber(subscriber_2, 'foo')
    hub.publish('foo', 1, foo='bar')
    subscriber_1.assert_called_once_with(1, foo='bar')
    subscriber_2.assert_called_once_with(1, foo='bar')
    hub.remove_subscriber(subscriber_2, 'foo')
    hub.publish('foo', 'bar')
    assert subscriber_2.call_count == 1
    assert subscriber_1.call_count == 2
    hub.remove_subscriber(subscriber_1, 'foo')
示例#4
0
def test_subscribe():
    subscriber = Mock()
    hub.add_subscriber(subscriber, 'foo')
    hub.publish('foo', 1, 2, 3)
    subscriber.assert_called_once_with(1, 2, 3)
    hub.remove_subscriber(subscriber, 'foo')
示例#5
0
def test_remove_subscriber():
    subscriber = Mock()
    hub.add_subscriber(subscriber, 'foo')
    hub.remove_subscriber(subscriber, 'foo')
    hub.publish('foo', 1, 2, 3)
    assert subscriber.call_count == 0