9982-getdata 1 import unittest
9982-getdata 2
9982-getdata 3 import time
9982-getdata 4 from mock import Mock
9982-getdata 5 from lib.message import Message
9982-getdata 6 from lib.getdata import GetData
9982-getdata 7 from lib.long_buffer import LongBuffer
9982-getdata 8 from lib.order_buffer import OrderBuffer
9982-getdata 9 from lib.state import State
9982-getdata 10 from lib.direct import Direct
9982-getdata 11 import helper
9982-getdata 12
9982-getdata 13 class TestGetData(unittest.TestCase):
9982-getdata 14 def setUp(self):
9982-getdata 15 helper.setup()
9982-getdata 16 self.alice_socket = Mock()
9982-getdata 17 self.alice_state = State(self.alice_socket)
9982-getdata 18 self.alice_state.set_knob('nick', 'alice')
9982-getdata 19 self.setupBob()
9982-getdata 20 self.bob_socket = Mock()
9982-getdata 21 self.bob_state = State(self.bob_socket)
9982-getdata 22 self.setupAlice()
9982-getdata 23
9982-getdata 24 def setupBob(self):
9982-getdata 25 self.alice_state.add_peer('bob')
9982-getdata 26 self.alice_state.add_key(
9982-getdata 27 'bob',
9982-getdata 28 '9h6wYndVjt8QpnIZOYb7KD2tYKCKw4rjlYg4LM1ODx1Qkr3qA0IuKNukkwKhQ4UP9ypMlhyPHa7AGD7NO7Ws5w=='
9982-getdata 29 )
9982-getdata 30 self.alice_state.update_at({
9982-getdata 31 'handle': 'bob',
9982-getdata 32 'address': '127.0.0.1',
9982-getdata 33 'port': 8889
9982-getdata 34 })
9982-getdata 35
9982-getdata 36 def setupAlice(self):
9982-getdata 37 self.bob_state.add_peer('alice')
9982-getdata 38 self.bob_state.add_key(
9982-getdata 39 'alice',
9982-getdata 40 '9h6wYndVjt8QpnIZOYb7KD2tYKCKw4rjlYg4LM1ODx1Qkr3qA0IuKNukkwKhQ4UP9ypMlhyPHa7AGD7NO7Ws5w=='
9982-getdata 41 )
9982-getdata 42 self.bob_state.update_at({
9982-getdata 43 'handle': 'alice',
9982-getdata 44 'address': '127.0.0.1',
9982-getdata 45 'port': 8888
9982-getdata 46 })
9982-getdata 47
9982-getdata 48 def test_send(self):
9982-getdata 49 long_buffer = LongBuffer(self.bob_state)
9982-getdata 50 m1 = Direct({
9982-getdata 51 'handle': 'alice',
9982-getdata 52 'speaker': 'bob',
9982-getdata 53 'body': 'm1',
9982-getdata 54 'timestamp': int(time.time()),
9982-getdata 55 'long_buffer': long_buffer
9982-getdata 56 }, self.bob_state)
9982-getdata 57 m2 = Direct({
9982-getdata 58 'handle': 'alice',
9982-getdata 59 'speaker': 'bob',
9982-getdata 60 'body': 'm2',
9982-getdata 61 'timestamp': int(time.time()),
9982-getdata 62 'long_buffer': long_buffer
9982-getdata 63 }, self.bob_state)
9982-getdata 64 m3 = Direct({
9982-getdata 65 'handle': 'alice',
9982-getdata 66 'speaker': 'bob',
9982-getdata 67 'body': 'm3',
9982-getdata 68 'timestamp': int(time.time()),
9982-getdata 69 'long_buffer': long_buffer
9982-getdata 70 }, self.bob_state)
9982-getdata 71
9982-getdata 72 alice = self.bob_state.get_peer_by_handle('alice')
9982-getdata 73 m1.message_bytes = m1.get_message_bytes(alice)
9982-getdata 74 m1.send()
9982-getdata 75 m2.message_bytes = m2.get_message_bytes(alice)
9982-getdata 76 m2.send()
9982-getdata 77 m3.message_bytes = m3.get_message_bytes(alice)
9982-getdata 78 m3.send()
9982-getdata 79
9982-getdata 80
9982-getdata 81
9982-getdata 82 m1_message_bytes = m1.get_message_bytes(alice)
9982-getdata 83 m1_black_packet = Message.pack(alice, m1.command, m1.bounces, m1_message_bytes)
9982-getdata 84
9982-getdata 85
9982-getdata 86
9982-getdata 87 m3_message_bytes = m3.get_message_bytes(alice)
9982-getdata 88
9982-getdata 89 m3_black_packet = Message.pack(alice, m3.command, m3.bounces, m3_message_bytes)
9982-getdata 90
9982-getdata 91
9982-getdata 92
9982-getdata 93 bob = self.alice_state.get_peer_by_handle('bob')
9982-getdata 94 m1_received = Message.unpack(bob,
9982-getdata 95 m1_black_packet,
9982-getdata 96 LongBuffer(self.alice_state),
9982-getdata 97 OrderBuffer(self.alice_state),
9982-getdata 98 {},
9982-getdata 99 self.alice_state)
9982-getdata 100
9982-getdata 101 m3_received = Message.unpack(bob,
9982-getdata 102 m3_black_packet,
9982-getdata 103 LongBuffer(self.alice_state),
9982-getdata 104 OrderBuffer(self.alice_state),
9982-getdata 105 {},
9982-getdata 106 self.alice_state)
9982-getdata 107
9982-getdata 108 gd_message = GetData(m3_received, 'self_chain', self.alice_state)
9982-getdata 109 gd_message.send()
9982-getdata 110
9982-getdata 111
9982-getdata 112 gd_black_packet = Message.pack(bob,
9982-getdata 113 gd_message.command,
9982-getdata 114 gd_message.bounces,
9982-getdata 115 gd_message.get_message_bytes(bob))
9982-getdata 116 self.alice_socket.sendto.called_once_with(gd_black_packet, (bob.address, bob.port))