summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorcliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a>2009-08-02 00:00:55 +0000
committercliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a>2009-08-02 00:00:55 +0000
commitb4b1886722248e0b85fb7f41ce69040fb80afa7e (patch)
treedd11a36bd189b935336c6a3246748c9192485633 /examples
parente73ca9e6a6d6df369703f5dff08d9681c94fea61 (diff)
downloadpyserial-b4b1886722248e0b85fb7f41ce69040fb80afa7e.tar.gz
udpate tests so that they also work with RFC2217 backend
git-svn-id: http://svn.code.sf.net/p/pyserial/code/trunk/pyserial@270 f19166aa-fa4f-0410-85c2-fa1106f25c8a
Diffstat (limited to 'examples')
-rw-r--r--examples/test.py32
-rw-r--r--examples/test_advanced.py10
-rw-r--r--examples/test_high_load.py8
-rw-r--r--examples/test_iolib.py5
4 files changed, 33 insertions, 22 deletions
diff --git a/examples/test.py b/examples/test.py
index 666275f..7f72741 100644
--- a/examples/test.py
+++ b/examples/test.py
@@ -7,7 +7,7 @@
"""\
Some tests for the serial module.
-Part of pyserial (http://pyserial.sf.net) (C)2001-2008 cliechti@gmx.net
+Part of pyserial (http://pyserial.sf.net) (C)2001-2009 cliechti@gmx.net
Intended to be run on different platforms, to ensure portability of
the code.
@@ -22,12 +22,14 @@ Shortcut these pin pairs:
On a 9 pole DSUB these are the pins (2-3) (4-6) (7-8)
"""
-import unittest, threading, time
+import unittest
+import threading
+import time
import sys
import serial
# on which port should the tests be performed:
-PORT=0
+PORT = 0
if sys.version_info >= (3, 0):
def data(string):
@@ -43,7 +45,7 @@ class Test4_Nonblocking(unittest.TestCase):
timeout = 0
def setUp(self):
- self.s = serial.Serial(PORT, timeout=self.timeout)
+ self.s = serial.serial_class_for_url(PORT, timeout=self.timeout)
def tearDown(self):
self.s.close()
@@ -63,7 +65,7 @@ class Test4_Nonblocking(unittest.TestCase):
for c in bytes_0to255:
self.s.write(c)
# there might be a small delay until the character is ready (especially on win32)
- time.sleep(0.02)
+ time.sleep(0.05)
self.failUnlessEqual(self.s.inWaiting(), 1, "expected exactly one character for inWainting()")
self.failUnlessEqual(self.s.read(1), c, "expected a '%s' which was written before" % c)
self.failUnlessEqual(self.s.read(1), data(''), "expected empty buffer after all sent chars are read")
@@ -72,7 +74,7 @@ class Test4_Nonblocking(unittest.TestCase):
"""timeout: test the timeout/immediate return.
partial results should be returned."""
self.s.write(data("HELLO"))
- time.sleep(0.1) # there might be a small delay until the character is ready (especially on win32)
+ time.sleep(0.1) # there might be a small delay until the character is ready (especially on win32 and rfc2217)
# read more characters as are available to run in the timeout
self.failUnlessEqual(self.s.read(10), data('HELLO'), "expected the 'HELLO' which was written before")
self.failUnlessEqual(self.s.read(1), data(''), "expected empty buffer after all sent chars are read")
@@ -87,8 +89,9 @@ class Test3_Timeout(Test4_Nonblocking):
# this is only here to write out the message in verbose mode
# because Test3 and Test4 print the same messages
+
class SendEvent(threading.Thread):
- def __init__(self, serial, delay=1):
+ def __init__(self, serial, delay=3):
threading.Thread.__init__(self)
self.serial = serial
self.delay = delay
@@ -114,7 +117,7 @@ class Test1_Forever(unittest.TestCase):
character is sent after some time to stop the test, this is done
through the SendEvent class and the Loopback HW."""
def setUp(self):
- self.s = serial.Serial(PORT, timeout=None)
+ self.s = serial.serial_class_for_url(PORT, timeout=None)
self.event = SendEvent(self.s)
def tearDown(self):
@@ -128,10 +131,11 @@ class Test1_Forever(unittest.TestCase):
if not (self.event.isSet() and c == data('E')):
self.fail("expected marker")
+
class Test2_Forever(unittest.TestCase):
"""Tests a port with no timeout"""
def setUp(self):
- self.s = serial.Serial(PORT, timeout=None)
+ self.s = serial.serial_class_for_url(PORT, timeout=None)
def tearDown(self):
self.s.close()
@@ -145,8 +149,8 @@ class Test2_Forever(unittest.TestCase):
this is also a test for the binary capability of a port."""
for c in bytes_0to255:
self.s.write(c)
- # there might be a small delay until the character is ready (especially on win32)
- time.sleep(0.02)
+ # there might be a small delay until the character is ready (especially on win32 and rfc2217)
+ time.sleep(0.05)
self.failUnlessEqual(self.s.inWaiting(), 1, "expected exactly one character for inWainting()")
self.failUnlessEqual(self.s.read(1), c, "expected an '%s' which was written before" % c)
self.failUnlessEqual(self.s.inWaiting(), 0, "expected empty buffer after all sent chars are read")
@@ -155,7 +159,7 @@ class Test2_Forever(unittest.TestCase):
class Test0_DataWires(unittest.TestCase):
"""Test modem control lines"""
def setUp(self):
- self.s = serial.Serial(PORT)
+ self.s = serial.serial_class_for_url(PORT)
def tearDown(self):
self.s.close()
@@ -182,10 +186,12 @@ class Test0_DataWires(unittest.TestCase):
"""Test RI"""
self.failUnless(not self.s.getRI(), "RI -> 0")
+
class Test_MoreTimeouts(unittest.TestCase):
"""Test with timeouts"""
def setUp(self):
- self.s = serial.Serial() # create an closed serial port
+ # create an closed serial port
+ self.s = serial.serial_class_for_url(PORT, do_not_open=True)
def tearDown(self):
self.s.close()
diff --git a/examples/test_advanced.py b/examples/test_advanced.py
index a19361c..d870735 100644
--- a/examples/test_advanced.py
+++ b/examples/test_advanced.py
@@ -16,7 +16,7 @@ the code.
These tests open a serial port and change all the settings on the fly.
If the port is really correctly configured cannot be determined - that
-would require external hardware or a nullmodem cable and an other
+would require external hardware or a null modem cable and an other
serial port library... Thus it mainly tests that all features are
correctly implemented and that the interface does what it should.
@@ -26,13 +26,14 @@ import unittest
import serial
# on which port should the tests be performed:
-PORT=0
+PORT = 0
class Test_ChangeAttributes(unittest.TestCase):
"""Test with timeouts"""
def setUp(self):
- self.s = serial.Serial() # create a closed serial port
+ # create a closed serial port
+ self.s = serial.serial_class_for_url(PORT, do_not_open=True)
def tearDown(self):
self.s.close()
@@ -81,7 +82,7 @@ class Test_ChangeAttributes(unittest.TestCase):
# test illegal values, depending on machine/port some of these may be valid...
self.s.port = PORT
self.s.open()
- for illegal_value in (500000,576000,921600,92160):
+ for illegal_value in (500000, 576000, 921600, 92160):
self.failUnlessRaises(ValueError, self.s.setBaudrate, illegal_value)
def test_BytesizeSetting(self):
@@ -162,6 +163,7 @@ class Test_ChangeAttributes(unittest.TestCase):
self.s.close()
self.failUnless(not self.s.isOpen())
+
if __name__ == '__main__':
import sys
sys.stdout.write(__doc__)
diff --git a/examples/test_high_load.py b/examples/test_high_load.py
index a4d8e04..25c01be 100644
--- a/examples/test_high_load.py
+++ b/examples/test_high_load.py
@@ -21,11 +21,11 @@ Shortcut these pin pairs:
On a 9 pole DSUB these are the pins (2-3) (4-6) (7-8)
"""
-import unittest, threading, time
+import unittest
import sys
import serial
-#on which port should the tests be performed:
+# on which port should the tests be performed:
PORT = 0
BAUDRATE = 115200
#~ BAUDRATE=9600
@@ -44,7 +44,8 @@ class TestHighLoad(unittest.TestCase):
#~ N = 1
def setUp(self):
- self.s = serial.Serial(PORT,BAUDRATE, timeout=10)
+ self.s = serial.serial_class_for_url(PORT, PORT, BAUDRATE, timeout=10)
+
def tearDown(self):
self.s.close()
@@ -65,6 +66,7 @@ class TestHighLoad(unittest.TestCase):
self.failUnless(read==q*self.N, "expected what was written before. got %d bytes, expected %d" % (len(read), self.N*len(q)))
self.failUnless(self.s.inWaiting()==0, "expected empty buffer after all sent chars are read")
+
if __name__ == '__main__':
import sys
sys.stdout.write(__doc__)
diff --git a/examples/test_iolib.py b/examples/test_iolib.py
index e740a4c..8c03a86 100644
--- a/examples/test_iolib.py
+++ b/examples/test_iolib.py
@@ -1,4 +1,5 @@
-##! /usr/bin/env python
+#! /usr/bin/env python
+#
# Python Serial Port Extension for Win32, Linux, BSD, Jython
# see __init__.py
#
@@ -54,7 +55,7 @@ PORT = 0
class Test_SerialAndIO(unittest.TestCase):
def setUp(self):
- self.s = serial.Serial(PORT, timeout=1)
+ self.s = serial.serial_class_for_url(PORT, timeout=1)
self.io = io.TextIOWrapper(io.BufferedRWPair(self.s, self.s))
def tearDown(self):