def test_subscribe(self): ebc = EventBusClient() sub = ebc.subscribe('news', lambda msg, topic: None) self.assertEqual(len(ebc.list_subscribers()), 1) self.assertEqual(len(ebc.list_subscribers('news')), 1) self.assertEqual(len(ebc.list_subscribers('other')), 0)
def test_unsubscribe(self): ebc = EventBusClient(EventBus()) sub = ebc.subscribe('news', lambda msg, topic: None) ebc.unsubscribe(sub) self.assertEqual(ebc.list_subscribers(), []) self.assertEqual(ebc.list_subscribers('news'), [])
def test_wildcard_topic(self): ebc = EventBusClient(EventBus()) subs = [] wildcard_sub = Mock() subs.append(ebc.subscribe(re.compile(r'.*'), wildcard_sub)) prefix_sub = Mock() subs.append(ebc.subscribe(re.compile(r'ham.*'), prefix_sub)) contains_sub = Mock() subs.append(ebc.subscribe(re.compile(r'.*burg.*'), contains_sub)) ebc.publish('news', 1) ebc.publish('hamsters', 2) ebc.publish('hamburgers', 3) ebc.publish('nonsense', 4) c = call self.assertEqual(wildcard_sub.call_count, 4) wildcard_sub.assert_has_calls([ c('news', 1), c('hamsters', 2), c('hamburgers', 3), c('nonsense', 4)]) self.assertEqual(prefix_sub.call_count, 2) prefix_sub.assert_has_calls([ c('hamsters', 2), c('hamburgers', 3)]) self.assertEqual(contains_sub.call_count, 1) contains_sub.assert_has_calls([c('hamburgers', 3)]) for sub in subs: ebc.unsubscribe(sub) self.assertEqual(ebc.list_subscribers(), [])