From 21992f1b9e43f3e0d2c3da8f1f8d94d9ee70b6fb Mon Sep 17 00:00:00 2001 From: Vincent de Phily Date: Fri, 9 Jul 2010 18:53:24 +0200 Subject: [PATCH] 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. --- erlang/msgpack.erl | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl index fb9a3e12..58ad4143 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, <>); + {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()->