def test_unsubscribe_not_existing(self): p = PubSubManager() p.unsubscribe("client", "topic") # check that the topic wasn't created self.assertTrue("topic" not in p.subscriptions)
def test_unsubscribe_existing_last(self): p = PubSubManager() p.subscriptions["topic"] = {"client"} p.unsubscribe("client", "topic") # check that the topic was garbage collected self.assertTrue("topic" not in p.subscriptions)
def test_unsubscribe_existing_last(self): p = PubSubManager() p.subscriptions["topic"] = set(["client"]) p.unsubscribe("client", "topic") # check that the topic was garbage collected self.assertTrue("topic" not in p.subscriptions)
def test_unsubscribe_existing(self): p = PubSubManager() p.subscriptions["topic"] = set(["client", "other client"]) p.unsubscribe("client", "topic") # check that we are not subbed anymore self.assertTrue("client" not in p.subscriptions["topic"])
def test_unsubscribe_existing(self): p = PubSubManager() p.subscriptions["topic"] = {"client", "other client"} p.unsubscribe("client", "topic") # check that we are not subbed anymore self.assertTrue("client" not in p.subscriptions["topic"])
def test_unsubscribe_all(self, unsubscribe): p = PubSubManager() p.subscriptions["topic"] = set(["client"]) p.subscriptions["topic 2"] = set(["client"]) p.unsubscribe_all("client") # check the number of calls # TODO: check the actual arguments of each call self.assertEqual(unsubscribe.call_count, 2)
def test_subscribe_new(self): p = PubSubManager() p.subscribe("client", "topic") # check that the topic was created self.assertEqual(len(p.subscriptions.keys()), 1) # check that we are subbed self.assertTrue("client" in p.subscriptions["topic"]) self.assertTrue(len(p.subscriptions["topic"]), 1)
def test_subscribe_existing(self): p = PubSubManager() p.subscriptions["topic"] = {"other client"} p.subscribe("client", "topic") # check that the topic was reused self.assertEqual(len(p.subscriptions.keys()), 1) # check that we are subbed self.assertTrue("client" in p.subscriptions["topic"]) self.assertTrue(len(p.subscriptions["topic"]), 2)
def test_subscribe_existing(self): p = PubSubManager() p.subscriptions["topic"] = set(["other client"]) p.subscribe("client", "topic") # check that the topic was reused self.assertEqual(len(p.subscriptions.keys()), 1) # check that we are subbed self.assertTrue("client" in p.subscriptions["topic"]) self.assertTrue(len(p.subscriptions["topic"]), 2)
def test_publish_not_subscribed(self): # subscribe a client to a topic client = MagicMock() p = PubSubManager() p.subscriptions["topic"] = {client} # publish data for another topic topic = MagicMock() topic.match = MagicMock(return_value=False) p.publish(topic) # make sure the client didn't get called self.assertFalse(client.send.called)
def test_publish_subscribed(self): # subscribe a client to a topic client = MagicMock() p = PubSubManager() p.subscriptions["topic"] = {client} # publish data for that topic topic = MagicMock() topic.payload = MagicMock(return_value="payload") p.publish(topic) # make sure the client got data client.send.assert_called_once_with("topic", "payload")
def test_publish_subscribed(self): # subscribe a client to a topic client = MagicMock() p = PubSubManager() p.subscriptions["topic"] = set([client]) # publish data for that topic topic = MagicMock() topic.payload = MagicMock(return_value="payload") p.publish(topic) # make sure the client got data client.send.assert_called_once_with("topic", "payload")
def test_publish_not_subscribed(self): # subscribe a client to a topic client = MagicMock() p = PubSubManager() p.subscriptions["topic"] = set([client]) # publish data for another topic topic = MagicMock() topic.match = MagicMock(return_value=False) p.publish(topic) # make sure the client didn't get called self.assertFalse(client.send.called)
def test_publish_one_not_existing(self, find): client = MagicMock() PubSubManager().publish_one(client, "topic") # make sure the client didn't get called self.assertFalse(client.send.called)
def test_publish_one_existing(self): client = MagicMock() topic = MagicMock() topic.payload = MagicMock(return_value="payload") with patch('twitchcancer.api.pubsubtopic.PubSubTopic.find', return_value=topic): PubSubManager().publish_one(client, "topic") # make sure the client got data client.send.assert_called_once_with("topic", "payload")
def test_unsubscribe_all(self, unsubscribe): p = PubSubManager() p.subscriptions["topic"] = {"client"} p.subscriptions["topic 2"] = {"client"} p.unsubscribe_all("client") # check the number of calls # TODO: check the actual arguments of each call self.assertEqual(unsubscribe.call_count, 2)
def onMessage(self, payload, isBinary): # noqa try: s = json.loads(payload.decode('utf8')) except ValueError as e: logger.warning('got a bogus payload from %s: %s', self, e) return # handle subscriptions if 'subscribe' in s: PubSubManager.instance().subscribe(self, s['subscribe']) # respond with the latest data from this topic PubSubManager.instance().publish_one(self, s['subscribe']) # handle unsubscriptions if 'unsubscribe' in s: PubSubManager.instance().unsubscribe(self, s['unsubscribe']) # handle requests if 'request' in s: response = RequestHandler.instance().handle(s) self.send(s['request'], response)
def onMessage(self, payload, isBinary): try: s = json.loads(payload.decode('utf8')) except ValueError as e: logger.warning('got a bogus payload from %s: %s', self, e) return # handle subscriptions if 'subscribe' in s: PubSubManager.instance().subscribe(self, s['subscribe']) # respond with the latest data from this topic PubSubManager.instance().publish_one(self, s['subscribe']) # handle unsubscriptions if 'unsubscribe' in s: PubSubManager.instance().unsubscribe(self, s['unsubscribe']) # handle requests if 'request' in s: response = RequestHandler.instance().handle(s) self.send(s['request'], response)
def onClose(self, wasClean, code, reason): logger.info('connection closed for %s: %s', self, reason) PubSubManager.instance().unsubscribe_all(self)
def test_all(self, new): PubSubManager.instance() PubSubManager.instance() self.assertEqual(new.call_count, 1)
async def publish(topic): while True: PubSubManager.instance().publish(topic) await asyncio.sleep(topic.sleep)
def publish(topic): while True: PubSubManager.instance().publish(topic) yield from asyncio.sleep(topic.sleep)
def onClose(self, wasClean, code, reason): # noqa logger.info('connection closed for %s: %s', self, reason) PubSubManager.instance().unsubscribe_all(self)