Update to have context use threads

pull/38/head
Chip Senkbeil 3 years ago
parent 188b5f74e4
commit aa2925d33a
No known key found for this signature in database
GPG Key ID: 35EF1F8EC72A4131

@ -7,14 +7,12 @@ use distant_core::{
use rstest::*;
#[rstest]
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn should_print_out_file_contents(#[future] ctx: DistantServerCtx) {
fn should_print_out_file_contents(ctx: DistantServerCtx) {
let temp = assert_fs::TempDir::new().unwrap();
let file = temp.child("test-file");
file.write_str("some\ntext\ncontent").unwrap();
ctx.await
.new_cmd("action")
ctx.new_cmd("action")
.args(&["file-read", file.to_str().unwrap()])
.assert()
.success()
@ -23,14 +21,12 @@ async fn should_print_out_file_contents(#[future] ctx: DistantServerCtx) {
}
#[rstest]
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn should_support_json_output(#[future] ctx: DistantServerCtx) {
fn should_support_json_output(ctx: DistantServerCtx) {
let temp = assert_fs::TempDir::new().unwrap();
let file = temp.child("test-file");
file.write_str("some\ntext\ncontent").unwrap();
let cmd = ctx
.await
.new_cmd("action")
.args(&["--format", "json"])
.args(&["file-read", file.to_str().unwrap()])
@ -48,13 +44,11 @@ async fn should_support_json_output(#[future] ctx: DistantServerCtx) {
}
#[rstest]
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn yield_an_error_when_fails(#[future] ctx: DistantServerCtx) {
fn yield_an_error_when_fails(ctx: DistantServerCtx) {
let temp = assert_fs::TempDir::new().unwrap();
let file = temp.child("missing-file");
let cmd = ctx
.await
.new_cmd("action")
.args(&["--format", "json"])
.args(&["file-read", file.to_str().unwrap()])

@ -1,7 +1,8 @@
use assert_cmd::Command;
use distant_core::*;
use rstest::*;
use std::{ffi::OsStr, net::SocketAddr, time::Duration};
use std::{ffi::OsStr, net::SocketAddr, thread, time::Duration};
use tokio::{runtime::Runtime, sync::mpsc};
/// Timeout to wait for a command to complete
const TIMEOUT_SECS: u64 = 10;
@ -10,7 +11,7 @@ const TIMEOUT_SECS: u64 = 10;
pub struct DistantServerCtx {
pub addr: SocketAddr,
pub auth_key: String,
pub server: DistantServer,
done_tx: mpsc::Sender<()>,
}
impl DistantServerCtx {
@ -33,24 +34,60 @@ impl DistantServerCtx {
.timeout(Duration::from_secs(TIMEOUT_SECS));
cmd
}
/// Attempts to shutdown the server if it is not already dead
pub fn shutdown(&self) {
let _ = self.done_tx.send(());
}
}
impl Drop for DistantServerCtx {
/// Kills server upon drop
fn drop(&mut self) {
self.server.abort();
self.shutdown();
}
}
#[fixture]
pub async fn ctx() -> DistantServerCtx {
pub fn ctx() -> DistantServerCtx {
let ip_addr = "127.0.0.1".parse().unwrap();
let server = DistantServer::bind(ip_addr, "0".parse().unwrap(), None, 100)
.await
.unwrap();
let (done_tx, mut done_rx) = mpsc::channel(1);
let (started_tx, mut started_rx) = mpsc::channel(1);
// NOTE: We spawn a dedicated thread that runs our tokio runtime separately
// from our test itself because using assert_cmd blocks the thread
// and prevents our runtime from working unless we make the tokio
// test multi-threaded using `tokio::test(flavor = "multi_thread", worker_threads = 1)`
// which isn't great because we're only using async tests for our
// server itself; so, we hide that away since our test logic doesn't need to be async
thread::spawn(move || match Runtime::new() {
Ok(rt) => {
println!("Starting...");
rt.block_on(async move {
println!("Async...");
let server = DistantServer::bind(ip_addr, "0".parse().unwrap(), None, 100)
.await
.unwrap();
started_tx
.send(Ok((server.port(), server.to_unprotected_hex_auth_key())))
.await
.unwrap();
let _ = done_rx.recv().await;
});
}
Err(x) => {
started_tx.blocking_send(Err(x)).unwrap();
}
});
// Extract our server startup data if we succeeded
let (port, auth_key) = started_rx.blocking_recv().unwrap().unwrap();
DistantServerCtx {
addr: SocketAddr::new(ip_addr, server.port()),
auth_key: server.to_unprotected_hex_auth_key(),
server,
addr: SocketAddr::new(ip_addr, port),
auth_key,
done_tx,
}
}

Loading…
Cancel
Save