Fix flaky test

This commit is contained in:
Igor Katson 2024-08-14 15:11:41 +01:00
parent 2618f8b389
commit 98038a2db4
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
2 changed files with 14 additions and 3 deletions

View file

@ -5,7 +5,9 @@ use tempfile::TempDir;
use tokio::{io::AsyncReadExt, time::timeout}; use tokio::{io::AsyncReadExt, time::timeout};
use tracing::info; use tracing::info;
use crate::{create_torrent, AddTorrent, CreateTorrentOptions, Session}; use crate::{
create_torrent, tests::test_util::TestPeerMetadata, AddTorrent, CreateTorrentOptions, Session,
};
use super::test_util::create_default_random_dir_with_torrents; use super::test_util::create_default_random_dir_with_torrents;
@ -21,11 +23,11 @@ async fn e2e_stream() -> anyhow::Result<()> {
.await?; .await?;
let orig_content = std::fs::read(files.path().join("0.data")).unwrap(); let orig_content = std::fs::read(files.path().join("0.data")).unwrap();
let server_session = Session::new_with_opts( let server_session = Session::new_with_opts(
files.path().into(), files.path().into(),
crate::SessionOptions { crate::SessionOptions {
disable_dht: true, disable_dht: true,
peer_id: Some(TestPeerMetadata::good().as_peer_id()),
persistence: false, persistence: false,
listen_port_range: Some(16001..16100), listen_port_range: Some(16001..16100),
enable_upnp_port_forwarding: false, enable_upnp_port_forwarding: false,
@ -71,6 +73,7 @@ async fn e2e_stream() -> anyhow::Result<()> {
crate::SessionOptions { crate::SessionOptions {
disable_dht: true, disable_dht: true,
persistence: false, persistence: false,
peer_id: Some(TestPeerMetadata::good().as_peer_id()),
listen_port_range: None, listen_port_range: None,
enable_upnp_port_forwarding: false, enable_upnp_port_forwarding: false,
..Default::default() ..Default::default()

View file

@ -1,7 +1,7 @@
use std::{io::Write, path::Path}; use std::{io::Write, path::Path};
use librqbit_core::Id20; use librqbit_core::Id20;
use rand::{RngCore, SeedableRng}; use rand::{thread_rng, Rng, RngCore, SeedableRng};
use tempfile::TempDir; use tempfile::TempDir;
pub fn create_new_file_with_random_content(path: &Path, mut size: usize) { pub fn create_new_file_with_random_content(path: &Path, mut size: usize) {
@ -43,8 +43,16 @@ pub struct TestPeerMetadata {
} }
impl TestPeerMetadata { impl TestPeerMetadata {
pub fn good() -> Self {
Self {
server_id: 0,
max_random_sleep_ms: 0,
}
}
pub fn as_peer_id(&self) -> Id20 { pub fn as_peer_id(&self) -> Id20 {
let mut peer_id = Id20::default(); let mut peer_id = Id20::default();
thread_rng().fill(&mut peer_id.0);
peer_id.0[0] = self.server_id; peer_id.0[0] = self.server_id;
peer_id.0[1] = self.max_random_sleep_ms; peer_id.0[1] = self.max_random_sleep_ms;
peer_id peer_id