Merge pull request #890 from sirupsen/hook_fire_order_test

Add a unit test to ensure hook are called in their registration order
This commit is contained in:
David Bariod 2019-01-17 13:59:11 +01:00 committed by GitHub
commit 659e47340e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 0 deletions

View File

@ -190,3 +190,27 @@ func TestAddHookRace(t *testing.T) {
// actually assert on the hook
})
}
type HookCallFunc struct {
F func()
}
func (h *HookCallFunc) Levels() []Level {
return AllLevels
}
func (h *HookCallFunc) Fire(e *Entry) error {
h.F()
return nil
}
func TestHookFireOrder(t *testing.T) {
checkers := []string{}
h := LevelHooks{}
h.Add(&HookCallFunc{F: func() { checkers = append(checkers, "first hook") }})
h.Add(&HookCallFunc{F: func() { checkers = append(checkers, "second hook") }})
h.Add(&HookCallFunc{F: func() { checkers = append(checkers, "third hook") }})
h.Fire(InfoLevel, &Entry{})
require.Equal(t, []string{"first hook", "second hook", "third hook"}, checkers)
}