return ErrRunLimit when restartLimit exceeded

This commit is contained in:
Nikolay Oskin 2022-08-06 13:30:40 +03:00
parent 14fcf184ae
commit 4a305861d9
2 changed files with 33 additions and 13 deletions

View file

@ -219,7 +219,7 @@ func (p *process) run(pctx context.Context, errCh chan error, logger logger) err
if errCh != nil { if errCh != nil {
errCh <- ErrRunLimit errCh <- ErrRunLimit
} }
return nil return ErrRunLimit
} }
} }
logger("restarting process #%d", p.id) logger("restarting process #%d", p.id)

View file

@ -8,7 +8,7 @@ import (
) )
func TestSuccess(t *testing.T) { func TestSuccess(t *testing.T) {
r := New(nil) r := New()
counter := 0 counter := 0
f := func(name string, ttl time.Duration) error { f := func(name string, ttl time.Duration) error {
counter++ counter++
@ -19,13 +19,13 @@ func TestSuccess(t *testing.T) {
} }
r.Go(func(ctx context.Context) error { r.Go(func(ctx context.Context) error {
return f("one", 1*time.Second) return f("one", 1*time.Second)
}, nil) })
r.Go(func(ctx context.Context) error { r.Go(func(ctx context.Context) error {
return f("two", 2*time.Second) return f("two", 2*time.Second)
}, nil) })
r.Go(func(ctx context.Context) error { r.Go(func(ctx context.Context) error {
return f("three", 3*time.Second) return f("three", 3*time.Second)
}, nil) })
if err := r.Wait(); err != nil { if err := r.Wait(); err != nil {
t.Error("Unexpected error", err) t.Error("Unexpected error", err)
} }
@ -37,7 +37,7 @@ func TestSuccess(t *testing.T) {
} }
func TestError(t *testing.T) { func TestError(t *testing.T) {
r := New(nil) r := New()
f := func(name string, ttl time.Duration) error { f := func(name string, ttl time.Duration) error {
<-time.After(ttl) <-time.After(ttl)
t.Log(name) t.Log(name)
@ -45,13 +45,13 @@ func TestError(t *testing.T) {
} }
r.Go(func(ctx context.Context) error { r.Go(func(ctx context.Context) error {
return f("one", 1*time.Second) return f("one", 1*time.Second)
}, nil) })
r.Go(func(ctx context.Context) error { r.Go(func(ctx context.Context) error {
return f("two", 2*time.Second) return f("two", 2*time.Second)
}, nil) })
r.Go(func(ctx context.Context) error { r.Go(func(ctx context.Context) error {
return f("three", 3*time.Second) return f("three", 3*time.Second)
}, nil) })
if err := r.Wait(); err != nil { if err := r.Wait(); err != nil {
if err.Error() != "error from one" { if err.Error() != "error from one" {
t.Error("Must be error from first routine") t.Error("Must be error from first routine")
@ -61,13 +61,33 @@ func TestError(t *testing.T) {
t.Log("All routines done") 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) { func TestContext(t *testing.T) {
r := New(nil) r := New()
cc := false cc := false
r.Go(func(ctx context.Context) error { r.Go(func(ctx context.Context) error {
<-time.After(1 * time.Second) <-time.After(1 * time.Second)
return nil return nil
}, RunOpt.SetOnDone(Shutdown)) }, RunOptions{OnDone: Shutdown})
r.Go(func(ctx context.Context) error { r.Go(func(ctx context.Context) error {
select { select {
case <-ctx.Done(): case <-ctx.Done():
@ -76,12 +96,12 @@ func TestContext(t *testing.T) {
case <-time.After(3 * time.Second): case <-time.After(3 * time.Second):
return errors.New("Timeout") return errors.New("Timeout")
} }
}, nil) })
if err := r.Wait(); err != nil { if err := r.Wait(); err != nil {
t.Error("Unexpected error", err) t.Error("Unexpected error", err)
} }
if cc { if cc {
t.Log("Second routine succesfuly complete by context done") t.Log("Second routine successfully complete by context done")
} else { } else {
t.Error("Routine not completed by context") t.Error("Routine not completed by context")
} }