Skip to content

Commit

Permalink
Fix a serialization bug, switch to rebar3
Browse files Browse the repository at this point in the history
  • Loading branch information
djnym committed Dec 15, 2018
1 parent 670122e commit 3bd4cf5
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 26 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ ebin
.eunit
.rebar
_build
rebar.lock
7 changes: 6 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
* Fri Jun 8 2018 Yoshihiro Tanaka (yoshi) 6.10.0
* Fri Dec 14 2018 Anthony Molinaro (djnym) 6.10.1
- use rebar3 in Makefile
- fix for when statsmsg contexts are mismatched, the library will no longer
crash

* Fri Jun 08 2018 Yoshihiro Tanaka (yoshi) 6.10.0
- Add literal memory usage stat

* Tue May 15 2018 Anthony Molinaro (djnym) 6.9.4
Expand Down
38 changes: 23 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
NAME=mondemand

REBAR3=rebar3
all:
@rebar get-deps compile
$(REBAR3) compile

dialyzer:
$(REBAR3) as test do dialyzer

perf:
@(cd tests ; erlc -I../src -I../include *.erl)
test:
$(REBAR3) as test do dialyzer,eunit,cover

edoc:
@rebar skip_deps=true doc
# Compile and run unit test for individual modules: 'make test-oxgw_util'
# or 'make test-oxgw_util test-oxgw_request'
test-%: src/%.erl
$(REBAR3) as test do eunit -m $*

check:
@rm -rf .eunit
@mkdir -p .eunit
@dialyzer --src src -I deps -pa deps/parse_trans/ebin
@rebar skip_deps=true eunit
name:
@echo $(NAME)

version:
@echo $(shell awk 'match($$0, /[0-9]+\.[0-9]+(\.[0-9]+)+/){print substr($$0, RSTART,RLENGTH); exit}' ChangeLog)

clean:
@rebar clean
if test -d _build; then $(REBAR3) clean; fi

maintainer-clean: clean
rm -rf _build

maintainer-clean:
@rebar clean
@rebar delete-deps
@rm -rf deps
.PHONY: all test name version clean maintainer-clean
10 changes: 2 additions & 8 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,7 @@

{deps,
[
{ lwes,
"4.6.1",
{git, "git://github.com/lwes/lwes-erlang.git", {tag, "4.6.1"}}
},
{ parse_trans,
"3.2.0",
{git, "git://github.com/uwiger/parse_trans.git", {tag, "3.2.0"} }
}
{ lwes, {git, "git://github.com/lwes/lwes-erlang.git", {tag, "4.7.1"}} },
{ parse_trans, {git, "git://github.com/uwiger/parse_trans.git", {tag, "3.2.0"} } }
]
}.
69 changes: 68 additions & 1 deletion src/mondemand_statsmsg.erl
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,19 @@ zip_and_find_host ([], [], Host, Context) ->
zip_and_find_host ([{K, <<"host">>} | Keys], [{K, Host} | Vals], _, Context) ->
zip_and_find_host (Keys, Vals, Host, Context);
zip_and_find_host ([{K, Key} | Keys ], [{K, Val} | Vals ], Host, Context) ->
zip_and_find_host (Keys, Vals, Host, [{Key, Val} | Context]).
zip_and_find_host (Keys, Vals, Host, [{Key, Val} | Context]);
zip_and_find_host (L1 = [{K1, Key} | Keys ],
L2 = [{K2, Val} | Vals],
Host, Context) ->
% error case, there's something missing so we will log and drop it
case K1 < K2 of
true ->
% we have a key but no value, so just add an empty context for the key
zip_and_find_host(Keys, L2, Host, [{Key, missing} | Context]);
false ->
% we have a context but no key! So make something up
zip_and_find_host(L1, Vals, Host, [{missing, Val} | Context])
end.

process ([],
ReceiptTime,
Expand Down Expand Up @@ -1787,6 +1799,48 @@ index (<<"1024">>) -> 1025.
-ifdef (TEST).
-include_lib ("eunit/include/eunit.hrl").

% we had the situation where some events were being constructed improperly
% and deserialization was crashing so here's a bad stats msg to test
% against.
bad_event1() ->
#lwes_event { name = <<"MonDemand::StatsMsg">>,
attrs =
[ {string,<<"prog_id">>,<<"foo">>},
{int16,<<"enc">>,1}, % ignored as this is not used

{uint16,<<"num">>,1},
{string,<<"t0">>,<<"counter">>},
{string,<<"k0">>,<<"blank">>},
{int64,<<"v0">>,0},
{uint16,<<"ctxt_num">>,3},
{string,<<"ctxt_k0">>,<<"host">>},
{string,<<"ctxt_v0">>,<<"localhost">>},
{string,<<"ctxt_k1">>,<<"p">>},
% notice the missing ctxt_v1 here
{string,<<"ctxt_k2">>,<<"s">>},
{string,<<"ctxt_v2">>, <<"bar">>}
]
}.
bad_event2() ->
#lwes_event { name = <<"MonDemand::StatsMsg">>,
attrs =
[ {string,<<"prog_id">>,<<"foo">>},
{int16,<<"enc">>,1}, % ignored as this is not used

{uint16,<<"num">>,1},
{string,<<"t0">>,<<"counter">>},
{string,<<"k0">>,<<"blank">>},
{int64,<<"v0">>,0},
{uint16,<<"ctxt_num">>,3},
{string,<<"ctxt_k0">>,<<"host">>},
{string,<<"ctxt_v0">>,<<"localhost">>},
% notice the missing ctxt_k1 here
{string,<<"ctxt_v1">>,<<"p">>},
{string,<<"ctxt_k2">>,<<"s">>},
{string,<<"ctxt_v2">>, <<"bar">>}
]
}.

statsmsg_test_ () ->
[
{ "basic constructor test",
Expand Down Expand Up @@ -2082,6 +2136,19 @@ statsmsg_test_ () ->
?assertEqual (prog_id (S1), prog_id (S2)),
?assertEqual (lists:sort(context(S1)), lists:sort(context (S2)))
end
},
{ "malformed events",
fun() ->
% missing value for key
{0, B1} =
from_lwes (
lwes_event:from_binary(lwes_event:to_binary(bad_event1()),list)),
?assertEqual( context_value(B1, <<"p">>), missing),
{0, B2} =
from_lwes (
lwes_event:from_binary(lwes_event:to_binary(bad_event2()),list)),
?assertEqual( context_value(B2, missing), <<"p">>)
end
}
].

Expand Down

0 comments on commit 3bd4cf5

Please sign in to comment.