summaryrefslogtreecommitdiff
path: root/tests/test_asyncio/mocks.py
diff options
context:
space:
mode:
authorKristján Valur Jónsson <sweskman@gmail.com>2023-01-05 12:42:37 +0000
committerGitHub <noreply@github.com>2023-01-05 14:42:37 +0200
commita9ef0c5d0080bd14e2f189d7f31d83e758346a8d (patch)
treebf22a1be3fccf5dbc5fcd03bf7ee2345d9b07487 /tests/test_asyncio/mocks.py
parenta94772848db87bfc2c3cee20d8ca8b257fc37466 (diff)
downloadredis-py-a9ef0c5d0080bd14e2f189d7f31d83e758346a8d.tar.gz
Make PythonParser resumable (#2510)
* PythonParser is now resumable if _stream IO is interrupted * Add test for parse resumability * Clear PythonParser state when connection or parsing errors occur. * disable test for cluster mode. * Perform "closed" check in a single place. * Update tests * Simplify code. * Remove reduntant test, EOF is detected inside _readline() * Make syncronous PythonParser restartable on error, same as HiredisParser Fix sync PythonParser * Add CHANGES * isort * Move MockStream and MockSocket into their own files
Diffstat (limited to 'tests/test_asyncio/mocks.py')
-rw-r--r--tests/test_asyncio/mocks.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/tests/test_asyncio/mocks.py b/tests/test_asyncio/mocks.py
new file mode 100644
index 0000000..89bd9c0
--- /dev/null
+++ b/tests/test_asyncio/mocks.py
@@ -0,0 +1,51 @@
+import asyncio
+
+# Helper Mocking classes for the tests.
+
+
+class MockStream:
+ """
+ A class simulating an asyncio input buffer, optionally raising a
+ special exception every other read.
+ """
+
+ class TestError(BaseException):
+ pass
+
+ def __init__(self, data, interrupt_every=0):
+ self.data = data
+ self.counter = 0
+ self.pos = 0
+ self.interrupt_every = interrupt_every
+
+ def tick(self):
+ self.counter += 1
+ if not self.interrupt_every:
+ return
+ if (self.counter % self.interrupt_every) == 0:
+ raise self.TestError()
+
+ async def read(self, want):
+ self.tick()
+ want = 5
+ result = self.data[self.pos : self.pos + want]
+ self.pos += len(result)
+ return result
+
+ async def readline(self):
+ self.tick()
+ find = self.data.find(b"\n", self.pos)
+ if find >= 0:
+ result = self.data[self.pos : find + 1]
+ else:
+ result = self.data[self.pos :]
+ self.pos += len(result)
+ return result
+
+ async def readexactly(self, length):
+ self.tick()
+ result = self.data[self.pos : self.pos + length]
+ if len(result) < length:
+ raise asyncio.IncompleteReadError(result, None)
+ self.pos += len(result)
+ return result