Skip to content

Commit

Permalink
feat(smol): add uds connector
Browse files Browse the repository at this point in the history
  • Loading branch information
jbr committed Apr 19, 2024
1 parent 75ce859 commit 52acd41
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions smol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ signal-hook-async-std = "0.2.2"

[dev-dependencies]
env_logger = "0.11.0"
tempfile = "3.10.1"
test-harness = "0.2.0"
trillium-client = { path = "../client" }
trillium-logger = { path = "../logger" }
trillium-testing = { path = "../testing" }
Expand Down
15 changes: 15 additions & 0 deletions smol/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,18 @@ impl Connector for ClientConfig {
Ok(tcp)
}
}

#[cfg(unix)]
impl Connector for SmolTransport<async_net::unix::UnixStream> {
type Transport = Self;

type Runtime = SmolRuntime;

async fn connect(&self, _url: &Url) -> Result<Self::Transport> {
Ok(self.clone())
}

fn runtime(&self) -> Self::Runtime {
SmolRuntime::default()
}
}
7 changes: 7 additions & 0 deletions smol/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,10 @@ impl Transport for SmolTransport<TcpStream> {

#[cfg(unix)]
impl Transport for SmolTransport<async_net::unix::UnixStream> {}
#[cfg(unix)]
impl SmolTransport<async_net::unix::UnixStream> {
/// connnect to the provided path as a unix domain socket
pub async fn connect_unix(path: impl AsRef<std::path::Path>) -> Result<Self> {
async_net::unix::UnixStream::connect(path).await.map(Self)
}
}
21 changes: 21 additions & 0 deletions smol/tests/unix_stream.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#![cfg(unix)]
use test_harness::test;
use trillium_client::Client;
use trillium_smol::{config, SmolTransport};
use trillium_testing::harness;

#[test(harness)]
async fn smoke() {
let temp_dir = tempfile::tempdir().unwrap();
let path = temp_dir.path().join("socket");

let handle = config().with_host(path.to_str().unwrap()).spawn("ok");
handle.info().await;

let stream = SmolTransport::connect_unix(path).await.unwrap();
let mut conn = Client::new(stream).get("http://localhost/").await.unwrap();

assert_eq!(conn.status().unwrap(), 200);
assert_eq!(conn.response_body().read_string().await.unwrap(), "ok");
handle.shut_down().await;
}

0 comments on commit 52acd41

Please sign in to comment.