def is_active(reporter_class): gc.collect() for candidate in EventBroker.get_subscribers(): if hasattr(candidate, "__self__") and isinstance( candidate.__self__, reporter_class): return True return False
from unittest import TestCase, main from expects import expect, equal from questions_three.event_broker import EventBroker from questions_three.scaffolds.common.activate_reporters import BUILT_IN_REPORTERS # We expect this import to activate the reporters import questions_three.scaffolds.test_table # noqa: F401 ACTIVE_REPORTERS = set([type(subscriber.__self__) for subscriber in EventBroker.get_subscribers()]) class TestDefaultReporters(TestCase): def test_contains_all(self): expect(ACTIVE_REPORTERS).to(equal(set(BUILT_IN_REPORTERS))) if "__main__" == __name__: main()
def test_returns_all_subscribers(self): EventBroker.subscribe(event=1, func=foo) EventBroker.subscribe(event=2, func=bar) EventBroker.subscribe(event=2, func=baz) expect(EventBroker.get_subscribers()).to(equal({foo, bar, baz}))
def test_ignores_dead_references(self): EventBroker.subscribe(event=1, func=lambda: 1) EventBroker.subscribe(event=1, func=foo) gc.collect() expect(EventBroker.get_subscribers()).to(equal({foo}))
def extract_active_reporters(): return set([s.__self__.__class__ for s in EventBroker.get_subscribers()])