summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2018-06-11 18:17:28 +0200
committerRyan Dahl <ry@tinyclouds.org>2018-06-11 18:17:28 +0200
commitcbbe8ad9992765bc0883759e4075cf7a4a1918ff (patch)
tree844555d6da87fc08e4ffb3f98a9c9d2742a41dd3
parent0e07e16dd63992f5f989dc99c891d53d930a2d5b (diff)
Add deno_send tests.
-rw-r--r--deno2/deno.cc15
-rw-r--r--deno2/include/deno.h5
-rw-r--r--deno2/js/mock_runtime.js10
-rw-r--r--deno2/mock_runtime_test.cc24
4 files changed, 45 insertions, 9 deletions
diff --git a/deno2/deno.cc b/deno2/deno.cc
index 7bca590a7..4fa5b2101 100644
--- a/deno2/deno.cc
+++ b/deno2/deno.cc
@@ -274,9 +274,10 @@ bool deno_load(Deno* d, const char* name_s, const char* source_s) {
return deno::Load(context, name_s, source_s);
}
-// Called from golang. Must route message to javascript lang.
-// non-zero return value indicates error. check deno_last_exception().
-int deno_send(Deno* d, deno_buf buf) {
+// Routes message to the javascript callback set with deno_recv().
+// False return value indicates error. Check deno_last_exception() for exception
+// text.
+bool deno_send(Deno* d, deno_buf buf) {
v8::Locker locker(d->isolate);
v8::Isolate::Scope isolate_scope(d->isolate);
v8::HandleScope handle_scope(d->isolate);
@@ -289,8 +290,8 @@ int deno_send(Deno* d, deno_buf buf) {
v8::Local<v8::Function> recv =
v8::Local<v8::Function>::New(d->isolate, d->recv);
if (recv.IsEmpty()) {
- d->last_exception = "V8Deno2.recv has not been called.";
- return 1;
+ d->last_exception = "deno_recv has not been called.";
+ return false;
}
v8::Local<v8::Value> args[1];
@@ -303,10 +304,10 @@ int deno_send(Deno* d, deno_buf buf) {
if (try_catch.HasCaught()) {
deno::HandleException(context, try_catch.Exception());
- return 2;
+ return false;
}
- return 0;
+ return true;
}
void deno_dispose(Deno* d) {
diff --git a/deno2/include/deno.h b/deno2/include/deno.h
index 1a42ec2b1..f67a7cf51 100644
--- a/deno2/include/deno.h
+++ b/deno2/include/deno.h
@@ -31,8 +31,9 @@ Deno* deno_new(void* data, deno_recv_cb cb);
// Get error text with deno_last_exception().
bool deno_load(Deno* d, const char* name_s, const char* source_s);
-// Returns nonzero on error.
-int deno_send(Deno* d, deno_buf buf);
+// Returns false on error.
+// Get error text with deno_last_exception().
+bool deno_send(Deno* d, deno_buf buf);
const char* deno_last_exception(Deno* d);
diff --git a/deno2/js/mock_runtime.js b/deno2/js/mock_runtime.js
index a91546f85..cd9766685 100644
--- a/deno2/js/mock_runtime.js
+++ b/deno2/js/mock_runtime.js
@@ -7,3 +7,13 @@ window['foo'] = () => {
return "foo";
}
+function assert(cond) {
+ if (!cond) throw Error("assert failed");
+}
+
+function recvabc() {
+ deno_recv((msg) => {
+ assert(msg instanceof ArrayBuffer);
+ assert(msg.byteLength === 3);
+ });
+}
diff --git a/deno2/mock_runtime_test.cc b/deno2/mock_runtime_test.cc
index 951710fb9..00651f68e 100644
--- a/deno2/mock_runtime_test.cc
+++ b/deno2/mock_runtime_test.cc
@@ -19,6 +19,30 @@ TEST(MockRuntimeTest, ErrorsCorrectly) {
EXPECT_FALSE(deno_load(d, "a.js", "throw Error()"));
}
+deno_buf strbuf(const char* str) {
+ void* d = reinterpret_cast<void*>(const_cast<char*>(str));
+ return deno_buf{d, strlen(str)};
+}
+
+TEST(MockRuntimeTest, SendSuccess) {
+ Deno* d = deno_new(NULL, NULL);
+ EXPECT_TRUE(deno_load(d, "a.js", "recvabc();"));
+ EXPECT_TRUE(deno_send(d, strbuf("abc")));
+}
+
+TEST(MockRuntimeTest, SendByteLength) {
+ Deno* d = deno_new(NULL, NULL);
+ EXPECT_TRUE(deno_load(d, "a.js", "recvabc();"));
+ // We send the wrong sized message, it should throw.
+ EXPECT_FALSE(deno_send(d, strbuf("abcd")));
+}
+
+TEST(MockRuntimeTest, SendNoCallback) {
+ Deno* d = deno_new(NULL, NULL);
+ // We didn't call deno_recv(), sending should fail.
+ EXPECT_FALSE(deno_send(d, strbuf("abc")));
+}
+
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
deno_init();