-
+ 991E1FF9817A01D4320ABDF30E09C890B5454C726E313A488DD6BEDC9E8E663019A63CAF9FC16979251F653BEDA05ECECA4A806A7E3C299E6847A8B6FE11A6E4blatta/tests/test_station.py(0 . 0)(1 . 194)
1483 # https://stackoverflow.com/questions/1896918/running-unittest-with-typical-test-directory-structure
1484 import unittest
1485 import logging
1486 from mock import Mock
1487 from mock import patch
1488
1489 from lib.station import Station
1490
1491 class TestStation(unittest.TestCase):
1492 def setUp(self):
1493 logging.basicConfig(level=logging.DEBUG)
1494 options = {
1495 "clients": {"clientsocket": Mock()},
1496 "db_path": "tests/test.db",
1497 "socket": Mock()
1498 }
1499 self.station = Station(options)
1500 self.station.deliver = Mock()
1501 self.station.rebroadcast = Mock()
1502 self.station.rebroadcast.return_value = "foobar"
1503
1504 def tearDown(self):
1505 pass
1506
1507 def test_embargo_bounce_ordering(self):
1508 peer1 = Mock()
1509 peer1.handles = ["a", "b"]
1510 peer2 = Mock()
1511 peer2.handles = ["c", "d"]
1512 low_bounce_message = Mock()
1513 low_bounce_message.peer = peer1
1514 low_bounce_message.bounces = 1
1515 low_bounce_message.message_hash = "messagehash"
1516 high_bounce_message = Mock()
1517 high_bounce_message.peer = peer2
1518 high_bounce_message.bounces = 2
1519 high_bounce_message.message_hash = "messagehash"
1520 self.station.embargo_queue = {
1521 "messagehash": [
1522 low_bounce_message,
1523 high_bounce_message
1524 ],
1525 }
1526 self.station.flush_hearsay_messages()
1527 self.station.deliver.assert_called_once_with(low_bounce_message)
1528 self.station.rebroadcast.assert_called_once_with(low_bounce_message)
1529
1530 def test_immediate_message_delivered(self):
1531 peer = Mock()
1532 peer.handles = ["a", "b"]
1533 message = Mock()
1534 message.speaker = "a"
1535 message.peer = peer
1536 self.station.embargo_queue = {
1537 "messagehash": [
1538 message
1539 ],
1540 }
1541 self.station.check_for_immediate_messages()
1542 self.station.deliver.assert_called_once_with(message)
1543 self.station.rebroadcast.assert_called_once_with(message)
1544
1545 def test_hearsay_message_not_delivered(self):
1546 peer = Mock()
1547 peer.handles = ["a", "b"]
1548 message = Mock()
1549 message.speaker = "c"
1550 message.peer = peer
1551 self.station.embargo_queue = {
1552 "messagehash": [
1553 message
1554 ],
1555 }
1556 self.station.check_for_immediate_messages()
1557 self.station.deliver.assert_not_called()
1558
1559 def test_embargo_queue_cleared(self):
1560 peer = Mock()
1561 peer.handles = ["a", "b"]
1562 message = Mock()
1563 message.speaker = "c"
1564 message.peer = peer
1565 self.station.embargo_queue = {
1566 "messagehash": [
1567 message
1568 ],
1569 }
1570 self.assertEqual(len(self.station.embargo_queue), 1)
1571 self.station.flush_hearsay_messages()
1572 self.assertEqual(len(self.station.embargo_queue), 0)
1573
1574 def test_immediate_prefix(self):
1575 peer = Mock()
1576 peer.handles = ["a", "b"]
1577 message = Mock()
1578 message.speaker = "a"
1579 message.prefix = None
1580 message.peer = peer
1581 self.station.embargo_queue = {
1582 "messagehash": [
1583 message
1584 ],
1585 }
1586 self.station.check_for_immediate_messages()
1587 self.assertEqual(message.prefix, None)
1588
1589 def test_simple_hearsay_prefix(self):
1590 peer = Mock()
1591 peer.handles = ["a", "b"]
1592 message = Mock()
1593 message.speaker = "c"
1594 message.prefix = None
1595 message.peer = peer
1596 self.station.embargo_queue = {
1597 "messagehash": [
1598 message
1599 ],
1600 }
1601 self.station.flush_hearsay_messages()
1602 self.assertEqual(message.prefix, "c[a]")
1603
1604 def test_in_wot_hearsay_prefix_under_four(self):
1605 peer1 = Mock()
1606 peer1.handles = ["a", "b"]
1607 peer2 = Mock()
1608 peer2.handles = ["d", "e"]
1609 peer3 = Mock()
1610 peer3.handles = ["f", "g"]
1611 message_via_peer1 = Mock()
1612 message_via_peer1.speaker = "c"
1613 message_via_peer1.prefix = None
1614 message_via_peer1.peer = peer1
1615 message_via_peer1.bounces = 1
1616 message_via_peer2 = Mock()
1617 message_via_peer2.speaker = "c"
1618 message_via_peer2.prefix = None
1619 message_via_peer2.peer = peer2
1620 message_via_peer2.bounces = 2
1621 message_via_peer3 = Mock()
1622 message_via_peer3.speaker = "c"
1623 message_via_peer3.prefix = None
1624 message_via_peer3.peer = peer3
1625 message_via_peer3.bounces = 1
1626 self.station.embargo_queue = {
1627 "messagehash": [
1628 message_via_peer1,
1629 message_via_peer2,
1630 message_via_peer3
1631 ],
1632 }
1633 self.station.flush_hearsay_messages()
1634 self.station.deliver.assert_called_once_with(message_via_peer1)
1635 self.assertEqual(message_via_peer1.prefix, "c[a|d|f]")
1636
1637 def test_in_wot_hearsay_prefix_more_than_three(self):
1638 peer1 = Mock()
1639 peer1.handles = ["a", "b"]
1640 peer2 = Mock()
1641 peer2.handles = ["d", "e"]
1642 peer3 = Mock()
1643 peer3.handles = ["f", "g"]
1644 peer4 = Mock()
1645 peer4.handles = ["f", "g"]
1646 message_via_peer1 = Mock()
1647 message_via_peer1.speaker = "c"
1648 message_via_peer1.prefix = None
1649 message_via_peer1.peer = peer1
1650 message_via_peer1.bounces = 1
1651 message_via_peer2 = Mock()
1652 message_via_peer2.speaker = "c"
1653 message_via_peer2.prefix = None
1654 message_via_peer2.peer = peer2
1655 message_via_peer2.bounces = 2
1656 message_via_peer3 = Mock()
1657 message_via_peer3.speaker = "c"
1658 message_via_peer3.prefix = None
1659 message_via_peer3.peer = peer3
1660 message_via_peer3.bounces = 1
1661 message_via_peer4 = Mock()
1662 message_via_peer4.speaker = "c"
1663 message_via_peer4.prefix = None
1664 message_via_peer4.peer = peer4
1665 message_via_peer4.bounces = 1
1666 self.station.embargo_queue = {
1667 "messagehash": [
1668 message_via_peer1,
1669 message_via_peer2,
1670 message_via_peer3,
1671 message_via_peer4
1672 ],
1673 }
1674 self.station.flush_hearsay_messages()
1675 self.station.deliver.assert_called_once_with(message_via_peer1)
1676 self.assertEqual(message_via_peer1.prefix, "c[4]")