Skip to content

Commit

Permalink
Add libdeno test
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkassimo committed Oct 11, 2018
1 parent 2b3a928 commit 93a9ee1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
13 changes: 13 additions & 0 deletions libdeno/libdeno_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,16 @@ TEST(LibDenoTest, DataBuf) {
EXPECT_EQ(data_buf_copy.data_ptr[1], 8);
deno_delete(d);
}

TEST(LibDenoTest, PromiseRejectCatchHandling) {
static int count = 0;
Deno* d = deno_new([](auto _, int req_id, auto buf, auto data_buf) {
// If no error, nothing should be sent, and count should not increment
count++;
});
EXPECT_TRUE(
deno_execute(d, nullptr, "a.js", "PromiseRejectCatchHandling()"));

EXPECT_EQ(count, 0);
deno_delete(d);
}
40 changes: 40 additions & 0 deletions libdeno/libdeno_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,43 @@ global.DataBuf = () => {
b[0] = 9;
b[1] = 8;
};

global.PromiseRejectCatchHandling = () => {
let count = 0;
let promiseRef = null;
// When we have an error, libdeno sends something
function assertOrSend(cond) {
if (!cond) {
libdeno.send(new Uint8Array([42]));
}
}
libdeno.setPromiseExaminer(() => {
assertOrSend(count === 2);
});
libdeno.setPromiseRejectHandler((error, event, promise) => {
count++;
if (event === libdeno.constants.promiseRejectEvents.kPromiseRejectWithNoHandler) {
assertOrSend(error instanceof Error);
assertOrSend(error.message === "message");
assertOrSend(count === 1);
promiseRef = promise;
} else if (event === libdeno.constants.promiseRejectEvents.kPromiseHandlerAddedAfterReject) {
assertOrSend(count === 2);
assertOrSend(promiseRef === promise);
}
// Should never reach 3!
assertOrSend(count !== 3);
});

async function fn() {
throw new Error("message");
}

(async () => {
try {
await fn();
} catch (e) {
assertOrSend(count === 2);
}
})();
}

0 comments on commit 93a9ee1

Please sign in to comment.