diff --git a/tests/async/_snaps/http-sse.md b/tests/async/_snaps/http-sse.md new file mode 100644 index 00000000..91dc73cb --- /dev/null +++ b/tests/async/_snaps/http-sse.md @@ -0,0 +1,127 @@ +# parse_sse_event + + Code + parse_sse_event(charToRaw(txt)) + Output + foo + "bar" + +--- + + Code + parse_sse_event(charToRaw(txt)) + Output + foo + "bar" + +--- + + Code + parse_sse_event(charToRaw(txt)) + Output + foo baz and + "bar" "foobar" "last" + +# chunk_sse_events + + Code + chunk_sse_events(charToRaw(txt)) + Output + $events + list() + + $rest + [1] 66 6f 6f 3a 20 62 61 72 0a 62 61 7a 3a 20 66 6f 6f 0a + + +--- + + Code + chunk_sse_events(charToRaw(txt)) + Output + $events + $events[[1]] + foo baz + "bar" "foobar" + + $events[[2]] + another + "event" + + $events[[3]] + and + "another" + + + $rest + raw(0) + + +--- + + Code + chunk_sse_events(charToRaw(txt)) + Output + $events + $events[[1]] + foo baz + "bar" "foobar" + + $events[[2]] + another + "event" + + $events[[3]] + and + "another" + + + $rest + raw(0) + + +--- + + Code + chunk_sse_events(charToRaw(txt)) + Output + $events + $events[[1]] + foo baz + "bar" "foobar" + + $events[[2]] + another + "event" + + + $rest + [1] 61 6e 64 3a 61 6e 6f 74 68 65 72 0a + + +# sse + + Code + events + Output + [[1]] + event message + "1" "live long and prosper" + + [[2]] + event message + "2" "live long and prosper" + + [[3]] + event message + "3" "live long and prosper" + + [[4]] + event message + "4" "live long and prosper" + + [[5]] + event message + "5" "live long and prosper" + + diff --git a/tests/async/test-http-events.R b/tests/async/test-http-events.R new file mode 100644 index 00000000..5d5c6eaf --- /dev/null +++ b/tests/async/test-http-events.R @@ -0,0 +1,55 @@ +test_that("end event", { + done <- NULL + do <- function() { + p1 <- http_get(http$url("/get")) + p2 <- p1$then(function() done <<- c(done, "done")) + p1$event_emitter$listen_on("end", function(...) { + done <<- c(done, "end") + }) + p2 + } + + synchronise(do()) + expect_equal(done, c("end", "done")) +}) + +test_that("data event", { + skip_on_cran() + chunks <- NULL + called <- 0L + do <- function() { + p1 <- http_get(http$url("/drip?duration=1&numbytes=10")) + p1$event_emitter$listen_on("data", function(bytes) { + chunks <<- c(chunks, list(bytes)) + }) + p1$event_emitter$listen_on("data", function(bytes) { + called <<- called + 1L + }) + p1 + } + + # there is an extra zero-length chunk currently, but let's not + # rely on that + synchronise(do()) + expect_true(length(chunks) >= 10) + expect_equal(length(unlist(chunks)), 10) + expect_true(called >= 10) +}) + +test_that("multiple end events", { + done <- NULL + do <- function() { + p1 <- http_get(http$url("/get")) + p2 <- p1$then(function() done <<- c(done, "done")) + p1$event_emitter$listen_on("end", function(...) { + done <<- c(done, "end1") + }) + p1$event_emitter$listen_on("end", function(...) { + done <<- c(done, "end2") + }) + p2 + } + + synchronise(do()) + expect_equal(done, c("end1", "end2", "done")) +}) diff --git a/tests/async/test-http-sse.R b/tests/async/test-http-sse.R new file mode 100644 index 00000000..b2c73713 --- /dev/null +++ b/tests/async/test-http-sse.R @@ -0,0 +1,59 @@ +test_that("parse_sse_event", { + testthat::local_edition(3) + + txt <- "foo: bar" + expect_snapshot(parse_sse_event(charToRaw(txt))) + + txt <- "foo:bar" + expect_snapshot(parse_sse_event(charToRaw(txt))) + + txt <- "foo: bar\nbaz:foobar\nand:last" + expect_snapshot(parse_sse_event(charToRaw(txt))) +}) + +test_that("chunk_sse_events", { + testthat::local_edition(3) + + # no events yet + txt <- "foo: bar\nbaz: foo\n" + expect_snapshot(chunk_sse_events(charToRaw(txt))) + + txt <- "foo: bar\nbaz: foobar\n\nanother: event\n\nand:another\n\n" + expect_snapshot(chunk_sse_events(charToRaw(txt))) + + # slightly bad separators + txt <- paste0( + "\n\n\n", + "foo: bar\nbaz: foobar", + "\n\n\n\n\n", + "another: event", + "\n\n", + "and:another", + "\n\n" + ) + expect_snapshot(chunk_sse_events(charToRaw(txt))) + + # incomplete last event + txt <- "foo: bar\nbaz: foobar\n\nanother: event\n\nand:another\n" + expect_snapshot(chunk_sse_events(charToRaw(txt))) + +}) + +test_that("sse", { + testthat::local_edition(3) + server <- webfakes::new_app_process(sseapp()) + url <- server$url("/sse") + + events <- NULL + do <- function() { + p1 <- http_get(url) + evs <- sse_events$new(p1) + evs$listen_on("event", function(ev) { + events <<- c(events, list(ev)) + }) + p1 + } + + synchronise(do()) + expect_snapshot(events) +})