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.

193 line
7.5 KiB

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