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

Assert that there is no message to receive using ApplicationCommunicator #32

Closed
dgilge opened this issue Feb 25, 2018 · 5 comments
Closed

Comments

@dgilge
Copy link
Contributor

dgilge commented Feb 25, 2018

What do you think about having a method to assert that there is no message to receive using the ApplicationCommunicator?

Right now I'm doing it like this (using Channels' WebsocketCommunicator):

    with pytest.raises(futures.TimeoutError):
        await communicator.receive_from()

I'd like to have something like

    assert communicator.new_messages(timeout=1) == 0
    # or maybe
    assert communicator.received_nothing(timeout=1)

Actually the timeout should be 0 by default. Otherwise the tests might take quite long if you have a lot of these checks.

@andrewgodwin
Copy link
Member

Yes, we should probably have this - I won't be able to add it myself for a while though as I've got bugs to fix elsewhere. Feel free to submit a PR if you want.

@dgilge
Copy link
Contributor Author

dgilge commented Mar 7, 2018

Ok, fine. What about the following? (I'm not sure about the sleep times yet. And I'd actually prefer to implement queue_empty simpler...)

    async def queue_empty(self, timeout=0.1):
        if not self.output_queue.empty():
            return False
        if timeout <= 0.01:
            await asyncio.sleep(timeout)
            return self.output_queue.empty()
        await asyncio.sleep(0.01)
        if not self.output_queue.empty():
            return False
        await asyncio.sleep(timeout - 0.01)
        return self.output_queue.empty()

    async def queue_count(self, wait=0.1):
        await asyncio.sleep(wait)
        return self.output_queue.qsize()

@andrewgodwin
Copy link
Member

I'd just have one top-level function called communicator.receive_nothing that raises an (assertion?) error if it does receive something, that just wraps receive_from and catches the TimeoutError.

@dgilge
Copy link
Contributor Author

dgilge commented Mar 7, 2018

I also thought about doing it that way. But as far as I understand the communicator is closed after this exception which is not what I want in some use cases...

@andrewgodwin
Copy link
Member

andrewgodwin commented Mar 8, 2018

Ah yes, the timeout will cancel it. I still think calling it receive_nothing makes sense though, and implement inside that as you see fit (using queue empty detection is fine)

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

No branches or pull requests

2 participants