这是indexloc提供的服务,不要输入任何密码
Skip to content

test(turborepo-analytics): improve unit test coverage #10651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 83 additions & 1 deletion crates/turborepo-analytics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,9 @@
};
use turborepo_api_client::{analytics::AnalyticsClient, APIAuth};
use turborepo_vercel_api::{AnalyticsEvent, CacheEvent, CacheSource};
use uuid::Uuid;

use crate::start_analytics;
use crate::{add_session_id, start_analytics};

#[derive(Clone)]
struct DummyClient {
Expand Down Expand Up @@ -380,4 +381,85 @@
let payloads = &found[0];
assert_eq!(payloads.len(), 2);
}

#[tokio::test]
async fn test_client_error_handling() {
let (tx, _rx) = mpsc::unbounded_channel();

let client = ErrorClient { tx };

let (analytics_sender, analytics_handle) = start_analytics(
APIAuth {
token: "foo".to_string(),
team_id: Some("bar".to_string()),
team_slug: None,
},
client,
);

// Send an event that will cause the client to error
assert!(
analytics_sender
.send(AnalyticsEvent {
session_id: None,
source: CacheSource::Local,
event: CacheEvent::Hit,
hash: "".to_string(),
duration: 0,
})
.is_ok(),
"sender should swallow client errors"
);

// Wait a bit for the error to be handled
tokio::time::sleep(Duration::from_millis(50)).await;

// Should not panic when closing
analytics_handle.close_with_timeout().await;
}

#[test]
fn test_add_session_id() {
let mut events = vec![
AnalyticsEvent {
session_id: None,
source: CacheSource::Local,
event: CacheEvent::Hit,
hash: "hash1".to_string(),
duration: 100,
},
AnalyticsEvent {
session_id: Some("existing".to_string()),
source: CacheSource::Remote,
event: CacheEvent::Miss,
hash: "hash2".to_string(),
duration: 200,
},
];

let session_id = Uuid::new_v4();
add_session_id(session_id, &mut events);

assert_eq!(events[0].session_id, Some(session_id.to_string()));
assert_eq!(events[1].session_id, Some(session_id.to_string()));
}

#[derive(Clone)]
struct ErrorClient {
tx: mpsc::UnboundedSender<()>,

Check warning on line 449 in crates/turborepo-analytics/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Rust testing on windows

field `tx` is never read

Check warning on line 449 in crates/turborepo-analytics/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Rust testing on ubuntu

field `tx` is never read

Check warning on line 449 in crates/turborepo-analytics/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Rust testing on macos

field `tx` is never read
}

impl AnalyticsClient for ErrorClient {
async fn record_analytics(
&self,
_api_auth: &APIAuth,
_events: Vec<AnalyticsEvent>,
) -> Result<(), turborepo_api_client::Error> {
// Simulate an error using a simple error type
Err(turborepo_api_client::Error::ReadError(std::io::Error::new(
std::io::ErrorKind::Other,
"test error",
)))
}
}
}
Loading