rewrite from lager
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
8.1 KiB

4 years ago
  1. %% -------------------------------------------------------------------
  2. %%
  3. %% Copyright (c) 2011-2017 Basho Technologies, Inc.
  4. %%
  5. %% This file is provided to you under the Apache License,
  6. %% Version 2.0 (the "License"); you may not use this file
  7. %% except in compliance with the License. You may obtain
  8. %% a copy of the License at
  9. %%
  10. %% http://www.apache.org/licenses/LICENSE-2.0
  11. %%
  12. %% Unless required by applicable law or agreed to in writing,
  13. %% software distributed under the License is distributed on an
  14. %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. %% KIND, either express or implied. See the License for the
  16. %% specific language governing permissions and limitations
  17. %% under the License.
  18. %%
  19. %% -------------------------------------------------------------------
  20. -module(lager_test_function_transform).
  21. -include("lager.hrl").
  22. -compile([{nowarn_deprecated_function, [{erlang, now, 0}]}]).
  23. -lager_function_transforms([
  24. {returns_static_emit, on_emit, {lager_test_function_transform, transform_static}},
  25. {returns_dynamic_emit, on_emit, {lager_test_function_transform, transform_dynamic}},
  26. {returns_undefined_emit, on_emit, {not_real_module_fake, fake_not_real_function}},
  27. {returns_static_log, on_log, {lager_test_function_transform, transform_static}},
  28. {returns_dynamic_log, on_log, {lager_test_function_transform, transform_dynamic}}
  29. ]).
  30. -compile({parse_transform, lager_transform}).
  31. -ifdef(TEST).
  32. -include_lib("eunit/include/eunit.hrl").
  33. -export([
  34. transform_static/0,
  35. transform_dynamic/0
  36. ]).
  37. -endif.
  38. -ifdef(TEST).
  39. transform_static() ->
  40. static_result.
  41. transform_dynamic() ->
  42. case lager_util:otp_version() >= 18 of
  43. true ->
  44. erlang:monotonic_time();
  45. false ->
  46. erlang:now()
  47. end.
  48. not_running_test() ->
  49. ?assertEqual({error, lager_not_running}, lager:log(info, self(), "not running")).
  50. setup() ->
  51. ok = error_logger:tty(false),
  52. ok = lager_util:safe_application_load(lager),
  53. ok = application:set_env(lager, handlers, [{lager_test_backend, info}]),
  54. ok = application:set_env(lager, error_logger_redirect, false),
  55. ok = application:unset_env(lager, traces),
  56. ok = lager:start(),
  57. %% There is a race condition between the application start up, lager logging its own
  58. %% start up condition and several tests that count messages or parse the output of
  59. %% tests. When the lager start up message wins the race, it causes these tests
  60. %% which parse output or count message arrivals to fail.
  61. %%
  62. %% We introduce a sleep here to allow `flush' to arrive *after* the start up
  63. %% message has been received and processed.
  64. %%
  65. %% This race condition was first exposed during the work on
  66. %% 4b5260c4524688b545cc12da6baa2dfa4f2afec9 which introduced the lager
  67. %% manager killer PR.
  68. ok = timer:sleep(250),
  69. ok = gen_event:call(lager_event, lager_test_backend, flush).
  70. cleanup(_) ->
  71. catch ets:delete(lager_config), %% kill the ets config table with fire
  72. ok = application:stop(lager),
  73. ok = application:stop(goldrush),
  74. ok = error_logger:tty(true).
  75. transform_function_test_() ->
  76. {foreach,
  77. fun setup/0,
  78. fun cleanup/1,
  79. [
  80. {"observe that there is nothing up my sleeve",
  81. fun() ->
  82. ?assertEqual(undefined, lager_test_backend:pop()),
  83. ?assertEqual(0, lager_test_backend:count())
  84. end
  85. },
  86. {"logging works",
  87. fun() ->
  88. lager:warning("test message"),
  89. ?assertEqual(1, lager_test_backend:count()),
  90. {Level, _Time, Message, _Metadata} = lager_test_backend:pop(),
  91. ?assertMatch(Level, lager_util:level_to_num(warning)),
  92. ?assertEqual("test message", Message),
  93. ok
  94. end
  95. },
  96. {"Testing calling a function returns the same content on emit",
  97. fun() ->
  98. lager:warning("static message"),
  99. ?assertEqual(1, lager_test_backend:count()),
  100. {_Level, _Time, _Message, Metadata} = lager_test_backend:pop(),
  101. Function = proplists:get_value(returns_static_emit, Metadata),
  102. ?assertEqual(transform_static(), Function()),
  103. ok
  104. end
  105. },
  106. {"Testing calling a function which returns content which can change on emit",
  107. fun() ->
  108. lager:warning("dynamic message"),
  109. ?assertEqual(1, lager_test_backend:count()),
  110. {_Level, _Time, _Message, Metadata} = lager_test_backend:pop(),
  111. Function = proplists:get_value(returns_dynamic_emit, Metadata),
  112. ?assert(Function() =< Function()),
  113. ?assert(Function() =< Function()),
  114. ?assert(Function() =< Function()),
  115. ?assert(Function() =< Function()),
  116. ok
  117. end
  118. },
  119. {"Testing a undefined function returns undefined on emit",
  120. fun() ->
  121. lager:warning("Undefined error"),
  122. ?assertEqual(1, lager_test_backend:count()),
  123. {_Level, _Time, _Message, Metadata} = lager_test_backend:pop(),
  124. Function = proplists:get_value(returns_undefined_emit, Metadata),
  125. [{module, Module}, {name, Name}|_] = erlang:fun_info(Function),
  126. ?assertNot(erlang:function_exported(Module, Name, 0)),
  127. ok
  128. end
  129. },
  130. {"Testing calling a function returns the same content on log",
  131. fun() ->
  132. lager:warning("static message"),
  133. ?assertEqual(1, lager_test_backend:count()),
  134. {_Level, _Time, _Message, Metadata} = lager_test_backend:pop(),
  135. ?assertEqual(transform_static(), proplists:get_value(returns_static_log, Metadata)),
  136. ok
  137. end
  138. },
  139. {"Testing calling a dynamic function on log which returns the same value",
  140. fun() ->
  141. lager:warning("dynamic message"),
  142. ?assertEqual(1, lager_test_backend:count()),
  143. {_Level, _Time, _Message, Metadata} = lager_test_backend:pop(),
  144. Value = proplists:get_value(returns_dynamic_log, Metadata),
  145. ?assert(Value =< transform_dynamic()),
  146. ?assert(Value =< transform_dynamic()),
  147. ?assert(Value =< transform_dynamic()),
  148. ?assert(Value =< transform_dynamic()),
  149. ?assert(Value =< transform_dynamic()),
  150. ok
  151. end
  152. },
  153. {"Testing differences in results for on_log vs on emit from dynamic function",
  154. fun() ->
  155. lager:warning("on_log vs on emit"),
  156. ?assertEqual(1, lager_test_backend:count()),
  157. {_Level, _Time, _Message, Metadata} = lager_test_backend:pop(),
  158. Value = proplists:get_value(returns_dynamic_log, Metadata),
  159. Function = proplists:get_value(returns_dynamic_emit, Metadata),
  160. FunctionResult = Function(),
  161. ?assert(Value =< FunctionResult),
  162. ?assert(Value =< Function()),
  163. ?assert(FunctionResult =< Function()),
  164. ok
  165. end
  166. },
  167. {"Testing a function provided via metadata",
  168. fun()->
  169. Provided = fun() ->
  170. provided_metadata
  171. end,
  172. lager:md([{provided, Provided}]),
  173. lager:warning("Provided metadata"),
  174. ?assertEqual(1, lager_test_backend:count()),
  175. {_Level, _Time, _Message, Metadata} = lager_test_backend:pop(),
  176. Function = proplists:get_value(provided, Metadata),
  177. ?assertEqual(Provided(), Function()),
  178. ok
  179. end
  180. }
  181. ]
  182. }.
  183. -endif.