Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

197 linhas
7.0 KiB

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