Skip to content

Commit 3cfb0a7

Browse files
committed
tests(all): add preliminary tests
1 parent 1298cd5 commit 3cfb0a7

File tree

9 files changed

+234
-5
lines changed

9 files changed

+234
-5
lines changed

src/backends/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::Result;
99
use std::fmt::Debug;
1010

1111
/// Backend State
12-
#[derive(Clone, Copy, PartialEq)]
12+
#[derive(Debug, Clone, Copy, PartialEq)]
1313
pub enum State {
1414
Uninitialized,
1515
Ready,

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use thiserror::Error;
1111
pub type Result<T> = std::result::Result<T, PyroscopeError>;
1212

1313
/// Error type of Pyroscope
14-
#[derive(Error, Debug)]
14+
#[derive(Error, Debug, PartialEq)]
1515
pub struct PyroscopeError {
1616
pub msg: String,
1717
}

src/timer/sleep.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
use crate::Result;
88

9-
use std::sync::{mpsc::Sender, Arc, Mutex};
9+
use std::sync::{
10+
mpsc::{channel, Receiver, Sender},
11+
Arc, Mutex,
12+
};
1013
use std::time::Duration;
1114
use std::{thread, thread::JoinHandle};
1215

src/utils.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,12 @@ pub fn merge_tags_with_app_name(
3030
}
3131

3232
#[cfg(test)]
33-
mod tests {
33+
mod merge_tags_with_app_name_tests {
3434
use std::collections::HashMap;
3535

3636
use crate::utils::merge_tags_with_app_name;
3737

3838
#[test]
39-
4039
fn merge_tags_with_app_name_with_tags() {
4140
let mut tags = HashMap::new();
4241
tags.insert("env".to_string(), "staging".to_string());
@@ -63,3 +62,18 @@ pub fn check_err<T: Ord + Default>(num: T) -> Result<T> {
6362
}
6463
Ok(num)
6564
}
65+
66+
#[cfg(test)]
67+
mod check_err_tests {
68+
use crate::utils::check_err;
69+
70+
#[test]
71+
fn check_err_success() {
72+
assert_eq!(check_err(1).unwrap(), 1)
73+
}
74+
75+
#[test]
76+
fn check_err_error() {
77+
assert!(check_err(-1).is_err())
78+
}
79+
}

tests/agent.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use pyroscope::pyroscope::PyroscopeConfig;
2+
3+
#[test]
4+
fn test_PyroscopeConfig_new() {
5+
let config = PyroscopeConfig::new("http://localhost:8080", "myapp");
6+
assert_eq!(config.url, "http://localhost:8080");
7+
assert_eq!(config.application_name, "myapp");
8+
assert_eq!(config.sample_rate, 100i32);
9+
assert_eq!(config.tags.len(), 0);
10+
}
11+
12+
#[test]
13+
fn test_PyroscopeConfig_sample_rate() {
14+
let config = PyroscopeConfig::new("http://localhost:8080", "myapp").sample_rate(10);
15+
assert_eq!(config.sample_rate, 10i32);
16+
}
17+
18+
#[test]
19+
fn test_PyroscopeConfig_tags_empty() {
20+
let config = PyroscopeConfig::new("http://localhost:8080", "myapp");
21+
assert_eq!(config.tags.len(), 0);
22+
}
23+
24+
#[test]
25+
fn test_PyroscopeConfig_tags() {
26+
let config = PyroscopeConfig::new("http://localhost:8080", "myapp").tags(&[("tag", "value")]);
27+
assert_eq!(config.tags.len(), 1);
28+
assert_eq!(config.tags.get("tag"), Some(&"value".to_owned()));
29+
}
30+
31+
#[test]
32+
fn test_PyroscopeConfig_tags_multiple() {
33+
let config = PyroscopeConfig::new("http://localhost:8080", "myapp")
34+
.tags(&[("tag1", "value1"), ("tag2", "value2")]);
35+
assert_eq!(config.tags.len(), 2);
36+
assert_eq!(config.tags.get("tag1"), Some(&"value1".to_owned()));
37+
assert_eq!(config.tags.get("tag2"), Some(&"value2".to_owned()));
38+
}

tests/backends.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use pyroscope::backends::{State};
2+
3+
#[test]
4+
fn test_state_default() {
5+
assert_eq!(State::default(), State::Uninitialized);
6+
}

tests/session.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use pyroscope::{
2+
pyroscope::PyroscopeConfig,
3+
session::{Session, SessionManager, SessionSignal},
4+
PyroscopeError,
5+
};
6+
use std::{
7+
collections::HashMap,
8+
sync::mpsc::{sync_channel, Receiver, SyncSender},
9+
thread,
10+
thread::JoinHandle,
11+
};
12+
13+
#[test]
14+
fn test_session_manager_new() {
15+
let session_manager = SessionManager::new().unwrap();
16+
assert!(session_manager.handle.is_some());
17+
}
18+
19+
#[test]
20+
fn test_session_manager_push_kill() {
21+
let session_manager = SessionManager::new().unwrap();
22+
session_manager.push(SessionSignal::Kill).unwrap();
23+
assert_eq!(session_manager.handle.unwrap().join().unwrap().unwrap(), ());
24+
}
25+
26+
#[test]
27+
fn test_session_new() {
28+
let config = PyroscopeConfig {
29+
url: "http://localhost:8080".to_string(),
30+
application_name: "test".to_string(),
31+
tags: HashMap::new(),
32+
sample_rate: 100,
33+
};
34+
35+
let report = vec![1, 2, 3];
36+
37+
let session = Session::new(1950, config, report).unwrap();
38+
39+
assert_eq!(session.from, 1940);
40+
assert_eq!(session.until, 1950);
41+
}
42+
43+
#[test]
44+
fn test_session_send_error() {
45+
let config = PyroscopeConfig {
46+
url: "http://invalid_url".to_string(),
47+
application_name: "test".to_string(),
48+
tags: HashMap::new(),
49+
sample_rate: 100,
50+
};
51+
52+
let report = vec![1, 2, 3];
53+
54+
let session = Session::new(1950, config, report).unwrap();
55+
56+
// TODO: to figure this out
57+
}

tests/timer-epoll.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#[cfg(target_os = "linux")]
2+
mod tests {
3+
use pyroscope::timer::epoll::{
4+
epoll_create1, epoll_ctl, epoll_wait, timerfd_create, timerfd_settime,
5+
};
6+
7+
#[test]
8+
fn test_timerfd_create() {
9+
let timer_fd = timerfd_create(libc::CLOCK_REALTIME, libc::TFD_NONBLOCK).unwrap();
10+
assert!(timer_fd > 0);
11+
}
12+
13+
#[test]
14+
fn test_timerfd_settime() {
15+
let mut new_value = libc::itimerspec {
16+
it_interval: libc::timespec {
17+
tv_sec: 10,
18+
tv_nsec: 0,
19+
},
20+
it_value: libc::timespec {
21+
tv_sec: 0,
22+
tv_nsec: 0,
23+
},
24+
};
25+
26+
let mut old_value = libc::itimerspec {
27+
it_interval: libc::timespec {
28+
tv_sec: 0,
29+
tv_nsec: 0,
30+
},
31+
it_value: libc::timespec {
32+
tv_sec: 0,
33+
tv_nsec: 0,
34+
},
35+
};
36+
37+
let timer_fd = timerfd_create(libc::CLOCK_REALTIME, libc::TFD_NONBLOCK).unwrap();
38+
let void = timerfd_settime(
39+
timer_fd,
40+
libc::TFD_TIMER_ABSTIME,
41+
&mut new_value,
42+
&mut old_value,
43+
)
44+
.unwrap();
45+
assert!(void == ());
46+
}
47+
48+
#[test]
49+
fn test_epoll_create1() {
50+
let epoll_fd = epoll_create1(0).unwrap();
51+
assert!(epoll_fd > 0);
52+
}
53+
54+
#[test]
55+
fn test_epoll_ctl() {
56+
let mut event = libc::epoll_event {
57+
events: libc::EPOLLIN as u32,
58+
u64: 1,
59+
};
60+
61+
let epoll_fd = epoll_create1(0).unwrap();
62+
let timer_fd = timerfd_create(libc::CLOCK_REALTIME, libc::TFD_NONBLOCK).unwrap();
63+
let void = epoll_ctl(epoll_fd, libc::EPOLL_CTL_ADD, timer_fd, &mut event).unwrap();
64+
assert!(void == ());
65+
}
66+
67+
#[test]
68+
fn test_epoll_wait() {
69+
let mut event = libc::epoll_event {
70+
events: libc::EPOLLIN as u32,
71+
u64: 1,
72+
};
73+
74+
let epoll_fd = epoll_create1(0).unwrap();
75+
let timer_fd = timerfd_create(libc::CLOCK_REALTIME, libc::TFD_NONBLOCK).unwrap();
76+
epoll_ctl(epoll_fd, libc::EPOLL_CTL_ADD, timer_fd, &mut event).unwrap();
77+
78+
let mut events = vec![libc::epoll_event { events: 0, u64: 0 }];
79+
80+
// Expire in 1ms
81+
let void = epoll_wait(epoll_fd, events.as_mut_ptr(), 1, 1).unwrap();
82+
83+
assert!(void == ());
84+
}
85+
}

tests/timer.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use pyroscope::timer::Timer;
2+
3+
#[test]
4+
fn test_timer() {
5+
// Initialize Timer
6+
let mut timer = Timer::default().initialize().unwrap();
7+
8+
// Attach a listener
9+
let (tx, rx) = std::sync::mpsc::channel();
10+
timer.attach_listener(tx).unwrap();
11+
12+
// Wait for event (should arrive in 10s)
13+
let recv: u64 = rx.recv().unwrap();
14+
15+
// Get current time
16+
let now = std::time::SystemTime::now()
17+
.duration_since(std::time::UNIX_EPOCH)
18+
.unwrap()
19+
.as_secs();
20+
21+
// Check that recv and now are within 10s of each other
22+
assert!(recv - now < 10);
23+
24+
// Check that recv is divisible by 10
25+
assert!(recv % 10 == 0);
26+
}

0 commit comments

Comments
 (0)