Fix the intermittent error in pool tests (#1123)

This commit is contained in:
Antioch Peverell 2018-06-01 23:18:58 +01:00 committed by Ignotus Peverell
parent 2fa32d15ce
commit 68c7cff6db
2 changed files with 21 additions and 7 deletions

View file

@ -183,6 +183,10 @@ fn test_transaction_pool_block_reconciliation() {
}).unwrap(); }).unwrap();
} }
let tip = Tip::from_block(&block.header);
chain.store.save_block_header(&block.header).unwrap();
chain.store.save_head(&tip).unwrap();
block block
}; };
@ -191,9 +195,9 @@ fn test_transaction_pool_block_reconciliation() {
let mut write_pool = pool.write().unwrap(); let mut write_pool = pool.write().unwrap();
write_pool.reconcile_block(&block).unwrap(); write_pool.reconcile_block(&block).unwrap();
assert_eq!(write_pool.total_size(), 4);
assert_eq!(write_pool.txpool.entries[0].tx, valid_transaction);
// TODO - this is the "correct" behavior (see below) // TODO - this is the "correct" behavior (see below)
// assert_eq!(write_pool.total_size(), 4);
// assert_eq!(write_pool.txpool.entries[0].tx, valid_transaction);
// assert_eq!(write_pool.txpool.entries[1].tx, pool_child); // assert_eq!(write_pool.txpool.entries[1].tx, pool_child);
// assert_eq!(write_pool.txpool.entries[2].tx, conflict_valid_child); // assert_eq!(write_pool.txpool.entries[2].tx, conflict_valid_child);
// assert_eq!(write_pool.txpool.entries[3].tx, valid_child_valid); // assert_eq!(write_pool.txpool.entries[3].tx, valid_child_valid);
@ -205,6 +209,8 @@ fn test_transaction_pool_block_reconciliation() {
// //
// TODO - wtf is with these name permutations... // TODO - wtf is with these name permutations...
// //
assert_eq!(write_pool.total_size(), 4);
assert_eq!(write_pool.txpool.entries[0].tx, valid_transaction);
assert_eq!(write_pool.txpool.entries[1].tx, conflict_valid_child); assert_eq!(write_pool.txpool.entries[1].tx, conflict_valid_child);
assert_eq!(write_pool.txpool.entries[2].tx, valid_child_conflict); assert_eq!(write_pool.txpool.entries[2].tx, valid_child_conflict);
assert_eq!(write_pool.txpool.entries[3].tx, valid_child_valid); assert_eq!(write_pool.txpool.entries[3].tx, valid_child_valid);

View file

@ -148,11 +148,8 @@ impl AppendOnlyFile {
/// Leverages the memory map. /// Leverages the memory map.
pub fn read(&self, offset: usize, length: usize) -> Vec<u8> { pub fn read(&self, offset: usize, length: usize) -> Vec<u8> {
if offset >= self.buffer_start { if offset >= self.buffer_start {
if self.buffer.is_empty() { let buffer_offset = offset - self.buffer_start;
return vec![]; return self.read_from_buffer(buffer_offset, length);
}
let offset = offset - self.buffer_start;
return self.buffer[offset..(offset + length)].to_vec();
} }
if let None = self.mmap { if let None = self.mmap {
return vec![]; return vec![];
@ -166,6 +163,17 @@ impl AppendOnlyFile {
(&mmap[offset..(offset + length)]).to_vec() (&mmap[offset..(offset + length)]).to_vec()
} }
// Read length bytes from the buffer, from offset.
// Return empty vec if we do not have enough bytes in the buffer to read a full
// vec.
fn read_from_buffer(&self, offset: usize, length: usize) -> Vec<u8> {
if self.buffer.len() < (offset + length) {
vec![]
} else {
self.buffer[offset..(offset + length)].to_vec()
}
}
/// Truncates the underlying file to the provided offset /// Truncates the underlying file to the provided offset
pub fn truncate(&self, offs: usize) -> io::Result<()> { pub fn truncate(&self, offs: usize) -> io::Result<()> {
let fd = self.file.as_raw_fd(); let fd = self.file.as_raw_fd();