def test_publish(self): p = Publisher() s = BasicSubscriber() p.subscribe(s) p.subscribe(afunction) p.publish('hello') self.assertEqual(s.calledwith, 'hello') self.assertEqual(afunction_calledwith, 'hello')
def test_process_unsubscribes_after_3(self): """ Attempt to make sure that SimpleSubscriber.process() unsubscribes itself sanely after 3 calls. """ p = Publisher() for i in range(6): newsub = SimpleSubscriber("Sub"+str(i), p) p.publish(str(i)) if i == 0: self.assertEqual(len(p.subscribers), 1) self.assertTrue(str(p.subscribers[0]).endswith("Sub0>")) elif i == 1: self.assertEqual(len(p.subscribers), 2) self.assertTrue(str(p.subscribers[0]).endswith("Sub0>")) self.assertTrue(str(p.subscribers[-1]).endswith("Sub1>")) else: # should never have more than 3 in current settings self.assertEqual(len(p.subscribers), 3) self.assertTrue(str(p.subscribers[0]).endswith("Sub{0}>".format(i-2))) self.assertTrue(str(p.subscribers[1]).endswith("Sub{0}>".format(i-1))) self.assertTrue(str(p.subscribers[2]).endswith("Sub{0}>".format(i)))