9987-embargoing 1
9987-embargoing 2 import unittest
9987-embargoing 3 import logging
9987-embargoing 4 from mock import Mock
9987-embargoing 5 from mock import patch
9987-embargoing 6
9987-embargoing 7 from lib.station import Station
9987-embargoing 8
9987-embargoing 9 class TestStation(unittest.TestCase):
9987-embargoing 10 def setUp(self):
9987-embargoing 11 logging.basicConfig(level=logging.DEBUG)
9987-embargoing 12 options = {
9987-embargoing 13 "clients": {"clientsocket": Mock()},
9987-embargoing 14 "db_path": "tests/test.db",
9987-embargoing 15 "socket": Mock()
9987-embargoing 16 }
9987-embargoing 17 self.station = Station(options)
9987-embargoing 18 self.station.deliver = Mock()
9987-embargoing 19 self.station.rebroadcast = Mock()
9987-embargoing 20 self.station.rebroadcast.return_value = "foobar"
9987-embargoing 21
9987-embargoing 22 def tearDown(self):
9987-embargoing 23 pass
9987-embargoing 24
9987-embargoing 25 def test_embargo_bounce_ordering(self):
9987-embargoing 26 peer1 = Mock()
9987-embargoing 27 peer1.handles = ["a", "b"]
9987-embargoing 28 peer2 = Mock()
9987-embargoing 29 peer2.handles = ["c", "d"]
9987-embargoing 30 low_bounce_message = Mock()
9987-embargoing 31 low_bounce_message.peer = peer1
9987-embargoing 32 low_bounce_message.bounces = 1
9987-embargoing 33 low_bounce_message.message_hash = "messagehash"
9987-embargoing 34 high_bounce_message = Mock()
9987-embargoing 35 high_bounce_message.peer = peer2
9987-embargoing 36 high_bounce_message.bounces = 2
9987-embargoing 37 high_bounce_message.message_hash = "messagehash"
9987-embargoing 38 self.station.embargo_queue = {
9987-embargoing 39 "messagehash": [
9987-embargoing 40 low_bounce_message,
9987-embargoing 41 high_bounce_message
9987-embargoing 42 ],
9987-embargoing 43 }
9987-embargoing 44 self.station.flush_hearsay_messages()
9987-embargoing 45 self.station.deliver.assert_called_once_with(low_bounce_message)
9987-embargoing 46 self.station.rebroadcast.assert_called_once_with(low_bounce_message)
9987-embargoing 47
9987-embargoing 48 def test_immediate_message_delivered(self):
9987-embargoing 49 peer = Mock()
9987-embargoing 50 peer.handles = ["a", "b"]
9987-embargoing 51 message = Mock()
9987-embargoing 52 message.speaker = "a"
9987-embargoing 53 message.peer = peer
9987-embargoing 54 self.station.embargo_queue = {
9987-embargoing 55 "messagehash": [
9987-embargoing 56 message
9987-embargoing 57 ],
9987-embargoing 58 }
9987-embargoing 59 self.station.check_for_immediate_messages()
9987-embargoing 60 self.station.deliver.assert_called_once_with(message)
9987-embargoing 61 self.station.rebroadcast.assert_called_once_with(message)
9987-embargoing 62
9987-embargoing 63 def test_hearsay_message_not_delivered(self):
9987-embargoing 64 peer = Mock()
9987-embargoing 65 peer.handles = ["a", "b"]
9987-embargoing 66 message = Mock()
9987-embargoing 67 message.speaker = "c"
9987-embargoing 68 message.peer = peer
9987-embargoing 69 self.station.embargo_queue = {
9987-embargoing 70 "messagehash": [
9987-embargoing 71 message
9987-embargoing 72 ],
9987-embargoing 73 }
9987-embargoing 74 self.station.check_for_immediate_messages()
9987-embargoing 75 self.station.deliver.assert_not_called()
9987-embargoing 76
9987-embargoing 77 def test_embargo_queue_cleared(self):
9987-embargoing 78 peer = Mock()
9987-embargoing 79 peer.handles = ["a", "b"]
9987-embargoing 80 message = Mock()
9987-embargoing 81 message.speaker = "c"
9987-embargoing 82 message.peer = peer
9987-embargoing 83 self.station.embargo_queue = {
9987-embargoing 84 "messagehash": [
9987-embargoing 85 message
9987-embargoing 86 ],
9987-embargoing 87 }
9987-embargoing 88 self.assertEqual(len(self.station.embargo_queue), 1)
9987-embargoing 89 self.station.flush_hearsay_messages()
9987-embargoing 90 self.assertEqual(len(self.station.embargo_queue), 0)
9987-embargoing 91
9987-embargoing 92 def test_immediate_prefix(self):
9987-embargoing 93 peer = Mock()
9987-embargoing 94 peer.handles = ["a", "b"]
9987-embargoing 95 message = Mock()
9987-embargoing 96 message.speaker = "a"
9987-embargoing 97 message.prefix = None
9987-embargoing 98 message.peer = peer
9987-embargoing 99 self.station.embargo_queue = {
9987-embargoing 100 "messagehash": [
9987-embargoing 101 message
9987-embargoing 102 ],
9987-embargoing 103 }
9987-embargoing 104 self.station.check_for_immediate_messages()
9987-embargoing 105 self.assertEqual(message.prefix, None)
9987-embargoing 106
9987-embargoing 107 def test_simple_hearsay_prefix(self):
9987-embargoing 108 peer = Mock()
9987-embargoing 109 peer.handles = ["a", "b"]
9987-embargoing 110 message = Mock()
9987-embargoing 111 message.speaker = "c"
9987-embargoing 112 message.prefix = None
9987-embargoing 113 message.peer = peer
9987-embargoing 114 self.station.embargo_queue = {
9987-embargoing 115 "messagehash": [
9987-embargoing 116 message
9987-embargoing 117 ],
9987-embargoing 118 }
9987-embargoing 119 self.station.flush_hearsay_messages()
9987-embargoing 120 self.assertEqual(message.prefix, "c[a]")
9987-embargoing 121
9987-embargoing 122 def test_in_wot_hearsay_prefix_under_four(self):
9987-embargoing 123 peer1 = Mock()
9987-embargoing 124 peer1.handles = ["a", "b"]
9987-embargoing 125 peer2 = Mock()
9987-embargoing 126 peer2.handles = ["d", "e"]
9987-embargoing 127 peer3 = Mock()
9987-embargoing 128 peer3.handles = ["f", "g"]
9987-embargoing 129 message_via_peer1 = Mock()
9987-embargoing 130 message_via_peer1.speaker = "c"
9987-embargoing 131 message_via_peer1.prefix = None
9987-embargoing 132 message_via_peer1.peer = peer1
9987-embargoing 133 message_via_peer1.bounces = 1
9987-embargoing 134 message_via_peer2 = Mock()
9987-embargoing 135 message_via_peer2.speaker = "c"
9987-embargoing 136 message_via_peer2.prefix = None
9987-embargoing 137 message_via_peer2.peer = peer2
9987-embargoing 138 message_via_peer2.bounces = 2
9987-embargoing 139 message_via_peer3 = Mock()
9987-embargoing 140 message_via_peer3.speaker = "c"
9987-embargoing 141 message_via_peer3.prefix = None
9987-embargoing 142 message_via_peer3.peer = peer3
9987-embargoing 143 message_via_peer3.bounces = 1
9987-embargoing 144 self.station.embargo_queue = {
9987-embargoing 145 "messagehash": [
9987-embargoing 146 message_via_peer1,
9987-embargoing 147 message_via_peer2,
9987-embargoing 148 message_via_peer3
9987-embargoing 149 ],
9987-embargoing 150 }
9987-embargoing 151 self.station.flush_hearsay_messages()
9987-embargoing 152 self.station.deliver.assert_called_once_with(message_via_peer1)
9987-embargoing 153 self.assertEqual(message_via_peer1.prefix, "c[a|d|f]")
9987-embargoing 154
9987-embargoing 155 def test_in_wot_hearsay_prefix_more_than_three(self):
9987-embargoing 156 peer1 = Mock()
9987-embargoing 157 peer1.handles = ["a", "b"]
9987-embargoing 158 peer2 = Mock()
9987-embargoing 159 peer2.handles = ["d", "e"]
9987-embargoing 160 peer3 = Mock()
9987-embargoing 161 peer3.handles = ["f", "g"]
9987-embargoing 162 peer4 = Mock()
9987-embargoing 163 peer4.handles = ["f", "g"]
9987-embargoing 164 message_via_peer1 = Mock()
9987-embargoing 165 message_via_peer1.speaker = "c"
9987-embargoing 166 message_via_peer1.prefix = None
9987-embargoing 167 message_via_peer1.peer = peer1
9987-embargoing 168 message_via_peer1.bounces = 1
9987-embargoing 169 message_via_peer2 = Mock()
9987-embargoing 170 message_via_peer2.speaker = "c"
9987-embargoing 171 message_via_peer2.prefix = None
9987-embargoing 172 message_via_peer2.peer = peer2
9987-embargoing 173 message_via_peer2.bounces = 2
9987-embargoing 174 message_via_peer3 = Mock()
9987-embargoing 175 message_via_peer3.speaker = "c"
9987-embargoing 176 message_via_peer3.prefix = None
9987-embargoing 177 message_via_peer3.peer = peer3
9987-embargoing 178 message_via_peer3.bounces = 1
9987-embargoing 179 message_via_peer4 = Mock()
9987-embargoing 180 message_via_peer4.speaker = "c"
9987-embargoing 181 message_via_peer4.prefix = None
9987-embargoing 182 message_via_peer4.peer = peer4
9987-embargoing 183 message_via_peer4.bounces = 1
9987-embargoing 184 self.station.embargo_queue = {
9987-embargoing 185 "messagehash": [
9987-embargoing 186 message_via_peer1,
9987-embargoing 187 message_via_peer2,
9987-embargoing 188 message_via_peer3,
9987-embargoing 189 message_via_peer4
9987-embargoing 190 ],
9987-embargoing 191 }
9987-embargoing 192 self.station.flush_hearsay_messages()
9987-embargoing 193 self.station.deliver.assert_called_once_with(message_via_peer1)
9987-embargoing 194 self.assertEqual(message_via_peer1.prefix, "c[4]")