2019-12-05 15:30:20 -05:00
|
|
|
const filenameBase = "test_plugin";
|
|
|
|
|
|
|
|
let filenameSuffix = ".so";
|
|
|
|
let filenamePrefix = "lib";
|
|
|
|
|
2020-04-28 12:35:23 -04:00
|
|
|
if (Deno.build.os === "windows") {
|
2019-12-05 15:30:20 -05:00
|
|
|
filenameSuffix = ".dll";
|
|
|
|
filenamePrefix = "";
|
|
|
|
}
|
2020-04-28 12:35:23 -04:00
|
|
|
if (Deno.build.os === "darwin") {
|
2019-12-05 15:30:20 -05:00
|
|
|
filenameSuffix = ".dylib";
|
|
|
|
}
|
|
|
|
|
2020-01-09 13:37:01 -05:00
|
|
|
const filename = `../target/${Deno.args[0]}/${filenamePrefix}${filenameBase}${filenameSuffix}`;
|
2019-12-05 15:30:20 -05:00
|
|
|
|
2020-04-17 06:51:10 -04:00
|
|
|
// This will be checked against open resources after Plugin.close()
|
|
|
|
// in runTestClose() below.
|
|
|
|
const resourcesPre = Deno.resources();
|
|
|
|
|
2020-04-20 10:27:15 -04:00
|
|
|
const rid = Deno.openPlugin(filename);
|
2019-12-05 15:30:20 -05:00
|
|
|
|
2020-04-20 10:27:15 -04:00
|
|
|
const { testSync, testAsync } = Deno.core.ops();
|
|
|
|
if (!(testSync > 0)) {
|
|
|
|
throw "bad op id for testSync";
|
|
|
|
}
|
|
|
|
if (!(testAsync > 0)) {
|
|
|
|
throw "bad op id for testAsync";
|
|
|
|
}
|
2019-12-05 15:30:20 -05:00
|
|
|
|
|
|
|
const textDecoder = new TextDecoder();
|
|
|
|
|
|
|
|
function runTestSync() {
|
2020-04-20 10:27:15 -04:00
|
|
|
const response = Deno.core.dispatch(
|
|
|
|
testSync,
|
2019-12-05 15:30:20 -05:00
|
|
|
new Uint8Array([116, 101, 115, 116]),
|
2020-06-01 14:20:47 -04:00
|
|
|
new Uint8Array([116, 101, 115, 116]),
|
|
|
|
new Uint8Array([49, 50, 51]),
|
|
|
|
new Uint8Array([99, 98, 97])
|
2019-12-05 15:30:20 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
console.log(`Plugin Sync Response: ${textDecoder.decode(response)}`);
|
|
|
|
}
|
|
|
|
|
2020-04-20 10:27:15 -04:00
|
|
|
Deno.core.setAsyncHandler(testAsync, (response) => {
|
2019-12-05 15:30:20 -05:00
|
|
|
console.log(`Plugin Async Response: ${textDecoder.decode(response)}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
function runTestAsync() {
|
2020-04-20 10:27:15 -04:00
|
|
|
const response = Deno.core.dispatch(
|
|
|
|
testAsync,
|
2019-12-05 15:30:20 -05:00
|
|
|
new Uint8Array([116, 101, 115, 116]),
|
2020-06-01 14:20:47 -04:00
|
|
|
new Uint8Array([116, 101, 115, 116]),
|
|
|
|
new Uint8Array([49, 50, 51])
|
2019-12-05 15:30:20 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
if (response != null || response != undefined) {
|
|
|
|
throw new Error("Expected null response!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-07 15:09:09 -05:00
|
|
|
function runTestOpCount() {
|
|
|
|
const start = Deno.metrics();
|
|
|
|
|
2020-04-20 10:27:15 -04:00
|
|
|
Deno.core.dispatch(testSync, new Uint8Array([116, 101, 115, 116]));
|
2019-12-07 15:09:09 -05:00
|
|
|
|
|
|
|
const end = Deno.metrics();
|
|
|
|
|
2020-04-20 10:27:15 -04:00
|
|
|
if (end.opsCompleted - start.opsCompleted !== 1) {
|
2019-12-07 15:09:09 -05:00
|
|
|
// one op for the plugin and one for Deno.metrics
|
|
|
|
throw new Error("The opsCompleted metric is not correct!");
|
|
|
|
}
|
2020-04-20 10:27:15 -04:00
|
|
|
if (end.opsDispatched - start.opsDispatched !== 1) {
|
2019-12-07 15:09:09 -05:00
|
|
|
// one op for the plugin and one for Deno.metrics
|
|
|
|
throw new Error("The opsDispatched metric is not correct!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 06:51:10 -04:00
|
|
|
function runTestPluginClose() {
|
2020-04-20 10:27:15 -04:00
|
|
|
Deno.close(rid);
|
2020-04-17 06:51:10 -04:00
|
|
|
|
|
|
|
const resourcesPost = Deno.resources();
|
|
|
|
|
|
|
|
const preStr = JSON.stringify(resourcesPre, null, 2);
|
|
|
|
const postStr = JSON.stringify(resourcesPost, null, 2);
|
|
|
|
if (preStr !== postStr) {
|
2020-06-01 14:20:47 -04:00
|
|
|
throw new Error(
|
|
|
|
`Difference in open resources before openPlugin and after Plugin.close():
|
2020-04-17 06:51:10 -04:00
|
|
|
Before: ${preStr}
|
2020-06-01 14:20:47 -04:00
|
|
|
After: ${postStr}`
|
|
|
|
);
|
2020-04-17 06:51:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 15:30:20 -05:00
|
|
|
runTestSync();
|
|
|
|
runTestAsync();
|
2019-12-07 15:09:09 -05:00
|
|
|
|
|
|
|
runTestOpCount();
|
2020-04-17 06:51:10 -04:00
|
|
|
runTestPluginClose();
|