From 0d428368e3c71fdefab39927ca142e3bccabd543 Mon Sep 17 00:00:00 2001
From: "John R. Daily" <git@epep.us>
Date: Thu, 19 May 2016 14:55:51 -0400
Subject: [PATCH 4/5] * Undo unnecessary whitespace * Make
 lager_handler_watcher test more resilient by scanning for   messages of
 interest rather than assuming we know exactly what   messages will arrive at
 the test backend

---
 src/lager_app.erl             |  4 ++--
 src/lager_handler_watcher.erl | 22 ++++++++++++++++------
 2 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/src/lager_app.erl b/src/lager_app.erl
index 8efd26b..7b02e54 100644
--- a/src/lager_app.erl
+++ b/src/lager_app.erl
@@ -237,9 +237,9 @@ boot() ->
     lager:update_loglevel_config(?DEFAULT_SINK),
 
     SavedHandlers = start_error_logger_handler(
-                                    get_env(lager, error_logger_redirect, true),
+                      get_env(lager, error_logger_redirect, true),
                       interpret_hwm(get_env(lager, error_logger_hwm, 0)),
-                                    get_env(lager, error_logger_whitelist, [])
+                      get_env(lager, error_logger_whitelist, [])
                      ),
 
     SavedHandlers.
diff --git a/src/lager_handler_watcher.erl b/src/lager_handler_watcher.erl
index d5fe140..4e595e4 100644
--- a/src/lager_handler_watcher.erl
+++ b/src/lager_handler_watcher.erl
@@ -178,12 +178,10 @@ reinstall_on_runtime_failure_test_() ->
                     try
                         ?assert(lists:member(lager_crash_backend, gen_event:which_handlers(lager_event))),
                         timer:sleep(6000),
-                        _ = lager_test_backend:pop(), %% throw away application start up message
-                        _ = lager_test_backend:pop(), %% throw away gen_event crash message
-                        {_Severity, _Date, Msg, _Metadata} = lager_test_backend:pop(),
-                        ?assertEqual("Lager event handler lager_crash_backend exited with reason crash", lists:flatten(Msg)),
-                        {_Severity2, _Date2, Msg2, _Metadata2} = lager_test_backend:pop(),
-                        ?assertMatch("Lager failed to install handler lager_crash_backend into lager_event, retrying later :"++_, lists:flatten(Msg2)),
+
+                        pop_until("Lager event handler lager_crash_backend exited with reason crash", fun lists:flatten/1),
+                        pop_until("Lager failed to install handler lager_crash_backend into lager_event, retrying later",
+                                  fun(Msg) -> string:substr(lists:flatten(Msg), 1, 84) end),
                         ?assertEqual(false, lists:member(lager_crash_backend, gen_event:which_handlers(lager_event)))
                     after
                        application:stop(lager),
@@ -194,5 +192,17 @@ reinstall_on_runtime_failure_test_() ->
         ]
     }.
 
+pop_until(String, Fun) ->
+    try_backend_pop(lager_test_backend:pop(), String, Fun).
+
+try_backend_pop(undefined, String, _Fun) ->
+    throw("Not found: " ++ String);
+try_backend_pop({_Severity, _Date, Msg, _Metadata}, String, Fun) ->
+    case Fun(Msg) of
+        String ->
+            ok;
+        _ ->
+            try_backend_pop(lager_test_backend:pop(), String, Fun)
+    end.
 
 -endif.
-- 
2.8.3

