Skip to content

Commit 3ed54a4

Browse files
committed
Make block_time variable and iterate until overflow
1 parent 7636045 commit 3ed54a4

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

packages/std/src/testing/mock.rs

+27-13
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,8 @@ pub fn mock_env() -> Env {
444444
/// ```
445445
pub struct Envs {
446446
contract_address: Addr,
447+
/// The number of nanoseconds between two consecutive blocks
448+
block_time: u64,
447449
last_height: u64,
448450
last_time: Timestamp,
449451
envs_produced: u64,
@@ -457,21 +459,26 @@ impl Envs {
457459
Envs {
458460
// Default values here for compatibility with old `mock_env` function. They could be changed to anything else if there is a good reason.
459461
contract_address: api.addr_make("cosmos2contract"),
462+
block_time: 5_000_000_000, // 5s
460463
last_height: 12_344,
461464
last_time: Timestamp::from_nanos(1_571_797_419_879_305_533).minus_seconds(5),
462465
envs_produced: 0,
463466
}
464467
}
465468

466469
pub fn make(&mut self) -> Env {
467-
let height = self.last_height + 1;
468-
let time = self.last_time.plus_seconds(5);
470+
self.checked_make().unwrap()
471+
}
472+
473+
fn checked_make(&mut self) -> Option<Env> {
474+
let height = self.last_height.checked_add(1)?;
475+
let time = Timestamp::from_nanos(self.last_time.nanos().checked_add(self.block_time)?);
469476

470477
self.last_height = height;
471478
self.last_time = time;
472-
self.envs_produced += 1;
479+
self.envs_produced += 1; // does not overflow because height increment fails first
473480

474-
Env {
481+
Some(Env {
475482
block: BlockInfo {
476483
height,
477484
time,
@@ -481,21 +488,17 @@ impl Envs {
481488
contract: ContractInfo {
482489
address: self.contract_address.clone(),
483490
},
484-
}
491+
})
485492
}
486493
}
487494

488-
// The iterator implementation can produce 1 million envs and then stops for no good reason.
495+
// The iterator implementation ends in case of overflows to avoid panics.
496+
// Using this is recommended for very long running test suites.
489497
impl Iterator for Envs {
490498
type Item = Env;
491499

492500
fn next(&mut self) -> Option<Self::Item> {
493-
if self.envs_produced < 1_000_000 {
494-
let item = self.make();
495-
Some(item)
496-
} else {
497-
None
498-
}
501+
self.checked_make()
499502
}
500503
}
501504

@@ -1417,7 +1420,7 @@ mod tests {
14171420
}
14181421

14191422
#[test]
1420-
fn envs_implements_iteratorworks() {
1423+
fn envs_implements_iterator() {
14211424
let envs = Envs::new("food");
14221425

14231426
let result: Vec<_> = envs.into_iter().take(5).collect();
@@ -1441,6 +1444,17 @@ mod tests {
14411444
result[4].block.time,
14421445
Timestamp::from_nanos(1_571_797_439_879_305_533)
14431446
);
1447+
1448+
// Get a millions envs through iterator
1449+
let mut envs = Envs::new("yo");
1450+
let first = envs.next().unwrap();
1451+
let last = envs.take(1_000_000).last().unwrap();
1452+
assert_eq!(first.block.height, 12_345);
1453+
assert_eq!(last.block.height, 1_012_345);
1454+
assert_eq!(
1455+
last.block.time,
1456+
first.block.time.plus_seconds(1_000_000 * 5)
1457+
);
14441458
}
14451459

14461460
#[test]

0 commit comments

Comments
 (0)