summaryrefslogtreecommitdiff
path: root/erlang
diff options
context:
space:
mode:
authorVincent de Phily <vincent.dephily@gmail.com>2010-07-09 18:53:24 +0200
committerVincent de Phily <vincent.dephily@gmail.com>2010-07-09 18:53:24 +0200
commit21992f1b9e43f3e0d2c3da8f1f8d94d9ee70b6fb (patch)
treea4578845a0895709a3d384a9e27de32d8cac527b /erlang
parent8a3f090684f0835958e60e6a5e3e81b4d8ff541a (diff)
downloadmsgpack-python-21992f1b9e43f3e0d2c3da8f1f8d94d9ee70b6fb.tar.gz
erlang: fix receiving from port_command in unit tests
Ports can send data bit by bit; make sure we read all the port has to offer in one go. This should fix the "broken pipe" error we sometime got during testing. We did not previously check the return of compare_all/2, which is why the bug was not noticed. Incidentally, this change fixes dialyzer warnings too.
Diffstat (limited to 'erlang')
-rw-r--r--erlang/msgpack.erl26
1 files changed, 16 insertions, 10 deletions
diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index fb9a3e1..58ad414 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -261,6 +261,15 @@ compare_all([LH|LTL], [RH|RTL]) ->
?assertEqual(LH, RH),
compare_all(LTL, RTL).
+port_receive(Port) ->
+ port_receive(Port, <<>>).
+port_receive(Port, Acc) ->
+ receive
+ {Port, {data, Data}} -> port_receive(Port, <<Acc/binary, Data/binary>>);
+ {Port, eof} -> Acc
+ after 1000 -> Acc
+ end.
+
test_([]) -> 0;
test_([Term|Rest])->
Pack = msgpack:pack(Term),
@@ -286,13 +295,12 @@ basic_test()->
Passed = length(Tests).
port_test()->
- Port = open_port({spawn, "ruby ../test/crosslang.rb"}, [binary]),
Tests = test_data(),
- {[Tests],<<>>} = msgpack:unpack(msgpack:pack([Tests])),
- true = port_command(Port, msgpack:pack(Tests) ),
- receive
- {Port, {data, Data}}-> {Tests, <<>>}=msgpack:unpack(Data)
- after 1024-> ?assert(false) end,
+ ?assertEqual({[Tests],<<>>}, msgpack:unpack(msgpack:pack([Tests]))),
+
+ Port = open_port({spawn, "ruby ../test/crosslang.rb"}, [binary, eof]),
+ true = port_command(Port, msgpack:pack(Tests)),
+ ?assertEqual({Tests, <<>>}, msgpack:unpack(port_receive(Port))),
port_close(Port).
test_p(Len,Term,OrigBin,Len) ->
@@ -319,7 +327,7 @@ map_test()->
ok.
unknown_test()->
- Port = open_port({spawn, "ruby testcase_generator.rb"}, [binary]),
+ Port = open_port({spawn, "ruby testcase_generator.rb"}, [binary, eof]),
Tests = [0, 1, 2, 123, 512, 1230, 678908,
-1, -23, -512, -1230, -567898,
<<"hogehoge">>, <<"243546rf7g68h798j">>,
@@ -331,9 +339,7 @@ unknown_test()->
-234, -50000,
42
],
- receive
- {Port, {data, Data}}-> compare_all(Tests, msgpack:unpack_all(Data))
- after 1024-> ?assert(false) end,
+ ?assertEqual(ok, compare_all(Tests, msgpack:unpack_all(port_receive(Port)))),
port_close(Port).
other_test()->