blob: 90ccb8514b2a177a5b3de90d34a7ab4c5a2990fd [file] [log] [blame]
Chris Liechti790be842016-09-08 23:36:57 +02001#!/usr/bin/env python
2#
3# This file is part of pySerial - Cross platform serial port support for Python
4# (C) 2016 Chris Liechti <cliechti@gmx.net>
5#
6# SPDX-License-Identifier: BSD-3-Clause
7"""\
8Test serial.threaded related functionality.
9"""
10
11import os
12import unittest
13import serial
14import serial.threaded
15import time
16
17
18# on which port should the tests be performed:
19PORT = 'loop://'
20
21class Test_asyncio(unittest.TestCase):
22 """Test asyncio related functionality"""
23
24 def test_line_reader(self):
25 """simple test of line reader class"""
26
27 class TestLines(serial.threaded.LineReader):
28 def __init__(self):
29 super(TestLines, self).__init__()
30 self.received_lines = []
31
32 def handle_line(self, data):
33 self.received_lines.append(data)
34
35 ser = serial.serial_for_url(PORT, baudrate=115200, timeout=1)
36 with serial.threaded.ReaderThread(ser, TestLines) as protocol:
37 protocol.write_line('hello')
38 time.sleep(1)
39 self.assertEqual(protocol.received_lines, ['hello'])
40
41
42if __name__ == '__main__':
43 import sys
44 sys.stdout.write(__doc__)
45 if len(sys.argv) > 1:
46 PORT = sys.argv[1]
47 sys.stdout.write("Testing port: {!r}\n".format(PORT))
48 sys.argv[1:] = ['-v']
49 # When this module is executed from the command-line, it runs all its tests
50 unittest.main()