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.

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