simplify API
This commit is contained in:
28
checks.go
28
checks.go
@@ -35,32 +35,32 @@ func GetHTTP(addr string, timeout time.Duration) (CheckFunc, error) {
|
|||||||
timeout = DefaultTimeout
|
timeout = DefaultTimeout
|
||||||
}
|
}
|
||||||
|
|
||||||
return func(ctx context.Context) (Status, error) {
|
return func(ctx context.Context) error {
|
||||||
ctx, cancel := context.WithTimeout(ctx, timeout)
|
ctx, cancel := context.WithTimeout(ctx, timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return StatusUnknown, fmt.Errorf("failed to create http request: %w", err)
|
return fmt.Errorf("failed to create http request: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return StatusDown, fmt.Errorf("do request failed: %w", err)
|
return fmt.Errorf("do request failed: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return StatusDown, fmt.Errorf("err reading response body: %w", err)
|
return fmt.Errorf("err reading response body: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return StatusDown, fmt.Errorf("got HTTP response code %d, body: %s", resp.StatusCode, string(body))
|
return fmt.Errorf("got HTTP response code %d, body: %s", resp.StatusCode, string(body))
|
||||||
}
|
}
|
||||||
|
|
||||||
return StatusOK, nil
|
return nil
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,27 +83,27 @@ func HeadHTTP(addr string, timeout time.Duration) (CheckFunc, error) {
|
|||||||
timeout = DefaultTimeout
|
timeout = DefaultTimeout
|
||||||
}
|
}
|
||||||
|
|
||||||
return func(ctx context.Context) (Status, error) {
|
return func(ctx context.Context) error {
|
||||||
ctx, cancel := context.WithTimeout(ctx, timeout)
|
ctx, cancel := context.WithTimeout(ctx, timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodHead, u.String(), nil)
|
req, err := http.NewRequestWithContext(ctx, http.MethodHead, u.String(), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return StatusUnknown, fmt.Errorf("failed to create http request: %w", err)
|
return fmt.Errorf("failed to create http request: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return StatusDown, fmt.Errorf("do request failed: %w", err)
|
return fmt.Errorf("do request failed: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return StatusDown, fmt.Errorf("got HTTP response code %d", resp.StatusCode)
|
return fmt.Errorf("got HTTP response code %d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
return StatusOK, nil
|
return nil
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,7 +121,7 @@ func DialTCP(addr string, timeout time.Duration) (CheckFunc, error) {
|
|||||||
timeout = DefaultTimeout
|
timeout = DefaultTimeout
|
||||||
}
|
}
|
||||||
|
|
||||||
return func(ctx context.Context) (Status, error) {
|
return func(ctx context.Context) error {
|
||||||
deadline := time.Now().Add(timeout)
|
deadline := time.Now().Add(timeout)
|
||||||
if t, ok := ctx.Deadline(); ok && t.Before(deadline) {
|
if t, ok := ctx.Deadline(); ok && t.Before(deadline) {
|
||||||
deadline = t
|
deadline = t
|
||||||
@@ -129,11 +129,11 @@ func DialTCP(addr string, timeout time.Duration) (CheckFunc, error) {
|
|||||||
|
|
||||||
conn, err := net.DialTimeout("tcp", addr, time.Until(deadline))
|
conn, err := net.DialTimeout("tcp", addr, time.Until(deadline))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return StatusDown, fmt.Errorf("error dialing: %w", err)
|
return fmt.Errorf("error dialing: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
return StatusOK, nil
|
return nil
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,14 +22,10 @@ func TestGetHTTPSuccess(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
status, err := fn(t.Context())
|
err = fn(t.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if status != watchdog.StatusOK {
|
|
||||||
t.Fatalf("incorrect status %s, expected %s", status, watchdog.StatusOK)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetHTTPError(t *testing.T) {
|
func TestGetHTTPError(t *testing.T) {
|
||||||
@@ -39,8 +35,8 @@ func TestGetHTTPError(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
status, err := fn(t.Context())
|
err = fn(t.Context())
|
||||||
if status != watchdog.StatusDown || err == nil {
|
if err == nil {
|
||||||
t.Errorf("incorrect status for unavalable host")
|
t.Errorf("incorrect status for unavalable host")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -57,14 +53,10 @@ func TestHeadHTTPSuccess(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
status, err := fn(t.Context())
|
err = fn(t.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if status != watchdog.StatusOK {
|
|
||||||
t.Fatalf("incorrect status %s, expected %s", status, watchdog.StatusOK)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHeadHTTPError(t *testing.T) {
|
func TestHeadHTTPError(t *testing.T) {
|
||||||
@@ -74,8 +66,8 @@ func TestHeadHTTPError(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
status, err := fn(t.Context())
|
err = fn(t.Context())
|
||||||
if status != watchdog.StatusDown || err == nil {
|
if err == nil {
|
||||||
t.Errorf("incorrect status for unavalable host")
|
t.Errorf("incorrect status for unavalable host")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -101,15 +93,10 @@ func TestDialTCPSuccess(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
status, err := fn(t.Context())
|
err = fn(t.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if status != watchdog.StatusOK {
|
|
||||||
t.Error("incorrect status for available addr")
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDialTCPError(t *testing.T) {
|
func TestDialTCPError(t *testing.T) {
|
||||||
@@ -120,8 +107,8 @@ func TestDialTCPError(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
status, err := fn(t.Context())
|
err = fn(t.Context())
|
||||||
if (status != watchdog.StatusDown) || (err == nil) {
|
if err == nil {
|
||||||
t.Errorf("incorrect status %s, expected %s", status, watchdog.StatusDown)
|
t.Errorf("incorrect status")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
26
entity.go
26
entity.go
@@ -5,30 +5,11 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Status uint8
|
|
||||||
|
|
||||||
const (
|
|
||||||
StatusUnknown Status = iota
|
|
||||||
StatusOK
|
|
||||||
StatusDown
|
|
||||||
)
|
|
||||||
|
|
||||||
func (s Status) String() string {
|
|
||||||
switch s {
|
|
||||||
case StatusOK:
|
|
||||||
return "OK"
|
|
||||||
case StatusDown:
|
|
||||||
return "DOWN"
|
|
||||||
default:
|
|
||||||
return "UNKNOWN"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// CheckFunc is a function that does the actual work.
|
// CheckFunc is a function that does the actual work.
|
||||||
// This package provides a number of check functions but
|
// This package provides a number of check functions but
|
||||||
// any function matching the signature may be provided.
|
// any function matching the signature may be provided.
|
||||||
// It must obey context dealine and cancellation.
|
// It must obey context dealine and cancellation.
|
||||||
type CheckFunc func(context.Context) (Status, error)
|
type CheckFunc func(context.Context) error
|
||||||
|
|
||||||
// Check represents a check that must be run by Watchdog.
|
// Check represents a check that must be run by Watchdog.
|
||||||
type Check struct {
|
type Check struct {
|
||||||
@@ -38,7 +19,6 @@ type Check struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type CheckResult struct {
|
type CheckResult struct {
|
||||||
Name string // identifier of check
|
Name string // identifier of check
|
||||||
Status Status // status as retuned by CheckFunc
|
Error error // error returned by CheckFunc
|
||||||
Error error // error returned by CheckFunc
|
|
||||||
}
|
}
|
||||||
|
|||||||
102
watchdog.go
102
watchdog.go
@@ -16,7 +16,7 @@ var (
|
|||||||
// Watchdog keeps checks to run either periodically
|
// Watchdog keeps checks to run either periodically
|
||||||
// or on demand.
|
// or on demand.
|
||||||
type Watchdog struct {
|
type Watchdog struct {
|
||||||
checks checksMap
|
checks map[string]*wdCheck
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
|
|
||||||
events chan CheckResult // output channel
|
events chan CheckResult // output channel
|
||||||
@@ -28,47 +28,6 @@ type Watchdog struct {
|
|||||||
running int // number of active checks monitored
|
running int // number of active checks monitored
|
||||||
}
|
}
|
||||||
|
|
||||||
type checksMap struct {
|
|
||||||
m map[string]*wdCheck
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *checksMap) build() {
|
|
||||||
if c.m == nil {
|
|
||||||
c.m = make(map[string]*wdCheck)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *checksMap) Map() map[string]*wdCheck {
|
|
||||||
c.build()
|
|
||||||
|
|
||||||
return c.m
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *checksMap) Set(key string, v *wdCheck) {
|
|
||||||
c.build()
|
|
||||||
|
|
||||||
c.m[key] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *checksMap) Lookup(key string) (*wdCheck, bool) {
|
|
||||||
c.build()
|
|
||||||
|
|
||||||
v, ok := c.m[key]
|
|
||||||
return v, ok
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *checksMap) Delete(key string) {
|
|
||||||
c.build()
|
|
||||||
|
|
||||||
delete(c.m, key)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *checksMap) Len() int {
|
|
||||||
c.build()
|
|
||||||
|
|
||||||
return len(c.m)
|
|
||||||
}
|
|
||||||
|
|
||||||
type wdCheck struct {
|
type wdCheck struct {
|
||||||
check Check
|
check Check
|
||||||
stop chan struct{}
|
stop chan struct{}
|
||||||
@@ -77,13 +36,16 @@ type wdCheck struct {
|
|||||||
// New creates instance of Watchdog with
|
// New creates instance of Watchdog with
|
||||||
// provided checks.
|
// provided checks.
|
||||||
func New(checks ...Check) *Watchdog {
|
func New(checks ...Check) *Watchdog {
|
||||||
w := Watchdog{}
|
w := Watchdog{
|
||||||
|
checks: make(map[string]*wdCheck),
|
||||||
|
}
|
||||||
|
|
||||||
for _, c := range checks {
|
for _, c := range checks {
|
||||||
nc := &wdCheck{
|
nc := &wdCheck{
|
||||||
check: c,
|
check: c,
|
||||||
}
|
}
|
||||||
|
|
||||||
w.checks.Set(c.Name, nc)
|
w.checks[c.Name] = nc
|
||||||
}
|
}
|
||||||
|
|
||||||
return &w
|
return &w
|
||||||
@@ -121,14 +83,18 @@ func (w *Watchdog) AddChecks(checks ...Check) {
|
|||||||
w.mu.Lock()
|
w.mu.Lock()
|
||||||
defer w.mu.Unlock()
|
defer w.mu.Unlock()
|
||||||
|
|
||||||
|
if w.checks == nil {
|
||||||
|
w.checks = make(map[string]*wdCheck)
|
||||||
|
}
|
||||||
|
|
||||||
for _, c := range checks {
|
for _, c := range checks {
|
||||||
nc := &wdCheck{
|
nc := &wdCheck{
|
||||||
check: c,
|
check: c,
|
||||||
}
|
}
|
||||||
|
|
||||||
old, haveOld := w.checks.Lookup(c.Name)
|
old, haveOld := w.checks[c.Name]
|
||||||
|
|
||||||
w.checks.Set(c.Name, nc)
|
w.checks[c.Name] = nc
|
||||||
|
|
||||||
if w.monitoring {
|
if w.monitoring {
|
||||||
w.startMonitoring(nc)
|
w.startMonitoring(nc)
|
||||||
@@ -146,7 +112,7 @@ func (w *Watchdog) RemoveChecks(names ...string) {
|
|||||||
defer w.mu.Unlock()
|
defer w.mu.Unlock()
|
||||||
|
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
c, ok := w.checks.Lookup(name)
|
c, ok := w.checks[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -155,7 +121,7 @@ func (w *Watchdog) RemoveChecks(names ...string) {
|
|||||||
w.stopMonitoring(c)
|
w.stopMonitoring(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
w.checks.Delete(name)
|
delete(w.checks, name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,7 +138,7 @@ func (w *Watchdog) Start(concurrency int) (<-chan CheckResult, error) {
|
|||||||
w.mu.Lock()
|
w.mu.Lock()
|
||||||
defer w.mu.Unlock()
|
defer w.mu.Unlock()
|
||||||
|
|
||||||
if w.checks.Len() == 0 {
|
if len(w.checks) == 0 {
|
||||||
return nil, ErrNotConfigured
|
return nil, ErrNotConfigured
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,7 +147,7 @@ func (w *Watchdog) Start(concurrency int) (<-chan CheckResult, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if concurrency == 0 {
|
if concurrency == 0 {
|
||||||
concurrency = w.checks.Len()
|
concurrency = len(w.checks)
|
||||||
}
|
}
|
||||||
|
|
||||||
if w.timeout == 0 {
|
if w.timeout == 0 {
|
||||||
@@ -191,7 +157,7 @@ func (w *Watchdog) Start(concurrency int) (<-chan CheckResult, error) {
|
|||||||
w.events = make(chan CheckResult, concurrency)
|
w.events = make(chan CheckResult, concurrency)
|
||||||
w.limiter = make(chan struct{}, concurrency)
|
w.limiter = make(chan struct{}, concurrency)
|
||||||
|
|
||||||
for _, c := range w.checks.Map() {
|
for _, c := range w.checks {
|
||||||
w.startMonitoring(c)
|
w.startMonitoring(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,7 +174,7 @@ func (w *Watchdog) Stop() error {
|
|||||||
return ErrNotRunning
|
return ErrNotRunning
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range w.checks.Map() {
|
for _, c := range w.checks {
|
||||||
w.stopMonitoring(c)
|
w.stopMonitoring(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -221,13 +187,13 @@ func (w *Watchdog) Stop() error {
|
|||||||
func (w *Watchdog) RunImmediately(ctx context.Context, concurrency int) ([]CheckResult, error) {
|
func (w *Watchdog) RunImmediately(ctx context.Context, concurrency int) ([]CheckResult, error) {
|
||||||
w.mu.Lock()
|
w.mu.Lock()
|
||||||
|
|
||||||
if w.checks.Len() == 0 {
|
if len(w.checks) == 0 {
|
||||||
w.mu.Unlock()
|
w.mu.Unlock()
|
||||||
return nil, ErrNotConfigured
|
return nil, ErrNotConfigured
|
||||||
}
|
}
|
||||||
|
|
||||||
cp := w.copyChecks()
|
cp := w.copyChecks()
|
||||||
w.mu.Unlock() // release
|
w.mu.Unlock()
|
||||||
|
|
||||||
if concurrency == 0 {
|
if concurrency == 0 {
|
||||||
concurrency = len(cp)
|
concurrency = len(cp)
|
||||||
@@ -248,8 +214,8 @@ func (w *Watchdog) RunImmediately(ctx context.Context, concurrency int) ([]Check
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *Watchdog) copyChecks() []Check {
|
func (w *Watchdog) copyChecks() []Check {
|
||||||
cp := make([]Check, 0, w.checks.Len())
|
cp := make([]Check, 0, len(w.checks))
|
||||||
for _, v := range w.checks.Map() {
|
for _, v := range w.checks {
|
||||||
cp = append(cp, v.check)
|
cp = append(cp, v.check)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -267,11 +233,7 @@ func (w *Watchdog) startMonitoring(wdc *wdCheck) {
|
|||||||
w.running++
|
w.running++
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
state := CheckResult{
|
var curr error = nil
|
||||||
// on first run return anything
|
|
||||||
// other that OK
|
|
||||||
Status: StatusOK,
|
|
||||||
}
|
|
||||||
|
|
||||||
ticker := time.Tick(wdc.check.Interval)
|
ticker := time.Tick(wdc.check.Interval)
|
||||||
|
|
||||||
@@ -281,23 +243,22 @@ func (w *Watchdog) startMonitoring(wdc *wdCheck) {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), w.timeout)
|
ctx, cancel := context.WithTimeout(context.Background(), w.timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
status, err := c.Check(ctx)
|
err := c.Check(ctx)
|
||||||
|
|
||||||
<-w.limiter
|
<-w.limiter
|
||||||
|
|
||||||
r := CheckResult{
|
r := CheckResult{
|
||||||
Name: c.Name,
|
Name: c.Name,
|
||||||
Status: status,
|
Error: err,
|
||||||
Error: err,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// if status changed or we've got an error
|
// if status changed or we've got an error
|
||||||
// then report this
|
// then report this
|
||||||
if r.Status != state.Status || r.Error != nil {
|
if !errors.Is(r.Error, curr) {
|
||||||
w.events <- r
|
w.events <- r
|
||||||
}
|
}
|
||||||
|
|
||||||
state = r
|
curr = r.Error
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-ticker:
|
case <-ticker:
|
||||||
@@ -336,12 +297,11 @@ func runChecksConcurrently(ctx context.Context, ch []Check, concurrency int) []C
|
|||||||
|
|
||||||
// relying on assumption that CheckFunc obeys context
|
// relying on assumption that CheckFunc obeys context
|
||||||
// cancellation
|
// cancellation
|
||||||
status, err := e.Check(ctx)
|
err := e.Check(ctx)
|
||||||
|
|
||||||
r := CheckResult{
|
r := CheckResult{
|
||||||
Name: e.Name,
|
Name: e.Name,
|
||||||
Status: status,
|
Error: err,
|
||||||
Error: err,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
done <- r
|
done <- r
|
||||||
|
|||||||
@@ -12,20 +12,19 @@ import (
|
|||||||
|
|
||||||
type mockChecker struct {
|
type mockChecker struct {
|
||||||
name string
|
name string
|
||||||
status watchdog.Status
|
|
||||||
err error
|
err error
|
||||||
called bool
|
called bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockChecker) Func(ctx context.Context) (watchdog.Status, error) {
|
func (m *mockChecker) Func(ctx context.Context) error {
|
||||||
m.called = true
|
m.called = true
|
||||||
|
|
||||||
time.Sleep(time.Millisecond * 10)
|
time.Sleep(time.Millisecond * 10)
|
||||||
if err := ctx.Err(); err != nil {
|
if err := ctx.Err(); err != nil {
|
||||||
return watchdog.StatusUnknown, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return m.status, m.err
|
return m.err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockChecker) HasBeenCalled() bool {
|
func (m *mockChecker) HasBeenCalled() bool {
|
||||||
@@ -40,8 +39,8 @@ func (m *mockChecker) Check() watchdog.Check {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newMockChecker(name string, s watchdog.Status, err error) *mockChecker {
|
func newMockChecker(name string, err error) *mockChecker {
|
||||||
return &mockChecker{name: name, status: s, err: err}
|
return &mockChecker{name: name, err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCreateWith_new(t *testing.T) {
|
func TestCreateWith_new(t *testing.T) {
|
||||||
@@ -59,8 +58,8 @@ func TestNew(t *testing.T) {
|
|||||||
t.Errorf("expected len = 0")
|
t.Errorf("expected len = 0")
|
||||||
}
|
}
|
||||||
|
|
||||||
m1 := newMockChecker("mock", watchdog.StatusOK, nil)
|
m1 := newMockChecker("mock", nil)
|
||||||
m2 := newMockChecker("mock2", watchdog.StatusOK, nil)
|
m2 := newMockChecker("mock2", nil)
|
||||||
|
|
||||||
w = watchdog.New()
|
w = watchdog.New()
|
||||||
w.AddChecks(m1.Check(), m2.Check())
|
w.AddChecks(m1.Check(), m2.Check())
|
||||||
@@ -96,8 +95,8 @@ func TestRunImmediately(t *testing.T) {
|
|||||||
t.Errorf("expected zero len slice for empty instance, got %d", len(out))
|
t.Errorf("expected zero len slice for empty instance, got %d", len(out))
|
||||||
}
|
}
|
||||||
|
|
||||||
m1 := newMockChecker("mock", watchdog.StatusOK, nil)
|
m1 := newMockChecker("mock", nil)
|
||||||
m2 := newMockChecker("mock2", watchdog.StatusOK, nil)
|
m2 := newMockChecker("mock2", nil)
|
||||||
|
|
||||||
w = watchdog.New(m1.Check(), m2.Check())
|
w = watchdog.New(m1.Check(), m2.Check())
|
||||||
out, _ = w.RunImmediately(t.Context(), 0)
|
out, _ = w.RunImmediately(t.Context(), 0)
|
||||||
@@ -119,8 +118,8 @@ func TestStartStop(t *testing.T) {
|
|||||||
t.Error("Start doen't error on empty checks slice")
|
t.Error("Start doen't error on empty checks slice")
|
||||||
}
|
}
|
||||||
|
|
||||||
m1 := newMockChecker("mock", watchdog.StatusOK, nil)
|
m1 := newMockChecker("mock", nil)
|
||||||
m2 := newMockChecker("mock2", watchdog.StatusOK, nil)
|
m2 := newMockChecker("mock2", nil)
|
||||||
|
|
||||||
w.AddChecks(m1.Check(), m2.Check())
|
w.AddChecks(m1.Check(), m2.Check())
|
||||||
|
|
||||||
@@ -167,7 +166,7 @@ func TestStartStop(t *testing.T) {
|
|||||||
func TestSetTimeout(t *testing.T) {
|
func TestSetTimeout(t *testing.T) {
|
||||||
w := new(watchdog.Watchdog)
|
w := new(watchdog.Watchdog)
|
||||||
w.SetTimeout(time.Millisecond)
|
w.SetTimeout(time.Millisecond)
|
||||||
m1 := newMockChecker("mock", watchdog.StatusOK, nil)
|
m1 := newMockChecker("mock", nil)
|
||||||
w.AddChecks(m1.Check())
|
w.AddChecks(m1.Check())
|
||||||
|
|
||||||
out, _ := w.Start(0)
|
out, _ := w.Start(0)
|
||||||
@@ -175,8 +174,8 @@ func TestSetTimeout(t *testing.T) {
|
|||||||
w.Stop()
|
w.Stop()
|
||||||
res := <-out
|
res := <-out
|
||||||
|
|
||||||
if !(res.Status == watchdog.StatusUnknown) || !errors.Is(res.Error, context.DeadlineExceeded) {
|
if !errors.Is(res.Error, context.DeadlineExceeded) {
|
||||||
t.Logf("got status: %s, err: %v", res.Status, res.Error)
|
t.Logf("got err: %v", res.Error)
|
||||||
t.Fatal("incorrect status for timed out op")
|
t.Fatal("incorrect status for timed out op")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user