Skip to content

Commit 7297920

Browse files
committed
Fix: Keep active assets sorted by ID after pool is replaced
1 parent 8696cf3 commit 7297920

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

src/asset.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,9 @@ impl AssetPool {
307307

308308
self.active.clear();
309309
self.active.extend(new_pool);
310+
311+
// New pool may not have been sorted, but active needs to be sorted by ID
312+
self.active.sort();
310313
}
311314
}
312315

@@ -538,4 +541,33 @@ mod tests {
538541
assert_eq!(asset_pool.active[0].id, Some(AssetID(2)));
539542
assert_eq!(asset_pool.active[0].agent_id, "some_other_agent".into());
540543
}
544+
545+
#[rstest]
546+
fn test_asset_pool_replace_active_pool_out_of_order(
547+
mut asset_pool: AssetPool,
548+
process: Process,
549+
) {
550+
let new_asset = Asset::new(
551+
"some_other_agent".into(),
552+
process.into(),
553+
"GBR".into(),
554+
2.0,
555+
2010,
556+
)
557+
.unwrap();
558+
559+
asset_pool.commission_new(2020);
560+
assert_eq!(asset_pool.active.len(), 2);
561+
let mut new_pool: Vec<Rc<Asset>> = asset_pool
562+
.iter()
563+
.map(|asset| asset.clone().into())
564+
.collect();
565+
new_pool.push(new_asset.into());
566+
new_pool.reverse();
567+
568+
asset_pool.replace_active_pool(new_pool);
569+
assert_equal(asset_pool.iter().map(|asset| asset.id.unwrap().0), 0..3);
570+
assert_eq!(asset_pool.active[2].id, Some(AssetID(2)));
571+
assert_eq!(asset_pool.active[2].agent_id, "some_other_agent".into());
572+
}
541573
}

0 commit comments

Comments
 (0)