Skip to content

Commit

Permalink
Add eunit tests for erlang api, network api, and pipes of both
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsta committed Feb 13, 2011
1 parent c026b3d commit 77016f4
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ ebin/
priv/
deps/
*.o
.eunit/
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ Build:

Testing
-------
Automated:
rebar eunit skip_deps=true

Automated with timing details:
rebar eunit skip_deps=true -v

In the `test/` directory there is a short script to verify error conditions.
You can load test error conditions with:
time seq 0 300 |xargs -n 1 -P 16 ./errors.sh TARGET-IP TARGET-PORT
Expand Down
99 changes: 99 additions & 0 deletions test/stdinout_tests.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
-module(stdinout_tests).
-include_lib("eunit/include/eunit.hrl").

-define(E(A, B), ?assertEqual(A, B)).
-define(_E(A, B), ?_assertEqual(A, B)).
-define(B(X), iolist_to_binary(X)).

get_base_dir(Module) ->
{file, Here} = code:is_loaded(Module),
filename:dirname(filename:dirname(Here)).

add_oneshot_dep() ->
code:add_path(get_base_dir(?MODULE) ++ "deps/oneshot/ebin").

setup_ports() ->
add_oneshot_dep(),

% Obviously this test only works if you have /bin/cat and /usr/bin/wc
S1 = stdinout:start_link(cat1, "/bin/cat"),
S2 = stdinout:start_link(cat2, "/bin/cat"),
S3 = stdinout:start_link(cat3, "/bin/cat"),
S4 = stdinout:start_link(cat4, "/bin/cat"),
S5 = stdinout:start_link(wc, "/usr/bin/wc"),

S6 = stdinout:start_link(catn1, "/bin/cat", "127.0.0.1", 6651),
S7 = stdinout:start_link(catn2, "/bin/cat", "127.0.0.1", 6652),
S8 = stdinout:start_link(wcn, "/usr/bin/wc", "127.0.0.1", 6653),

[P || {ok, P} <- [S1, S2, S3, S4, S5, S6, S7, S8]].

cleanup_ports(Ps) ->
[exit(P, normal) || P <- Ps].

everything_erlang_API_in_parallel_test_() ->
{setup,
fun setup_ports/0,
fun cleanup_ports/1,
fun(_) ->
{inparallel,
[
?_E(<<"hello">>, ?B(stdinout:send(cat1, "hello"))),
?_E(<<"hello">>, ?B(stdinout:send(cat1, <<"hello">>))),
?_E(<<"hello">>, ?B(stdinout:send(cat1, [<<"hello">>]))),
?_E(<<"hello">>, ?B(stdinout:send(cat1, [<<"he">>, <<"llo">>]))),
?_E(<<"hello">>, ?B(stdinout:send(cat1, ["he", "llo"]))),
?_E([], stdinout:send(cat1, "")),
?_E(<<"hello">>, ?B(stdinout:pipe("hello",[cat1, cat2, cat3, cat4]))),
?_E({error, cat1, <<"hello">>},
% this wacky fun exists to erlang:iolist_to_binary/1 the ErrorIoList
fun() ->
{error, cat1, ErrorIoList} =
stdinout:pipe("hello", [{cat1, "he"}, cat2, cat3, cat4]),
{error, cat1, ?B(ErrorIoList)}
end()),
?_E({error, wc, <<" 0 1 5\n">>},
fun() ->
{error, wc, ErrorIoList} =
stdinout:pipe("hello", [{cat1, "bob"}, {wc, "5"}, cat3, cat4]),
{error, wc, ?B(ErrorIoList)}
end())
]
}
end
}.

-define(C1, {"localhost", 6651}).
-define(C2, {"localhost", 6652}).
-define(W1, {"localhost", 6653}).
network_API_test_() ->
{setup,
fun setup_ports/0,
fun cleanup_ports/1,
fun(_) ->
{inparallel,
[
?_E(<<"hello">>, ?B(stdinout:send(?C1, "hello"))),
?_E(<<"hello">>, ?B(stdinout:send(?C2, <<"hello">>))),
?_E(<<"hello">>, ?B(stdinout:send(?C1, [<<"hello">>]))),
?_E(<<"hello">>, ?B(stdinout:send(?C1, [<<"he">>, <<"llo">>]))),
?_E(<<"hello">>, ?B(stdinout:send(?C2, ["he", "llo"]))),
?_E([], stdinout:send(?C1, "")),
?_E(<<"hello">>, ?B(stdinout:pipe("hello", [?C1, ?C2, ?C1, ?C2]))),
?_E({error, ?C1, <<"hello">>},
% this wacky fun exists to erlang:iolist_to_binary/1 the ErrorIoList
fun() ->
{error, ?C1, ErrorIoList} =
stdinout:pipe("hello", [{?C1, "he"}, ?C2, ?C2, ?C1]),
{error, ?C1, ?B(ErrorIoList)}
end()),
?_E({error, ?W1, <<" 0 1 5\n">>},
fun() ->
{error, ?W1, ErrorIoList} =
stdinout:pipe("hello", [{?C2, "bob"}, {?W1, "5"}, ?C2, ?C1]),
{error, ?W1, ?B(ErrorIoList)}
end())
]
}
end
}.

0 comments on commit 77016f4

Please sign in to comment.