-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes pull.simple.RunOnce by correctly deciding to call sync/async po…
…ll()
- Loading branch information
1 parent
caff206
commit 3c02adb
Showing
5 changed files
with
115 additions
and
42 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
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,36 @@ | ||
from pnp.plugins.pull import Polling, AsyncPolling, PullBase | ||
|
||
|
||
class SyncPollingDummy(Polling): | ||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
|
||
def poll(self): | ||
return 42 | ||
|
||
|
||
class AsyncPollingDummy(AsyncPolling): | ||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
|
||
def poll(self): | ||
raise Exception("Don't call the sync version of pull") | ||
|
||
async def async_poll(self): | ||
return 42 | ||
|
||
|
||
class ErrorPollingDummy(Polling): | ||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
|
||
def poll(self): | ||
raise Exception("Crash on purpose!") | ||
|
||
|
||
class NoPollingDummy(PullBase): | ||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
|
||
def poll(self): | ||
raise Exception("Do not call me!") |
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,65 @@ | ||
import time | ||
|
||
import pytest | ||
|
||
from pnp.plugins.pull.simple import RunOnce | ||
from . import make_runner, start_runner | ||
|
||
|
||
def test_init_with_no_poll(): | ||
with pytest.raises(TypeError, match="The component to wrap has to be a polling component"): | ||
RunOnce(name='pytest', poll={'plugin': 'tests.dummies.polling.NoPollingDummy'}) | ||
|
||
|
||
def test_pull_without_wrapped_poll(): | ||
events = [] | ||
def callback(plugin, payload): | ||
events.append(payload) | ||
|
||
dut = RunOnce(name='pytest') | ||
runner = make_runner(dut, callback) | ||
with start_runner(runner): | ||
time.sleep(0.01) | ||
|
||
assert events == [{}] | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_async_pull_without_wrapped_poll(): | ||
events = [] | ||
def callback(plugin, payload): | ||
events.append(payload) | ||
|
||
dut = RunOnce(name='pytest') | ||
runner = make_runner(dut, callback) | ||
with start_runner(runner): | ||
time.sleep(0.01) | ||
|
||
assert events == [{}] | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_async_pull_with_sync_wrapped_poll(): | ||
events = [] | ||
def callback(plugin, payload): | ||
events.append(payload) | ||
|
||
dut = RunOnce(name='pytest', poll={'plugin': 'tests.dummies.polling.SyncPollingDummy'}) | ||
runner = make_runner(dut, callback) | ||
with start_runner(runner): | ||
time.sleep(0.01) | ||
|
||
assert events == [42] | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_async_pull_with_async_wrapped_poll(): | ||
events = [] | ||
def callback(plugin, payload): | ||
events.append(payload) | ||
|
||
dut = RunOnce(name='pytest', poll={'plugin': 'tests.dummies.polling.AsyncPollingDummy'}) | ||
dut.on_payload = callback | ||
await dut.async_pull() | ||
|
||
assert events == [42] |
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