Evan Chen 2021-12-16 18:11:34 +08:00
parent b232685b63
commit c81b014919
2 changed files with 35 additions and 7 deletions

View File

@ -29,17 +29,19 @@ var quit chan struct{}
func init() { func init() {
Tunnel = make(chan *History) Tunnel = make(chan *History)
quit = make(chan struct{}) quit = make(chan struct{}, 1)
} }
func Start(r Receiver) { func Start(r Receiver) {
go func() { go func() {
for {
select { select {
case h := <-Tunnel: case h := <-Tunnel:
r(h) r(h)
case <-quit: case <-quit:
return return
} }
}
}() }()
} }
@ -47,6 +49,10 @@ func Stop() {
quit <- struct{}{} quit <- struct{}{}
} }
func Send(h *History) {
Tunnel <- h
}
type Receiver func(*History) type Receiver func(*History)
var DBReceiver Receiver = func(h *History) { var DBReceiver Receiver = func(h *History) {

View File

@ -3,8 +3,30 @@ package history
import ( import (
"testing" "testing"
"time" "time"
"kumoly.io/kumoly/app/util"
) )
func TestHistory(t *testing.T) { 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)
} }