(注意:此问题的背景非常详细,但底部有一个SSCCE可以跳过)
我正在尝试开发基于Python的CLI来与Web服务进行交互。在我的代码库中,我有一个CommunicationService
类,它处理与Web服务的所有直接通信。它公开了一个received_response
属性,该属性返回Observable
(来自RxPY)其他对象可以订阅的属性,以便在从Web服务接收到响应时得到通知。
我已经在click
库上建立了我的CLI逻辑,其中一个子命令的实现如下:
async def enabled(self, request: str, response_handler: Callable[[str], Tuple[bool, str]]) -> None:
self._generate_request(request)
if response_handler is None:
return None
while True:
response = await self.on_response
success, value = response_handler(response)
print(success, value)
if success:
return value
此处发生的事情(在response_handler
不是None
的情况下)是子命令表现为等待来自Web服务的响应的协程(self.on_response == CommunicationService.received_response
并从它可以处理的第一个响应中返回一些处理过的值。
我试图通过创建CommunicationService
被完全嘲笑的测试用例来测试CLI的行为;创建了假Subject
(可以充当Observable
)并且CommunicationService.received_response
被模拟返回它。作为测试的一部分,调用主题的on_next
方法将模拟Web服务响应传递回生产代码:
@when('the communications service receives a response from TestCube Web Service')
def step_impl(context):
context.mock_received_response_subject.on_next(context.text)
我使用点击结果回调'在CLI调用结束时调用的函数,并阻塞,直到coroutine(子命令)完成:
@cli.resultcallback()
def _handle_command_task(task: Coroutine, **_) -> None:
if task:
loop = asyncio.get_event_loop()
result = loop.run_until_complete(task)
loop.close()
print('RESULT:', result)
在测试开始时,我运行CliRunner.invoke
来解雇整个shebang。问题是这是一个阻塞调用,并且会阻塞线程直到CLI完成并返回结果,如果我需要我的测试线程继续进行这样做它没有用,所以它可以同时产生模拟Web服务响应它
我想我需要做的是使用CliRunner.invoke
在新线程上运行ThreadPoolExecutor
。这允许测试逻辑继续在原始线程上执行上面发布的@when
步骤。但是,使用 mock_received_response_subject.on_next
发布的通知似乎不会触发执行以在子命令中继续执行。
我认为解决方案将涉及使用RxPY AsyncIOScheduler
,但我发现这方面的文档有点稀疏且无益。
下面的代码段抓住了我希望问题的本质。如果它可以被修改为可以工作,我应该能够将相同的解决方案应用于我的实际代码,以使其按照我的意愿运行。
import asyncio
import logging
import sys
import time
import click
from click.testing import CliRunner
from rx.subjects import Subject
web_response_subject = Subject()
web_response_observable = web_response_subject.as_observable()
thread_loop = asyncio.new_event_loop()
@click.group()
def cli():
asyncio.set_event_loop(thread_loop)
@cli.resultcallback()
def result_handler(task, **_):
loop = asyncio.get_event_loop()
result = loop.run_until_complete(task) # Should block until subject publishes value
loop.close()
print(result)
@cli.command()
async def get_web_response():
return await web_response_observable
def test():
runner = CliRunner()
future = thread_loop.run_in_executor(None, runner.invoke, cli, ['get_web_response'])
time.sleep(1)
web_response_subject.on_next('foo') # Simulate reception of web response.
time.sleep(1)
result = future.result()
print(result.output)
logging.basicConfig(
level=logging.DEBUG,
format='%(threadName)10s %(name)18s: %(message)s',
stream=sys.stderr,
)
test()
当前行为
程序在运行时挂起,在result = loop.run_until_complete(task)
处阻止。
验收标准
该程序终止并在foo
上打印stdout
。
根据Vincent的帮助,我对代码进行了一些更改。
Relay.enabled
(等待来自Web服务的响应以便处理它们的子命令)现在实现如下:
async def enabled(self, request: str, response_handler: Callable[[str], Tuple[bool, str]]) -> None:
self._generate_request(request)
if response_handler is None:
return None
return await self.on_response \
.select(response_handler) \
.where(lambda result, i: result[0]) \
.select(lambda result, index: result[1]) \
.first()
我不太确定await
如何使用RxPY
observables - 它们会在生成的每个元素上将执行返回给调用者,还是仅在observable已完成(或错误? )。我现在知道它是后者,老实说感觉就像是更自然的选择,并且让我觉得这个功能的实现感觉更加优雅和反应。
我还修改了生成模拟Web服务响应的测试步骤:
@when('the communications service receives a response from TestCube Web Service')
def step_impl(context):
loop = asyncio.get_event_loop()
loop.call_soon_threadsafe(context.mock_received_response_subject.on_next, context.text)
不幸的是,这将无法正常工作,因为CLI是在自己的线程中调用的......
@when('the CLI is run with "{arguments}"')
def step_impl(context, arguments):
loop = asyncio.get_event_loop()
if 'async.cli' in context.tags:
context.async_result = loop.run_in_executor(None, context.cli_runner.invoke, testcube.cli, arguments.split())
else:
...
CLI在调用时创建自己的线程私有事件循环...
def cli(context, hostname, port):
_initialize_logging(context.meta['click_log.core.logger']['level'])
# Create a new event loop for processing commands asynchronously on.
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
...
我认为我需要的是一种允许我的测试步骤在新线程上调用CLI,然后使用获取事件循环的方法:
@when('the communications service receives a response from TestCube Web Service')
def step_impl(context):
loop = _get_cli_event_loop() # Needs to be implemented.
loop.call_soon_threadsafe(context.mock_received_response_subject.on_next, context.text)
似乎并不是一种简单的方法来获取特定线程为自己创建和使用的事件循环,因此我接受了Victor的建议并嘲笑asyncio.new_event_loop
以返回我的测试代码创建和存储的事件循环:
def _apply_mock_event_loop_patch(context):
# Close any already-existing exit stacks.
if hasattr(context, 'mock_event_loop_exit_stack'):
context.mock_event_loop_exit_stack.close()
context.test_loop = asyncio.new_event_loop()
print(context.test_loop)
context.mock_event_loop_exit_stack = ExitStack()
context.mock_event_loop_exit_stack.enter_context(
patch.object(asyncio, 'new_event_loop', spec=True, return_value=context.test_loop))
我更改了我收到的模拟网络回复'测试步骤执行以下操作:
@when('the communications service receives a response from TestCube Web Service')
def step_impl(context):
loop = context.test_loop
loop.call_soon_threadsafe(context.mock_received_response_subject.on_next, context.text)
好消息是,当我执行此步骤时,我实际上会触发Relay.enabled
协程!
现在唯一的问题是最后的测试步骤,等待我在自己的线程中执行CLI并确认CLI在stdout
上发送它的未来:
@then('the CLI should print "{output}"')
def step_impl(context, output):
if 'async.cli' in context.tags:
loop = asyncio.get_event_loop() # main loop, not test loop
result = loop.run_until_complete(context.async_result)
else:
result = context.result
assert_that(result.output, equal_to(output))
我已经尝试过玩这个但是我似乎无法将context.async_result
(将loop.run_in_executor
的未来存储起来)很好地转换为done
并且返回结果。对于当前的实现,我在第一次测试时遇到错误(1.1
)并且第二次测试(1.2
)无限期挂起:
@mock.comms @async.cli @wip
Scenario Outline: Querying relay enable state -- @1.1 # testcube/tests/features/relay.feature:45
When the user queries the enable state of relay 0 # testcube/tests/features/steps/relay.py:17 0.003s
Then the CLI should query the web service about the enable state of relay 0 # testcube/tests/features/steps/relay.py:48 0.000s
When the communications service receives a response from TestCube Web Service # testcube/tests/features/steps/core.py:58 0.000s
"""
{'module':'relays','path':'relays[0].enabled','data':[True]}'
"""
Then the CLI should print "True" # testcube/tests/features/steps/core.py:94 0.003s
Traceback (most recent call last):
File "/Users/davidfallah/testcube_env/lib/python3.5/site-packages/behave/model.py", line 1456, in run
match.run(runner.context)
File "/Users/davidfallah/testcube_env/lib/python3.5/site-packages/behave/model.py", line 1903, in run
self.func(context, *args, **kwargs)
File "testcube/tests/features/steps/core.py", line 99, in step_impl
result = loop.run_until_complete(context.async_result)
File "/usr/local/Cellar/python3/3.5.2_1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/base_events.py", line 387, in run_until_complete
return future.result()
File "/usr/local/Cellar/python3/3.5.2_1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/futures.py", line 274, in result
raise self._exception
File "/usr/local/Cellar/python3/3.5.2_1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/concurrent/futures/thread.py", line 55, in run
result = self.fn(*self.args, **self.kwargs)
File "/Users/davidfallah/testcube_env/lib/python3.5/site-packages/click/testing.py", line 299, in invoke
output = out.getvalue()
ValueError: I/O operation on closed file.
Captured stdout:
RECEIVED WEB RESPONSE: {'module':'relays','path':'relays[0].enabled','data':[True]}'
<Future pending cb=[_chain_future.<locals>._call_check_cancel() at /usr/local/Cellar/python3/3.5.2_1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/futures.py:431]>
@mock.comms @async.cli @wip
Scenario Outline: Querying relay enable state -- @1.2 # testcube/tests/features/relay.feature:46
When the user queries the enable state of relay 1 # testcube/tests/features/steps/relay.py:17 0.005s
Then the CLI should query the web service about the enable state of relay 1 # testcube/tests/features/steps/relay.py:48 0.001s
When the communications service receives a response from TestCube Web Service # testcube/tests/features/steps/core.py:58 0.000s
"""
{'module':'relays','path':'relays[1].enabled','data':[False]}'
"""
RECEIVED WEB RESPONSE: {'module':'relays','path':'relays[1].enabled','data':[False]}'
Then the CLI should print "False" # testcube/tests/features/steps/core.py:94
搞砸了所有这些异步多线程的东西,我太愚蠢了。
首先,不要像这样描述场景......
When the user queries the enable state of relay <relay_id>
Then the CLI should query the web service about the enable state of relay <relay_id>
When the communications service receives a response from TestCube Web Service:
"""
{"module":"relays","path":"relays[<relay_id>].enabled","data":[<relay_enabled>]}
"""
Then the CLI should print "<relay_enabled>"
我们这样描述:
Given the communications service will respond to requests:
"""
{"module":"relays","path":"relays[<relay_id>].enabled","data":[<relay_enabled>]}
"""
When the user queries the enable state of relay <relay_id>
Then the CLI should query the web service about the enable state of relay <relay_id>
And the CLI should print "<relay_enabled>"
实施新的给定步骤:
@given('the communications service will respond to requests')
def step_impl(context):
response = context.text
def publish_mock_response(_):
loop = context.test_loop
loop.call_soon_threadsafe(context.mock_received_response_subject.on_next, response)
# Configure the mock comms service to publish a mock response when a request is made.
instance = context.mock_comms.return_value
instance.send_request.on_next.side_effect = publish_mock_response
BOOM
2 features passed, 0 failed, 0 skipped
22 scenarios passed, 0 failed, 0 skipped
58 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.111s
答案 0 :(得分:4)
我可以看到您的代码存在两个问题:
RxPy
没有使用Observable.to_future中的任何一个,因此您必须在运行asyncio事件循环的同一个线程中访问RxPy
个对象。RxPy
设置调用on_completed
时的未来结果,以便等待observable返回最后发出的对象。这意味着您必须同时致电on_next
和on_completed
才能让await
返回。这是一个有效的例子:
import click
import asyncio
from rx.subjects import Subject
from click.testing import CliRunner
web_response_subject = Subject()
web_response_observable = web_response_subject.as_observable()
main_loop = asyncio.get_event_loop()
@click.group()
def cli():
pass
@cli.resultcallback()
def result_handler(task, **_):
future = asyncio.run_coroutine_threadsafe(task, main_loop)
print(future.result())
@cli.command()
async def get_web_response():
return await web_response_observable
def test():
runner = CliRunner()
future = main_loop.run_in_executor(
None, runner.invoke, cli, ['get_web_response'])
main_loop.call_later(1, web_response_subject.on_next, 'foo')
main_loop.call_later(2, web_response_subject.on_completed)
result = main_loop.run_until_complete(future)
print(result.output, end='')
if __name__ == '__main__':
test()