rstudio / promises

A promise library for R
https://rstudio.github.io/promises
Other
198 stars 19 forks source link

Provide a way to block and wait for a promise (for testing) #48

Open antoine-sachet opened 5 years ago

antoine-sachet commented 5 years ago

In some context, it would be useful to be able to block and wait for a promise to be resolved or rejected.

Typically, I want to test with testthat some code that acts on futures or promises, so I want to synchronously check that the resolved value is correct. The future API offers the value method but it is not implemented by promises.

I see how you solved this with the extract function in tests/testthat/common.R. It would be useful to have access to this blocking extract function from the package, without confusing the end-users who shouldn't use it.

How about as a non-exported function such as promises:::extract_block or promises:::value?

Or maybe it would be neater if testthat was promise-aware, with e.g. expect_promise_resolved and expect_promise_rejected?

jcheng5 commented 4 years ago

Tricky... I'm not sure ::: would be allowed on CRAN. We could make it :: but not include it in the package index? 😐

For sure though, it's super useful for testing. For now I'd encourage anyone who needs this to just copy our code.

wch commented 4 years ago

I believe ::: is allowed in tests.

jcheng5 commented 4 years ago

Oooh, that's perfect then.

jcheng5 commented 4 years ago

If we do this, wait_for_it should lose the Sys.sleep(0.1) and instead pass a timeout to run_now, I think.

cderv commented 4 years ago

For testing in crrri with promises, we used a hold() function that wait for promises or reject after a timeout (using a timeout() function.)

It is one approach that helps us cover with tests all the code using promises.

I just mention this here if you have feedback on doing things better or if it could help others for testing with promises.