9982-getdata 1 import unittest
9982-getdata 2 import helper
9982-getdata 3 from mock import Mock
9982-getdata 4 from lib.state import State
9982-getdata 5 from lib.message import Message
9982-getdata 6 from lib.broadcast import Broadcast
9982-getdata 7 from lib.long_buffer import LongBuffer
9982-getdata 8 from lib.order_buffer import OrderBuffer
9982-getdata 9
9982-getdata 10 class TestMessage(unittest.TestCase):
9982-getdata 11 def setUp(self):
9982-getdata 12 helper.setup()
9982-getdata 13 self.alice_socket = Mock()
9982-getdata 14 self.alice_state = State(self.alice_socket)
9982-getdata 15 self.bob_socket = Mock()
9982-getdata 16 self.bob_state = State(self.bob_socket)
9982-getdata 17 self.setupAlice()
9982-getdata 18 self.setupBob()
9982-getdata 19
9982-getdata 20 def setupAlice(self):
9982-getdata 21 self.bob_state.add_peer('alice')
9982-getdata 22 self.bob_state.add_key(
9982-getdata 23 'alice',
9982-getdata 24 '9h6wYndVjt8QpnIZOYb7KD2tYKCKw4rjlYg4LM1ODx1Qkr3qA0IuKNukkwKhQ4UP9ypMlhyPHa7AGD7NO7Ws5w=='
9982-getdata 25 )
9982-getdata 26 self.bob_state.update_at({
9982-getdata 27 'handle': 'alice',
9982-getdata 28 'address': '127.0.0.1',
9982-getdata 29 'port': 8888
9982-getdata 30 })
9982-getdata 31
9982-getdata 32 def setupBob(self):
9982-getdata 33 self.alice_state.add_peer('bob')
9982-getdata 34 self.alice_state.add_key(
9982-getdata 35 'bob',
9982-getdata 36 '9h6wYndVjt8QpnIZOYb7KD2tYKCKw4rjlYg4LM1ODx1Qkr3qA0IuKNukkwKhQ4UP9ypMlhyPHa7AGD7NO7Ws5w=='
9982-getdata 37 )
9982-getdata 38 self.alice_state.update_at({
9982-getdata 39 'handle': 'bob',
9982-getdata 40 'address': '127.0.0.1',
9982-getdata 41 'port': 8889
9982-getdata 42 })
9982-getdata 43
9982-getdata 44 def tearDown(self):
9982-getdata 45 self.alice_state.remove_peer('bob')
9982-getdata 46 self.bob_state.remove_peer('alice')
9982-getdata 47
9982-getdata 48 def test_broadcast_message(self):
9982-getdata 49 message = Broadcast({
9982-getdata 50 'handle': 'bob',
9982-getdata 51 'speaker': 'alice',
9982-getdata 52 'body': 'm1',
9982-getdata 53 'long_buffer': LongBuffer(self.alice_state)
9982-getdata 54 }, self.alice_state)
9982-getdata 55 message.send()
9982-getdata 56 bob = self.alice_state.get_peer_by_handle('bob')
9982-getdata 57 message_bytes = message.get_message_bytes(bob)
9982-getdata 58 black_packet = Message.pack(bob, message.command, message.bounces, message_bytes)
9982-getdata 59 self.bob_socket.sendto.called_once_with(black_packet, (bob.address, bob.port))
9982-getdata 60
9982-getdata 61
9982-getdata 62 alice = self.bob_state.get_peer_by_handle('alice')
9982-getdata 63 received_message = Message.unpack(alice,
9982-getdata 64 black_packet,
9982-getdata 65 LongBuffer(self.bob_state),
9982-getdata 66 OrderBuffer(self.bob_state),
9982-getdata 67 {})
9982-getdata 68 self.assertEqual(message.body, received_message['body'])
9982-getdata 69