Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

142 строки
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. ]).
  26. -compile({parse_transform, lager_transform}).
  27. -ifdef(TEST).
  28. -include_lib("eunit/include/eunit.hrl").
  29. -export([
  30. transform_static/0,
  31. transform_dynamic/0
  32. ]).
  33. -endif.
  34. -ifdef(TEST).
  35. transform_static() ->
  36. static_result.
  37. transform_dynamic() ->
  38. rand:uniform(10).
  39. not_running_test() ->
  40. ?assertEqual({error, lager_not_running}, lager:log(info, self(), "not running")).
  41. setup() ->
  42. error_logger:tty(false),
  43. application:load(lager),
  44. application:set_env(lager, handlers, [{lager_test_backend, info}]),
  45. application:set_env(lager, error_logger_redirect, false),
  46. application:unset_env(lager, traces),
  47. lager:start(),
  48. %% There is a race condition between the application start up, lager logging its own
  49. %% start up condition and several tests that count messages or parse the output of
  50. %% tests. When the lager start up message wins the race, it causes these tests
  51. %% which parse output or count message arrivals to fail.
  52. %%
  53. %% We introduce a sleep here to allow `flush' to arrive *after* the start up
  54. %% message has been received and processed.
  55. %%
  56. %% This race condition was first exposed during the work on
  57. %% 4b5260c4524688b545cc12da6baa2dfa4f2afec9 which introduced the lager
  58. %% manager killer PR.
  59. timer:sleep(5),
  60. gen_event:call(lager_event, lager_test_backend, flush).
  61. cleanup(_) ->
  62. catch ets:delete(lager_config), %% kill the ets config table with fire
  63. application:stop(lager),
  64. application:stop(goldrush),
  65. error_logger:tty(true).
  66. transform_function_test_() ->
  67. {foreach,
  68. fun setup/0,
  69. fun cleanup/1,
  70. [
  71. {"observe that there is nothing up my sleeve",
  72. fun() ->
  73. ?assertEqual(undefined, lager_test_backend:pop()),
  74. ?assertEqual(0, lager_test_backend:count())
  75. end
  76. },
  77. {"logging works",
  78. fun() ->
  79. lager:warning("test message"),
  80. ?assertEqual(1, lager_test_backend:count()),
  81. {Level, _Time, Message, _Metadata} = lager_test_backend:pop(),
  82. ?assertMatch(Level, lager_util:level_to_num(warning)),
  83. ?assertEqual("test message", Message),
  84. ok
  85. end
  86. },
  87. {"Test calling a function returns the same content",
  88. fun() ->
  89. lager:warning("static message"),
  90. ?assertEqual(1, lager_test_backend:count()),
  91. {_Level, _Time, _Message, Metadata} = lager_test_backend:pop(),
  92. ?assertEqual(transform_static(), proplists:get_value(returns_static, Metadata)),
  93. ok
  94. end
  95. },
  96. {"Test calling a function which returns content which can change",
  97. fun() ->
  98. lager:warning("dynamic message 1"),
  99. lager:warning("dynamic message 2"),
  100. lager:warning("dynamic message 3"),
  101. lager:warning("dynamic message 4"),
  102. lager:warning("dynamic message 5"),
  103. lager:warning("dynamic message 6"),
  104. ?assertEqual(6, lager_test_backend:count()),
  105. {_Level1, _Time1, _Message1, Metadata1} = lager_test_backend:pop(),
  106. Replacement1 = proplists:get_value(returns_dynamic, Metadata1),
  107. ?assert((1 =< Replacement1) and (Replacement1 =< 10)),
  108. {_Level2, _Time2, _Message2, Metadata2} = lager_test_backend:pop(),
  109. Replacement2 = proplists:get_value(returns_dynamic, Metadata2),
  110. ?assert((1 =< Replacement2) and (Replacement2 =< 10)),
  111. {_Level3, _Time3, _Message3, Metadata3} = lager_test_backend:pop(),
  112. Replacement3 = proplists:get_value(returns_dynamic, Metadata3),
  113. ?assert((1 =< Replacement3) and (Replacement3 =< 10)),
  114. {_Level4, _Time4, _Message4, Metadata4} = lager_test_backend:pop(),
  115. Replacement4 = proplists:get_value(returns_dynamic, Metadata4),
  116. ?assert((1 =< Replacement4) and (Replacement4 =< 10)),
  117. {_Level5, _Time5, _Message5, Metadata5} = lager_test_backend:pop(),
  118. Replacement5 = proplists:get_value(returns_dynamic, Metadata5),
  119. ?assert((1 =< Replacement5) and (Replacement5 =< 10)),
  120. {_Level6, _Time6, _Message6, Metadata6} = lager_test_backend:pop(),
  121. Replacement6 = proplists:get_value(returns_dynamic, Metadata6),
  122. ?assert((1 =< Replacement6) and (Replacement6 =< 10)),
  123. ok
  124. end
  125. }
  126. ]
  127. }.
  128. -endif.