Skip to content

Commit

Permalink
chore: add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nyannyacha committed Dec 23, 2024
1 parent cd0ff7d commit 8b340f9
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
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

0 comments on commit 8b340f9

Please sign in to comment.