From 66230b287179d93b8a9ca65d5e08495b24163535 Mon Sep 17 00:00:00 2001 From: Aditya Mukerjee Date: Fri, 4 Aug 2017 13:00:10 -0400 Subject: [PATCH] Add test for race condition in hooks --- hook_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/hook_test.go b/hook_test.go index 13f34cb..d14d1e3 100644 --- a/hook_test.go +++ b/hook_test.go @@ -1,6 +1,7 @@ package logrus import ( + "sync" "testing" "github.com/stretchr/testify/assert" @@ -120,3 +121,22 @@ func TestErrorHookShouldFireOnError(t *testing.T) { assert.Equal(t, hook.Fired, true) }) } + +func TestAddHookRace(t *testing.T) { + var wg sync.WaitGroup + wg.Add(2) + hook := new(ErrorHook) + LogAndAssertJSON(t, func(log *Logger) { + go func() { + defer wg.Done() + log.AddHook(hook) + }() + go func() { + defer wg.Done() + log.Error("test") + }() + wg.Wait() + }, func(fields Fields) { + assert.Equal(t, hook.Fired, true) + }) +}