Skip to content

Commit

Permalink
Use NATS_URL environment variable to determine NATS addr
Browse files Browse the repository at this point in the history
So that we can run GitHub Action tests with service
container and connect to it. By default service containers
do not share the host network

Signed-off-by: Pavel Abramov <[email protected]>
  • Loading branch information
uncleDecart committed Aug 12, 2024
1 parent 9881876 commit 6f66b07
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ jobs:

runs-on: ubuntu-latest

services:
mongodb:
image: nats

steps:
- uses: actions/checkout@v4
- name: Build
run: cargo build --verbose
env:
NATS_URL: "nats:4222"
- name: Run tests
run: cargo test --verbose
NATS_URL: "nats:4222"
5 changes: 4 additions & 1 deletion src/nkv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::sync::Arc;
use crate::persist_value::PersistValue;
use crate::notifier::Notifier;
use std::fmt;
use std::env;

#[derive(Debug, PartialEq)]
pub enum NotifyKeyValueError {
Expand Down Expand Up @@ -47,10 +48,12 @@ pub struct NotifyKeyValue {

impl NotifyKeyValue {
pub fn new(path: std::path::PathBuf) -> Self {
let nats_url = env::var("NATS_URL")
.unwrap_or_else(|_| "nats://localhost:4222".to_string());
Self{
state: HashMap::new(),
persist_path: path,
sock_path: "nats://localhost:4222".to_string(),
sock_path: nats_url,
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/notifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ extern crate serde;
extern crate serde_json;

use serde::{Deserialize, Serialize};
use std::env;

#[derive(Serialize, Deserialize, Debug, PartialEq)]
enum MessageType<T>
Expand Down Expand Up @@ -94,7 +95,10 @@ mod tests {

// Helper function to create a Notifier instance for testing
async fn create_test_notifier() -> Notifier {
let notifier = Notifier::new("nats://localhost:4222".into(), "topic".into()).await.unwrap();
let nats_url = env::var("NATS_URL")
.unwrap_or_else(|_| "nats://localhost:4222".to_string());

let notifier = Notifier::new(nats_url, "topic".into()).await.unwrap();
notifier
}

Expand Down
12 changes: 9 additions & 3 deletions src/srv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::nkv::{self, NotifyKeyValue};
use crate::request_msg::{self, BaseResp, GetResp, PutResp, ServerResponse};
use http::StatusCode;
use futures::StreamExt;
use std::env;

pub struct PutMsg {
key: String,
Expand Down Expand Up @@ -254,7 +255,10 @@ mod tests {
#[tokio::test]
async fn test_server() {
let temp_dir = TempDir::new().expect("Failed to create temporary directory");
let srv = Server::new("localhost:4222".to_string(), temp_dir.path().to_path_buf()).await.unwrap();
let nats_url = env::var("NATS_URL")
.unwrap_or_else(|_| "nats://localhost:4222".to_string());

let srv = Server::new(nats_url.to_string(), temp_dir.path().to_path_buf()).await.unwrap();

let put_tx = srv.put_tx();
let get_tx = srv.get_tx();
Expand Down Expand Up @@ -289,9 +293,11 @@ mod tests {
let temp_dir = TempDir::new().expect("Failed to create temporary directory");

// creates background task where it serves threads
let _srv = Server::new("localhost:4222".to_string(), temp_dir.path().to_path_buf()).await.unwrap();
let nats_url = env::var("NATS_URL")
.unwrap_or_else(|_| "nats://localhost:4222".to_string());

let _srv = Server::new(nats_url.clone(), temp_dir.path().to_path_buf()).await.unwrap();

let nats_url = "localhost:4222".to_string();
let client = NatsClient::new(&nats_url).await.unwrap();

let value: Box<[u8]> = Box::new([1, 2, 3, 4, 5]);
Expand Down

0 comments on commit 6f66b07

Please sign in to comment.