dont spawn a runtime in machine; inherit from outside with runtime::handle::Current

This commit is contained in:
Joshua Parkin
2023-08-03 16:22:03 +01:00
parent 3ff02da314
commit 65eb93793c
4 changed files with 24 additions and 19 deletions

View File

@@ -69,8 +69,8 @@ mod tests {
use super::*;
use crate::machine::{QueryMatch, Value, QueryResolution};
#[test]
fn programatic_query() {
#[tokio::test]
async fn programatic_query() {
let mut machine = Machine::new_lib();
machine.load_module_string(
@@ -108,8 +108,8 @@ mod tests {
);
}
#[test]
fn failing_query() {
#[tokio::test]
async fn failing_query() {
let mut machine = Machine::new_lib();
let query = String::from(r#"triple("a",P,"b")."#);
let output = machine.run_query(query);
@@ -119,8 +119,8 @@ mod tests {
);
}
#[test]
fn complex_results() {
#[tokio::test]
async fn complex_results() {
let mut machine = Machine::new_lib();
machine.load_module_string(
"facts",
@@ -172,8 +172,8 @@ mod tests {
}
#[test]
fn consult() {
#[tokio::test]
async fn consult() {
let mut machine = Machine::new_lib();
machine.consult_module_string(

View File

@@ -53,7 +53,6 @@ use std::env;
use std::io::Read;
use std::path::PathBuf;
use std::sync::atomic::AtomicBool;
use tokio::runtime::Runtime;
use self::config::MachineConfig;
use self::parsed_results::*;
@@ -71,7 +70,6 @@ pub struct Machine {
pub(super) user_output: Stream,
pub(super) user_error: Stream,
pub(super) load_contexts: Vec<LoadContext>,
pub(super) runtime: Runtime,
}
#[derive(Debug)]
@@ -452,8 +450,6 @@ impl Machine {
),
};
let runtime = tokio::runtime::Runtime::new().unwrap();
let mut wam = Machine {
machine_st,
indices: IndexStore::new(),
@@ -462,7 +458,6 @@ impl Machine {
user_output,
user_error,
load_contexts: vec![],
runtime,
};
let mut lib_path = current_dir();

View File

@@ -3864,7 +3864,8 @@ impl Machine {
let address_string = address_sink.as_str(); //to_string();
let address: Uri = address_string.parse().unwrap();
let stream = self.runtime.block_on(async {
let runtime = tokio::runtime::Handle::current();
let stream = runtime.block_on(async {
let https = HttpsConnector::new();
let client = Client::builder()
.build::<_, hyper::Body>(https);
@@ -3945,7 +3946,8 @@ impl Machine {
let (tx, rx) = channel(1);
let tx = Arc::new(Mutex::new(tx));
let _guard = self.runtime.enter();
let runtime = tokio::runtime::Handle::current();
let _guard = runtime.enter();
let server = match Server::try_bind(&addr) {
Ok(server) => server,
Err(_) => {
@@ -3953,7 +3955,7 @@ impl Machine {
}
};
self.runtime.spawn(async move {
runtime.spawn(async move {
let make_svc = make_service_fn(move |_conn| {
let tx = tx.clone();
async move { Ok::<_, Infallible>(service_fn(move |req| http::serve_req(req, tx.clone()))) }
@@ -4016,7 +4018,8 @@ impl Machine {
let query_cell = string_as_cstr_cell!(query_atom);
let hyper_req = request.request;
let buf = self.runtime.block_on(async {hyper::body::aggregate(hyper_req).await.unwrap()});
let runtime = tokio::runtime::Handle::current();
let buf = runtime.block_on(async {hyper::body::aggregate(hyper_req).await.unwrap()});
let reader = buf.reader();
let mut stream = Stream::from_http_stream(