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