def test_neq(self): foo1 = PubSubTopic("foo", None, None) foo2 = PubSubTopic("foo", None, None) bar = PubSubTopic("bar", None, None) self.assertFalse(foo1 != foo2) self.assertTrue(foo1 != bar)
def test_payload_no_cache_no_data(self): callback = MagicMock(return_value="computed") topic = PubSubTopic("no_cache_no_data", callback, None) payload = topic.payload(False) callback.assert_called_once_with() self.assertEqual(payload, "computed")
def test_payload_with_cache_with_fresh_data(self): callback = MagicMock(return_value="computed") topic = PubSubTopic("with_cache_with_fresh_data", callback, None) topic.data = self.cached_data payload = topic.payload(True) self.assertFalse(callback.called) self.assertEqual(payload, "cached")
def test_payload_with_cache_with_old_data(self): callback = MagicMock(return_value="computed") topic = PubSubTopic("with_cache_with_old_data", callback, None) topic.data = self.cached_data topic.data["date"] = datetime.datetime.now() - datetime.timedelta(seconds=62) payload = topic.payload(True) self.assertEqual(payload, "computed")
def test_init(self): topic = PubSubTopic(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, None)
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_hash(self): foo = PubSubTopic("foo", None, None) self.assertEqual(hash(foo), hash(foo.name))
def test_exists(self): topic = PubSubTopic("foo", None, None) self.assertEqual(PubSubTopic.find("foo"), topic)
def test_not_exists(self): self.assertEqual(PubSubTopic.find("foobar"), None)
def test_duplicate_instances(self): PubSubTopic("foo", "bar", None) PubSubTopic("foo", "baz", None) self.assertEqual(len(PubSubTopic.instances), 1)
def test_instances(self): topic = PubSubTopic("foo", None, None) self.assertEqual(len(PubSubTopic.instances), 1) self.assertEqual(PubSubTopic.instances.pop(), topic)
def test_str(self): topic = PubSubTopic("foo", None, None) self.assertEqual(str(topic), "foo")
def test_match(self): topic = PubSubTopic("foo", None, None) self.assertTrue(topic.match("foo")) self.assertFalse(topic.match("bar"))
def publish_one(self, client, topic): t = PubSubTopic.find(topic) if t: logger.debug('publishing data once for %s to %s', topic, client) client.send(topic, t.payload(useCache=True, name=topic))
def publish_one(self, client, topic): t = PubSubTopic.find(topic) if t: logger.debug('publishing data once for %s to %s', topic, client) client.send(topic, t.payload(use_cache=True, name=topic))