From d4d54450a1ae4b31743e2ebaf471c33a61ae5416 Mon Sep 17 00:00:00 2001 From: Aram Drevekenin Date: Thu, 26 Nov 2020 10:49:53 +0100 Subject: [PATCH] fix(tests): do not read stdin until we started reading from pty --- src/tests/fakes.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/tests/fakes.rs b/src/tests/fakes.rs index 22370d98..f531586e 100644 --- a/src/tests/fakes.rs +++ b/src/tests/fakes.rs @@ -4,6 +4,7 @@ use ::std::io::{Read, Write}; use ::std::os::unix::io::RawFd; use ::std::path::PathBuf; use ::std::sync::{Arc, Mutex}; +use ::std::sync::atomic::{AtomicBool, Ordering}; use ::std::time::{Duration, Instant}; use crate::os_input_output::OsApi; @@ -24,14 +25,16 @@ pub struct FakeStdinReader { pub input_chars: Vec<[u8; 10]>, pub read_position: usize, last_snapshot_time: Arc>, + started_reading_from_pty: Arc, } impl FakeStdinReader { - pub fn new(input_chars: Vec<[u8; 10]>, last_snapshot_time: Arc>) -> Self { + pub fn new(input_chars: Vec<[u8; 10]>, last_snapshot_time: Arc>, started_reading_from_pty: Arc) -> Self { FakeStdinReader { input_chars, read_position: 0, last_snapshot_time, + started_reading_from_pty, } } } @@ -39,11 +42,15 @@ impl FakeStdinReader { impl Read for FakeStdinReader { fn read(&mut self, buf: &mut [u8]) -> Result { loop { - let last_snapshot_time = { *self.last_snapshot_time.lock().unwrap() }; - if last_snapshot_time.elapsed() > MIN_TIME_BETWEEN_SNAPSHOTS { - break; + if self.started_reading_from_pty.load(Ordering::SeqCst) == false { + ::std::thread::sleep(MIN_TIME_BETWEEN_SNAPSHOTS); } else { - ::std::thread::sleep(MIN_TIME_BETWEEN_SNAPSHOTS - last_snapshot_time.elapsed()); + let last_snapshot_time = { *self.last_snapshot_time.lock().unwrap() }; + if last_snapshot_time.elapsed() > MIN_TIME_BETWEEN_SNAPSHOTS { + break; + } else { + ::std::thread::sleep(MIN_TIME_BETWEEN_SNAPSHOTS - last_snapshot_time.elapsed()); + } } } let read_position = self.read_position; @@ -120,6 +127,7 @@ pub struct FakeInputOutput { win_sizes: Arc>>, possible_tty_inputs: HashMap, last_snapshot_time: Arc>, + started_reading_from_pty: Arc, } impl FakeInputOutput { @@ -137,6 +145,7 @@ impl FakeInputOutput { io_events: Arc::new(Mutex::new(vec![])), win_sizes: Arc::new(Mutex::new(win_sizes)), possible_tty_inputs: get_possible_tty_inputs(), + started_reading_from_pty: Arc::new(AtomicBool::new(false)), } } pub fn with_tty_inputs(mut self, tty_inputs: HashMap) -> Self { @@ -200,6 +209,7 @@ impl OsApi for FakeInputOutput { // them fail ::std::thread::sleep(::std::time::Duration::from_millis(25)); } else if attempts_left == 0 { + self.started_reading_from_pty.store(true, Ordering::SeqCst); return Ok(0); } let mut read_buffers = self.read_buffers.lock().unwrap(); @@ -213,6 +223,7 @@ impl OsApi for FakeInputOutput { if bytes_read > bytes.read_position { bytes.set_read_position(bytes_read); } + self.started_reading_from_pty.store(true, Ordering::SeqCst); return Ok(bytes_read); } None => { @@ -245,7 +256,7 @@ impl OsApi for FakeInputOutput { input_chars.push(*bytes); } } - let reader = FakeStdinReader::new(input_chars, self.last_snapshot_time.clone()); + let reader = FakeStdinReader::new(input_chars, self.last_snapshot_time.clone(), self.started_reading_from_pty.clone()); Box::new(reader) } fn get_stdout_writer(&self) -> Box {