Skip to content

Commit

Permalink
Tweak the did_change_stress_test_random_wait test (#5724)
Browse files Browse the repository at this point in the history
## Description
This test was originally added in #5704. The initial ambition of
compiling 400 times, aimed at rigorously evaluating the garbage
collector's resilience, inadvertently led to stack overflows and adverse
interactions when executed alongside concurrent tests. To address these
issues while preserving the test's core objectives, the following
modifications have been made:

Compilation Frequency Reduction: The iteration count for compilations
has been adjusted from 400 to 60. This change significantly accelerates
test completion without compromising the test's ability to effectively
assess garbage collector performance and stability.

Runtime Isolation: Transitioning from #[tokio::test] to a standard
#[test] annotation, we now initiate a dedicated Tokio runtime within the
test. This approach prevents the test from sharing the runtime
environment with other tests, thereby eliminating concurrent execution
and its associated complexities.

---------

Co-authored-by: Sophie Dankel <[email protected]>
  • Loading branch information
JoshuaBatty and sdankel authored Mar 14, 2024
1 parent 461bebe commit 63cee46
Showing 1 changed file with 47 additions and 36 deletions.
83 changes: 47 additions & 36 deletions sway-lsp/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,43 +138,54 @@ async fn did_change_stress_test() {
shutdown_and_exit(&mut service).await;
}

#[tokio::test]
#[allow(dead_code)]
async fn did_change_stress_test_random_wait() {
std::env::set_var("RUST_BACKTRACE", "1");
let default_panic = std::panic::take_hook();
std::panic::set_hook(Box::new(move |panic_info| {
default_panic(panic_info); // Print the panic message
std::process::exit(1);
}));
let (mut service, _) = LspService::build(ServerState::new)
.custom_method("sway/metrics", ServerState::metrics)
.finish();
let example_dir = sway_workspace_dir()
.join(e2e_language_dir())
.join("generics_in_contract");
let uri = init_and_open(&mut service, example_dir.join("src/main.sw")).await;
let times = 400;
for version in 0..times {
//eprintln!("version: {}", version);
let _ = lsp::did_change_request(&mut service, &uri, version + 1).await;
if version == 0 {
service.inner().wait_for_parsing().await;
}
// wait for a random amount of time between 1-30ms
tokio::time::sleep(tokio::time::Duration::from_millis(
rand::random::<u64>() % 30 + 1,
))
.await;
// there is a 10% chance that a longer 300-1000ms wait will be added
if rand::random::<u64>() % 10 < 1 {
tokio::time::sleep(tokio::time::Duration::from_millis(
rand::random::<u64>() % 700 + 300,
))
.await;
#[test]
fn did_change_stress_test_random_wait() {
let rt = tokio::runtime::Runtime::new().expect("Failed to create a runtime");
rt.block_on(async {
let test_duration = tokio::time::Duration::from_secs(5 * 60); // 5 minutes timeout
let test_future = async {
std::env::set_var("RUST_BACKTRACE", "1");
let default_panic = std::panic::take_hook();
std::panic::set_hook(Box::new(move |panic_info| {
default_panic(panic_info); // Print the panic message
std::process::exit(1);
}));
let (mut service, _) = LspService::new(ServerState::new);
let example_dir = sway_workspace_dir()
.join(e2e_language_dir())
.join("generics_in_contract");
let uri = init_and_open(&mut service, example_dir.join("src/main.sw")).await;
let times = 60;
for version in 0..times {
//eprintln!("version: {}", version);
let _ = lsp::did_change_request(&mut service, &uri, version + 1).await;
if version == 0 {
service.inner().wait_for_parsing().await;
}
// wait for a random amount of time between 1-30ms
tokio::time::sleep(tokio::time::Duration::from_millis(
rand::random::<u64>() % 30 + 1,
))
.await;
// there is a 10% chance that a longer 100-800ms wait will be added
if rand::random::<u64>() % 10 < 1 {
tokio::time::sleep(tokio::time::Duration::from_millis(
rand::random::<u64>() % 700 + 100,
))
.await;
}
}
shutdown_and_exit(&mut service).await;
};
if tokio::time::timeout(test_duration, test_future)
.await
.is_err()
{
panic!(
"did_change_stress_test_random_wait did not complete within the timeout period."
);
}
}
shutdown_and_exit(&mut service).await;
});
}

#[tokio::test]
Expand Down

0 comments on commit 63cee46

Please sign in to comment.