From 8f8beafe32c783158c98c4f9f9b86ae23e12b9bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 30 Oct 2018 11:07:02 +0100 Subject: [PATCH 01/15] initial pass at resources op --- BUILD.gn | 1 + js/deno.ts | 1 + js/resources.ts | 41 +++++++++++++++++++++++++++++++++++++++++ src/msg.fbs | 13 +++++++++++++ src/ops.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/resources.rs | 11 +++++++++++ 6 files changed, 115 insertions(+) create mode 100644 js/resources.ts diff --git a/BUILD.gn b/BUILD.gn index 68cb2c6db7b899..e81ac64ddd1a78 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -102,6 +102,7 @@ ts_sources = [ "js/read_link.ts", "js/remove.ts", "js/rename.ts", + "js/resources.ts", "js/stat.ts", "js/symlink.ts", "js/text_encoding.ts", diff --git a/js/deno.ts b/js/deno.ts index 8a6b627b6b25ba..346997d4dcceec 100644 --- a/js/deno.ts +++ b/js/deno.ts @@ -38,6 +38,7 @@ export { truncateSync, truncate } from "./truncate"; export { FileInfo } from "./file_info"; export { connect, dial, listen, Listener, Conn } from "./net"; export { metrics } from "./metrics"; +export { resources } from "./resources"; export const args: string[] = []; // Provide the compiler API in an obfuscated way diff --git a/js/resources.ts b/js/resources.ts new file mode 100644 index 00000000000000..c1ec9fda771587 --- /dev/null +++ b/js/resources.ts @@ -0,0 +1,41 @@ +// Copyright 2018 the Deno authors. All rights reserved. MIT license. +import * as msg from "gen/msg_generated"; +import * as flatbuffers from "./flatbuffers"; +import { assert } from "./util"; +import * as dispatch from "./dispatch"; + +interface Resource { + rid: number; + repr: string; +} + +export function resources(): Resource[] { + return res(dispatch.sendSync(...req())); +} + +function req(): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] { + const builder = flatbuffers.createBuilder(); + msg.Resources.startResources(builder); + const inner = msg.Resource.endResource(builder); + return [builder, msg.Any.Resources, inner]; +} + +function res(baseRes: null | msg.Base): Resource[] { + assert(baseRes !== null); + assert(msg.Any.ResourcesRes === baseRes!.innerType()); + const res = new msg.ResourcesRes(); + assert(baseRes!.inner(res) !== null); + + const resources: Resource[] = []; + + for (let i = 0; i < res.resourcesLength(); i++) { + const item = res.resources(i)!; + + resources.push({ + rid: item.rid()!, + repr: item.repr()!, + }); + } + + return resources; +} diff --git a/src/msg.fbs b/src/msg.fbs index e7ebe66842f659..ba7031621c26ea 100644 --- a/src/msg.fbs +++ b/src/msg.fbs @@ -24,6 +24,8 @@ union Any { Rename, Readlink, ReadlinkRes, + Resources, + ResourcesRes, Symlink, Stat, StatRes, @@ -270,6 +272,17 @@ table ReadlinkRes { path: string; } +table Resources {} + +table Resource { + rid: int; + repr: string; +} + +table ResourcesRes { + resources: [Resource]; +} + table Symlink { oldname: string; newname: string; diff --git a/src/ops.rs b/src/ops.rs index 95522fb6b8305a..489caa1ac9feee 100644 --- a/src/ops.rs +++ b/src/ops.rs @@ -19,6 +19,7 @@ use futures::Poll; use hyper; use hyper::rt::{Future, Stream}; use remove_dir_all::remove_dir_all; +use resources::get_resource_table_entries; use std; use std::fs; use std::net::{Shutdown, SocketAddr}; @@ -94,6 +95,7 @@ pub fn dispatch( msg::Any::Read => op_read, msg::Any::Remove => op_remove, msg::Any::Rename => op_rename, + msg::Any::Resources => op_resources, msg::Any::SetEnv => op_set_env, msg::Any::Shutdown => op_shutdown, msg::Any::Start => op_start, @@ -1288,3 +1290,49 @@ fn op_metrics( }, )) } + +fn op_resources( + _state: Arc, + base: &msg::Base, + data: &'static mut [u8], +) -> Box { + assert_eq!(data.len(), 0); + let cmd_id = base.cmd_id(); + + let builder = &mut FlatBufferBuilder::new(); + let serialized_resources = get_resource_table_entries(); + + let res: Vec<_> = serialized_resources.iter() + .map(|key| { + // TODO: use actual repr of resource + let value = builder.create_string("foobar"); + + msg::Resource::create( + builder, + &msg::ResourceArgs { + rid: key.clone(), + repr: Some(value), + ..Default::default() + }, + ) + }).collect(); + + let resources = builder.create_vector(&res); + let inner = msg::ResourcesRes::create( + builder, + &msg::ResourcesResArgs { + resources: Some(resources), + ..Default::default() + }, + ); + + ok_future(serialize_response( + cmd_id, + builder, + msg::BaseArgs { + inner: Some(inner.as_union_value()), + inner_type: msg::Any::ResourcesRes, + ..Default::default() + }, + )) +} \ No newline at end of file diff --git a/src/resources.rs b/src/resources.rs index a959d5c556f908..18458001b19142 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -58,6 +58,17 @@ enum Repr { TcpStream(tokio::net::TcpStream), } +pub fn get_resource_table_entries() -> Vec { + let table = RESOURCE_TABLE.lock().unwrap(); + + let serialized_resources: Vec = table.iter() + .map(|(key, _value)| { + key.clone() + }).collect(); + + serialized_resources +} + // Abstract async file interface. // Ideally in unix, if Resource represents an OS rid, it will be the same. #[derive(Debug)] From 8dfb0846ffa1dcee4efe3792bac2993ea6700239 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 30 Oct 2018 11:28:27 +0100 Subject: [PATCH 02/15] return text repr of Resources --- src/ops.rs | 7 +++---- src/resources.rs | 24 +++++++++++++++++++----- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/ops.rs b/src/ops.rs index 489caa1ac9feee..1a45b4f29956be 100644 --- a/src/ops.rs +++ b/src/ops.rs @@ -1303,15 +1303,14 @@ fn op_resources( let serialized_resources = get_resource_table_entries(); let res: Vec<_> = serialized_resources.iter() - .map(|key| { - // TODO: use actual repr of resource - let value = builder.create_string("foobar"); + .map(|(key, value)| { + let s_value = builder.create_string(value); msg::Resource::create( builder, &msg::ResourceArgs { rid: key.clone(), - repr: Some(value), + repr: Some(s_value), ..Default::default() }, ) diff --git a/src/resources.rs b/src/resources.rs index 18458001b19142..e3dd3f431125c6 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -58,17 +58,31 @@ enum Repr { TcpStream(tokio::net::TcpStream), } -pub fn get_resource_table_entries() -> Vec { +pub fn get_resource_table_entries() -> HashMap { let table = RESOURCE_TABLE.lock().unwrap(); - let serialized_resources: Vec = table.iter() - .map(|(key, _value)| { - key.clone() + let tuples = table.iter() + .map(|(key, value)| { + (key.clone(), inspect_repr(&value)) }).collect(); - serialized_resources + tuples } +fn inspect_repr(repr: &Repr) -> String { + let h_repr = match repr { + Repr::Stdin(_) => "stdin", + Repr::Stdout(_) => "stdout", + Repr::Stderr(_) => "stderr", + Repr::FsFile(_) => "fsfile", + Repr::TcpListener(_) => "tcpListener", + Repr::TcpStream(_) => "tcpStream", + }; + + String::from(h_repr) +} + + // Abstract async file interface. // Ideally in unix, if Resource represents an OS rid, it will be the same. #[derive(Debug)] From 7447d3a5274797a0ac4f2527aab428b46a718fe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 30 Oct 2018 11:34:20 +0100 Subject: [PATCH 03/15] use Vector instead of HashMap --- src/ops.rs | 4 ++-- src/resources.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ops.rs b/src/ops.rs index 1a45b4f29956be..002ec934568e14 100644 --- a/src/ops.rs +++ b/src/ops.rs @@ -1304,13 +1304,13 @@ fn op_resources( let res: Vec<_> = serialized_resources.iter() .map(|(key, value)| { - let s_value = builder.create_string(value); + let repr = builder.create_string(value); msg::Resource::create( builder, &msg::ResourceArgs { rid: key.clone(), - repr: Some(s_value), + repr: Some(repr), ..Default::default() }, ) diff --git a/src/resources.rs b/src/resources.rs index e3dd3f431125c6..f32729738c9dbc 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -58,7 +58,7 @@ enum Repr { TcpStream(tokio::net::TcpStream), } -pub fn get_resource_table_entries() -> HashMap { +pub fn get_resource_table_entries() -> Vec<(i32, String)> { let table = RESOURCE_TABLE.lock().unwrap(); let tuples = table.iter() From 0e68a90fbada722069e22744718ee36409740d36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 30 Oct 2018 11:34:49 +0100 Subject: [PATCH 04/15] lint & format --- js/resources.ts | 4 ++-- src/ops.rs | 5 +++-- src/resources.rs | 25 ++++++++++++------------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/js/resources.ts b/js/resources.ts index c1ec9fda771587..f67017b9c123fe 100644 --- a/js/resources.ts +++ b/js/resources.ts @@ -32,8 +32,8 @@ function res(baseRes: null | msg.Base): Resource[] { const item = res.resources(i)!; resources.push({ - rid: item.rid()!, - repr: item.repr()!, + rid: item.rid()!, + repr: item.repr()! }); } diff --git a/src/ops.rs b/src/ops.rs index 002ec934568e14..ca8d20557b4ef4 100644 --- a/src/ops.rs +++ b/src/ops.rs @@ -1302,7 +1302,8 @@ fn op_resources( let builder = &mut FlatBufferBuilder::new(); let serialized_resources = get_resource_table_entries(); - let res: Vec<_> = serialized_resources.iter() + let res: Vec<_> = serialized_resources + .iter() .map(|(key, value)| { let repr = builder.create_string(value); @@ -1334,4 +1335,4 @@ fn op_resources( ..Default::default() }, )) -} \ No newline at end of file +} diff --git a/src/resources.rs b/src/resources.rs index f32729738c9dbc..60466b0207877a 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -61,28 +61,27 @@ enum Repr { pub fn get_resource_table_entries() -> Vec<(i32, String)> { let table = RESOURCE_TABLE.lock().unwrap(); - let tuples = table.iter() - .map(|(key, value)| { - (key.clone(), inspect_repr(&value)) - }).collect(); + let tuples = table + .iter() + .map(|(key, value)| (key.clone(), inspect_repr(&value))) + .collect(); tuples } fn inspect_repr(repr: &Repr) -> String { - let h_repr = match repr { - Repr::Stdin(_) => "stdin", - Repr::Stdout(_) => "stdout", - Repr::Stderr(_) => "stderr", - Repr::FsFile(_) => "fsfile", - Repr::TcpListener(_) => "tcpListener", - Repr::TcpStream(_) => "tcpStream", - }; + let h_repr = match repr { + Repr::Stdin(_) => "stdin", + Repr::Stdout(_) => "stdout", + Repr::Stderr(_) => "stderr", + Repr::FsFile(_) => "fsfile", + Repr::TcpListener(_) => "tcpListener", + Repr::TcpStream(_) => "tcpStream", + }; String::from(h_repr) } - // Abstract async file interface. // Ideally in unix, if Resource represents an OS rid, it will be the same. #[derive(Debug)] From 5351b4de05ccd2239ac39611ff20f59cf0318642 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 30 Oct 2018 11:54:06 +0100 Subject: [PATCH 05/15] add some tests --- js/resources_test.ts | 32 ++++++++++++++++++++++++++++++++ js/unit_tests.ts | 1 + 2 files changed, 33 insertions(+) create mode 100644 js/resources_test.ts diff --git a/js/resources_test.ts b/js/resources_test.ts new file mode 100644 index 00000000000000..d0e564669be641 --- /dev/null +++ b/js/resources_test.ts @@ -0,0 +1,32 @@ +// Copyright 2018 the Deno authors. All rights reserved. MIT license. +import { test, testPerm, assert, assertEqual } from "./test_util.ts"; +import * as deno from "deno"; + +testPerm({ net: true }, async function resources() { + const res = deno.resources(); + console.log('resources', res) + // there should be only stdio + assertEqual(res.length, 3); + + const stdio = [{rid: 0, repr: "stdin"}, {rid: 1, repr: "stdout"}, {rid: 2, repr: "stderr"}]; + + stdio.forEach((stdioItem) => { + const found = res.find(resItem => resItem.rid === stdioItem.rid && resItem.repr === stdioItem.repr); + assert(!!found); + }); + + const addr = "127.0.0.1:4500"; + const listener = deno.listen("tcp", addr); + listener.accept().then(async conn => { + // TODO: this is failing, we have 6 resources open intead of 4 + const res1 = deno.resources(); + console.log('resources', res1) + // there should be only stdio + assertEqual(res1.length, 4); + conn.close(); + }); + + const conn = await deno.dial("tcp", addr); + const buf = new Uint8Array(1024); + await conn.read(buf); +}); diff --git a/js/unit_tests.ts b/js/unit_tests.ts index af7b421e617670..22e5fbdc04bce0 100644 --- a/js/unit_tests.ts +++ b/js/unit_tests.ts @@ -22,6 +22,7 @@ import "./read_dir_test.ts"; import "./read_file_test.ts"; import "./read_link_test.ts"; import "./rename_test.ts"; +import "./resources_test.ts"; import "./stat_test.ts"; import "./symlink_test.ts"; import "./text_encoding_test.ts"; From 91b0b93ae22a084cfb8a39b8cc63c1afb62cf959 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 30 Oct 2018 14:56:29 +0100 Subject: [PATCH 06/15] update tests --- js/resources.ts | 2 +- js/resources_test.ts | 37 +++++++++++++++++++++++-------------- src/resources.rs | 2 +- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/js/resources.ts b/js/resources.ts index f67017b9c123fe..9f55ac54d905d7 100644 --- a/js/resources.ts +++ b/js/resources.ts @@ -37,5 +37,5 @@ function res(baseRes: null | msg.Base): Resource[] { }); } - return resources; + return resources.sort((a, b) => a.rid - b.rid); } diff --git a/js/resources_test.ts b/js/resources_test.ts index d0e564669be641..1bf89c238f32c0 100644 --- a/js/resources_test.ts +++ b/js/resources_test.ts @@ -2,27 +2,30 @@ import { test, testPerm, assert, assertEqual } from "./test_util.ts"; import * as deno from "deno"; -testPerm({ net: true }, async function resources() { +test(function stdioResources() { const res = deno.resources(); - console.log('resources', res) - // there should be only stdio - assertEqual(res.length, 3); - const stdio = [{rid: 0, repr: "stdin"}, {rid: 1, repr: "stdout"}, {rid: 2, repr: "stderr"}]; + assertEqual(res[0].rid, 0); + assertEqual(res[0].repr, "stdin"); - stdio.forEach((stdioItem) => { - const found = res.find(resItem => resItem.rid === stdioItem.rid && resItem.repr === stdioItem.repr); - assert(!!found); - }); + assertEqual(res[1].rid, 1); + assertEqual(res[1].repr, "stdout"); + + assertEqual(res[2].rid, 2); + assertEqual(res[2].repr, "stderr"); +}); +testPerm({ net: true }, async function netResources() { const addr = "127.0.0.1:4500"; const listener = deno.listen("tcp", addr); + listener.accept().then(async conn => { - // TODO: this is failing, we have 6 resources open intead of 4 - const res1 = deno.resources(); - console.log('resources', res1) - // there should be only stdio - assertEqual(res1.length, 4); + const res = deno.resources(); + // besides 3 stdio resources, we should have additional 3 from listen(), accept() and dial() + assertEqual(res.length, 6); + assertEqual(res.filter(r => r.repr === "tcpListener").length, 1); + assertEqual(res.filter(r => r.repr === "tcpStream").length, 2); + conn.close(); }); @@ -30,3 +33,9 @@ testPerm({ net: true }, async function resources() { const buf = new Uint8Array(1024); await conn.read(buf); }); + +test(function fileResources() { + deno.readFileSync("package.json"); + const res = deno.resources(); + assertEqual(res.filter(r => r.repr === "fsFile").length, 1); +}); diff --git a/src/resources.rs b/src/resources.rs index 60466b0207877a..28c412d5b0e65c 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -74,7 +74,7 @@ fn inspect_repr(repr: &Repr) -> String { Repr::Stdin(_) => "stdin", Repr::Stdout(_) => "stdout", Repr::Stderr(_) => "stderr", - Repr::FsFile(_) => "fsfile", + Repr::FsFile(_) => "fsFile", Repr::TcpListener(_) => "tcpListener", Repr::TcpStream(_) => "tcpStream", }; From f397af6bfb89059458a65af7a1156fd9b9deaf01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 30 Oct 2018 15:05:54 +0100 Subject: [PATCH 07/15] close connections in tests --- js/resources_test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/js/resources_test.ts b/js/resources_test.ts index 1bf89c238f32c0..87037b0e3e7bfa 100644 --- a/js/resources_test.ts +++ b/js/resources_test.ts @@ -27,11 +27,13 @@ testPerm({ net: true }, async function netResources() { assertEqual(res.filter(r => r.repr === "tcpStream").length, 2); conn.close(); + listener.close(); }); const conn = await deno.dial("tcp", addr); const buf = new Uint8Array(1024); await conn.read(buf); + conn.close(); }); test(function fileResources() { From ce935be93e971abb0f505b64214b702ff6c67326 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 30 Oct 2018 16:04:17 +0100 Subject: [PATCH 08/15] update resources and tests --- js/resources.ts | 26 +++++--------------------- js/resources_test.ts | 23 ++++++++++------------- 2 files changed, 15 insertions(+), 34 deletions(-) diff --git a/js/resources.ts b/js/resources.ts index 9f55ac54d905d7..a28270fca6f22b 100644 --- a/js/resources.ts +++ b/js/resources.ts @@ -4,38 +4,22 @@ import * as flatbuffers from "./flatbuffers"; import { assert } from "./util"; import * as dispatch from "./dispatch"; -interface Resource { - rid: number; - repr: string; -} - -export function resources(): Resource[] { - return res(dispatch.sendSync(...req())); -} - -function req(): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] { +export function resources(): { [key: number]: string } { const builder = flatbuffers.createBuilder(); msg.Resources.startResources(builder); const inner = msg.Resource.endResource(builder); - return [builder, msg.Any.Resources, inner]; -} - -function res(baseRes: null | msg.Base): Resource[] { + const baseRes = dispatch.sendSync(builder, msg.Any.Resources, inner); assert(baseRes !== null); assert(msg.Any.ResourcesRes === baseRes!.innerType()); const res = new msg.ResourcesRes(); assert(baseRes!.inner(res) !== null); - const resources: Resource[] = []; + const resources: { [key: number]: string } = {}; for (let i = 0; i < res.resourcesLength(); i++) { const item = res.resources(i)!; - - resources.push({ - rid: item.rid()!, - repr: item.repr()! - }); + resources[item.rid()!] = item.repr()!; } - return resources.sort((a, b) => a.rid - b.rid); + return resources; } diff --git a/js/resources_test.ts b/js/resources_test.ts index 87037b0e3e7bfa..3012a0650785ba 100644 --- a/js/resources_test.ts +++ b/js/resources_test.ts @@ -5,14 +5,10 @@ import * as deno from "deno"; test(function stdioResources() { const res = deno.resources(); - assertEqual(res[0].rid, 0); - assertEqual(res[0].repr, "stdin"); - - assertEqual(res[1].rid, 1); - assertEqual(res[1].repr, "stdout"); - - assertEqual(res[2].rid, 2); - assertEqual(res[2].repr, "stderr"); + assertEqual(Object.keys(res).length, 3); + assertEqual(res[0], "stdin"); + assertEqual(res[1], "stdout"); + assertEqual(res[2], "stderr"); }); testPerm({ net: true }, async function netResources() { @@ -22,9 +18,9 @@ testPerm({ net: true }, async function netResources() { listener.accept().then(async conn => { const res = deno.resources(); // besides 3 stdio resources, we should have additional 3 from listen(), accept() and dial() - assertEqual(res.length, 6); - assertEqual(res.filter(r => r.repr === "tcpListener").length, 1); - assertEqual(res.filter(r => r.repr === "tcpStream").length, 2); + assertEqual(Object.keys(res).length, 6); + assertEqual(Object.values(res).filter(r => r === "tcpListener").length, 1); + assertEqual(Object.values(res).filter(r => r === "tcpStream").length, 2); conn.close(); listener.close(); @@ -37,7 +33,8 @@ testPerm({ net: true }, async function netResources() { }); test(function fileResources() { - deno.readFileSync("package.json"); + deno.open("package.json"); const res = deno.resources(); - assertEqual(res.filter(r => r.repr === "fsFile").length, 1); + console.log(res); + assertEqual(Object.values(res).filter(r => r === "fsFile").length, 1); }); From 21aa0aa99f1aee0cde7f678fb85ba884763113de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 30 Oct 2018 16:32:40 +0100 Subject: [PATCH 09/15] update fileResource tests --- js/resources_test.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/js/resources_test.ts b/js/resources_test.ts index 3012a0650785ba..955643427d48ce 100644 --- a/js/resources_test.ts +++ b/js/resources_test.ts @@ -5,7 +5,6 @@ import * as deno from "deno"; test(function stdioResources() { const res = deno.resources(); - assertEqual(Object.keys(res).length, 3); assertEqual(res[0], "stdin"); assertEqual(res[1], "stdout"); assertEqual(res[2], "stderr"); @@ -33,8 +32,17 @@ testPerm({ net: true }, async function netResources() { }); test(function fileResources() { + const resourcesBefore = deno.resources(); deno.open("package.json"); - const res = deno.resources(); - console.log(res); - assertEqual(Object.values(res).filter(r => r === "fsFile").length, 1); + const resourcesAfter = deno.resources(); + + // 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(key => { + return !resourcesBefore.hasOwnProperty(key); + }); + assertEqual(resourcesAfter[newRid], "fsFile"); }); From c9c86499070a9eeab1605be6afef269355de9982 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 30 Oct 2018 18:29:12 +0100 Subject: [PATCH 10/15] import resource_test.ts --- js/resources_test.ts | 12 ++++++------ tests/hello.txt | 1 + 2 files changed, 7 insertions(+), 6 deletions(-) create mode 100644 tests/hello.txt diff --git a/js/resources_test.ts b/js/resources_test.ts index 955643427d48ce..b5faf24d83d8f4 100644 --- a/js/resources_test.ts +++ b/js/resources_test.ts @@ -2,7 +2,7 @@ import { test, testPerm, assert, assertEqual } from "./test_util.ts"; import * as deno from "deno"; -test(function stdioResources() { +test(function resourcesStdio() { const res = deno.resources(); assertEqual(res[0], "stdin"); @@ -10,7 +10,7 @@ test(function stdioResources() { assertEqual(res[2], "stderr"); }); -testPerm({ net: true }, async function netResources() { +testPerm({ net: true }, async function resourcesNet() { const addr = "127.0.0.1:4500"; const listener = deno.listen("tcp", addr); @@ -31,9 +31,9 @@ testPerm({ net: true }, async function netResources() { conn.close(); }); -test(function fileResources() { +test(async function resourcesFile() { const resourcesBefore = deno.resources(); - deno.open("package.json"); + await deno.open("tests/hello.txt"); const resourcesAfter = deno.resources(); // check that exactly one new resource (file) was added @@ -41,8 +41,8 @@ test(function fileResources() { Object.keys(resourcesAfter).length, Object.keys(resourcesBefore).length + 1 ); - const newRid = Object.keys(resourcesAfter).find(key => { - return !resourcesBefore.hasOwnProperty(key); + const newRid = Object.keys(resourcesAfter).find(rid => { + return !resourcesBefore.hasOwnProperty(rid); }); assertEqual(resourcesAfter[newRid], "fsFile"); }); diff --git a/tests/hello.txt b/tests/hello.txt new file mode 100644 index 00000000000000..6769dd60bdf536 --- /dev/null +++ b/tests/hello.txt @@ -0,0 +1 @@ +Hello world! \ No newline at end of file From bea2c198d41406a2b14fe391a979357ae7091962 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 30 Oct 2018 18:49:12 +0100 Subject: [PATCH 11/15] add test for resources.rs --- src/resources.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/resources.rs b/src/resources.rs index 28c412d5b0e65c..de11f55a66a3be 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -16,6 +16,7 @@ use tokio_write; use futures; use futures::future::{Either, FutureResult}; +use futures::Future; use futures::Poll; use std; use std::collections::HashMap; @@ -69,6 +70,12 @@ pub fn get_resource_table_entries() -> Vec<(i32, String)> { tuples } +#[test] +fn test_get_resource_table_entries() { + assert_eq!(get_resource_table_entries().len(), 3); + // TODO: add asserts for add_fs_file, add_tcp_listener, add_tcp_stream +} + fn inspect_repr(repr: &Repr) -> String { let h_repr = match repr { Repr::Stdin(_) => "stdin", From 95a38f866a5b0381eee00364e86224c434fe1b55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 30 Oct 2018 19:02:47 +0100 Subject: [PATCH 12/15] remove unused import --- src/resources.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/resources.rs b/src/resources.rs index de11f55a66a3be..3ab616e12e04ee 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -16,7 +16,6 @@ use tokio_write; use futures; use futures::future::{Either, FutureResult}; -use futures::Future; use futures::Poll; use std; use std::collections::HashMap; From f07d25b7343085b9ba2909681095c3b8ab2f395a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 30 Oct 2018 19:24:56 +0100 Subject: [PATCH 13/15] change port in resourcesNet test --- js/resources_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/resources_test.ts b/js/resources_test.ts index b5faf24d83d8f4..ba63519ad30c49 100644 --- a/js/resources_test.ts +++ b/js/resources_test.ts @@ -11,7 +11,7 @@ test(function resourcesStdio() { }); testPerm({ net: true }, async function resourcesNet() { - const addr = "127.0.0.1:4500"; + const addr = "127.0.0.1:4501"; const listener = deno.listen("tcp", addr); listener.accept().then(async conn => { From f1bfc51fb95d1076b41979e6d7dc832e7a5e789b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 30 Oct 2018 19:50:56 +0100 Subject: [PATCH 14/15] update resources_test.ts --- js/resources_test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/js/resources_test.ts b/js/resources_test.ts index ba63519ad30c49..9bb8b54cc2fa3d 100644 --- a/js/resources_test.ts +++ b/js/resources_test.ts @@ -13,6 +13,7 @@ test(function resourcesStdio() { testPerm({ net: true }, async function resourcesNet() { const addr = "127.0.0.1:4501"; const listener = deno.listen("tcp", addr); + let counter = 0; listener.accept().then(async conn => { const res = deno.resources(); @@ -23,12 +24,12 @@ testPerm({ net: true }, async function resourcesNet() { conn.close(); listener.close(); + counter++; }); const conn = await deno.dial("tcp", addr); - const buf = new Uint8Array(1024); - await conn.read(buf); conn.close(); + assertEqual(counter, 1); }); test(async function resourcesFile() { From a6240e2c93a4cd58055138ff3b5f795edc290e51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 30 Oct 2018 20:42:33 +0100 Subject: [PATCH 15/15] update tests for resources --- js/resources_test.ts | 26 ++++++++++---------------- src/ops.rs | 4 ++-- src/resources.rs | 12 ++++++++---- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/js/resources_test.ts b/js/resources_test.ts index 9bb8b54cc2fa3d..3b34d7395c6c14 100644 --- a/js/resources_test.ts +++ b/js/resources_test.ts @@ -13,23 +13,17 @@ test(function resourcesStdio() { testPerm({ net: true }, async function resourcesNet() { const addr = "127.0.0.1:4501"; const listener = deno.listen("tcp", addr); - let counter = 0; - - listener.accept().then(async conn => { - const res = deno.resources(); - // besides 3 stdio resources, we should have additional 3 from listen(), accept() and dial() - assertEqual(Object.keys(res).length, 6); - assertEqual(Object.values(res).filter(r => r === "tcpListener").length, 1); - assertEqual(Object.values(res).filter(r => r === "tcpStream").length, 2); - - conn.close(); - listener.close(); - counter++; - }); - const conn = await deno.dial("tcp", addr); - conn.close(); - assertEqual(counter, 1); + const dialerConn = await deno.dial("tcp", addr); + const listenerConn = await listener.accept(); + + const res = deno.resources(); + 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(); }); test(async function resourcesFile() { diff --git a/src/ops.rs b/src/ops.rs index ca8d20557b4ef4..ce123ccd2af885 100644 --- a/src/ops.rs +++ b/src/ops.rs @@ -19,7 +19,7 @@ use futures::Poll; use hyper; use hyper::rt::{Future, Stream}; use remove_dir_all::remove_dir_all; -use resources::get_resource_table_entries; +use resources::table_entries; use std; use std::fs; use std::net::{Shutdown, SocketAddr}; @@ -1300,7 +1300,7 @@ fn op_resources( let cmd_id = base.cmd_id(); let builder = &mut FlatBufferBuilder::new(); - let serialized_resources = get_resource_table_entries(); + let serialized_resources = table_entries(); let res: Vec<_> = serialized_resources .iter() diff --git a/src/resources.rs b/src/resources.rs index 3ab616e12e04ee..72fdf9777442a1 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -58,7 +58,7 @@ enum Repr { TcpStream(tokio::net::TcpStream), } -pub fn get_resource_table_entries() -> Vec<(i32, String)> { +pub fn table_entries() -> Vec<(i32, String)> { let table = RESOURCE_TABLE.lock().unwrap(); let tuples = table @@ -70,9 +70,13 @@ pub fn get_resource_table_entries() -> Vec<(i32, String)> { } #[test] -fn test_get_resource_table_entries() { - assert_eq!(get_resource_table_entries().len(), 3); - // TODO: add asserts for add_fs_file, add_tcp_listener, add_tcp_stream +fn test_table_entries() { + let mut entries = table_entries(); + entries.sort(); + assert_eq!(entries.len(), 3); + assert_eq!(entries[0], (0, String::from("stdin"))); + assert_eq!(entries[1], (1, String::from("stdout"))); + assert_eq!(entries[2], (2, String::from("stderr"))); } fn inspect_repr(repr: &Repr) -> String {