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

103 行
3.8 KiB

  1. -module(lager_trace_test).
  2. -compile([{parse_transform, lager_transform}]).
  3. -ifdef(TEST).
  4. -include_lib("eunit/include/eunit.hrl").
  5. % Our expectation is that the first log entry will appear so we won't actually
  6. % wait out ?FIRST_LOG_ENTRY_TIMEOUT. On the other hand, the second log entry is
  7. % expected never to arrive, so the test will wait out ?SECOND_LOG_ENTRY_TIMEOUT;
  8. % that's why it is shorter.
  9. -define(FIRST_LOG_ENTRY_TIMEOUT, (10 * 1000)). % 10 seconds
  10. -define(SECOND_LOG_ENTRY_TIMEOUT, 1000). % 1 second
  11. -define(FNAME, "test/test1.log").
  12. trace_test_() ->
  13. {timeout,
  14. 10,
  15. {foreach,
  16. fun() ->
  17. file:write_file(?FNAME, ""),
  18. error_logger:tty(false),
  19. application:load(lager),
  20. application:set_env(lager, log_root, "test"),
  21. application:set_env(lager, handlers,
  22. [{lager_file_backend,
  23. [{file, "test1.log"},
  24. {level, none},
  25. {formatter, lager_default_formatter},
  26. {formatter_config, [message, "\n"]}
  27. ]}]),
  28. application:set_env(lager, traces,
  29. [{{lager_file_backend, "test1.log"},
  30. [{tag, mytag}], info}]),
  31. application:set_env(lager, error_logger_redirect, false),
  32. application:set_env(lager, async_threshold, undefined),
  33. lager:start()
  34. end,
  35. fun(_) ->
  36. file:delete(?FNAME),
  37. application:stop(lager),
  38. application:stop(goldrush),
  39. application:unset_env(lager, log_root),
  40. application:unset_env(lager, handlers),
  41. application:unset_env(lager, traces),
  42. application:unset_env(lager, error_logger_redirect),
  43. application:unset_env(lager, async_threshold),
  44. error_logger:tty(true)
  45. end,
  46. [{"Trace combined with log_root",
  47. fun() ->
  48. lager:info([{tag, mytag}], "Test message"),
  49. % Wait until we have the expected log entry in the log file.
  50. case wait_until(fun() ->
  51. count_lines(?FNAME) >= 1
  52. end, ?FIRST_LOG_ENTRY_TIMEOUT) of
  53. ok ->
  54. ok;
  55. {error, timeout} ->
  56. throw({file_empty, file:read_file(?FNAME)})
  57. end,
  58. % Let's wait a little to see that we don't get a duplicate log
  59. % entry.
  60. case wait_until(fun() ->
  61. count_lines(?FNAME) >= 2
  62. end, ?SECOND_LOG_ENTRY_TIMEOUT) of
  63. ok ->
  64. throw({too_many_entries, file:read_file(?FNAME)});
  65. {error, timeout} ->
  66. ok
  67. end
  68. end}
  69. ]}}.
  70. % Wait until Fun() returns true.
  71. wait_until(Fun, Timeout) ->
  72. wait_until(Fun, Timeout, {8, 13}).
  73. wait_until(_Fun, Timeout, {T1, _}) when T1 > Timeout ->
  74. {error, timeout};
  75. wait_until(Fun, Timeout, {T1, T2}) ->
  76. case Fun() of
  77. true ->
  78. ok;
  79. false ->
  80. timer:sleep(T1),
  81. wait_until(Fun, Timeout, {T2, T1 + T2})
  82. end.
  83. % Return the number of lines in a file. Return 0 for a non-existent file.
  84. count_lines(Filename) ->
  85. case file:read_file(Filename) of
  86. {ok, Content} ->
  87. Lines = binary:split(Content, <<"\n">>, [global, trim]),
  88. length(Lines);
  89. {error, _} ->
  90. 0
  91. end.
  92. -endif.