Exemplo n.º 1
0
def test_messagebus_unsubscribe():
	"""
	Test unsubscribe from messages.
	"""
	cb = mock.Mock()
	sender = object()

	ExampleMessage.subscribe(cb)
	Message.subscribe(cb, sender=sender)

	# broadcast local and global message, cb called two times
	ExampleMessage.broadcast(sender)
	Message.broadcast(sender)
	assert cb.call_count == 2
	cb.reset_mock()

	# after unsubscribing globally, only the local message is received
	ExampleMessage.unsubscribe(cb)
	ExampleMessage.broadcast(sender)
	Message.broadcast(sender)
	assert_called_once_with_message(cb, Message, sender=sender)
	cb.reset_mock()

	# after unsubscribing locally, all subscriptions should be gone, cb not called
	Message.unsubscribe(cb, sender=sender)
	ExampleMessage.broadcast(sender)
	Message.broadcast(sender)
	assert not cb.called
Exemplo n.º 2
0
	def test_global_subscribe(self):
		ExampleMessage.subscribe(self.cb)

		# correct message type, cb is called
		ExampleMessage.broadcast(self)
		self.assert_called_once_with(self.cb, ExampleMessage, sender=self)
		self.cb.reset_mock()

		# wrong message type, cb is not called
		Message.broadcast(self)
		self.assertFalse(self.cb.called)
Exemplo n.º 3
0
def test_messagebus_global_subscribe():
	"""
	Test global (sender-independent) subscription to messages.
	"""
	cb = mock.Mock()
	sender = object()

	ExampleMessage.subscribe(cb)

	# correct message type, cb is called
	ExampleMessage.broadcast(sender)
	assert_called_once_with_message(cb, ExampleMessage, sender=sender)
	cb.reset_mock()

	# wrong message type, cb is not called
	Message.broadcast(sender)
	assert not cb.called
Exemplo n.º 4
0
	def test_unsubscribe(self):
		ExampleMessage.subscribe(self.cb)
		Message.subscribe(self.cb, sender=self)

		# broadcast local and global message, cb called two times
		ExampleMessage.broadcast(self)
		Message.broadcast(self)
		assert self.cb.call_count == 2
		self.cb.reset_mock()

		# after unsubscribing globally, only the local message is received
		ExampleMessage.unsubscribe(self.cb)
		ExampleMessage.broadcast(self)
		Message.broadcast(self)
		self.assert_called_once_with(self.cb, Message, sender=self)
		self.cb.reset_mock()

		# after unsubscribing locally, all subscriptions should be gone, cb not called
		Message.unsubscribe(self.cb, sender=self)
		ExampleMessage.broadcast(self)
		Message.broadcast(self)
		self.assertFalse(self.cb.called)
Exemplo n.º 5
0
def test_messagebus_local_subscribe():
	"""
	Test local (sender-specific) subscription to messages.
	"""
	cb = mock.Mock()
	sender = object()

	ExampleMessage.subscribe(cb, sender=sender)

	# correct message type, correct sender, cb is called
	ExampleMessage.broadcast(sender)
	assert_called_once_with_message(cb, ExampleMessage, sender=sender)
	cb.reset_mock()

	# correct message type, wrong sender, cb is not called
	ExampleMessage.broadcast(1)
	assert not cb.called
	cb.reset_mock()

	# wrong message type, correct sender, cb is not called
	Message.broadcast(sender)
	assert not cb.called
Exemplo n.º 6
0
	def test_sender_argument(self):
		msg = Message(self)
		self.assertEqual(msg.sender, self)
Exemplo n.º 7
0
def test_message_sender_argument():
	sender = object()
	msg = Message(sender)
	assert msg.sender == sender