2019-01-22 04:03:30 +09:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-01-12 23:16:18 +09:00
|
|
|
import { test, testPerm, assertEqual } from "./test_util.ts";
|
2018-10-30 20:58:55 +01:00
|
|
|
|
|
|
|
test(function resourcesStdio() {
|
2019-02-13 02:08:56 +11:00
|
|
|
const res = Deno.resources();
|
2018-10-30 20:58:55 +01:00
|
|
|
|
|
|
|
assertEqual(res[0], "stdin");
|
|
|
|
assertEqual(res[1], "stdout");
|
|
|
|
assertEqual(res[2], "stderr");
|
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ net: true }, async function resourcesNet() {
|
|
|
|
const addr = "127.0.0.1:4501";
|
2019-02-13 02:08:56 +11:00
|
|
|
const listener = Deno.listen("tcp", addr);
|
2018-10-30 20:58:55 +01:00
|
|
|
|
2019-02-13 02:08:56 +11:00
|
|
|
const dialerConn = await Deno.dial("tcp", addr);
|
2018-10-30 20:58:55 +01:00
|
|
|
const listenerConn = await listener.accept();
|
|
|
|
|
2019-02-13 02:08:56 +11:00
|
|
|
const res = Deno.resources();
|
2018-10-30 20:58:55 +01:00
|
|
|
assertEqual(Object.values(res).filter(r => r === "tcpListener").length, 1);
|
|
|
|
assertEqual(Object.values(res).filter(r => r === "tcpStream").length, 2);
|
|
|
|
|
|
|
|
listenerConn.close();
|
|
|
|
dialerConn.close();
|
|
|
|
listener.close();
|
|
|
|
});
|
|
|
|
|
2019-02-08 23:59:38 +03:00
|
|
|
testPerm({ read: true }, async function resourcesFile() {
|
2019-02-13 02:08:56 +11:00
|
|
|
const resourcesBefore = Deno.resources();
|
|
|
|
await Deno.open("tests/hello.txt");
|
|
|
|
const resourcesAfter = Deno.resources();
|
2018-10-30 20:58:55 +01:00
|
|
|
|
|
|
|
// check that exactly one new resource (file) was added
|
|
|
|
assertEqual(
|
|
|
|
Object.keys(resourcesAfter).length,
|
|
|
|
Object.keys(resourcesBefore).length + 1
|
|
|
|
);
|
|
|
|
const newRid = Object.keys(resourcesAfter).find(rid => {
|
|
|
|
return !resourcesBefore.hasOwnProperty(rid);
|
|
|
|
});
|
|
|
|
assertEqual(resourcesAfter[newRid], "fsFile");
|
|
|
|
});
|