1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-29 02:29:06 -05:00
denoland-deno/js/trace_test.ts

83 lines
3 KiB
TypeScript
Raw Normal View History

2018-10-19 15:25:29 -04:00
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
2018-09-22 03:59:26 -04:00
import { testPerm, assertEqual } from "./test_util.ts";
import * as deno from "deno";
testPerm({ write: true }, async function traceFunctionSuccess() {
const op = await deno.trace(async () => {
const enc = new TextEncoder();
const data = enc.encode("Hello");
// Mixing sync and async calls
const filename = deno.makeTempDirSync() + "/test.txt";
await deno.writeFile(filename, data, 0o666);
2018-09-25 10:40:37 -04:00
deno.removeSync(filename);
2018-09-22 03:59:26 -04:00
});
assertEqual(op.length, 3);
assertEqual(op[0], { sync: true, name: "MakeTempDir" });
assertEqual(op[1], { sync: false, name: "WriteFile" });
assertEqual(op[2], { sync: true, name: "Remove" });
});
testPerm({ write: true }, async function tracePromiseSuccess() {
// Ensure we don't miss any send actions
// (new Promise(fn), fn runs synchronously)
const asyncFunction = async () => {
const enc = new TextEncoder();
const data = enc.encode("Hello");
// Mixing sync and async calls
const filename = deno.makeTempDirSync() + "/test.txt";
await deno.writeFile(filename, data, 0o666);
2018-09-25 10:40:37 -04:00
deno.removeSync(filename);
2018-09-22 03:59:26 -04:00
};
const promise = Promise.resolve().then(asyncFunction);
const op = await deno.trace(promise);
assertEqual(op.length, 3);
assertEqual(op[0], { sync: true, name: "MakeTempDir" });
assertEqual(op[1], { sync: false, name: "WriteFile" });
assertEqual(op[2], { sync: true, name: "Remove" });
});
testPerm({ write: true }, async function traceRepeatSuccess() {
const op1 = await deno.trace(async () => await deno.makeTempDir());
assertEqual(op1.length, 1);
assertEqual(op1[0], { sync: false, name: "MakeTempDir" });
2018-09-25 10:40:37 -04:00
const op2 = await deno.trace(() => deno.statSync("."));
2018-09-22 03:59:26 -04:00
assertEqual(op2.length, 1);
assertEqual(op2[0], { sync: true, name: "Stat" });
});
testPerm({ write: true }, async function traceIdempotence() {
let op1, op2, op3;
op1 = await deno.trace(async () => {
const filename = (await deno.makeTempDir()) + "/test.txt";
op2 = await deno.trace(async () => {
const enc = new TextEncoder();
const data = enc.encode("Hello");
deno.writeFileSync(filename, data, 0o666);
op3 = await deno.trace(async () => {
await deno.remove(filename);
});
await deno.makeTempDir();
});
});
// Flatten the calls
assertEqual(op1.length, 4);
assertEqual(op1[0], { sync: false, name: "MakeTempDir" });
assertEqual(op1[1], { sync: true, name: "WriteFile" });
assertEqual(op1[2], { sync: false, name: "Remove" });
assertEqual(op1[3], { sync: false, name: "MakeTempDir" });
assertEqual(op2.length, 3);
assertEqual(op2[0], { sync: true, name: "WriteFile" });
assertEqual(op2[1], { sync: false, name: "Remove" });
assertEqual(op2[2], { sync: false, name: "MakeTempDir" });
assertEqual(op3.length, 1);
assertEqual(op3[0], { sync: false, name: "Remove" });
// Expect top-level repeat still works after all the nestings
2018-09-25 10:40:37 -04:00
const op4 = await deno.trace(() => deno.statSync("."));
2018-09-22 03:59:26 -04:00
assertEqual(op4.length, 1);
assertEqual(op4[0], { sync: true, name: "Stat" });
});