Skip to content

Commit

Permalink
fix: remove bad async, use random port in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KFearsoff committed Aug 15, 2023
1 parent b66e1b8 commit 96cd51d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
10 changes: 4 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ use handlers::{ping_handler, webhook_handler};
use opentelemetry::KeyValue;
use opentelemetry_sdk::{trace, Resource};
use secrecy::SecretString;
use std::fs::read_to_string;
use tap::Tap;
use tokio::fs::read_to_string;
use tokio::signal;
use tower_http::trace::TraceLayer;
use tracing::{debug, info, warn};
Expand Down Expand Up @@ -126,20 +126,18 @@ pub fn setup_tracing() -> Result<()> {
}

#[tracing::instrument]
pub async fn setup_app(settings: &tailforward_cfg::Config) -> Result<Router> {
pub fn setup_app(settings: &tailforward_cfg::Config) -> Result<Router> {
let chat_id = settings.chat_id;
let tailscale_secret_path = &settings.tailscale_secret_file;
debug!(?tailscale_secret_path, "Reading Tailscale secret");
let tailscale_secret: SecretString = read_to_string(tailscale_secret_path)
.await?
let tailscale_secret: SecretString = read_to_string(tailscale_secret_path)?
.tap_dbg(|tailscale_secret| debug!(?tailscale_secret))
.into();
info!(?tailscale_secret_path, "Read Tailscale secret");

let telegram_secret_path = &settings.telegram_secret_file;
debug!(?telegram_secret_path, "Reading Telegram secret");
let telegram_secret: SecretString = read_to_string(telegram_secret_path)
.await?
let telegram_secret: SecretString = read_to_string(telegram_secret_path)?
.split('=')
.collect::<Vec<_>>()[1]
.to_string()
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async fn main() -> Result<()> {
let settings = new_config()?.tap(|settings| debug!(?settings, "Read settings"));

let addr = settings.address;
let app = setup_app(&settings).await?;
let app = setup_app(&settings)?;
axum::Server::bind(&addr)
.serve(app.into_make_service())
.with_graceful_shutdown(shutdown_signal())
Expand Down
18 changes: 11 additions & 7 deletions tests/ping.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
use once_cell::sync::Lazy;
use std::net::{SocketAddr, TcpListener};
use tailforward_cfg::Config;

static GLOBAL_CONFIG: Lazy<Config> =
Lazy::new(|| tailforward::config::new_config().expect("Failed to setup config"));
static GLOBAL_CONFIG: Lazy<Config> = Lazy::new(|| Config::default());

#[tokio::test]
async fn ping_works() {
// Arrange
spawn_app(&GLOBAL_CONFIG).await;
let addr = TcpListener::bind("127.0.0.1:0")
.unwrap()
.local_addr()
.unwrap();
spawn_app(&GLOBAL_CONFIG, &addr);
let client = reqwest::Client::new();

// Act
let response = client
.get("http://127.0.0.1:33010/ping")
.get(format!("http://127.0.0.1:{}/ping", addr.port()))
.send()
.await
.expect("Failed to execute request");
Expand All @@ -22,8 +26,8 @@ async fn ping_works() {
assert_eq!(Some(0), response.content_length());
}

async fn spawn_app(config: &'static Config) {
let app = tailforward::setup_app(config).await.unwrap();
let server = axum::Server::bind(&config.address).serve(app.into_make_service());
fn spawn_app(config: &'static Config, address: &SocketAddr) {
let app = tailforward::setup_app(config).unwrap();
let server = axum::Server::bind(address).serve(app.into_make_service());
let _ = tokio::spawn(server);
}

0 comments on commit 96cd51d

Please sign in to comment.