ropensci / vcr

Record and replay HTTP requests
https://docs.ropensci.org/vcr
Other
77 stars 12 forks source link

Context-dependent fixture recording? #269

Open sckott opened 3 months ago

sckott commented 3 months ago

So in https://github.com/getwilds/rcromwell/issues/44 a use case has come up that it'd be nice to have context-dependent usage of cassettes.

Consider the following use case where jobs are submitted to some engine that makes widgets. However, it takes some time before the metadata on jobs is available after submission. This is not a great situation for tests.

vcr::use_cassette("job_metadata_prep", {
  res <- submit_job(some_input = tempfile())
})

# Needed only for recording new fixture
Sys.sleep(10)

vcr::use_cassette("job_metadata", {
  res <- job_metadata(res$id)
})

expect_s3_class(res, "tbl")

Right now when I want to re-record the cassette (or on the very first recording I did) I have to make sure the Sys.sleep is not commented out because the submit_job call kicks off some process that leads to some wait time before appropriate data will return from job_metadata. (of course you may want to test these different behaviors, but lets's assume we want to test the behavior of job_metadata when the metadata is present.

Consider the possibility of

add_sleep <- function(sleep = 10) Sys.sleep(sleep)

vcr::use_cassette("job_metadata_prep", {
    res <- submit_job(some_input = tempfile())
  }, 
  on_new_cassette = list(before = NULL, after = add_sleep),
  if_vcr_off = list(before = NULL, after = add_sleep)
)

vcr::use_cassette("job_metadata", {
  res <- job_metadata(res$id)
})

expect_s3_class(res, "tbl")

Where

I think this takes care of the 3 scenarios in the linked issue - making it so that tests could be run without any manual toggling on/off of sleeps

sckott commented 3 months ago

What are the definitions of before and after? Right now they are (based on work on my local machiune):