-
-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e312086
commit c1fa224
Showing
4 changed files
with
72 additions
and
12 deletions.
There are no files selected for viewing
Empty file.
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,30 @@ | ||
from functools import partial | ||
from snap7.client import Client | ||
from asyncio import get_running_loop | ||
from concurrent.futures import ThreadPoolExecutor | ||
|
||
|
||
class AsyncClient: | ||
def __await__(self) -> "AsyncClient": | ||
return self | ||
|
||
def __init__(self) -> None: | ||
self.client = Client() | ||
self.loop = get_running_loop() | ||
self.pool = ThreadPoolExecutor() | ||
|
||
async def disconnect(self) -> None: | ||
await self.loop.run_in_executor(self.pool, lambda: self.client.disconnect) | ||
|
||
async def destroy(self) -> None: | ||
await self.loop.run_in_executor(self.pool, lambda: self.client.destroy) | ||
|
||
async def db_write(self, db_number: int, start: int, data: bytearray) -> int: | ||
# func: Callable[[], int] = lambda: self.client.db_write(db_number, start, data) | ||
func = partial(self.client.db_write, db_number, start, data) | ||
result: int = await self.loop.run_in_executor(self.pool, func) # type: ignore | ||
return result | ||
|
||
async def db_get(self, db_number: int) -> bytearray: | ||
result: bytearray = await self.loop.run_in_executor(self.pool, lambda: self.client.db_get(db_number)) | ||
return result |
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,42 @@ | ||
from snap7.asio.client import AsyncClient | ||
from snap7.server import mainloop | ||
from unittest import IsolatedAsyncioTestCase, main | ||
from multiprocessing import Process | ||
from time import sleep | ||
|
||
|
||
class AsyncClientTest(IsolatedAsyncioTestCase): | ||
process = None | ||
|
||
@classmethod | ||
def setUpClass(cls) -> None: | ||
cls.process = Process(target=mainloop) | ||
cls.process.start() | ||
sleep(2) # wait for server to start | ||
|
||
@classmethod | ||
def tearDownClass(cls) -> None: | ||
if cls.process: | ||
cls.process.terminate() | ||
cls.process.join(1) | ||
if cls.process.is_alive(): | ||
cls.process.kill() | ||
|
||
async def asyncSetUp(self) -> None: | ||
self.client = AsyncClient() | ||
|
||
async def asyncTearDown(self) -> None: | ||
await self.client.disconnect() | ||
await self.client.destroy() | ||
|
||
async def test_db_write(self) -> None: | ||
size = 40 | ||
data = bytearray(size) | ||
await self.client.db_write(db_number=1, start=0, data=data) | ||
|
||
async def test_db_get(self) -> None: | ||
await self.client.db_get(db_number=1) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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