Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed flat_map with mapper returning an asyncio Future (#460)
As of python 3.7 Asyncio Futures implement an __iter__ method, probably for backward compatibility. As a consequence, asyncio futures are also iterable. So we must check if the mapper result is a Future before checking if it is Iterable. Fixes #457
- Loading branch information
Showing
2 changed files
with
37 additions
and
3 deletions.
There are no files selected for viewing
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
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,33 @@ | ||
import unittest | ||
import asyncio | ||
from rx import operators as ops | ||
from rx.subject import Subject | ||
|
||
from rx.scheduler.eventloop import AsyncIOScheduler | ||
|
||
|
||
class TestFlatMapAsync(unittest.TestCase): | ||
|
||
def test_flat_map_async(self): | ||
actual_next = None | ||
loop = asyncio.get_event_loop() | ||
scheduler = AsyncIOScheduler(loop=loop) | ||
|
||
def mapper(i): | ||
async def _mapper(i): | ||
return i + 1 | ||
|
||
return asyncio.ensure_future(_mapper(i)) | ||
|
||
def on_next(i): | ||
nonlocal actual_next | ||
actual_next = i | ||
|
||
async def test_flat_map(): | ||
x = Subject() | ||
x.pipe(ops.flat_map(mapper)).subscribe(on_next, scheduler=scheduler) | ||
x.on_next(10) | ||
await asyncio.sleep(0.1) | ||
|
||
loop.run_until_complete(test_flat_map()) | ||
assert actual_next == 11 |