Starting to move the tests to C.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3652 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2021-08-19 19:29:37 +00:00
parent be6a39bd15
commit dd90fe4fbf
9 changed files with 154 additions and 102 deletions

View File

@ -1,6 +1,7 @@
#include "ssb.h"
#include "task.h"
#include "taskstub.h"
#include "tests.h"
#include <quickjs-libc.h>
#include <quickjs.h>
@ -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();

View File

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

145
src/tests.c Normal file
View File

@ -0,0 +1,145 @@
#include "tests.h"
#include "ssb.h"
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
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");
}

3
src/tests.h Normal file
View File

@ -0,0 +1,3 @@
#pragma once
void tf_tests(const char* exe_path);

View File

@ -1,8 +0,0 @@
#!/bin/bash
cat > test.js << EOF
print("hi");
EOF
echo $TILDEFRIENDS test.js
$TILDEFRIENDS test.js

View File

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

View File

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

View File

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

View File

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