Ejemplo n.º 1
0
 def setUp(self):
     self.message_handler = DummyMessageHandler()
     self.gw = Gateway(self.message_handler)
     self.gw.start()
Ejemplo n.º 2
0
class TestGateway(unittest.TestCase):

    def setUp(self):
        self.message_handler = DummyMessageHandler()
        self.gw = Gateway(self.message_handler)
        self.gw.start()

    def tearDown(self):
        self.gw.stop()
        self.gw.join()

    def test_noNodesAfterStartup(self):
        self.assertEqual(len(self.gw.get_connected_nodes()), 0)

    def test_Register(self):
        led = Device("name", self.message_handler)
        self.assertEqual(led.connect(), True)
        time.sleep(0.1)
        connected_nodes = self.gw.get_connected_nodes()
        self.assertEqual(len(connected_nodes), 1)
        node = connected_nodes[led.node_id]
        self.assertEqual(node.name, led.name)
        self.assertEqual(node.registered, True)

        last_seen = node.last_seen

        led._send_status()
        time.sleep(0.1)

        connected_nodes = self.gw.get_connected_nodes()
        self.assertEqual(len(connected_nodes), 1)
        node = connected_nodes[led.node_id]
        self.assertEqual(node.name, led.name)
        self.assertEqual(node.registered, True)
        self.assertNotEqual(node.last_seen, last_seen)

        # calling connect again should change nothing
        self.assertEqual(led.connect(), True)
        connected_nodes = self.gw.get_connected_nodes()
        self.assertEqual(len(connected_nodes), 1)

    def test_sendUnknownMessage(self):
        header = MessageHeader(node_id = 1, group_id = 1, wants_ack = False)
        m = Message(99, header)
        self.message_handler.write_message_from_device(m)
        self.assertEqual(len(self.gw.get_connected_nodes()), 0)
        # wait so the gateway can finish processing
        time.sleep(0.01)

    def test_sendACKForUnknownMessage(self):
        dev = Device("name", self.message_handler)
        self.assertEqual(dev.connect(), True)
        header = MessageHeader(node_id = dev.node_id, group_id = dev.group_id, wants_ack = False)
        m = ACKMessage(header)
        self.message_handler.write_message_from_device(m)
        time.sleep(0.1)
        self.assertEqual(len(self.gw.get_connected_nodes()), 1)

    def test_sendACKForUnknownNode(self):
        header = MessageHeader(node_id = 1, group_id = 1, wants_ack = False)
        m = ACKMessage(header)
        self.message_handler.write_message_from_device(m)
        self.assertEqual(len(self.gw.get_connected_nodes()), 0)
        # wait so the gateway can finish processing
        time.sleep(0.1)

    def test_moreThan30Nodes(self):
        for i in range(0, 30):
            dev = Device('%s' % i, self.message_handler)
            if i == 29:
                # too many nodes registered
                self.assertEqual(dev.connect(), False)
            else:
                self.assertEqual(dev.connect(), True)

        time.sleep(0.1)
        connected_nodes = self.gw.get_connected_nodes()
        self.assertEqual(len(connected_nodes), 29)

    def test_sendRegisterMessageWithoutACKRequest(self):
        header = MessageHeader(node_id = 1, group_id = 1, wants_ack = False)
        m = RegisterMessage(header)
        self.message_handler.write_message_from_device(m)
        time.sleep(0.1)
        self.assertEqual(len(self.gw.get_connected_nodes()), 1)

    def test_sendRegisterMessageWithWrongNodeId(self):
        header = MessageHeader(node_id = 5, group_id = 1, wants_ack = False)
        m = RegisterMessage(header)
        self.message_handler.write_message_from_device(m)
        time.sleep(0.1)
        self.assertEqual(len(self.gw.get_connected_nodes()), 0)

    def test_sendStatusForUnknownNode(self):
        header = MessageHeader(node_id = 5, group_id = 1, wants_ack = False)
        m = StatusMessage(header, name = 'dev name')
        self.message_handler.write_message_from_device(m)
        self.assertEqual(len(self.gw.get_connected_nodes()), 0)
        # wait so the gateway can finish processing
        time.sleep(0.1)

    def test_Register_lostRegisterResponse(self):
        token = random.random()

        dev = Device('test dev', self.message_handler)

        header = MessageHeader(node_id = 1, group_id = 1, wants_ack = False)
        p = RegisterMessage(header, name = dev.name, token = token)
        self.message_handler.write_message_from_device(p)

        # ignore RegisterResponseMessage for now
        m = dev._incoming_messages.get(True, 1)

        # the gateway should already list the node as not registered
        connected_nodes = self.gw.get_connected_nodes()
        self.assertEqual(len(connected_nodes), 1)
        self.assertEqual(connected_nodes[m.new_node_id].name, dev.name)
        self.assertEqual(connected_nodes[m.new_node_id].registered, False)

        # write a register message again, let's assume the RegisterResponse
        # Message was lost
        self.message_handler.write_message_from_device(p)

        m2 = dev._incoming_messages.get(True, 1)
        self.assertEqual(m.token, m2.token)
        self.assertEqual(m.new_node_id, m2.new_node_id)

        # the gateway should still list the node as not registered
        connected_nodes = self.gw.get_connected_nodes()
        self.assertEqual(len(connected_nodes), 1)
        self.assertEqual(connected_nodes[m.new_node_id].name, dev.name)
        self.assertEqual(connected_nodes[m.new_node_id].registered, False)