You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kratos/health/health_test.go

54 lines
1.1 KiB

2 years ago
package health
import (
"fmt"
"golang.org/x/net/context"
"math/rand"
2 years ago
"testing"
"time"
)
type A struct {
}
func (A) Check(ctx context.Context) (interface{}, error) {
fmt.Println("check A")
if rand.Int()%5 == 0 {
return "出错A", fmt.Errorf("错误:%s", "123")
}
2 years ago
return "正常A", nil
}
type B struct {
}
func (B) Check(ctx context.Context) (interface{}, error) {
fmt.Println("check B")
if rand.Int()%5 == 0 {
return "出错B", fmt.Errorf("错误:%s", "123B")
}
2 years ago
return "正常B", nil
}
func TestNew(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())
cm := New(ctx)
cm.RegisterChecker(NewChecker("A", A{}, WithInterval(time.Second), WithInterval(time.Second)))
cm.RegisterChecker(NewChecker("B", B{}, WithInterval(time.Second), WithInterval(time.Second)))
2 years ago
cm.Start()
go func() {
w := cm.NewWatcher()
2 years ago
s := cm.GetStatus()
fmt.Println("----", s)
defer w.Close()
for i := range w.Ch {
fmt.Println("--->>", i, cm.GetStatus(i))
2 years ago
}
}()
2 years ago
time.Sleep(time.Second * 20)
cm.Stop()
t.Log("-----=")
time.Sleep(time.Second * 5)
2 years ago
cancel()
}