def testUnsubscribeWeakById(self): s = Subscriber([]) p = Publisher() sid = p.subscribe('bar', s.on) p.unsubscribe('bar', sid) p.notify('foo', 'bar') self.assertEqual(s.events, [])
def testAdaptSubscriber(self): pub = Publisher() events = [] # Adapt the subscriber (add 10 to each event info). sub = Subscriber(events) asub = SubAdapter('info', sub, lambda s, e, i: s(e, i+10)) pub.addSub(asub) del asub pub.notify('info', 1) del sub pub.notify('info', 2) # Only adapted event 1 arrives before asub dies with sub. self.assertEqual(events, [11]) # Adapt the subscribing method (add 10 to each event info). sub = Subscriber(events) asub = SubAdapter('info', sub, lambda s, e, i: s.onEvent(e, i+10)) pub.addSub(asub) del asub pub.notify('info', 1) del sub pub.notify('info', 2) # Only adapted event 1 arrives before asub dies with sub. self.assertEqual(events, [11, 31])
def testAdaptSubscriber(self): pub = Publisher() events = [] # Adapt the subscriber (add 10 to each event info). sub = Subscriber(events) asub = SubAdapter('info', sub, lambda s, e, i: s(e, i + 10)) pub.addSub(asub) del asub pub.notify('info', 1) del sub pub.notify('info', 2) # Only adapted event 1 arrives before asub dies with sub. self.assertEqual(events, [11]) # Adapt the subscribing method (add 10 to each event info). sub = Subscriber(events) asub = SubAdapter('info', sub, lambda s, e, i: s.onEvent(e, i + 10)) pub.addSub(asub) del asub pub.notify('info', 1) del sub pub.notify('info', 2) # Only adapted event 1 arrives before asub dies with sub. self.assertEqual(events, [11, 31])
def testLazyInfo(self): s = Subscriber([]) p = Publisher() p.subscribe('bar', s.on) self.info_calls = 0 def info(): self.info_calls += 1 return {'n': 42} # no subscribers to 'foo' so info won't be called. p.notify('foo', info) self.assertEqual(s.events, []) self.assertEqual(self.info_calls, 0) p.notify('bar', info) self.assertEqual(s.events, [('bar', {'n': 42})]) self.assertEqual(self.info_calls, 1)
def testArgs(self): # Non-persistent. events = [] def cb(*args): events.append(args) p = Publisher() p.subscribe('done', cb, args=('x', 42)) p.notify('done', True) self.assertEqual(events, [('done', True, 'x', 42)]) # Persistent. store = {} storage = Storage(store) storage['sub'] = s = Subscriber([]) sid = p.subscribe('dusted', s.on, args=('hello',), how=PERSISTENT) storage['pub'] = p del s, p p = storage['pub'] p.notify('dusted', 14) # FIXME: a tuple is converted to a list here. That's a # serializer bug. self.assertEqual(storage['sub'].events, [['dusted', 14, 'hello']]) p.unsubscribe('dusted', sid, how=PERSISTENT) p.notify('dusted', 15) self.assertEqual(len(storage['sub'].events), 1)
def testArgs(self): # Non-persistent. events = [] def cb(*args): events.append(args) p = Publisher() p.subscribe('done', cb, args=('x', 42)) p.notify('done', True) self.assertEqual(events, [('done', True, 'x', 42)]) # Persistent. store = {} storage = Storage(store) storage['sub'] = s = Subscriber([]) sid = p.subscribe('dusted', s.on, args=('hello', ), how=PERSISTENT) storage['pub'] = p del s, p p = storage['pub'] p.notify('dusted', 14) # FIXME: a tuple is converted to a list here. That's a # serializer bug. self.assertEqual(storage['sub'].events, [['dusted', 14, 'hello']]) p.unsubscribe('dusted', sid, how=PERSISTENT) p.notify('dusted', 15) self.assertEqual(len(storage['sub'].events), 1)