Skip to content

Add support for service blocking #85

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

Merged
merged 5 commits into from
Nov 6, 2020

Conversation

raman325
Copy link
Contributor

@raman325 raman325 commented Nov 6, 2020

I have created several functions in pyscript that make calls to other services. I could use task.wait_until to block execution until the service is finished running, but it seems more convenient to be able to specify that in the service call directly. As such, I have added support for the blocking and limit parameters which will give users more control over the service call behavior.

I am unsure how to test this - I'd be happy to add tests if you have some ideas on the best way to do that.

@raman325
Copy link
Contributor Author

raman325 commented Nov 6, 2020

I figured out how to test at least the changes in pyscript.function. Please hold off on merging while I add tests.

@craigbarratt
Copy link
Member

This looks good - I don't have any suggested changes.

For testing, it would be great if you could add some. These are quite tricky though because you can't actually wait any noticeable amount of time, and there are race conditions. Here's what I'd recommend.

For blocking, the test could use these steps:

  • copy the initialization where we handshake the hass started event to make sure everything is up and running
  • in the pyscript startup function:
    • set pyscript.var1 = 1
    • make a service call to another pyscript function with blocking=True
    • that second pyscript function sets pyscript.var1 = 2
    • immediately after the service call returns, send pyscript.var1 back through the pyscript.done queue
    • the test code asserts that pyscript.var1 = 2
    • repeat the test for other type of service call (eg: both service.call() and pyscript.func())

I don't see a good way to test blocking=False since there are race conditions.

For limit, do a test similar to the above, but the second function just does a long task.delay() and the caller uses limit = 1e-6 and blocking=True. Just the fact the service call returns means limit is working.

@raman325
Copy link
Contributor Author

raman325 commented Nov 6, 2020

Let me know what you think of the tests I added. We don't actually have to test that the function blocks since that is Home Assistant's job, we just need to make sure that the appropriate params get passed to async_call

@craigbarratt
Copy link
Member

Do you think this is ready now? I'm happy to try adding the couple of tests I proposed. Your tests look good too.

@raman325
Copy link
Contributor Author

raman325 commented Nov 6, 2020

Yes I feel good about this PR now, but if you would like to add some tests that actually test the blocking please do. I think I understand what you are proposing but you would probably be able to add the tests faster

@craigbarratt craigbarratt merged commit 713f949 into custom-components:master Nov 6, 2020
@craigbarratt
Copy link
Member

Great - thanks. Merged.

@raman325 raman325 deleted the service_blocking branch November 6, 2020 23:24
@craigbarratt
Copy link
Member

I added some additional tests in a25063d.

@raman325
Copy link
Contributor Author

raman325 commented Nov 7, 2020

Great, thank you!

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 this pull request may close these issues.

2 participants