mirror of https://github.com/grafana/grafana
parent
cfb061ddab
commit
9edaa3fa8c
@ -0,0 +1,22 @@ |
||||
package lifecycle |
||||
|
||||
type Event int |
||||
|
||||
const ( |
||||
ApplicationStarting Event = iota |
||||
ApplicationStarted |
||||
) |
||||
|
||||
type EventHandlerFunc func() |
||||
|
||||
var listeners = map[int][]EventHandlerFunc{} |
||||
|
||||
func AddListener(evt Event, fn EventHandlerFunc) { |
||||
listeners[int(evt)] = append(listeners[int(evt)], fn) |
||||
} |
||||
|
||||
func Notify(evt Event) { |
||||
for _, handler := range listeners[int(evt)] { |
||||
handler() |
||||
} |
||||
} |
@ -0,0 +1,35 @@ |
||||
package lifecycle |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
. "github.com/smartystreets/goconvey/convey" |
||||
) |
||||
|
||||
func TestLifecycle(t *testing.T) { |
||||
Convey("TestLifecycle", t, func() { |
||||
Convey("Given listeners", func() { |
||||
applicationStartingCounter := 0 |
||||
AddListener(ApplicationStarting, func() { |
||||
applicationStartingCounter++ |
||||
}) |
||||
|
||||
applicationStartedCounter := 0 |
||||
AddListener(ApplicationStarted, func() { |
||||
applicationStartedCounter++ |
||||
}) |
||||
|
||||
Convey("When notify application starting should call listener", func() { |
||||
Notify(ApplicationStarting) |
||||
So(applicationStartingCounter, ShouldEqual, 1) |
||||
So(applicationStartedCounter, ShouldEqual, 0) |
||||
}) |
||||
|
||||
Convey("When notify application started should call listener", func() { |
||||
Notify(ApplicationStarted) |
||||
So(applicationStartingCounter, ShouldEqual, 0) |
||||
So(applicationStartedCounter, ShouldEqual, 1) |
||||
}) |
||||
}) |
||||
}) |
||||
} |
Loading…
Reference in new issue