Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(base): make able to trigger early drop with other resources #465

Merged
merged 4 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
chore: add integration tests
  • Loading branch information
nyannyacha committed Dec 23, 2024
commit 1184aeec7f52707548aae36b9b6d98d707f05ed0
26 changes: 26 additions & 0 deletions crates/base/test_cases/early-drop-mem/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function sleep(ms: number) {
return new Promise(res => {
setTimeout(() => {
res(void 0);
}, ms)
});
}

addEventListener("beforeunload", ev => {
console.log(ev.detail);
});

const mem = [];

export default {
async fetch() {
for (const _ of [...Array(12).keys()]) {
const buf = new Uint8Array(1024 * 1024);
buf.fill(1, 0);
mem.push(buf);
await sleep(300);
}

return new Response();
}
}
18 changes: 18 additions & 0 deletions crates/base/test_cases/early-drop-wall-clock/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function sleep(ms: number) {
return new Promise(res => {
setTimeout(() => {
res(void 0);
}, ms)
});
}

addEventListener("beforeunload", ev => {
console.log(ev.detail);
});

export default {
async fetch() {
await sleep(2000);
return new Response();
}
}
82 changes: 82 additions & 0 deletions crates/base/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3295,6 +3295,88 @@ async fn test_should_not_wait_for_background_tests() {
}
}

#[tokio::test]
#[serial]
async fn test_should_be_able_to_trigger_early_drop_with_wall_clock() {
let (tx, mut rx) = mpsc::unbounded_channel();
let tb = TestBedBuilder::new("./test_cases/main")
.with_per_worker_policy(None)
.with_worker_event_sender(Some(tx))
.build()
.await;

let resp = tb
.request(|b| {
b.uri("/early-drop-wall-clock")
.header("x-worker-timeout-ms", HeaderValue::from_static("3000"))
.body(Body::empty())
.context("can't make request")
})
.await
.unwrap();

assert_eq!(resp.status().as_u16(), StatusCode::OK);

sleep(Duration::from_secs(2)).await;
rx.close();
tb.exit(Duration::from_secs(TESTBED_DEADLINE_SEC)).await;

while let Some(ev) = rx.recv().await {
let WorkerEvents::Log(ev) = ev.event else {
continue;
};
if ev.level != LogLevel::Info {
continue;
}
if ev.msg.contains("early_drop") {
return;
}
}

unreachable!("test failed");
}

#[tokio::test]
#[serial]
async fn test_should_be_able_to_trigger_early_drop_with_mem() {
let (tx, mut rx) = mpsc::unbounded_channel();
let tb = TestBedBuilder::new("./test_cases/main")
.with_per_worker_policy(None)
.with_worker_event_sender(Some(tx))
.build()
.await;

let resp = tb
.request(|b| {
b.uri("/early-drop-mem")
.header("x-memory-limit-mb", HeaderValue::from_static("20"))
.body(Body::empty())
.context("can't make request")
})
.await
.unwrap();

assert_eq!(resp.status().as_u16(), StatusCode::OK);

sleep(Duration::from_secs(2)).await;
rx.close();
tb.exit(Duration::from_secs(TESTBED_DEADLINE_SEC)).await;

while let Some(ev) = rx.recv().await {
let WorkerEvents::Log(ev) = ev.event else {
continue;
};
if ev.level != LogLevel::Info {
continue;
}
if ev.msg.contains("early_drop") {
return;
}
}

unreachable!("test failed");
}

#[derive(Deserialize)]
struct ErrorResponsePayload {
msg: String,
Expand Down
Loading