def testPubSub(self):
        sub_data = {'secret': 'SECRET'}
        id = PubSub.subscribe ('EVENT', 'not_an_url', sub_data)
        self.assertTrue (PubSub.unsubscribe (id))
        self.assertFalse (PubSub.unsubscribe (id))

        pub_data = {'message': 123}
        self.assertEqual(0, PubSub.publish ('EVENT', pub_data))
        PubSub.subscribe ('EVENT', 'not_an_url', sub_data)
        self.assertEqual(1, PubSub.publish ('EVENT', pub_data))
        response = self.executeTask() # /pubsub/notify
        self.assertEqual(response.status_int, 200)
        response.mustcontain ('unknown url type')
        
        url = "/pubsub/test" 
        PubSub.subscribe ('EVENT2', url, sub_data)
        self.assertEqual(1, PubSub.publish ('EVENT2', pub_data))
        response = self.executeTask()  # /pubsub/notify
        self.assertEqual (response.json['status'], 'OK')
        response = self.executeTask() # /pubsub/test
        self.assertEqual (response.json['pub_data']["message"], 123)

        url = "/pubsub/task" 
        sub_data = {'secret': 'SECRET', 'channel': 'CHANNEL', 'taskname': 'NAME'}
        PubSub.subscribe ('EVENT3', url, sub_data)
        self.assertEqual(1, PubSub.publish ('EVENT3', pub_data))
        response = self.executeTask()  # /pubsub/notify
        self.assertEqual (response.json['status'], 'OK')
        response = self.executeTask() # /pubsub/task
        self.assertEqual (response.json['status'], 'OK')
        queue = TaskQueue()
        lease = queue.lease (channel='CHANNEL')
        self.assertIsNotNone(lease)
        self.assertEqual(lease['id'], response.json['id'])
Example #2
0
 def test_unsubscribe(self):
     service = PubSub('test_unsubscribe')
     sub1 = Subscriber('sub1')
     sub2 = Subscriber('sub2')
     service.subscribe(sub1, sub1.key, 'topic1', sub1.cb1)
     service.subscribe(sub1, sub1.key, 'topic1', sub1.cb2)
     service.subscribe(sub1, sub1.key, 'topic2', sub1.cb1)
     service.subscribe(sub1, sub1.key, 'topic2', sub1.cb2)
     service.subscribe(sub2, sub2.key, 'topic1', sub2.cb1)
     service.subscribe(sub2, sub2.key, 'topic1', sub2.cb2)
     service.subscribe(sub2, sub2.key, 'topic2', sub2.cb1)
     service.subscribe(sub2, sub2.key, 'topic2', sub2.cb2)
     subscriptions = service.subscriptions()
     self.assertEqual(len(subscriptions), 2)
     self.assertIn('topic1', subscriptions)
     self.assertIn((sub1, sub1.key, sub1.cb1), subscriptions['topic1'])
     self.assertIn((sub1, sub1.key, sub1.cb2), subscriptions['topic1'])
     self.assertIn((sub2, sub2.key, sub2.cb1), subscriptions['topic1'])
     self.assertIn((sub2, sub2.key, sub2.cb2), subscriptions['topic1'])
     self.assertIn('topic2', subscriptions)
     self.assertIn((sub1, sub1.key, sub1.cb1), subscriptions['topic2'])
     self.assertIn((sub1, sub1.key, sub1.cb2), subscriptions['topic2'])
     self.assertIn((sub2, sub2.key, sub2.cb1), subscriptions['topic2'])
     self.assertIn((sub2, sub2.key, sub2.cb2), subscriptions['topic2'])
     self.assertIn((sub2, sub2.key, sub2.cb2), subscriptions['topic1'])
     service.unsubscribe(sub2, sub2.key, 'topic2', sub2.cb2)
     subscriptions = service.subscriptions()
     self.assertEqual(len(subscriptions), 2)
     self.assertIn('topic1', subscriptions)
     self.assertIn((sub1, sub1.key, sub1.cb1), subscriptions['topic1'])
     self.assertIn((sub1, sub1.key, sub1.cb2), subscriptions['topic1'])
     self.assertIn((sub2, sub2.key, sub2.cb1), subscriptions['topic1'])
     self.assertIn((sub2, sub2.key, sub2.cb2), subscriptions['topic1'])
     self.assertIn('topic2', subscriptions)
     self.assertIn((sub1, sub1.key, sub1.cb1), subscriptions['topic2'])
     self.assertIn((sub1, sub1.key, sub1.cb2), subscriptions['topic2'])
     self.assertIn((sub2, sub2.key, sub2.cb1), subscriptions['topic2'])
     self.assertNotIn((sub2, sub2.key, sub2.cb2), subscriptions['topic2'])
     service.unsubscribe(sub1, sub1.key, callback=sub1.cb2)
     subscriptions = service.subscriptions()
     self.assertEqual(len(subscriptions), 2)
     self.assertIn('topic1', subscriptions)
     self.assertIn((sub1, sub1.key, sub1.cb1), subscriptions['topic1'])
     self.assertNotIn((sub1, sub1.key, sub1.cb2), subscriptions['topic1'])
     self.assertIn((sub2, sub2.key, sub2.cb1), subscriptions['topic1'])
     self.assertIn((sub2, sub2.key, sub2.cb2), subscriptions['topic1'])
     self.assertIn('topic2', subscriptions)
     self.assertIn((sub1, sub1.key, sub1.cb1), subscriptions['topic2'])
     self.assertNotIn((sub1, sub1.key, sub1.cb2), subscriptions['topic2'])
     self.assertIn((sub2, sub2.key, sub2.cb1), subscriptions['topic2'])
     self.assertNotIn((sub2, sub2.key, sub2.cb2), subscriptions['topic2'])
     service.unsubscribe(callback=sub1.cb1)
     subscriptions = service.subscriptions()
     self.assertEqual(len(subscriptions), 2)
     self.assertIn('topic1', subscriptions)
     self.assertNotIn((sub1, sub1.key, sub1.cb1), subscriptions['topic1'])
     self.assertNotIn((sub1, sub1.key, sub1.cb2), subscriptions['topic1'])
     self.assertIn((sub2, sub2.key, sub2.cb1), subscriptions['topic1'])
     self.assertIn((sub2, sub2.key, sub2.cb2), subscriptions['topic1'])
     self.assertIn('topic2', subscriptions)
     self.assertNotIn((sub1, sub1.key, sub1.cb1), subscriptions['topic2'])
     self.assertNotIn((sub1, sub1.key, sub1.cb2), subscriptions['topic2'])
     self.assertIn((sub2, sub2.key, sub2.cb1), subscriptions['topic2'])
     self.assertNotIn((sub2, sub2.key, sub2.cb2), subscriptions['topic2'])
     service.unsubscribe(callback=sub2.cb1)
     subscriptions = service.subscriptions()
     self.assertEqual(len(subscriptions), 1)
     self.assertIn('topic1', subscriptions)
     self.assertNotIn((sub1, sub1.key, sub1.cb1), subscriptions['topic1'])
     self.assertNotIn((sub1, sub1.key, sub1.cb2), subscriptions['topic1'])
     self.assertNotIn((sub2, sub2.key, sub2.cb1), subscriptions['topic1'])
     self.assertIn((sub2, sub2.key, sub2.cb2), subscriptions['topic1'])
     self.assertNotIn('topic2', subscriptions)
     service.unsubscribe()
     subscriptions = service.subscriptions()
     self.assertEqual(len(subscriptions), 0)