Skip to content

Commit

Permalink
Add tests that grow containers until running out of memory budget. (#…
Browse files Browse the repository at this point in the history
…1242)

### What

Add tests that grow containers until running out of memory budget.

I've also added some simple time measurements for eye-balling the
runtime; at least on my machine it is a bit suspiciously slow.

### Why

Increasing test coverage.

### Known limitations

N/A
  • Loading branch information
dmkozh authored Nov 22, 2023
1 parent e2e6ff6 commit 8cc4b43
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
34 changes: 34 additions & 0 deletions soroban-env-host/src/test/map.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::rc::Rc;
use std::time::Instant;

use soroban_env_common::{
xdr::{
Expand Down Expand Up @@ -338,3 +339,36 @@ fn map_build_bad_element_integrity() -> Result<(), HostError> {

Ok(())
}

#[test]
fn large_map_exceeds_budget() {
let host = Host::default();
// Set a fixed budget higher than defaults, but still realistic.
const MEMORY_LIMIT: u64 = 200 * 1024 * 1024;
host.budget_ref()
.reset_limits(200_000_000, MEMORY_LIMIT)
.unwrap();
let start = Instant::now();
let mut m = host.map_new().unwrap();
let mut i = 0;
loop {
i += 1;
let new_m_res = host.map_put(m, U32Val::from(u32::MAX - i).into(), U32Val::from(i).into());
match new_m_res {
Ok(new_m) => {
m = new_m;
}
Err(e) => {
assert!(e.error.is_type(ScErrorType::Budget));
assert!(e.error.is_code(ScErrorCode::ExceededLimit));
assert!(host.budget_ref().get_mem_bytes_consumed().unwrap() > MEMORY_LIMIT);
break;
}
}
}
eprintln!(
"total iterations: {}, real time: {}",
i,
(Instant::now() - start).as_secs_f64()
);
}
34 changes: 34 additions & 0 deletions soroban-env-host/src/test/vec.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use core::cmp::Ordering;
use std::time::Instant;

use soroban_env_common::{xdr::ScVal, Compare, Symbol, Tag, TryFromVal, U32Val};
use soroban_test_wasms::LINEAR_MEMORY;
Expand Down Expand Up @@ -368,3 +369,36 @@ fn linear_memory_operations() -> Result<(), HostError> {
}
Ok(())
}

#[test]
fn large_vec_exceeds_budget() {
let host = Host::default();
// Set a fixed budget higher than defaults, but still realistic.
const MEMORY_LIMIT: u64 = 200 * 1024 * 1024;
host.budget_ref()
.reset_limits(200_000_000, MEMORY_LIMIT)
.unwrap();
let start = Instant::now();
let mut v = host.vec_new().unwrap();
let mut i = 0;
loop {
i += 1;
let new_v_res = host.vec_push_back(v, U32Val::from(i).into());
match new_v_res {
Ok(new_v) => {
v = new_v;
}
Err(e) => {
assert!(e.error.is_type(ScErrorType::Budget));
assert!(e.error.is_code(ScErrorCode::ExceededLimit));
assert!(host.budget_ref().get_mem_bytes_consumed().unwrap() > MEMORY_LIMIT);
break;
}
}
}
eprintln!(
"total iterations: {}, real time: {}",
i,
(Instant::now() - start).as_secs_f64()
);
}

0 comments on commit 8cc4b43

Please sign in to comment.