def test_emit_receiveOnce(self): """ Events should only be received by each object once. """ world = World(MagicMock()) obj1 = world.create('foo')['id'] obj2 = world.create('foo')['id'] obj1_received = [] world.receiveFor(obj1, obj1_received.append) obj2_received = [] world.receiveFor(obj2, obj2_received.append) # obj1 emits everything it receives world.receiveFor(obj1, world.emitterFor(obj1)) # obj2 emits everything it receives world.receiveFor(obj2, world.emitterFor(obj2)) # obj2 receives emissions from obj1 world.subscribeTo(obj1, world.receiverFor(obj2)) # obj1 receives emissions from obj2 world.subscribeTo(obj2, world.receiverFor(obj1)) # we have a nice loop set up. When this test fails, it is likely to # continue spinning forever. world.emit('echo', obj1) self.assertEqual(obj1_received, ['echo'], "Should have received the " "message once") self.assertEqual(obj2_received, ['echo'], "Should have received the " "message once")
def test_emit_receiveOnce(self): """ Events should only be received by each object once. """ world = World(MagicMock()) obj1 = world.create('foo')['id'] obj2 = world.create('foo')['id'] obj1_received = [] world.receiveFor(obj1, obj1_received.append) obj2_received = [] world.receiveFor(obj2, obj2_received.append) # obj1 emits everything it receives world.receiveFor(obj1, world.emitterFor(obj1)) # obj2 emits everything it receives world.receiveFor(obj2, world.emitterFor(obj2)) # obj2 receives emissions from obj1 world.subscribeTo(obj1, world.receiverFor(obj2)) # obj1 receives emissions from obj2 world.subscribeTo(obj2, world.receiverFor(obj1)) # we have a nice loop set up. When this test fails, it is likely to # continue spinning forever. world.emit('echo', obj1) self.assertEqual(obj1_received, ['echo'], "Should have received the " "message once") self.assertEqual(obj2_received, ['echo'], "Should have received the " "message once")
def test_receiverFor_same(self): """ You should get the same function each time you ask for a receiver for the same object. """ world = World(MagicMock()) self.assertEqual(world.receiverFor('foo'), world.receiverFor('foo'))
def test_receiverFor_same(self): """ You should get the same function each time you ask for a receiver for the same object. """ world = World(MagicMock()) self.assertEqual(world.receiverFor('foo'), world.receiverFor('foo'))
def test_emit_oneEventAtATime(self): """ Only one event should be emitted at a time. If an event emission causes another event to be emitted, it should be appended to a queue and be emitted after the current event is finished. """ world = World(MagicMock()) obj1 = world.create('foo')['id'] obj2 = world.create('foo')['id'] obj3 = world.create('foo')['id'] obj1_received = [] world.receiveFor(obj1, obj1_received.append) obj2_received = [] world.receiveFor(obj2, obj2_received.append) obj3_received = [] world.receiveFor(obj3, obj3_received.append) # obj1 will emit to # obj2 and obj3 world.subscribeTo(obj1, world.receiverFor(obj2)) world.subscribeTo(obj1, world.receiverFor(obj3)) # obj2 will emit to # obj1 and obj3 world.subscribeTo(obj2, world.receiverFor(obj1)) world.subscribeTo(obj2, world.receiverFor(obj3)) # 1_ # /|\ # / \ # |/_ _\| # 3 <---- 2 # obj2 will emit "obj2" every time he receives an event def noisy(_): world.emit('obj2', obj2) world.receiveFor(obj2, noisy) # If things are working properly, then obj3 will receive # the 'obj2' emissions AFTER it receives the event from obj1 world.emit('obj1', obj1) self.assertEqual(obj1_received, ['obj1', 'obj2'], "obj1 should receive obj1 from self, then obj2") self.assertEqual(obj2_received, ['obj1', 'obj2'], "obj2 should receive obj1, then obj2 from self") self.assertEqual(obj3_received, ['obj1', 'obj2'], "obj3 should receiver obj1 first, then obj2")
def test_emit_oneEventAtATime(self): """ Only one event should be emitted at a time. If an event emission causes another event to be emitted, it should be appended to a queue and be emitted after the current event is finished. """ world = World(MagicMock()) obj1 = world.create('foo')['id'] obj2 = world.create('foo')['id'] obj3 = world.create('foo')['id'] obj1_received = [] world.receiveFor(obj1, obj1_received.append) obj2_received = [] world.receiveFor(obj2, obj2_received.append) obj3_received = [] world.receiveFor(obj3, obj3_received.append) # obj1 will emit to # obj2 and obj3 world.subscribeTo(obj1, world.receiverFor(obj2)) world.subscribeTo(obj1, world.receiverFor(obj3)) # obj2 will emit to # obj1 and obj3 world.subscribeTo(obj2, world.receiverFor(obj1)) world.subscribeTo(obj2, world.receiverFor(obj3)) # 1_ # /|\ # / \ # |/_ _\| # 3 <---- 2 # obj2 will emit "obj2" every time he receives an event def noisy(_): world.emit('obj2', obj2) world.receiveFor(obj2, noisy) # If things are working properly, then obj3 will receive # the 'obj2' emissions AFTER it receives the event from obj1 world.emit('obj1', obj1) self.assertEqual(obj1_received, ['obj1', 'obj2'], "obj1 should receive obj1 from self, then obj2") self.assertEqual(obj2_received, ['obj1', 'obj2'], "obj2 should receive obj1, then obj2 from self") self.assertEqual(obj3_received, ['obj1', 'obj2'], "obj3 should receiver obj1 first, then obj2")
def test_destroy_disableSubscribers(self): """ When an object is destroyed, things subscribed to its events will no longer receive events. """ world = World(MagicMock()) thing = world.create('foo') received = [] world.receiveFor(thing['id'], received.append) emitted = [] world.subscribeTo(thing['id'], emitted.append) receiver = world.receiverFor(thing['id']) emitter = world.emitterFor(thing['id']) world.destroy(thing['id']) received.pop() emitted.pop() receiver('foo') self.assertEqual(received, []) self.assertEqual(emitted, []) emitter('foo') self.assertEqual(received, []) self.assertEqual(emitted, [])
def test_destroy_disableSubscribers(self): """ When an object is destroyed, things subscribed to its events will no longer receive events. """ world = World(MagicMock()) thing = world.create('foo') received = [] world.receiveFor(thing['id'], received.append) emitted = [] world.subscribeTo(thing['id'], emitted.append) receiver = world.receiverFor(thing['id']) emitter = world.emitterFor(thing['id']) world.destroy(thing['id']) received.pop() emitted.pop() receiver('foo') self.assertEqual(received, []) self.assertEqual(emitted, []) emitter('foo') self.assertEqual(received, []) self.assertEqual(emitted, [])
def test_receiverFor(self): """ You can get a function that will call eventReceived for a given object. """ world = World(MagicMock()) obj = world.create('foo') called = [] world.receiveFor(obj['id'], called.append) receiver = world.receiverFor(obj['id']) receiver('hey') self.assertEqual(called, ['hey'])
def test_receiverFor(self): """ You can get a function that will call eventReceived for a given object. """ world = World(MagicMock()) obj = world.create('foo') called = [] world.receiveFor(obj['id'], called.append) receiver = world.receiverFor(obj['id']) receiver('hey') self.assertEqual(called, ['hey'])