From 4a305861d99d7e2d76615c06ef49dda5d849bf40 Mon Sep 17 00:00:00 2001 From: Nikolay Oskin Date: Sat, 6 Aug 2022 13:30:40 +0300 Subject: [PATCH] return ErrRunLimit when restartLimit exceeded --- rutina.go | 2 +- rutina_test.go | 44 ++++++++++++++++++++++++++++++++------------ 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/rutina.go b/rutina.go index 3cf140c..4206f54 100755 --- a/rutina.go +++ b/rutina.go @@ -219,7 +219,7 @@ func (p *process) run(pctx context.Context, errCh chan error, logger logger) err if errCh != nil { errCh <- ErrRunLimit } - return nil + return ErrRunLimit } } logger("restarting process #%d", p.id) diff --git a/rutina_test.go b/rutina_test.go index 3cbdf40..71fd30d 100755 --- a/rutina_test.go +++ b/rutina_test.go @@ -8,7 +8,7 @@ import ( ) func TestSuccess(t *testing.T) { - r := New(nil) + r := New() counter := 0 f := func(name string, ttl time.Duration) error { counter++ @@ -19,13 +19,13 @@ func TestSuccess(t *testing.T) { } r.Go(func(ctx context.Context) error { return f("one", 1*time.Second) - }, nil) + }) r.Go(func(ctx context.Context) error { return f("two", 2*time.Second) - }, nil) + }) r.Go(func(ctx context.Context) error { return f("three", 3*time.Second) - }, nil) + }) if err := r.Wait(); err != nil { t.Error("Unexpected error", err) } @@ -37,7 +37,7 @@ func TestSuccess(t *testing.T) { } func TestError(t *testing.T) { - r := New(nil) + r := New() f := func(name string, ttl time.Duration) error { <-time.After(ttl) t.Log(name) @@ -45,13 +45,13 @@ func TestError(t *testing.T) { } r.Go(func(ctx context.Context) error { return f("one", 1*time.Second) - }, nil) + }) r.Go(func(ctx context.Context) error { return f("two", 2*time.Second) - }, nil) + }) r.Go(func(ctx context.Context) error { return f("three", 3*time.Second) - }, nil) + }) if err := r.Wait(); err != nil { if err.Error() != "error from one" { t.Error("Must be error from first routine") @@ -61,13 +61,33 @@ func TestError(t *testing.T) { t.Log("All routines done") } +func TestErrorWithRestart(t *testing.T) { + maxCount := 2 + + r := New() + r.Go(func(ctx context.Context) error { + return nil + }) + r.Go(func(ctx context.Context) error { + return errors.New("error") + }, RunOptions{ + OnError: Restart, + MaxCount: &maxCount, + }) + + err := r.Wait() + if err != ErrRunLimit { + t.Error("Must be an error ErrRunLimit from r.Wait since all restarts was executed") + } +} + func TestContext(t *testing.T) { - r := New(nil) + r := New() cc := false r.Go(func(ctx context.Context) error { <-time.After(1 * time.Second) return nil - }, RunOpt.SetOnDone(Shutdown)) + }, RunOptions{OnDone: Shutdown}) r.Go(func(ctx context.Context) error { select { case <-ctx.Done(): @@ -76,12 +96,12 @@ func TestContext(t *testing.T) { case <-time.After(3 * time.Second): return errors.New("Timeout") } - }, nil) + }) if err := r.Wait(); err != nil { t.Error("Unexpected error", err) } if cc { - t.Log("Second routine succesfuly complete by context done") + t.Log("Second routine successfully complete by context done") } else { t.Error("Routine not completed by context") }