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,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) {

View File

@ -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)
}