def test_subscribe(): p = Publisher() result = [] def callback(data): result.append(data) p.subscribe('test', callback) p.publish('test', 4) assert result[0] == 4 p.publish('test', 4) assert result[1] == 4 p.publish('test2', 4) assert len(result) == 2 p.unsubscribe('test', callback) p.publish('test', 4) assert len(result) == 2
from publisher import Publisher from subscriber import Subscriber, DifferentSubscriber andre = Subscriber('Andre') ivan = Subscriber('Ivan') igor = DifferentSubscriber('Igor') oki = DifferentSubscriber('Oki') pub = Publisher() pub.subscribe(andre) pub.subscribe(ivan) pub.subscribe(igor, igor.mycallback) pub.subscribe(oki, oki.mycallback) pub.broadcast('Hello all!') pub.unsubscribe(oki) pub.broadcast('Bye!')