本文整理汇总了Python中xatro.world.World.subscribeTo方法的典型用法代码示例。如果您正苦于以下问题:Python World.subscribeTo方法的具体用法?Python World.subscribeTo怎么用?Python World.subscribeTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xatro.world.World
的用法示例。
在下文中一共展示了World.subscribeTo方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_emit_receiveOnce
# 需要导入模块: from xatro.world import World [as 别名]
# 或者: from xatro.world.World import subscribeTo [as 别名]
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")
示例2: test_destroy_disableSubscribers
# 需要导入模块: from xatro.world import World [as 别名]
# 或者: from xatro.world.World import subscribeTo [as 别名]
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, [])
示例3: test_emit_oneEventAtATime
# 需要导入模块: from xatro.world import World [as 别名]
# 或者: from xatro.world.World import subscribeTo [as 别名]
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")
示例4: test_emitterFor
# 需要导入模块: from xatro.world import World [as 别名]
# 或者: from xatro.world.World import subscribeTo [as 别名]
def test_emitterFor(self):
"""
You can get a function that takes a single argument and emits events
for a particular object.
"""
ev1 = MagicMock()
world = World(ev1)
ev2 = MagicMock()
world.subscribeTo('1234', ev2)
emitter = world.emitterFor('1234')
emitter('foo')
ev1.assert_called_once_with('foo')
ev2.assert_called_once_with('foo')
示例5: test_emit_Exception
# 需要导入模块: from xatro.world import World [as 别名]
# 或者: from xatro.world.World import subscribeTo [as 别名]
def test_emit_Exception(self):
"""
Exceptions caused by event receivers should not prevent other event
receivers from receiving the events.
"""
ev1 = MagicMock()
ev1.side_effect = Exception()
world = World(ev1)
ev2 = MagicMock()
ev2.side_effect = Exception()
world.subscribeTo('1234', ev2)
ev3 = MagicMock()
world.subscribeTo('1234', ev3)
world.emit('hey', '1234')
ev1.assert_called_once_with('hey')
ev2.assert_called_once_with('hey')
ev3.assert_called_once_with('hey')
示例6: test_subscribeTo
# 需要导入模块: from xatro.world import World [as 别名]
# 或者: from xatro.world.World import subscribeTo [as 别名]
def test_subscribeTo(self):
"""
You can subscribe to the events that are emitted by a particular object.
"""
ev = MagicMock()
world = World(ev)
obj = world.create('foo')
called = []
world.subscribeTo(obj['id'], called.append)
ev.reset_mock()
world.emit('event', obj['id'])
self.assertEqual(called, ['event'])
ev.assert_called_once_with('event')
ev.reset_mock()
world.unsubscribeFrom(obj['id'], called.append)
world.emit('event', obj['id'])
self.assertEqual(called, ['event'], "Should not have changed")
ev.assert_called_once_with('event')