示例#1
0
    def test_background_consumer_passthrough(self):
        action = master_api.basic_action()
        fields = {'action_type': 1, 'action_number': 2}

        pty = DummyPty([action.create_input(1, fields)])
        SetUpTestInjections(controller_serial=pty)

        got_output = {'passed': False}

        def callback(output_):
            """ Callback that check if the correct result was returned for OL. """
            self.assertEqual([(3, int(12 * 10.0 / 6.0))], output_['outputs'])
            got_output['passed'] = True

        consumer = BackgroundConsumer(master_api.output_list(), 0, callback, True)
        comm = MasterCommunicator(init_master=False)
        comm.enable_passthrough()
        comm.register_consumer(consumer)
        comm.start()

        pty.fd.write(bytearray(b'OL\x00\x01'))
        pty.fd.write(bytearray(b'\x03\x0c\r\n'))

        pty.master_reply(action.create_output(1, {'resp': 'OK'}))
        output = comm.do_command(action, fields)
        self.assertEqual('OK', output['resp'])

        try:
            consumer._consume()
        except:
            pass  # Just ensure it has at least consumed once

        self.assertEqual(True, got_output['passed'])
        self.assertEqual(bytearray(b'OL\x00\x01\x03\x0c\r\n'), comm.get_passthrough_data())
示例#2
0
    def test_background_consumer(self):
        action = master_api.basic_action()
        fields = {'action_type': 1, 'action_number': 2}

        pty = DummyPty([action.create_input(1, fields)])
        SetUpTestInjections(controller_serial=pty)

        got_output = {'phase': 1}

        def callback(output):
            """ Callback that check if the correct result was returned for OL. """
            if got_output['phase'] == 1:
                self.assertEqual([(3, int(12 * 10.0 / 6.0))], output['outputs'])
                got_output['phase'] = 2
            elif got_output['phase'] == 2:
                self.assertEqual([(3, int(12 * 10.0 / 6.0)), (5, int(6 * 10.0 / 6.0))],
                                 output['outputs'])
                got_output['phase'] = 3

        comm = MasterCommunicator(init_master=False)
        comm.enable_passthrough()
        comm.register_consumer(BackgroundConsumer(master_api.output_list(), 0, callback))
        comm.start()

        pty.fd.write(bytearray(b'OL\x00\x01\x03\x0c\r\n'))
        pty.fd.write(bytearray(b'junkOL\x00\x02\x03\x0c\x05\x06\r\n here'))

        pty.master_reply(action.create_output(1, {'resp': 'OK'}))
        output = comm.do_command(action, fields)
        self.assertEqual('OK', output['resp'])

        time.sleep(0.2)
        self.assertEqual(3, got_output['phase'])
        self.assertEqual(bytearray(b'junk here'), comm.get_passthrough_data())
示例#3
0
    def test_passthrough(self):
        pty = DummyPty([b'data from passthrough'])
        SetUpTestInjections(controller_serial=pty)

        comm = MasterCommunicator(init_master=False)
        comm.enable_passthrough()
        comm.start()

        pty.master_reply(bytearray(b'got it!'))
        comm.send_passthrough_data(bytearray(b'data from passthrough'))
        self.assertEqual(bytearray(b'got it!'), comm.get_passthrough_data())
示例#4
0
    def test_do_command(self):
        action = master_api.basic_action()
        fields = {'action_type': 1, 'action_number': 2}

        pty = DummyPty([action.create_input(1, fields)])
        SetUpTestInjections(controller_serial=pty)

        comm = MasterCommunicator(init_master=False)
        comm.start()

        pty.master_reply(action.create_output(1, {'resp': 'OK'}))
        output = comm.do_command(action, fields)
        self.assertEqual('OK', output['resp'])
示例#5
0
    def test_passthrough_with_commands(self):
        action = master_api.basic_action(self.master_version)
        fields = {'action_type': 1, 'action_number': 2}

        pty = DummyPty([
            action.create_input(1, fields),
            action.create_input(2, fields),
            action.create_input(3, fields)
        ])
        SetUpTestInjections(controller_serial=pty)

        comm = MasterCommunicator(init_master=False)
        comm.enable_passthrough()
        comm.start()

        pty.master_reply(
            bytearray(b'hello') + action.create_output(1, {'resp': 'OK'}))
        self.assertEqual('OK', comm.do_command(action, fields)['resp'])
        self.assertEqual(bytearray(b'hello'), comm.get_passthrough_data())

        pty.master_reply(
            action.create_output(2, {'resp': 'OK'}) + bytearray(b'world'))
        self.assertEqual('OK', comm.do_command(action, fields)['resp'])
        self.assertEqual(bytearray(b'world'), comm.get_passthrough_data())

        pty.master_reply(
            bytearray(b'hello') + action.create_output(3, {'resp': 'OK'}) +
            bytearray(b' world'))
        self.assertEqual('OK', comm.do_command(action, fields)['resp'])
        self.assertEqual(bytearray(b'hello world'),
                         comm.get_passthrough_data())
