-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathrouter_v8_SUITE.erl
102 lines (84 loc) · 3.35 KB
/
router_v8_SUITE.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
-module(router_v8_SUITE).
-export([
all/0,
init_per_testcase/2,
end_per_testcase/2
]).
-export([
invalid_context_test/1
]).
-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl").
%%--------------------------------------------------------------------
%% @public
%% @doc
%% Running tests for this suite
%% @end
%%--------------------------------------------------------------------
all() ->
[invalid_context_test].
%%--------------------------------------------------------------------
%% TEST CASE SETUP
%%--------------------------------------------------------------------
init_per_testcase(TestCase, Config) ->
test_utils:init_per_testcase(TestCase, Config).
%%--------------------------------------------------------------------
%% TEST CASE TEARDOWN
%%--------------------------------------------------------------------
end_per_testcase(TestCase, Config) ->
test_utils:end_per_testcase(TestCase, Config).
%%--------------------------------------------------------------------
%% TEST CASES
%%--------------------------------------------------------------------
invalid_context_test(_Config) ->
GoodFunction =
<<
"function Decoder(bytes, port) {\n"
" var payload = {\"Testing\": \"42\"};\n"
" return payload;\n"
"}"
>>,
BadFunction = <<"function Decoder() { returrrrrrrn 0; }">>,
VMPid =
case router_v8:start_link(#{}) of
{ok, Pid} -> Pid;
{error, {already_started, Pid}} -> Pid
end,
{ok, VM} = router_v8:get(),
{ok, Context1} = erlang_v8:create_context(VM),
{ok, Context2} = erlang_v8:create_context(VM),
?assertNotMatch(Context1, Context2),
Payload = erlang:binary_to_list(base64:decode(<<"H4Av/xACRU4=">>)),
Port = 6,
Result = #{<<"Testing">> => <<"42">>},
%% Eval good function and ensure function works more than once
?assertMatch({ok, undefined}, erlang_v8:eval(VM, Context1, GoodFunction)),
?assertMatch({ok, Result}, erlang_v8:call(VM, Context1, <<"Decoder">>, [Payload, Port])),
?assertMatch({ok, Result}, erlang_v8:call(VM, Context1, <<"Decoder">>, [Payload, Port])),
%% Call undefined function
?assertMatch(
{error, <<"ReferenceError: Decoder is not defined", _/binary>>},
erlang_v8:call(VM, Context2, <<"Decoder">>, [Payload, Port])
),
%% First Context still works
?assertMatch({ok, Result}, erlang_v8:call(VM, Context1, <<"Decoder">>, [Payload, Port])),
%% Eval bad function
?assertMatch({error, crashed}, erlang_v8:eval(VM, Context2, BadFunction)),
%% Upon an error (other than invalid_source_size), v8 Port gets
%% killed and restarted
?assertMatch(true, erlang:is_process_alive(VMPid)),
%% Unintuitively, we get invalid context when attempting to reuse first Context:
?assertMatch(
{error, invalid_context},
erlang_v8:call(VM, Context1, <<"Decoder">>, [Payload, Port])
),
%% But that's because set of Context needs to be repopulated within V8's VM:
erlang_v8:restart_vm(VM),
%% First Context no longer works-- not ideal behavior but what we
%% have at hand. Ideally, return value would still match `Result'.
?assertMatch(
{error, invalid_context},
erlang_v8:call(VM, Context1, <<"Decoder">>, [Payload, Port])
),
gen_server:stop(VMPid),
ok.