Skip to content

Commit

Permalink
chore: added an integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
laktek committed Sep 12, 2023
1 parent 010ecd5 commit d06174e
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
58 changes: 58 additions & 0 deletions crates/base/test_cases/main_with_abort/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { serve } from 'https://deno.land/[email protected]/http/server.ts';

console.log('main function started');

serve(async (req: Request) => {
const url = new URL(req.url);
const { pathname } = url;
const path_parts = pathname.split('/');
const service_name = path_parts[1];

if (!service_name || service_name === '') {
const error = { msg: 'missing function name in request' };
return new Response(
JSON.stringify(error),
{ status: 400, headers: { 'Content-Type': 'application/json' } },
);
}

const servicePath = `./test_cases/${service_name}`;
console.error(`serving the request with ${servicePath}`);

const createWorker = async () => {
const memoryLimitMb = 150;
const workerTimeoutMs = 1 * 60 * 1000;
const noModuleCache = false;
const importMapPath = null;
const envVarsObj = Deno.env.toObject();
const envVars = Object.keys(envVarsObj).map((k) => [k, envVarsObj[k]]);

return await EdgeRuntime.userWorkers.create({
servicePath,
memoryLimitMb,
workerTimeoutMs,
noModuleCache,
importMapPath,
envVars,
});
};

const callWorker = async () => {
try {
const worker = await createWorker();
const controller = new AbortController();
const signal = controller.signal;
controller.abort();
return await worker.fetch(req, { signal });
} catch (e) {
console.error(e);
const error = { msg: e.toString() };
return new Response(
JSON.stringify(error),
{ status: 500, headers: { 'Content-Type': 'application/json' } },
);
}
};

return callWorker();
});
49 changes: 49 additions & 0 deletions crates/base/tests/main_worker_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,55 @@ async fn test_main_worker_boot_error() {
assert_eq!(result.unwrap_err().to_string(), "worker boot error");
}

#[tokio::test]
async fn test_main_worker_abort_request() {
// create a user worker pool
let user_worker_msgs_tx = create_user_worker_pool(None).await.unwrap();
let opts = WorkerContextInitOpts {
service_path: "./test_cases/main_with_abort".into(),
no_module_cache: false,
import_map_path: None,
env_vars: HashMap::new(),
events_rx: None,
maybe_eszip: None,
maybe_entrypoint: None,
maybe_module_code: None,
conf: WorkerRuntimeOpts::MainWorker(MainWorkerRuntimeOpts {
worker_pool_tx: user_worker_msgs_tx,
}),
};
let worker_req_tx = create_worker(opts).await.unwrap();
let (res_tx, res_rx) = oneshot::channel::<Result<Response<Body>, hyper::Error>>();

let body_chunk = "{ \"name\": \"bar\"}";

let content_length = &body_chunk.len();
let chunks: Vec<Result<_, std::io::Error>> = vec![Ok(body_chunk)];
let stream = futures_util::stream::iter(chunks);
let body = Body::wrap_stream(stream);

let req = Request::builder()
.uri("/std_user_worker")
.method("POST")
.header("Content-Type", "application/json")
.header("Content-Length", content_length.to_string())
.body(body)
.unwrap();

let msg = WorkerRequestMsg { req, res_tx };
let _ = worker_req_tx.send(msg);

let res = res_rx.await.unwrap().unwrap();
assert!(res.status().as_u16() == 500);

let body_bytes = hyper::body::to_bytes(res.into_body()).await.unwrap();

assert_eq!(
body_bytes,
"{\"msg\":\"AbortError: The signal has been aborted\"}"
);
}

//#[tokio::test]
//async fn test_main_worker_user_worker_mod_evaluate_exception() {
// // create a user worker pool
Expand Down

0 comments on commit d06174e

Please sign in to comment.