1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

Add deno_send tests.

This commit is contained in:
Ryan Dahl 2018-06-11 18:17:28 +02:00
parent 0e07e16dd6
commit cbbe8ad999
4 changed files with 45 additions and 9 deletions

View file

@ -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) {

View file

@ -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);

View file

@ -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);
});
}

View file

@ -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();