Skip to content

Commit

Permalink
fix(tests): fix issue with static reqwest clients
Browse files Browse the repository at this point in the history
  • Loading branch information
Threated committed Oct 24, 2024
1 parent 9f3a4d2 commit 87b7ae5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
8 changes: 6 additions & 2 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ pub const APP_KEY: &str = match option_env!("APP_KEY") {
None => "App1Secret"
};

pub static CLIENT1: Lazy<BeamClient> = Lazy::new(|| BeamClient::new(&APP1, APP_KEY, PROXY1.parse().unwrap()));
pub static CLIENT2: Lazy<BeamClient> = Lazy::new(|| BeamClient::new(&APP2, APP_KEY, PROXY2.parse().unwrap()));
pub fn client1() -> BeamClient {
BeamClient::new(&APP1, APP_KEY, PROXY1.parse().unwrap())
}
pub fn client2() -> BeamClient {
BeamClient::new(&APP2, APP_KEY, PROXY2.parse().unwrap())
}

#[tokio::test]
async fn test_time_out() {
Expand Down
8 changes: 4 additions & 4 deletions tests/src/socket_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ async fn test_full() -> Result<()> {
"id": id
});
let app1 = async {
CLIENT1.create_socket_with_metadata(&APP2, &metadata).await.map_err(anyhow::Error::from)
client1().create_socket_with_metadata(&APP2, &metadata).await.map_err(anyhow::Error::from)
};
let app2 = async {
let task = CLIENT2
.get_socket_tasks(&BlockingOptions::from_time(Duration::from_secs(1)))
let task = client2()
.get_socket_tasks(&beam_lib::BlockingOptions::from_count(1))
.await?
.into_iter()
.find(|t| t.metadata["id"].as_str() == Some(&id_str))
.ok_or(anyhow::anyhow!("Failed to get a socket task"))?;
assert_eq!(&task.metadata, &metadata);
Ok(CLIENT2.connect_socket(&task.id).await?)
Ok(client2().connect_socket(&task.id).await?)
};

let (app1, app2) = tokio::try_join!(app1, app2)?;
Expand Down
10 changes: 5 additions & 5 deletions tests/src/task_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::{de::DeserializeOwned, Serialize};
use serde_json::Value;
use tokio::sync::oneshot;

use crate::{CLIENT1, APP1, APP2, CLIENT2};
use crate::{client1, APP1, APP2, client2};

#[tokio::test]
async fn test_full_task_cycle() -> Result<()> {
Expand Down Expand Up @@ -52,7 +52,7 @@ async fn test_task_claiming() -> Result<()> {

pub async fn post_task<T: Serialize + 'static>(body: T) -> Result<MsgId> {
let id = MsgId::new();
CLIENT1.post_task(&TaskRequest {
client1().post_task(&TaskRequest {
id,
from: APP1.clone(),
to: vec![APP2.clone()],
Expand All @@ -65,7 +65,7 @@ pub async fn post_task<T: Serialize + 'static>(body: T) -> Result<MsgId> {
}

pub async fn poll_task<T: DeserializeOwned + 'static>(expected_id: MsgId) -> Result<TaskRequest<T>> {
CLIENT2.poll_pending_tasks::<Value>(&BlockingOptions::from_time(Duration::from_secs(1)))
client2().poll_pending_tasks::<Value>(&BlockingOptions::from_time(Duration::from_secs(1)))
.await?
.into_iter()
.find(|t| t.id == expected_id)
Expand All @@ -77,14 +77,14 @@ pub async fn poll_task<T: DeserializeOwned + 'static>(expected_id: MsgId) -> Res
}

pub async fn poll_result<T: DeserializeOwned + 'static>(task_id: MsgId, block: &BlockingOptions) -> Result<TaskResult<T>> {
CLIENT1.poll_results(&task_id, block)
client1().poll_results(&task_id, block)
.await?
.pop()
.ok_or(anyhow::anyhow!("Got no task"))
}

pub async fn put_result<T: Serialize + 'static>(task_id: MsgId, body: T, status: Option<beam_lib::WorkStatus>) -> Result<()> {
CLIENT2.put_result(&TaskResult {
client2().put_result(&TaskResult {
from: APP2.clone(),
to: vec![APP1.clone()],
task: task_id,
Expand Down
4 changes: 2 additions & 2 deletions tests/src/test_sse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use beam_lib::TaskResult;
use futures::{StreamExt, TryStreamExt};
use reqwest::{header::{self, HeaderValue}, Method};

use crate::{CLIENT1, task_test};
use crate::{client1, task_test};

#[tokio::test]
async fn test_sse() -> Result<()> {
let id = task_test::post_task("test").await?;
let res = CLIENT1
let res = client1()
.raw_beam_request(
Method::GET,
&format!("v1/tasks/{id}/results?wait_count=1"),
Expand Down

0 comments on commit 87b7ae5

Please sign in to comment.