diff --git a/tests-pl/issue-http_open-hanging.pl b/tests-pl/issue-http_open-hanging.pl index b26b5d06..360f3b68 100644 --- a/tests-pl/issue-http_open-hanging.pl +++ b/tests-pl/issue-http_open-hanging.pl @@ -1,5 +1,26 @@ -:- module(http_open_hanging, [submit_request/0]). +% Server +:- use_module(library(process)). +:- use_module(library(iso_ext)). +:- use_module(library(os)). +prolog_path(Prolog) :- + read(Body), + term_variables(Body, [Prolog]), + Body. + +server_start([Process,Out]) :- + prolog_path(Prolog), + process_create(Prolog, + ["tests-pl/issue-http_open-hanging_server", "-t", "server"], + [process(Process), stdout(pipe(Out))]). + +server_wait_start([_Process, Out]) :- + get_char(Out, _C). + +server_stop([Process,_Out]) :- + process_kill(Process). + +% Client :- use_module(library(charsio)). :- use_module(library(http/http_open)). @@ -10,14 +31,21 @@ send_request :- request_headers([]), headers(_) ], - http_open("https://scryer.pl", _Stream, Options), + http_open("http://localhost:8472", _Stream, Options), write_term('received response with status code':StatusCode, []), nl. main :- - send_request, - send_request, - send_request, - send_request, - send_request. + setup_call_cleanup( + server_start(Server), + ( + server_wait_start(Server), + send_request, + send_request, + send_request, + send_request, + send_request + ), + server_stop(Server) + ). :- initialization(main). \ No newline at end of file diff --git a/tests-pl/issue-http_open-hanging_server.pl b/tests-pl/issue-http_open-hanging_server.pl new file mode 100644 index 00000000..826c2627 --- /dev/null +++ b/tests-pl/issue-http_open-hanging_server.pl @@ -0,0 +1,8 @@ +:- use_module(library(process)). +:- use_module(library(http/http_server)). + +server :- + http_listen(8472, [get(/, hello)]). + +hello(_Req, Res) :- + http_body(Res, text("Ok")). \ No newline at end of file diff --git a/tests/scryer/helper.rs b/tests/scryer/helper.rs index 910c0258..91fe9880 100644 --- a/tests/scryer/helper.rs +++ b/tests/scryer/helper.rs @@ -52,6 +52,27 @@ pub(crate) fn load_module_test_with_tokio_runtime(file: &str, exp }); } +#[cfg(not(target_arch = "wasm32"))] +pub(crate) fn load_module_test_with_tokio_runtime_and_input( + file: &str, + input: impl Into>, + expected: T, +) { + let runtime = tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .unwrap(); + + runtime.block_on(async move { + let mut wam = MachineBuilder::default() + .with_streams( + StreamConfig::in_memory().with_user_input(InputStreamConfig::string(input)), + ) + .build(); + expected.assert_eq(wam.test_load_file(file).as_slice()) + }); +} + pub(crate) fn load_module_test_with_input( file: &str, input: impl Into>, diff --git a/tests/scryer/issues.rs b/tests/scryer/issues.rs index ddecb468..838f4fa4 100644 --- a/tests/scryer/issues.rs +++ b/tests/scryer/issues.rs @@ -1,7 +1,7 @@ use crate::helper::load_module_test; use crate::helper::load_module_test_with_input; #[cfg(not(target_arch = "wasm32"))] -use crate::helper::load_module_test_with_tokio_runtime; +use crate::helper::load_module_test_with_tokio_runtime_and_input; use serial_test::serial; // issue #831 @@ -167,10 +167,10 @@ fn issue3262_read_from_stdin_no_newline() { #[cfg(feature = "http")] #[cfg(not(target_arch = "wasm32"))] #[cfg_attr(miri, ignore = "it takes too long to run")] -#[cfg_attr(not(miri), ignore = "flaky due to network requests")] fn http_open_hanging() { - load_module_test_with_tokio_runtime( + load_module_test_with_tokio_runtime_and_input( "tests-pl/issue-http_open-hanging.pl", + format!("PROLOG={:?}.", env!("CARGO_BIN_EXE_scryer-prolog")), "received response with status code:200\nreceived response with status code:200\nreceived response with status code:200\nreceived response with status code:200\nreceived response with status code:200\n" ); }