Skip to content
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

Make put_mock an AsyncMock #539

Closed
DominicOram opened this issue Aug 22, 2024 · 4 comments · Fixed by #559
Closed

Make put_mock an AsyncMock #539

DominicOram opened this issue Aug 22, 2024 · 4 comments · Fixed by #559
Milestone

Comments

@DominicOram
Copy link
Contributor

As a developer I was expecting to be able to do the following:

async def my_async_function():
    await ...

callback_on_mock_put(my_mocked_signal, my_mocked_function)

However, because put_mock is a Mock not an AsyncMock I cannot. Instead I have had to go around the mock and do:

@AsyncStatus.wrap
async def my_async_function(value: float, wait=None, timeout=None):
    await ...

my_mocked_signal.set = my_async_function
@coretl coretl added this to the 1.0 milestone Aug 23, 2024
@noemifrisina
Copy link
Contributor

Adding an example from the PMAC device which has this issue:

@AsyncStatus.wrap
async def set(self, value: int, wait=True, timeout=None):
    prog_str = f"&2b{value}r"
    assert isinstance(timeout, SupportsFloat) or (
        timeout is None
    ), f"ProgramRunner does not support calculating timeout itself, {timeout}"
    await self.signal.set(prog_str, wait=wait)
    # First wait for signal to go to 1, then wait for the scan to finish.
    await wait_for_value(
        self.status,
        ScanState.RUNNING,
        timeout=DEFAULT_TIMEOUT,
    )
    await wait_for_value(self.status, ScanState.DONE, timeout)

In the test, callback_on_mock_put won't take an async function so the workaround is:

async def go_high_then_low():
    set_mock_value(fake_pmac.scanstatus, 1)
    await asyncio.sleep(0.01)
    set_mock_value(fake_pmac.scanstatus, 0)

prog_num = 10
callback_on_mock_put(
    fake_pmac.pmac_string,
    lambda *args, **kwargs: asyncio.create_task(go_high_then_low()),  # type: ignore
)

@coretl
Copy link
Collaborator

coretl commented Sep 5, 2024

Would someone from MX like to work on this? Or should we put it on our backlog?

@DominicOram
Copy link
Contributor Author

If you're happy for us to do it we can, I think it's like a one line change.

@coretl
Copy link
Collaborator

coretl commented Sep 5, 2024

Yes please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants