Skip to content
This repository has been archived by the owner on Aug 25, 2021. It is now read-only.

Commit

Permalink
Improve traffic watcher services tests coverage (#101, #27)
Browse files Browse the repository at this point in the history
- add unit tests for the PeerMetricsService and TrafficWatcher
- fix incorrect stats checking in the PeerMetricsService
  • Loading branch information
evdokimovs authored May 29, 2020
1 parent e542079 commit a3cd7d7
Show file tree
Hide file tree
Showing 11 changed files with 1,306 additions and 179 deletions.
84 changes: 42 additions & 42 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions proto/client-api/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,18 @@ impl From<HighResTimeStamp> for SystemTime {
}
}

impl From<SystemTime> for HighResTimeStamp {
#[allow(clippy::cast_precision_loss)]
#[inline]
fn from(time: SystemTime) -> Self {
HighResTimeStamp(
time.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_millis() as f64,
)
}
}

/// Hashing string representation.
///
/// Some people believe that such behavior is incorrect (but in some programming
Expand Down
2 changes: 1 addition & 1 deletion src/api/control/refs/fid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl TryFrom<String> for StatefulFid {
member_id
}
} else {
return Ok(Fid::<ToRoom>::new(room_id.to_string().into()).into());
return Ok(Fid::<ToRoom>::new(room_id.into()).into());
};

let endpoint_id = if let Some(endpoint_id) = splitted.next() {
Expand Down
63 changes: 63 additions & 0 deletions src/media/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,3 +463,66 @@ impl Peer<Stable> {
Ok(mids)
}
}

#[cfg(test)]
pub mod tests {
use super::*;

/// Returns [`PeerStateMachine`] with provided count of the `MediaTrack`s
/// media types.
pub fn test_peer_from_peer_tracks(
send_audio: u32,
send_video: u32,
recv_audio: u32,
recv_video: u32,
) -> PeerStateMachine {
let mut peer = Peer {
state: Stable {},
context: Context {
id: Id(1),
sdp_offer: None,
sdp_answer: None,
senders: HashMap::new(),
receivers: HashMap::new(),
member_id: MemberId::from("test-member"),
is_force_relayed: false,
partner_peer: Id(2),
ice_user: None,
endpoints: Vec::new(),
partner_member: MemberId::from("partner-member"),
},
};

let mut track_id_counter = Counter::default();

for _ in 0..send_audio {
let track_id = track_id_counter.next_id();
let track =
MediaTrack::new(track_id, MediaType::Audio(AudioSettings {}));
peer.context.senders.insert(track_id, Rc::new(track));
}

for _ in 0..send_video {
let track_id = track_id_counter.next_id();
let track =
MediaTrack::new(track_id, MediaType::Video(VideoSettings {}));
peer.context.senders.insert(track_id, Rc::new(track));
}

for _ in 0..recv_audio {
let track_id = track_id_counter.next_id();
let track =
MediaTrack::new(track_id, MediaType::Audio(AudioSettings {}));
peer.context.receivers.insert(track_id, Rc::new(track));
}

for _ in 0..recv_video {
let track_id = track_id_counter.next_id();
let track =
MediaTrack::new(track_id, MediaType::Video(VideoSettings {}));
peer.context.receivers.insert(track_id, Rc::new(track));
}

peer.into()
}
}
22 changes: 22 additions & 0 deletions src/signalling/peers/media_traffic_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,20 @@ mod tracks_state_tests {
get_diff_added(state_before, state_after).unwrap(),
MediaType::Video
);

let state_before = MediaTrafficState::with_media_type(MediaType::Audio);
let state_after = MediaTrafficState::with_media_type(MediaType::Both);
assert_eq!(
get_diff_added(state_before, state_after).unwrap(),
MediaType::Video,
);

let state_before = MediaTrafficState::with_media_type(MediaType::Video);
let state_after = MediaTrafficState::with_media_type(MediaType::Both);
assert_eq!(
get_diff_added(state_before, state_after).unwrap(),
MediaType::Audio,
);
}

#[test]
Expand Down Expand Up @@ -278,5 +292,13 @@ mod tracks_state_tests {
let before = MediaTrafficState::new();
let after = MediaTrafficState::with_media_type(MediaType::Both);
assert!(get_diff_removed(before, after).is_none());

let before = MediaTrafficState::with_media_type(MediaType::Both);
let after = MediaTrafficState::with_media_type(MediaType::Audio);
assert_eq!(get_diff_removed(before, after).unwrap(), MediaType::Video);

let before = MediaTrafficState::with_media_type(MediaType::Both);
let after = MediaTrafficState::with_media_type(MediaType::Video);
assert_eq!(get_diff_removed(before, after).unwrap(), MediaType::Audio);
}
}
Loading

0 comments on commit a3cd7d7

Please sign in to comment.