-
-
Notifications
You must be signed in to change notification settings - Fork 86
Fix branching off an async rx node #1178
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
Open
philippjfr
wants to merge
5
commits into
main
Choose a base branch
from
fix/async-branch-stranded-undefined
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+110
−5
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
819b332
Fix branching off an async rx node
philippjfr 4f910c4
Simplify comment
philippjfr e94628a
Apply batched suggestions from code review
philippjfr 4bbd55d
Apply batched suggestions from code review
philippjfr cc3667b
Apply batched suggestions from code review
philippjfr 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
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 |
|---|---|---|
|
|
@@ -1069,6 +1069,100 @@ async def expensive_compute(a): | |
| # the superseded updates are not computed at all. | ||
| assert call_count == 2 | ||
|
|
||
| async def _pair(value): | ||
| await asyncio.sleep(0.02) | ||
| return (value * 2, value * 3) | ||
|
|
||
| async def test_async_shared_rx_branch_after_settling_resolves(): | ||
| irx = rx(1) | ||
| node = irx.rx.pipe(_pair) | ||
| node.rx.value | ||
| await async_wait_until(lambda: node.rx.value == (2, 3)) | ||
|
|
||
| # The branch mirrors a node that has already settled, so it must resolve | ||
| # on its first read rather than being stranded on Undefined. | ||
| first = node[0] | ||
| assert first.rx.value == 2 | ||
|
|
||
| irx.rx.value = 3 | ||
| await async_wait_until(lambda: first.rx.value == 6) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned #1179 (comment). Having numbers only make it harder to read the test, than what it should be. |
||
|
|
||
| async def test_async_shared_rx_branch_while_awaiting_resolves(): | ||
| irx = rx(1) | ||
| node = irx.rx.pipe(_pair) | ||
| node.rx.value | ||
|
|
||
| first = node[0] | ||
| assert first.rx.value is param.Undefined | ||
| await async_wait_until(lambda: first.rx.value == 2) | ||
|
|
||
| async def test_async_shared_rx_branch_before_resolving_resolves(): | ||
| irx = rx(1) | ||
| node = irx.rx.pipe(_pair) | ||
| first, second = node[0], node[1] | ||
|
|
||
| assert first.rx.value is param.Undefined | ||
|
philippjfr marked this conversation as resolved.
|
||
| await async_wait_until(lambda: first.rx.value == 2) | ||
|
|
||
| # The shared node has settled by now, so the sibling branch resolves on | ||
| # its first read. | ||
| assert second.rx.value == 3 | ||
|
|
||
| async def test_async_shared_rx_branch_computed_once(): | ||
| call_count = 0 | ||
|
|
||
| async def count_pair(value): | ||
| nonlocal call_count | ||
| call_count += 1 | ||
| await asyncio.sleep(0.02) | ||
| return (value * 2, value * 3) | ||
|
|
||
| irx = rx(1) | ||
| node = irx.rx.pipe(count_pair) | ||
| first, second = node[0], node[1] | ||
|
|
||
| # Request the value for both nodes | ||
| first.rx.value | ||
|
philippjfr marked this conversation as resolved.
|
||
| second.rx.value | ||
|
philippjfr marked this conversation as resolved.
|
||
| await async_wait_until(lambda: first.rx.value == 2 and second.rx.value == 3) | ||
|
|
||
| # The branches resolve through the shared node rather than recomputing. | ||
| assert call_count == 1 | ||
|
|
||
| irx.rx.value = 3 | ||
| await async_wait_until(lambda: first.rx.value == 6 and second.rx.value == 9) | ||
| assert call_count == 2 | ||
|
|
||
| async def test_async_shared_rx_branch_notifies_watcher(): | ||
| irx = rx(1) | ||
| node = irx.rx.pipe(_pair) | ||
| node.rx.value | ||
| await async_wait_until(lambda: node.rx.value == (2, 3)) | ||
|
|
||
| items = [] | ||
| first = node[0] | ||
| assert first.rx.value == 2 | ||
| first.rx.watch(items.append) | ||
|
|
||
| irx.rx.value = 3 | ||
| await async_wait_until(lambda: items == [6]) | ||
| assert items == [6] | ||
|
|
||
| async def test_async_gen_shared_rx_branch_resolves(): | ||
| async def gen(value): | ||
| for i in range(3): | ||
| await asyncio.sleep(0.02) | ||
| yield (value + i, i) | ||
|
|
||
| irx = rx(1) | ||
| node = irx.rx.pipe(gen) | ||
| node.rx.value | ||
| await async_wait_until(lambda: node.rx.value == (3, 2)) | ||
|
|
||
| # A branch of a generator node adopts the value the generator settled on. | ||
| first = node[0] | ||
| assert first.rx.value == 3 | ||
|
|
||
| @pytest.mark.parametrize('lazy', [False, True]) | ||
| def test_root_invalidation(lazy): | ||
| arx = rx('a', lazy=lazy) | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this always be an async resolve?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then the comments should be updated right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Depends on how you read it, it's only important for the the async case (so that's what it calls out) and is effectively a no-op for the synchronous case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously it was behind a
self._is_asyncwhich made the comment clear. The changes in this PR make the comment more obscure. I think we should add your comment here in the code.