tests: Make it possible to block until metric value will match predicate

parent 9a735bf9
......@@ -63,7 +63,7 @@ recv_packet_inner(#client{sock = Sock, codec = Codec0} = Client, Timeout) ->
recv_all(#client{sock = Sock, codec = Codec0} = Client, Timeout) ->
case tcp_recv_all(Sock, Timeout) of
{ok, Stream} ->
io:format("~p: ~p~n", [byte_size(Stream), Stream]),
%% io:format("~p: ~p~n", [byte_size(Stream), Stream]),
{ok, Packets, Codec} =
mtp_codec:fold_packets(
fun(Packet, Acc, Codec) ->
......@@ -79,7 +79,7 @@ recv_all(#client{sock = Sock, codec = Codec0} = Client, Timeout) ->
end.
tcp_recv_all(Sock, Timeout) ->
io:format("Sock: ~p; Timeout: ~p~n~n~n", [Sock, Timeout]),
%% io:format("Sock: ~p; Timeout: ~p~n~n~n", [Sock, Timeout]),
case gen_tcp:recv(Sock, 0, Timeout) of
{ok, Stream} ->
tcp_recv_all_inner(Sock, Stream);
......
......@@ -9,7 +9,9 @@
-export([notify/4]).
-export([get/2,
get/3,
get_tags/3]).
get_tags/3,
wait_for_value/5,
wait_for/5]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
......@@ -37,6 +39,29 @@ get(Type, Name, Extra) ->
get_tags(Type, Name, Tags) ->
get(Type, Name, #{labels => Tags}).
wait_for_value(Type, Name, Tags, Value, Timeout) ->
Now = erlang:monotonic_time(millisecond),
Test = fun(Current) -> Current == Value end,
wait_for_till(Type, Name, #{labels => Tags}, Test, Now + Timeout).
wait_for(Type, Name, Tags, Test, Timeout) ->
Now = erlang:monotonic_time(millisecond),
wait_for_till(Type, Name, #{labels => Tags}, Test, Now + Timeout).
wait_for_till(Type, Name, Extra, Test, Deadline) ->
case Test(get(Type, Name, Extra)) of
true -> ok;
false ->
Now = erlang:monotonic_time(millisecond),
case Now >= Deadline of
true ->
timeout;
false ->
timer:sleep(10),
wait_for_till(Type, Name, Extra, Test, Deadline)
end
end.
init([]) ->
{ok, #state{}}.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment