Add bogus peer simulation to e2e tests
This commit is contained in:
parent
2ef5124a2d
commit
048f4d1645
2 changed files with 25 additions and 6 deletions
|
|
@ -272,8 +272,12 @@ impl<H: PeerConnectionHandler> PeerConnection<H> {
|
||||||
.and_then(|e| e.ut_metadata())
|
.and_then(|e| e.ut_metadata())
|
||||||
})?,
|
})?,
|
||||||
WriterRequest::ReadChunkRequest(chunk) => {
|
WriterRequest::ReadChunkRequest(chunk) => {
|
||||||
|
#[allow(unused_mut)]
|
||||||
|
let mut skip_reading_for_e2e_tests = false;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
{
|
{
|
||||||
|
use tracing::warn;
|
||||||
// This is poor-mans fault injection for running e2e tests.
|
// This is poor-mans fault injection for running e2e tests.
|
||||||
use crate::tests::test_util::TestPeerMetadata;
|
use crate::tests::test_util::TestPeerMetadata;
|
||||||
let tpm = TestPeerMetadata::from_peer_id(self.peer_id);
|
let tpm = TestPeerMetadata::from_peer_id(self.peer_id);
|
||||||
|
|
@ -286,6 +290,12 @@ impl<H: PeerConnectionHandler> PeerConnection<H> {
|
||||||
* (tpm.max_random_sleep_ms as f64))
|
* (tpm.max_random_sleep_ms as f64))
|
||||||
as u64;
|
as u64;
|
||||||
tokio::time::sleep(Duration::from_millis(sleep_ms)).await;
|
tokio::time::sleep(Duration::from_millis(sleep_ms)).await;
|
||||||
|
|
||||||
|
if rand::thread_rng().gen_bool(tpm.bad_data_probability()) {
|
||||||
|
warn!("will NOT actually read the data to simulate a malicious peer that sends garbage");
|
||||||
|
write_buf.fill(0);
|
||||||
|
skip_reading_for_e2e_tests = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// this whole section is an optimization
|
// this whole section is an optimization
|
||||||
|
|
@ -293,12 +303,14 @@ impl<H: PeerConnectionHandler> PeerConnection<H> {
|
||||||
let preamble_len = serialize_piece_preamble(chunk, &mut write_buf);
|
let preamble_len = serialize_piece_preamble(chunk, &mut write_buf);
|
||||||
let full_len = preamble_len + chunk.size as usize;
|
let full_len = preamble_len + chunk.size as usize;
|
||||||
write_buf.resize(full_len, 0);
|
write_buf.resize(full_len, 0);
|
||||||
self.spawner
|
if !skip_reading_for_e2e_tests {
|
||||||
.spawn_block_in_place(|| {
|
self.spawner
|
||||||
self.handler
|
.spawn_block_in_place(|| {
|
||||||
.read_chunk(chunk, &mut write_buf[preamble_len..])
|
self.handler
|
||||||
})
|
.read_chunk(chunk, &mut write_buf[preamble_len..])
|
||||||
.with_context(|| format!("error reading chunk {chunk:?}"))?;
|
})
|
||||||
|
.with_context(|| format!("error reading chunk {chunk:?}"))?;
|
||||||
|
}
|
||||||
|
|
||||||
uploaded_add = Some(chunk.size);
|
uploaded_add = Some(chunk.size);
|
||||||
full_len
|
full_len
|
||||||
|
|
|
||||||
|
|
@ -63,4 +63,11 @@ impl TestPeerMetadata {
|
||||||
}
|
}
|
||||||
0f64
|
0f64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn bad_data_probability(&self) -> f64 {
|
||||||
|
if self.server_id % 2 == 0 {
|
||||||
|
return 0.05f64;
|
||||||
|
}
|
||||||
|
0f64
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue