您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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