-
Notifications
You must be signed in to change notification settings - Fork 14
feat: auto-convert Python objects that are passed to/from Queue Binding #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+227
−18
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c996aca
feat: auto-convert Python objects that are passed to/from Queue
ryanking13 fa0650e
chore: add comment about sleep
ryanking13 90e92bc
chore: merge queue tests to reduce test time
ryanking13 83b3872
Merge remote-tracking branch 'origin/main' into gyeongjae/queue-wrapper
ryanking13 339b737
chore: forward env and ctx to queue handler
ryanking13 6dd6cc3
chore: tidy up queue tests
ryanking13 72b2581
chore: fix cancel_all_tasks to properly cancel pending tasks
ryanking13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,125 @@ | ||
| import asyncio | ||
|
|
||
| import pytest | ||
|
|
||
| pytestmark = pytest.mark.asyncio | ||
|
|
||
| _cache = None | ||
|
|
||
|
|
||
| def _find(messages, predicate): | ||
| return next(m for m in messages if predicate(m)) | ||
|
|
||
|
|
||
| # Send everything at once to reduce the overall test time. | ||
| # Receiving a message from the queue takes ~2 seconds, | ||
| # so batching all sends into a single sleep is more efficient. | ||
| async def _send_all_messages(env): | ||
| global _cache | ||
| if _cache is not None: | ||
| return _cache | ||
|
|
||
| from worker import RECEIVED_MESSAGES | ||
|
|
||
| RECEIVED_MESSAGES.clear() | ||
|
|
||
| q = env.TEST_QUEUE | ||
| await asyncio.gather( | ||
| q.send("hello queue"), | ||
| q.send({"key": "value", "number": 42}), | ||
| q.send(123), | ||
| q.send("text message", contentType="text"), | ||
| q.send(None), | ||
| q.send(True), | ||
| q.send([1, 2, 3]), | ||
| q.send(""), | ||
| q.send({"outer": {"inner": "deep"}, "list": [1, 2]}), | ||
| q.sendBatch( | ||
| [ | ||
| {"body": "batch 1"}, | ||
| {"body": "batch 2"}, | ||
| {"body": "batch 3"}, | ||
| ] | ||
| ), | ||
| q.sendBatch( | ||
| [{"body": "text msg", "contentType": "text"}], | ||
| delaySeconds=0, | ||
| ), | ||
| ) | ||
|
|
||
| await asyncio.sleep(2) | ||
|
|
||
| assert len(RECEIVED_MESSAGES) >= 13 | ||
| _cache = list(RECEIVED_MESSAGES) | ||
| return _cache | ||
|
|
||
|
|
||
| async def test_send_string(env): | ||
| msgs = await _send_all_messages(env) | ||
| msg = _find(msgs, lambda m: m["body"] == "hello queue") | ||
| assert isinstance(msg["id"], str) | ||
| assert msg["attempts"] >= 1 | ||
|
|
||
|
|
||
| async def test_send_dict(env): | ||
| msgs = await _send_all_messages(env) | ||
| msg = _find( | ||
| msgs, | ||
| lambda m: isinstance(m["body"], dict) and m["body"].get("key") == "value", | ||
| ) | ||
| assert msg["body"]["number"] == 42 | ||
|
|
||
|
|
||
| async def test_send_number(env): | ||
| msgs = await _send_all_messages(env) | ||
| _find(msgs, lambda m: m["body"] == 123) | ||
|
|
||
|
|
||
| async def test_send_with_content_type(env): | ||
| msgs = await _send_all_messages(env) | ||
| _find(msgs, lambda m: m["body"] == "text message") | ||
|
|
||
|
|
||
| async def test_send_none(env): | ||
| msgs = await _send_all_messages(env) | ||
| _find(msgs, lambda m: m["body"] is None) | ||
|
|
||
|
|
||
| async def test_send_bool(env): | ||
| msgs = await _send_all_messages(env) | ||
| _find(msgs, lambda m: m["body"] is True) | ||
|
|
||
|
|
||
| async def test_send_list(env): | ||
| msgs = await _send_all_messages(env) | ||
| _find(msgs, lambda m: m["body"] == [1, 2, 3]) | ||
|
|
||
|
|
||
| async def test_send_empty_string(env): | ||
| msgs = await _send_all_messages(env) | ||
| _find(msgs, lambda m: m["body"] == "") | ||
|
|
||
|
|
||
| async def test_send_nested_dict(env): | ||
| msgs = await _send_all_messages(env) | ||
| msg = _find( | ||
| msgs, | ||
| lambda m: isinstance(m["body"], dict) | ||
| and isinstance(m["body"].get("outer"), dict), | ||
| ) | ||
| assert msg["body"]["outer"]["inner"] == "deep" | ||
| assert msg["body"]["list"] == [1, 2] | ||
|
|
||
|
|
||
| async def test_send_batch(env): | ||
| msgs = await _send_all_messages(env) | ||
| bodies = [m["body"] for m in msgs] | ||
| assert "batch 1" in bodies | ||
| assert "batch 2" in bodies | ||
| assert "batch 3" in bodies | ||
|
|
||
|
|
||
| async def test_send_batch_with_options(env): | ||
| msgs = await _send_all_messages(env) | ||
| bodies = [m["body"] for m in msgs] | ||
| assert "text msg" in bodies |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.