Skip to content

Commit e280233

Browse files
author
Guy Bedford
authored
refine task assertions, add test for task timeout (#894)
1 parent 392fa18 commit e280233

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

integration-tests/js-compute/fixtures/app/src/timers.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,30 @@ import { routes } from "./routes.js";
342342
}
343343
}))
344344
});
345+
routes.set('/setTimeout/fetch-timeout', async () => {
346+
let timedOut = false
347+
const first = fetch('https://httpbin.org/delay/1', {
348+
backend: 'httpbin'
349+
})
350+
const second = Promise.race([
351+
fetch('https://httpbin.org/delay/1', {
352+
backend: 'httpbin'
353+
}),
354+
new Promise(resolve => setTimeout(resolve, 5)).then(() => {
355+
timedOut = true
356+
return { status: 504, errors: 'timeout' }
357+
})
358+
])
359+
const firstValue = await first
360+
let error = assert(timedOut, true, 'should have timed out')
361+
if (error) { return error }
362+
error = assert(firstValue.status, 200, 'should get first value')
363+
if (error) { return error }
364+
const secondValue = await second
365+
error = assert(secondValue.status, 504, 'should get second value timeout')
366+
if (error) { return error }
367+
return pass()
368+
});
345369
}
346370

347371
// clearInterval

integration-tests/js-compute/fixtures/app/tests.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4866,6 +4866,16 @@
48664866
"body": ["START\nEND\n"]
48674867
}
48684868
},
4869+
"GET /setTimeout/fetch-timeout": {
4870+
"environments": ["viceroy", "compute"],
4871+
"downstream_request": {
4872+
"method": "GET",
4873+
"pathname": "/setTimeout/fetch-timeout"
4874+
},
4875+
"downstream_response": {
4876+
"status": 200
4877+
}
4878+
},
48694879
"GET /clearInterval/exposed-as-global": {
48704880
"environments": ["viceroy", "compute"],
48714881
"downstream_request": {

runtime/fastly/host-api/host_api.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ size_t api::AsyncTask::select(std::vector<api::AsyncTask *> &tasks) {
118118

119119
// When there are no async tasks, sleep until the deadline
120120
if (handles.size() == 0) {
121-
MOZ_ASSERT(soonest_deadline > 0);
121+
MOZ_ASSERT(soonest_deadline > now);
122122
sleep_until(soonest_deadline, now);
123123
return soonest_deadline_idx;
124124
}
@@ -127,13 +127,17 @@ size_t api::AsyncTask::select(std::vector<api::AsyncTask *> &tasks) {
127127
fastly::fastly_host_error err = 0;
128128

129129
while (true) {
130-
if (!convert_result(fastly::async_select(handles.data(), handles.size(),
131-
(soonest_deadline - now) / MILLISECS_IN_NANOSECS,
132-
&ret),
130+
MOZ_ASSERT(soonest_deadline == 0 || soonest_deadline > now);
131+
// timeout value of 0 means no timeout for async_select
132+
uint32_t timeout = soonest_deadline > 0 ? (soonest_deadline - now) / MILLISECS_IN_NANOSECS : 0;
133+
if (!convert_result(fastly::async_select(handles.data(), handles.size(), timeout, &ret),
133134
&err)) {
134-
if (host_api::error_is_optional_none(err)) {
135-
abort();
135+
if (host_api::error_is_bad_handle(err)) {
136+
fprintf(stderr, "Critical Error: An invalid handle was provided to async_select.\n");
137+
} else {
138+
fprintf(stderr, "Critical Error: An unknown error occurred in async_select.\n");
136139
}
140+
abort();
137141
}
138142

139143
// The result is only valid if the timeout didn't expire.
@@ -151,17 +155,19 @@ size_t api::AsyncTask::select(std::vector<api::AsyncTask *> &tasks) {
151155
}
152156
}
153157
abort();
154-
} else {
155-
// No value case means a timeout, which means soonest_deadline_idx is set.
156-
MOZ_ASSERT(soonest_deadline > 0);
158+
} else if (soonest_deadline > 0) {
159+
MOZ_ASSERT(soonest_deadline > now);
157160
MOZ_ASSERT(soonest_deadline_idx != -1);
158161
// Verify that the task definitely is ready from a time perspective, and if not loop the host
159162
// call again.
160163
now = MonotonicClock::now();
161164
if (soonest_deadline > now) {
165+
err = 0;
162166
continue;
163167
}
164168
return soonest_deadline_idx;
169+
} else {
170+
abort();
165171
}
166172
}
167173
}

0 commit comments

Comments
 (0)