Add a unit test to ensure hook are called in their registration order

This commit is contained in:
David Bariod 2019-01-17 13:42:01 +01:00
parent 11dad09cee
commit 0f544bf278
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 // 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)
}