-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve read_byte retry mechanism #8
- Loading branch information
Showing
2 changed files
with
63 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import asyncio | ||
import unittest | ||
import comfospot40 | ||
import logging | ||
from serial import SerialException | ||
|
||
|
||
class TestStateLoad(unittest.IsolatedAsyncioTestCase): | ||
async def test_read_byte_ok(self): | ||
class TestSerial: | ||
async def read(self, _): | ||
return b"A" | ||
|
||
s = TestSerial() | ||
p = comfospot40.Parser(s) | ||
self.assertEqual(b"A", await p.read_byte()) | ||
|
||
async def test_read_byte_error(self): | ||
logging.disable(logging.CRITICAL) | ||
|
||
class TestSerial: | ||
async def read(self, _): | ||
raise SerialException("something") | ||
return b"A" | ||
|
||
s = TestSerial() | ||
p = comfospot40.Parser(s) | ||
with self.assertRaises(SerialException): | ||
await p.read_byte(0) | ||
|
||
async def test_read_byte_retry(self): | ||
logging.disable(logging.CRITICAL) | ||
|
||
class TestSerial: | ||
async def read(self, _): | ||
raise SerialException("something") | ||
return b"A" | ||
|
||
s = TestSerial() | ||
p = comfospot40.Parser(s) | ||
with self.assertRaises(SerialException): | ||
await asyncio.wait_for(p.read_byte(0), timeout=1) | ||
|
||
async def test_read_byte_retry_timeout(self): | ||
logging.disable(logging.CRITICAL) | ||
|
||
class TestSerial: | ||
async def read(self, _): | ||
raise SerialException("something") | ||
return b"A" | ||
|
||
s = TestSerial() | ||
p = comfospot40.Parser(s) | ||
with self.assertRaises(asyncio.exceptions.TimeoutError): | ||
await asyncio.wait_for(p.read_byte(2), timeout=0.1) |