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 Sep 11, 2024
1 parent cec5122 commit 082d2cf
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 @@ -13,22 +13,22 @@ async fn test_full() -> Result<()> {
let stream = stream::iter(range.clone())
.map(|i| Ok::<_, Infallible>(u32::to_be_bytes(i).to_vec()))
.then(|b| async {
tokio::time::sleep(Duration::from_millis(2)).await;
tokio::time::sleep(Duration::from_millis(1)).await;
b
});
let app1 = async move {
let res = tokio::time::timeout(Duration::from_secs(60), CLIENT1.create_socket_with_metadata(&APP2, stream, metadata)).await??;
let res = tokio::time::timeout(Duration::from_secs(60), client1().create_socket_with_metadata(&APP2, stream, metadata)).await??;
assert!(res.status().is_success());
Ok(())
};
let app2 = async {
let task = CLIENT2
let task = client2()
.get_socket_tasks(&beam_lib::BlockingOptions::from_count(1))
.await?
.pop()
.ok_or(anyhow::anyhow!("Failed to get a socket task"))?;
assert_eq!(&task.metadata, metadata);
let s = CLIENT2.connect_socket(&task.id).await?;
let s = client2().connect_socket(&task.id).await?;
let expected = range.map(u32::to_be_bytes).flatten().collect::<Vec<_>>();
let mut buf = Vec::with_capacity(expected.len());
s.for_each(|b| {
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 082d2cf

Please sign in to comment.