From ea4f3d195ad2b9d7b8364dd6bc20a2c04e8dc37a Mon Sep 17 00:00:00 2001 From: James Ward Date: Mon, 13 Nov 2023 18:07:19 -0500 Subject: [PATCH] test: add test cases to validate Queue behavior --- tests/asyncio/asyncio_queue.py | 89 ++++++++++++++++++++++++++++++ tests/asyncio/asyncio_queue.py.exp | 35 ++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 tests/asyncio/asyncio_queue.py create mode 100644 tests/asyncio/asyncio_queue.py.exp diff --git a/tests/asyncio/asyncio_queue.py b/tests/asyncio/asyncio_queue.py new file mode 100644 index 0000000..cfa1e55 --- /dev/null +++ b/tests/asyncio/asyncio_queue.py @@ -0,0 +1,89 @@ +# SPDX-FileCopyrightText: 2019 Damien P. George +# +# SPDX-License-Identifier: MIT +# +# MicroPython uasyncio module +# MIT license; Copyright (c) 2019 Damien P. George +# +# pylint: skip-file + +import asyncio + + +def print_queue_info(queue: asyncio.Queue): + print("Size:", queue.qsize()) + print("Empty:", queue.empty()) + print("Full:", queue.full()) + + +async def put_queue(queue: asyncio.Queue, value): + await queue.put(value) + + +async def get_queue(queue: asyncio.Queue): + print(await queue.get()) + + +async def main(): + # Can put and retrieve items in the right order + print("=" * 10) + queue = asyncio.Queue() + await queue.put("a") + await queue.put("b") + + print(await queue.get()) + print(await queue.get()) + + # Waits for item to be put if get is called on empty queue + print("=" * 10) + queue = asyncio.Queue() + + task = asyncio.create_task(get_queue(queue)) + await asyncio.sleep(0.01) + print("putting on the queue") + await queue.put("example") + await task + + # Waits for item to be taken off the queue if max size is specified + print("=" * 10) + queue = asyncio.Queue(1) + await queue.put("hello world") + task = asyncio.create_task(put_queue(queue, "example")) + await asyncio.sleep(0.01) + print_queue_info(queue) + print(await queue.get()) + await task + print_queue_info(queue) + print(await queue.get()) + + # Raises an error if not waiting + print("=" * 10) + queue = asyncio.Queue(1) + try: + queue.get_nowait() + except Exception as e: + print(repr(e)) + + await queue.put("hello world") + + try: + queue.put_nowait("example") + except Exception as e: + print(repr(e)) + + # Sets the size, empty, and full values as expected when no max size is set + print("=" * 10) + queue = asyncio.Queue() + print_queue_info(queue) + await queue.put("example") + print_queue_info(queue) + + # Sets the size, empty, and full values as expected when max size is set + print("=" * 10) + queue = asyncio.Queue(1) + print_queue_info(queue) + await queue.put("example") + print_queue_info(queue) + + +asyncio.run(main()) diff --git a/tests/asyncio/asyncio_queue.py.exp b/tests/asyncio/asyncio_queue.py.exp new file mode 100644 index 0000000..793f174 --- /dev/null +++ b/tests/asyncio/asyncio_queue.py.exp @@ -0,0 +1,35 @@ +# SPDX-FileCopyrightText: 2019 Damien P. George +# +# SPDX-License-Identifier: MIT +========== +a +b +========== +putting on the queue +example +========== +Size: 1 +Empty: False +Full: True +hello world +Size: 1 +Empty: False +Full: True +example +========== +QueueEmpty() +QueueFull() +========== +Size: 0 +Empty: True +Full: False +Size: 1 +Empty: False +Full: False +========== +Size: 0 +Empty: True +Full: False +Size: 1 +Empty: False +Full: True