Browse Source

Add time_as_millis/1 and time_as_micros/1 helper functions

These helper functions make is possible for users to specify a time
unit for config values that need a time, e.g., {3, min} or {300,
sec}. This keeps config readable and avoids confusion over the units
of the time. It also allows for testing of time/schedule related
functionality at short time scales without requiring that end users
specify everything in milliseconds.
Seth Falcon 13 years ago
parent
commit
d620e8f2df
2 changed files with 43 additions and 0 deletions
  1. 23 0
      src/pooler.erl
  2. 20 0
      test/pooler_test.erl

+ 23 - 0
src/pooler.erl

@@ -17,6 +17,8 @@
 
 
 -type member_info() :: {string(), free | pid(), {_, _, _}}.
 -type member_info() :: {string(), free | pid(), {_, _, _}}.
 -type free_member_info() :: {string(), free, {_, _, _}}.
 -type free_member_info() :: {string(), free, {_, _, _}}.
+-type time_unit() :: min | sec | ms | mu.
+-type time_spec() :: {non_neg_integer(), time_unit()}.
 
 
 -record(pool, {
 -record(pool, {
           name             :: string(),
           name             :: string(),
@@ -74,6 +76,11 @@
          terminate/2,
          terminate/2,
          code_change/3]).
          code_change/3]).
 
 
+%% To help with testing internal functions
+-ifdef(TEST).
+-compile([export_all]).
+-endif.
+
 %% ------------------------------------------------------------------
 %% ------------------------------------------------------------------
 %% API Function Definitions
 %% API Function Definitions
 %% ------------------------------------------------------------------
 %% ------------------------------------------------------------------
@@ -571,3 +578,19 @@ send_metric(Name, Value, Type) ->
 pool_metric(PoolName, Metric) ->
 pool_metric(PoolName, Metric) ->
     iolist_to_binary([<<"pooler.">>, PoolName, ".",
     iolist_to_binary([<<"pooler.">>, PoolName, ".",
                       atom_to_binary(Metric, utf8)]).
                       atom_to_binary(Metric, utf8)]).
+
+-spec time_as_millis(time_spec()) -> non_neg_integer().
+%% @doc Convert time unit into milliseconds.
+time_as_millis({Time, Unit}) ->
+    time_as_micros({Time, Unit}) div 1000.
+
+-spec time_as_micros(time_spec()) -> non_neg_integer().
+%% @doc Convert time unit into microseconds
+time_as_micros({Time, min}) ->
+    60 * 1000 * 1000 * Time;
+time_as_micros({Time, sec}) ->
+    1000 * 1000 * Time;
+time_as_micros({Time, ms}) ->
+    1000 * Time;
+time_as_micros({Time, mu}) ->
+    Time.

+ 20 - 0
test/pooler_test.erl

@@ -392,6 +392,26 @@ pooler_integration_test_() ->
      ]
      ]
     }.
     }.
 
 
+time_as_millis_test_() ->
+    Zeros = [ {{0, U}, 0} || U <- [min, sec, ms, mu] ],
+    Ones = [{{1, min}, 60000},
+            {{1, sec}, 1000},
+            {{1, ms}, 1},
+            {{1, mu}, 0}],
+    Misc = [{{3000, mu}, 3}],
+    Tests = Zeros ++ Ones ++ Misc,
+    [ ?_assertEqual(E, pooler:time_as_millis(I)) || {I, E} <- Tests ].
+
+time_as_micros_test_() ->
+    Zeros = [ {{0, U}, 0} || U <- [min, sec, ms, mu] ],
+    Ones = [{{1, min}, 60000000},
+            {{1, sec}, 1000000},
+            {{1, ms}, 1000},
+            {{1, mu}, 1}],
+    Misc = [{{3000, mu}, 3000}],
+    Tests = Zeros ++ Ones ++ Misc,
+    [ ?_assertEqual(E, pooler:time_as_micros(I)) || {I, E} <- Tests ].
+
 % testing crash recovery means race conditions when either pids
 % testing crash recovery means race conditions when either pids
 % haven't yet crashed or pooler hasn't recovered.  So this helper loops
 % haven't yet crashed or pooler hasn't recovered.  So this helper loops
 % forver until N pids are obtained, ignoring error_no_members.
 % forver until N pids are obtained, ignoring error_no_members.