示例#6
0
    def test_split_data(self):
        action = master_api.basic_action()
        fields = {'action_type': 1, 'action_number': 2}

        sequence = []
        for i in range(1, 18):
            sequence.append(action.create_input(i, fields))

        pty = DummyPty(sequence)
        SetUpTestInjections(controller_serial=pty)

        comm = MasterCommunicator(init_master=False)
        comm.start()

        for i in range(1, 18):
            data = action.create_output(i, {'resp': 'OK'})

            ready = threading.Event()

            def write():
                pty.master_reply(data[:i])
                ready.set()
                pty.master_wait()
                pty.fd.write(data[i:])

            thread = threading.Thread(target=write)
            thread.start()

            ready.wait(2)
            output = comm.do_command(action, fields)
            self.assertEqual('OK', output['resp'])
            thread.join(2)
            assert not thread.is_alive()
示例#7
0
    def test_maintenance_mode(self):
        action = master_api.basic_action(self.master_version)
        fields = {'action_type': 1, 'action_number': 2}

        pty = DummyPty([
            master_api.to_cli_mode().create_input(0),
            bytearray(b'error list\r\n'),
            bytearray(b'exit\r\n')
        ])
        SetUpTestInjections(controller_serial=pty)

        comm = MasterCommunicator(init_master=False)
        comm.start()

        comm.start_maintenance_mode()
        pty.fd.write(bytearray(b'OK'))
        self.assertRaises(InMaintenanceModeException, comm.do_command, action,
                          fields)
        self.assertEqual(bytearray(b'OK'), comm.get_maintenance_data())

        comm.send_maintenance_data(bytearray(b'error list\r\n'))
        pty.fd.write(bytearray(b'the list\n'))
        self.assertEqual(bytearray(b'the list\n'), comm.get_maintenance_data())

        comm.stop_maintenance_mode()
示例#8
0
    def test_timeout_ongoing(self):
        action = master_api.basic_action()
        fields = {'action_type': 1, 'action_number': 2}

        pty = DummyPty([action.create_input(1, fields),
                        action.create_input(2, fields)])
        SetUpTestInjections(controller_serial=pty)

        comm = MasterCommunicator(init_master=False)
        comm.start()

        self.assertRaises(CommunicationTimedOutException, comm.do_command, action, fields,
                          timeout=0.1)

        pty.master_reply(action.create_output(2, {'resp': 'OK'}))
        output = comm.do_command(action, fields)
        self.assertEqual('OK', output['resp'])
示例#9
0
    def test_bytes_counter(self):
        action = master_api.basic_action()
        fields = {'action_type': 1, 'action_number': 2}

        pty = DummyPty([action.create_input(1, fields)])
        SetUpTestInjections(controller_serial=pty)

        comm = MasterCommunicator(init_master=False)
        comm.enable_passthrough()
        comm.start()

        pty.fd.write(bytearray(b'hello'))
        pty.master_reply(action.create_output(1, {'resp': 'OK'}))
        comm.do_command(action, fields)
        self.assertEqual(bytearray(b'hello'), comm.get_passthrough_data())

        self.assertEqual(21, comm.get_communication_statistics()['bytes_written'])
        self.assertEqual(5 + 18, comm.get_communication_statistics()['bytes_read'])
示例#10
0
    def test_timeout(self):
        action = master_api.basic_action()
        fields = {'action_type': 1, 'action_number': 2}

        pty = DummyPty([action.create_input(1, fields)])
        SetUpTestInjections(controller_serial=pty)

        comm = MasterCommunicator(init_master=False)
        comm.start()

        self.assertRaises(CommunicationTimedOutException, comm.do_command, action, fields)
示例#11
0
    def test_crc_checking(self):
        action = master_api.sensor_humidity_list()

        fields1 = {}
        for i in range(0, 32):
            fields1['hum%d' % i] = master_api.Svt(master_api.Svt.RAW, i)
        fields1['crc'] = bytearray([ord('C'), 1, 240])

        fields2 = {}
        for i in range(0, 32):
            fields2['hum%d' % i] = master_api.Svt(master_api.Svt.RAW, 2 * i)
        fields2['crc'] = bytearray([ord('C'), 0, 0])

        pty = DummyPty([action.create_input(1),
                        action.create_input(2)])
        SetUpTestInjections(controller_serial=pty)

        comm = MasterCommunicator(init_master=False)
        comm.start()

        pty.master_reply(action.create_output(1, fields1))
        output = comm.do_command(action)
        self.assertEqual(bytearray(b'\x00'), output['hum0'].get_byte())
        self.assertEqual(bytearray(b'\x01'), output['hum1'].get_byte())

        pty.master_reply(action.create_output(2, fields2))
        with self.assertRaises(CrcCheckFailedException):
            comm.do_command(action)
示例#12
0
    def test_maintenance_passthrough(self):
        action = master_api.basic_action(self.master_version)
        fields = {'action_type': 1, 'action_number': 2}

        pty = DummyPty([
            master_api.to_cli_mode().create_input(0),
            bytearray(b'error list\r\n'),
            bytearray(b'exit\r\n')
        ])
        SetUpTestInjections(controller_serial=pty)

        comm = MasterCommunicator(init_master=False)
        comm.enable_passthrough()
        comm.start()

        ready = threading.Event()

        def get_passthrough():
            """ Background thread that reads the passthrough data. """
            self.assertEqual('Before maintenance', comm.get_passthrough_data())
            ready.set()
            self.assertEqual('After maintenance', comm.get_passthrough_data())

        thread = threading.Thread(target=get_passthrough)
        thread.start()

        pty.fd.write(bytearray(b'Before maintenance'))
        ready.wait(2)
        comm.start_maintenance_mode()

        pty.fd.write(bytearray(b'OK'))
        time.sleep(0.2)
        self.assertRaises(InMaintenanceModeException, comm.do_command, action,
                          fields)
        self.assertEqual(bytearray(b'OK'), comm.get_maintenance_data())

        comm.send_maintenance_data(bytearray(b'error list\r\n'))
        pty.fd.write(bytearray(b'the list\n'))
        time.sleep(0.2)
        self.assertEqual(bytearray(b'the list\n'), comm.get_maintenance_data())

        comm.stop_maintenance_mode()
        pty.fd.write(bytearray(b'After maintenance'))

        thread.join(2)
        assert not thread.is_alive()
示例#13
0
    def test_misalignment(self):
        action = master_api.basic_action()
        fields = {'action_type': 1, 'action_number': 2}

        pty = DummyPty([action.create_input(1, fields),
                        action.create_input(2, fields)])
        SetUpTestInjections(controller_serial=pty)

        comm = MasterCommunicator(init_master=False)
        comm.start()

        data = action.create_output(1, {'resp': 'OK'})
        # Only send partial data.
        pty.master_reply(data[:-4])
        pty.master_reply(b'\r\n')
        with self.assertRaises(CommunicationTimedOutException):
            comm.do_command(action, fields)

        # Next message processed correctly.
        pty.master_reply(action.create_output(2, {'resp': 'OK'}))
        output = comm.do_command(action, fields)
        self.assertEqual('OK', output['resp'])
示例#14
0
    def test_passthrough(self):
        """ Test the passthrough. """
        master_pty = DummyPty(
            [bytearray(b'response'),
             bytearray(b'more response')])
        passthrough_mock = SerialMock([
            sin(bytearray(b'data for the passthrough')),
            sout(bytearray(b'response')),
            sin(bytearray(b'more data')),
            sout(bytearray(b'more response'))
        ])
        SetUpTestInjections(controller_serial=master_pty,
                            passthrough_serial=passthrough_mock)

        master_communicator = MasterCommunicator(init_master=False)
        master_communicator.enable_passthrough()
        master_communicator.start()

        SetUpTestInjections(master_communicator=master_communicator)

        passthrough = PassthroughService()
        passthrough.start()

        master_pty.fd.write(bytearray(b'data for the passthrough'))
        master_pty.master_wait()
        master_pty.fd.write(bytearray(b'more data'))
        master_pty.master_wait()
        time.sleep(0.2)

        self.assertEqual(
            33,
            master_communicator.get_communication_statistics()['bytes_read'])
        self.assertEqual(
            21,
            master_communicator.get_communication_statistics()
            ['bytes_written'])

        self.assertEqual(21, passthrough_mock.bytes_read)
        self.assertEqual(33, passthrough_mock.bytes_written)

        passthrough.stop()