feat: stable version

Co-authored-by: Dmitry Fedotov <dmitry@uint32.ru>
Co-committed-by: Dmitry Fedotov <dmitry@uint32.ru>
This commit is contained in:
2025-07-25 18:42:16 +03:00
committed by dmitry
parent 82a4641ab0
commit 3aadddbcac
8 changed files with 362 additions and 228 deletions

View File

@@ -2,6 +2,7 @@ package watchdog_test
import (
"context"
"errors"
"reflect"
"testing"
"time"
@@ -16,8 +17,14 @@ type mockChecker struct {
called bool
}
func (m *mockChecker) Func(_ context.Context) (watchdog.Status, error) {
func (m *mockChecker) Func(ctx context.Context) (watchdog.Status, error) {
m.called = true
time.Sleep(time.Millisecond * 10)
if err := ctx.Err(); err != nil {
return watchdog.StatusUnknown, err
}
return m.status, m.err
}
@@ -106,9 +113,9 @@ func TestRunImmediately(t *testing.T) {
}
func TestStart(t *testing.T) {
func TestStartStop(t *testing.T) {
w := new(watchdog.Watchdog)
if _, err := w.Start(t.Context(), 0); err == nil {
if _, err := w.Start(0); err == nil {
t.Error("Start doen't error on empty checks slice")
}
@@ -117,12 +124,12 @@ func TestStart(t *testing.T) {
w.AddChecks(m1.Check(), m2.Check())
out, err := w.Start(t.Context(), 0)
out, err := w.Start(0)
if err != nil {
t.Error("Start returns error", err)
}
out2, err := w.Start(t.Context(), 0)
out2, err := w.Start(0)
if err != nil {
t.Error("second call to Start returns error")
}
@@ -131,13 +138,12 @@ func TestStart(t *testing.T) {
t.Error("returned channels are not equal")
}
time.Sleep(time.Second)
if err := w.Stop(); err != nil {
t.Error("Stop returned error", err)
}
count := 0
go func() {
time.Sleep(time.Second)
if err := w.Stop(); err != nil {
t.Error("Stop returned error", err)
}
}()
for res := range out {
if res.Status != watchdog.StatusOK || res.Error != nil {
@@ -162,22 +168,19 @@ func TestStart(t *testing.T) {
}
}
func TestWatchdogObeysContext(t *testing.T) {
func TestSetTimeout(t *testing.T) {
w := new(watchdog.Watchdog)
w.SetTimeout(time.Millisecond)
m1 := newMockChecker("mock", watchdog.StatusOK, nil)
m2 := newMockChecker("mock2", watchdog.StatusOK, nil)
w.AddChecks(m1.Check())
w.AddChecks(m1.Check(), m2.Check())
ctx, stop := context.WithCancel(t.Context())
if _, err := w.Start(ctx, 2); err != nil {
t.Error(err)
}
stop()
// wait for goroutines to finish
out, _ := w.Start(0)
time.Sleep(time.Second)
if err := w.Stop(); err == nil {
t.Error("no error calling Stop for stopped instance")
w.Stop()
res := <-out
if !(res.Status == watchdog.StatusUnknown) || !errors.Is(res.Error, context.DeadlineExceeded) {
t.Logf("got status: %s, err: %v", res.Status, res.Error)
t.Fatal("incorrect status for timed out op")
}
}