Skip to content

Commit

Permalink
add runners tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kostiantyn Masliuk committed Aug 31, 2020
1 parent 90a41af commit e993555
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 13 deletions.
4 changes: 0 additions & 4 deletions atomic.go
Expand Up @@ -41,10 +41,6 @@ func atomicIncr(number *uint64) uint64 {
return atomic.AddUint64(number, 1)
}

func atomicDecr(number *uint64) uint64 {
return atomic.AddUint64(number, ^uint64(0))
}

func atomicSet(number *uint64, value uint64) uint64 {
atomic.StoreUint64(number, value)
return value
Expand Down
6 changes: 6 additions & 0 deletions executors.go → runnables.go
Expand Up @@ -9,6 +9,12 @@ import (

type Runnable func(context.Context) error

func use(err error) Runnable {
return func(ctx context.Context) error {
return err
}
}

func nope(context.Context) error {
return nil
}
Expand Down
17 changes: 9 additions & 8 deletions runners.go
Expand Up @@ -22,7 +22,7 @@ func NewRunnerSync(ctx context.Context, thr Throttler) *rsync {
ctx, cancel := context.WithCancel(ctx)
r := rsync{thr: thr, ctx: ctx}
r.report = func(err error) {
if r.err != nil {
if r.err == nil && err != nil {
r.err = err
cancel()
}
Expand All @@ -47,7 +47,7 @@ func (r *rsync) Run(run Runnable) {
default:
}
if err := run(r.ctx); err != nil {
r.report(fmt.Errorf("function error has happened %w", err))
r.report(fmt.Errorf("runnable error has happened %w", err))
return
}
}
Expand All @@ -69,10 +69,12 @@ func NewRunnerAsync(ctx context.Context, thr Throttler) *rasync {
r := rasync{thr: thr, ctx: ctx}
var once sync.Once
r.report = func(err error) {
once.Do(func() {
r.err = err
cancel()
})
if err != nil {
once.Do(func() {
r.err = err
cancel()
})
}
}
return &r
}
Expand All @@ -97,14 +99,13 @@ func (r *rasync) Run(run Runnable) {
default:
}
if err := run(r.ctx); err != nil {
r.report(fmt.Errorf("function error has happened %w", err))
r.report(fmt.Errorf("runnable error has happened %w", err))
return
}
}()
}

func (r *rasync) Result() error {
r.wg.Wait()
r.report(nil)
return r.err
}
68 changes: 68 additions & 0 deletions runners_test.go
@@ -0,0 +1,68 @@
package gohalt

import (
"context"
"errors"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestRunners(t *testing.T) {
cctx, cancel := context.WithCancel(context.Background())
cancel()
table := map[string]struct {
r Runner
run Runnable
err error
}{
"Runner sync should return error on throttling": {
r: NewRunnerSync(context.Background(), tmock{aerr: errors.New("test")}),
run: nope,
err: fmt.Errorf("throttler error has happened %w", errors.New("test")),
},
"Runner sync should return error on realising error": {
r: NewRunnerSync(context.Background(), tmock{rerr: errors.New("test")}),
run: nope,
err: fmt.Errorf("throttler error has happened %w", errors.New("test")),
},
"Runner sync should return error on runnable error": {
r: NewRunnerSync(context.Background(), tmock{}),
run: use(errors.New("test")),
err: fmt.Errorf("runnable error has happened %w", errors.New("test")),
},
"Runner sync should return error on canceled context": {
r: NewRunnerSync(cctx, tmock{}),
run: nope,
err: fmt.Errorf("context error has happened %w", cctx.Err()),
},
"Runner async should return error on throttling": {
r: NewRunnerAsync(context.Background(), tmock{aerr: errors.New("test")}),
run: nope,
err: fmt.Errorf("throttler error has happened %w", errors.New("test")),
},
"Runner async should return error on realising error": {
r: NewRunnerAsync(context.Background(), tmock{rerr: errors.New("test")}),
run: nope,
err: fmt.Errorf("throttler error has happened %w", errors.New("test")),
},
"Runner async should return error on runnable error": {
r: NewRunnerAsync(context.Background(), tmock{}),
run: use(errors.New("test")),
err: fmt.Errorf("runnable error has happened %w", errors.New("test")),
},
"Runner async should return error on canceled context": {
r: NewRunnerAsync(cctx, tmock{}),
run: nope,
err: fmt.Errorf("context error has happened %w", cctx.Err()),
},
}
for tname, tcase := range table {
t.Run(tname, func(t *testing.T) {
tcase.r.Run(tcase.run)
err := tcase.r.Result()
assert.Equal(t, tcase.err, err)
})
}
}
16 changes: 16 additions & 0 deletions throttlers.go
Expand Up @@ -46,6 +46,22 @@ type tvisitor interface {
tvisitSuppress(context.Context, *tsuppress)
}

type tmock struct {
aerr error
rerr error
}

func (tmock) accept(context.Context, tvisitor) {
}

func (thr tmock) Acquire(context.Context) error {
return thr.aerr
}

func (thr tmock) Release(context.Context) error {
return thr.rerr
}

type techo struct {
err error
}
Expand Down
2 changes: 1 addition & 1 deletion throttlers_test.go
Expand Up @@ -98,7 +98,7 @@ func (t *tcase) result(index int) (err error, dur time.Duration) {
return
}

func TestThrottlerPattern(t *testing.T) {
func TestThrottlers(t *testing.T) {
cctx, cancel := context.WithCancel(context.Background())
cancel()
table := map[string]tcase{
Expand Down

0 comments on commit e993555

Please sign in to comment.