Commit 76570bf9 authored by Michael Aaron Murphy's avatar Michael Aaron Murphy

Fix Tests

parent dad2a4aa
...@@ -68,10 +68,11 @@ impl History { ...@@ -68,10 +68,11 @@ impl History {
let max_file_size = max_size.load(Ordering::Relaxed); let max_file_size = max_size.load(Ordering::Relaxed);
let _ = write_to_disk(max_file_size, &command, &filepath); let _ = write_to_disk(max_file_size, &command, &filepath);
} }
sleep(Duration::from_millis(1)); sleep(Duration::from_millis(100));
} }
if let Ok((command, filepath)) = receiver.try_recv() { // Deplete the receiver of commands to write, before exiting the thread.
while let Ok((command, filepath)) = receiver.try_recv() {
let max_file_size = max_size.load(Ordering::Relaxed); let max_file_size = max_size.load(Ordering::Relaxed);
let _ = write_to_disk(max_file_size, &command, &filepath); let _ = write_to_disk(max_file_size, &command, &filepath);
} }
...@@ -105,6 +106,7 @@ impl History { ...@@ -105,6 +106,7 @@ impl History {
{ {
return Ok(()); return Ok(());
} }
self.buffers.push_back(new_item); self.buffers.push_back(new_item);
while self.buffers.len() > self.max_size { while self.buffers.len() > self.max_size {
self.buffers.pop_front(); self.buffers.pop_front();
...@@ -242,14 +244,14 @@ fn write_to_disk(max_file_size: usize, new_item: &Buffer, file_name: &str) -> io ...@@ -242,14 +244,14 @@ fn write_to_disk(max_file_size: usize, new_item: &Buffer, file_name: &str) -> io
try!(file.read_to_end(&mut buffer)); try!(file.read_to_end(&mut buffer));
try!(file.set_len(0)); try!(file.set_len(0));
try!(io::copy(&mut buffer.as_slice(), &mut file)); try!(io::copy(&mut buffer.as_slice(), &mut file));
} }
// Seek to end for appending // Seek to end for appending
try!(file.seek(SeekFrom::End(0))); try!(file.seek(SeekFrom::End(0)));
// Write the command to the history file. // Write the command to the history file.
try!(file.write_all(String::from(new_item.clone()).as_bytes())); try!(file.write_all(String::from(new_item.clone()).as_bytes()));
try!(file.write(b"\n")); try!(file.write_all(b"\n"));
file.flush()?;
Ok(()) Ok(())
} }
......
...@@ -77,7 +77,9 @@ fn test_in_memory_history_truncating() { ...@@ -77,7 +77,9 @@ fn test_in_memory_history_truncating() {
h.set_max_size(2); h.set_max_size(2);
for _ in 0..4 { for _ in 0..4 {
h.push(Buffer::from("a")).unwrap(); h.push(Buffer::from("a")).unwrap();
h.push(Buffer::from("b")).unwrap();
} }
h.commit_history();
assert_eq!(h.len(), 2); assert_eq!(h.len(), 2);
} }
...@@ -93,6 +95,7 @@ fn test_in_file_history_truncating() { ...@@ -93,6 +95,7 @@ fn test_in_file_history_truncating() {
for _ in 0..20 { for _ in 0..20 {
h.push(Buffer::from("a")).unwrap(); h.push(Buffer::from("a")).unwrap();
} }
h.commit_history();
} }
let f = fs::File::open(tmp_file.clone()).unwrap(); let f = fs::File::open(tmp_file.clone()).unwrap();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment