diff --git a/src/main.c b/src/main.c index 6676779f..91b31585 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,7 @@ #include "ssb.h" #include "task.h" #include "taskstub.h" +#include "tests.h" #include #include @@ -137,7 +138,7 @@ static int _tf_command_test(const char* file, int argc, char* argv[]) return 2; } - tf_ssb_test(); + tf_tests(file); return 0; xopt_help: if (extras) { @@ -434,6 +435,7 @@ static int _tf_command_usage(const char* file, int argc, char* argv[]) int main(int argc, char* argv[]) { + prctl(PR_SET_PDEATHSIG, SIGKILL); uv_setup_args(argc, argv); tf_taskstub_startup(); diff --git a/src/task.c b/src/task.c index 8d40f11b..297d3fa4 100644 --- a/src/task.c +++ b/src/task.c @@ -1192,7 +1192,9 @@ void tf_task_activate(tf_task_t* task) tf_ssb_set_trace(task->_ssb, task->_trace); tf_ssb_broadcast_listener_start(task->_ssb, false); tf_ssb_init(context, task->_ssb); - tf_ssb_server_open(task->_ssb, task->_ssb_port); + if (task->_ssb_port) { + tf_ssb_server_open(task->_ssb, task->_ssb_port); + } JS_SetPropertyStr(context, global, "trace", JS_NewCFunction(context, _tf_task_trace, "trace", 1)); } else { diff --git a/src/tests.c b/src/tests.c new file mode 100644 index 00000000..ef880cf2 --- /dev/null +++ b/src/tests.c @@ -0,0 +1,145 @@ +#include "tests.h" + +#include "ssb.h" + +#include +#include +#include +#include + +static void _test_nop(const char* exe_path) +{ + FILE* file = fopen("out/test.js", "w"); + fprintf(file, "print('hi');"); + fclose(file); + + char command[256]; + snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path); + printf("%s\n", command); + int result = system(command); + assert(WIFEXITED(result)); + assert(WEXITSTATUS(result) == 0); +} + +static void _test_child(const char* exe_path) +{ + FILE* file = fopen("out/test.js", "w"); + fprintf(file, + "var task = new Task();\n" + "task.onExit = function() {\n" + " print('child exited');\n" + "};\n" + "task.activate();\n" + "task.execute({name: 'child.js', source: utf8Decode(File.readFile('out/child.js'))}).then(function() {\n" + " print('child started');\n" + "});"); + fclose(file); + + file = fopen("out/child.js", "w"); + fprintf(file, + "print('I am the child process.');\n" + "exit(0);\n"); + fclose(file); + + char command[256]; + snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path); + printf("%s\n", command); + int result = system(command); + assert(WIFEXITED(result)); + assert(WEXITSTATUS(result) == 0); + + unlink("out/test.js"); + unlink("out/child.js"); +} + +static void _test_promise(const char* exe_path) +{ + FILE* file = fopen("out/test.js", "w"); + fprintf(file, + "var task = new Task();\n" + "task.activate();\n" + "task.execute({name: 'child.js', source: utf8Decode(File.readFile('out/child.js'))}).then(function() {\n" + " task.getExports().then(function(exports) {\n" + " return exports.add(1, 1);\n" + " }).then(function(sum) {\n" + " if (sum == 2) {\n" + " exit(0);\n" + " } else {\n" + " exit(1);\n" + " }\n" + " });\n" + "});\n"); + fclose(file); + + file = fopen("out/child.js", "w"); + fprintf(file, + "exports = {\n" + " add: function(left, right) {\n" + " return left + right;\n" + " }\n" + "}\n"); + fclose(file); + + char command[256]; + snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path); + printf("%s\n", command); + int result = system(command); + assert(WIFEXITED(result)); + assert(WEXITSTATUS(result) == 0); + + unlink("out/test.js"); + unlink("out/child.js"); +} + +static void _test_promise_remote_throw(const char* exe_path) +{ + FILE* file = fopen("out/test.js", "w"); + fprintf(file, + "var task = new Task();\n" + "task.activate();\n" + "task.execute({name: 'child.js', source: utf8Decode(File.readFile('out/child.js'))}).then(function() {\n" + " task.getExports().then(function(exp) {\n" + " return exp.add(1, 1);\n" + " }).then(function(sum) {\n" + " exit(1);\n" + " }).catch(function(error) {\n" + " print('Caught: ' + error.message);\n" + " if (error.stack) {\n" + " print('stack: ' + error.stack);\n" + " }\n" + " exit(0);\n" + " });\n" + "}).catch(function(e) {\n" + " print('caught', e.message);\n" + "});\n"); + fclose(file); + + file = fopen("out/child.js", "w"); + fprintf(file, + "exports = {\n" + " add: function(left, right) {\n" + " throw new Error('fail');\n" + " }\n" + "}\n"); + fclose(file); + + char command[256]; + snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path); + printf("%s\n", command); + int result = system(command); + assert(WIFEXITED(result)); + assert(WEXITSTATUS(result) == 0); + + unlink("out/test.js"); + unlink("out/child.js"); +} + +void tf_tests(const char* exe_path) +{ + tf_ssb_test(); + _test_nop(exe_path); + _test_child(exe_path); + _test_promise(exe_path); + _test_promise_remote_throw(exe_path); + printf("Tests completed.\n"); +} diff --git a/src/tests.h b/src/tests.h new file mode 100644 index 00000000..65799dc4 --- /dev/null +++ b/src/tests.h @@ -0,0 +1,3 @@ +#pragma once + +void tf_tests(const char* exe_path); diff --git a/tests/01-nop b/tests/01-nop deleted file mode 100755 index debd6b9a..00000000 --- a/tests/01-nop +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash - -cat > test.js << EOF -print("hi"); -EOF - -echo $TILDEFRIENDS test.js -$TILDEFRIENDS test.js diff --git a/tests/02-valgrind b/tests/02-valgrind deleted file mode 100755 index 62622376..00000000 --- a/tests/02-valgrind +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/bash - -echo "SKIP" -exit 0 - -if [ ! -x /usr/bin/valgrind ]; then - echo "SKIP" - exit 0 -fi - -cat > test.js << EOF -print("hi"); -EOF - -valgrind --log-file=$LOGDIR/valgrind.log $TILDEFRIENDS test.js diff --git a/tests/03-child b/tests/03-child deleted file mode 100755 index 95b040ae..00000000 --- a/tests/03-child +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/bash - -cat > test.js << EOF -var task = new Task(); -task.onExit = function() { - print("child exited"); -}; -task.activate(); -task.execute({name: "child.js", source: utf8Decode(File.readFile("child.js"))}).then(function() { - print("child started"); -}); -EOF - -cat > child.js << EOF -print("I am the child process."); -exit(0); -EOF - -$TILDEFRIENDS test.js diff --git a/tests/04-promise b/tests/04-promise deleted file mode 100755 index efe40d14..00000000 --- a/tests/04-promise +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/bash - -cat > test.js << EOF -var task = new Task(); -task.activate(); -task.execute({name: "child.js", source: utf8Decode(File.readFile("child.js"))}).then(function() { - task.getExports().then(function(exports) { - return exports.add(1, 1); - }).then(function(sum) { - if (sum == 2) { - exit(0); - } else { - exit(1); - } - }); -}); -EOF - -cat > child.js << EOF -exports = { - add: function(left, right) { - return left + right; - } -} -EOF - -$TILDEFRIENDS test.js diff --git a/tests/05-promise-remote-throw b/tests/05-promise-remote-throw deleted file mode 100755 index 94e22d27..00000000 --- a/tests/05-promise-remote-throw +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/bash - -cat > test.js << EOF -var task = new Task(); -task.activate(); -task.execute({name: "child.js", source: utf8Decode(File.readFile("child.js"))}).then(function() { - task.getExports().then(function(exp) { - return exp.add(1, 1); - }).then(function(sum) { - exit(1); - }).catch(function(error) { - print("Caught: " + error.message); - if (error.stack) { - print("stack: " + error.stack); - } - exit(0); - }); -}).catch(function(e) { - print("caught", e.message); -}); -EOF - -cat > child.js << EOF -exports = { - add: function(left, right) { - throw new Error("fail"); - } -}; -EOF - -$TILDEFRIENDS test.js