diff --git a/history/history.go b/history/history.go index 99af2d1..95dc4b0 100644 --- a/history/history.go +++ b/history/history.go @@ -29,16 +29,18 @@ var quit chan struct{} func init() { Tunnel = make(chan *History) - quit = make(chan struct{}) + quit = make(chan struct{}, 1) } func Start(r Receiver) { go func() { - select { - case h := <-Tunnel: - r(h) - case <-quit: - return + for { + select { + case h := <-Tunnel: + r(h) + case <-quit: + return + } } }() } @@ -47,6 +49,10 @@ func Stop() { quit <- struct{}{} } +func Send(h *History) { + Tunnel <- h +} + type Receiver func(*History) var DBReceiver Receiver = func(h *History) { diff --git a/history/history_test.go b/history/history_test.go index e522f6b..b9852fa 100644 --- a/history/history_test.go +++ b/history/history_test.go @@ -3,8 +3,30 @@ package history import ( "testing" "time" + + "kumoly.io/kumoly/app/util" ) func TestHistory(t *testing.T) { - time.Sleep(time.Second * 30) + t.Log("start") + Start(func(h *History) { + t.Logf("%+v\n", h) + }) + go func() { + for { + Send(&History{ + Module: "test", + Type: INFO, + Message: "testing", + Body: time.Now().String(), + Caller: util.Caller(2), + }) + time.Sleep(time.Second) + } + }() + + time.Sleep(time.Second * 20) + Stop() + t.Log("stoped") + time.Sleep(time.Second * 2) }