def test_match(self): topic = PubSubVariableTopic("match.*", None, None) self.assertTrue(topic.match("match.foo")) self.assertTrue(topic.match("match.bar.baz")) self.assertFalse(topic.match("match")) self.assertFalse(topic.match("foo"))
def test_payload_with_cache_no_data(self): callback = MagicMock(return_value="computed") topic = PubSubVariableTopic("with_cache_no_data.*", callback, None) variable = "with_cache_no_data.foo" payload = topic.payload(True, name=variable) callback.assert_called_once_with("foo") self.assertEqual(payload, "computed")
def test_payload_with_cache_with_old_data(self): callback = MagicMock(return_value="computed") topic = PubSubVariableTopic("with_cache_with_old_data.*", callback, None) variable = "with_cache_with_old_data.foo" topic.data[variable] = self.cached_data topic.data[variable]["date"] = datetime.datetime.now() - datetime.timedelta(seconds=62) payload = topic.payload(True, name=variable) self.assertEqual(payload, "computed")
def test_payload_with_cache_with_fresh_data(self): callback = MagicMock(return_value="computed") topic = PubSubVariableTopic("with_cache_with_fresh_data.*", callback, None) variable = "with_cache_with_fresh_data.foo" topic.data[variable] = self.cached_data payload = topic.payload(True, name=variable) self.assertFalse(callback.called) self.assertEqual(payload, "cached")
def test_init_members(self): topic = PubSubVariableTopic(name="foo.*", callback="bar", sleep="baz") self.assertEqual(topic.name, "foo.*") self.assertEqual(topic.callback, "bar") self.assertEqual(topic.sleep, "baz") self.assertEqual(topic.data, {})
async def create_publishers(): # setup storage storage = Storage() # list of topics clients can subscribe to topics = [ PubSubTopic('twitchcancer.live', storage.cancer, 1), PubSubVariableTopic('twitchcancer.leaderboards.*', storage.leaderboards, 60), PubSubVariableTopic('twitchcancer.leaderboard.*', storage.leaderboard, 60), PubSubTopic('twitchcancer.status', storage.status, 3), PubSubVariableTopic('twitchcancer.channel.*', storage.channel, 60), ] # add publisher tasks to the loop tasks = [publish(topic) for topic in topics] logger.info('added publisher topics: %s', ', '.join(map(str, topics))) await asyncio.gather(*tasks)
def test_argument(self): topic = PubSubVariableTopic("arg.*", None, None) self.assertEqual(topic.argument("arg.foo"), "foo") self.assertEqual(topic.argument("arg.foo.bar"), "foo.bar")
def test_init_members_variables(self): for name in ["foo", "foo.bar", "*.foo", "*", ".*"]: self.assertRaises(NotImplementedError, lambda: PubSubVariableTopic(name=name, callback="bar", sleep="baz"))