From c81b014919474ff7d6b85a7af5189a1c4e2733bc Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Thu, 16 Dec 2021 18:11:34 +0800 Subject: [PATCH] save --- history/history.go | 18 ++++++++++++------ history/history_test.go | 24 +++++++++++++++++++++++- 2 files changed, 35 insertions(+), 7 deletions(-) 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) }