Skip to content

Commit

Permalink
Remove Convey
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-silveira committed Oct 16, 2020
1 parent f699bcd commit 8beb845
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 127 deletions.
93 changes: 37 additions & 56 deletions async_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@ import (
"testing"
"time"

"github.com/StudioSol/async"
. "github.com/smartystreets/goconvey/convey"
)
"github.com/stretchr/testify/require"

var (
ctx context.Context
"github.com/StudioSol/async"
)

func init() {
ctx = context.Background()
}

func TestRun(t *testing.T) {
Convey("Given two AsyncFunc functions returning non error", t, func() {
t.Run("Two AsyncFunc success", func(t *testing.T) {
var exec [2]bool
f1 := func(_ context.Context) error {
exec[0] = true
Expand All @@ -32,15 +25,13 @@ func TestRun(t *testing.T) {
return nil
}

Convey("It should be executed properly", func() {
err := async.Run(context.Background(), f1, f2)
So(err, ShouldBeNil)
So(exec[0], ShouldBeTrue)
So(exec[1], ShouldBeTrue)
})
err := async.Run(context.Background(), f1, f2)
require.Nil(t, err)
require.True(t, exec[0])
require.True(t, exec[1])
})

Convey("Given two AsyncFunc and one of them returning an error", t, func() {
t.Run("Two AsyncFunc, one fails", func(t *testing.T) {
var errTest = errors.New("test error")
f1 := func(_ context.Context) error {
return errTest
Expand All @@ -50,13 +41,11 @@ func TestRun(t *testing.T) {
return nil
}

Convey("async.Run() should return that error", func() {
err := async.Run(context.Background(), f1, f2)
So(err, ShouldEqual, errTest)
})
err := async.Run(context.Background(), f1, f2)
require.True(t, errors.Is(errTest, err))
})

Convey("Given two AsyncFunc and one of them executing a panic call", t, func() {
t.Run("Two AsyncFunc, one panics", func(t *testing.T) {
f1 := func(_ context.Context) error {
panic(errors.New("test panic"))
}
Expand All @@ -65,14 +54,12 @@ func TestRun(t *testing.T) {
return nil
}

Convey("async.Run() should return that panic error", func() {
err := async.Run(context.Background(), f1, f2)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "async.Run: panic test panic")
})
err := async.Run(context.Background(), f1, f2)
require.Error(t, err)
require.Contains(t, err.Error(), "async.Run: panic test panic")
})

Convey("Given two AsyncFunc and one of them executing a panic call", t, func() {
t.Run("Two AsyncFunc and one panics, the other doesn't execute", func(t *testing.T) {
var mu sync.Mutex
var exec [2]bool

Expand All @@ -89,44 +76,38 @@ func TestRun(t *testing.T) {
return nil
}

Convey("The other function should not be executed if does not need it", func() {
_ = async.Run(context.Background(), f1, f2)
mu.Lock()
So(exec[1], ShouldBeFalse)
mu.Unlock()
})
_ = async.Run(context.Background(), f1, f2)
mu.Lock()
require.False(t, exec[1])
mu.Unlock()
})

Convey("Given an AsyncFunc executing a panic call", t, func() {
t.Run("If panics, cancel context", func(t *testing.T) {
var copyCtx context.Context
f1 := func(ctx context.Context) error {
copyCtx = ctx
panic(errors.New("test panic"))
}

Convey("It should cancel the context", func() {
err := async.Run(context.Background(), f1)
So(err, ShouldNotBeNil)
<-copyCtx.Done()
So(copyCtx.Err(), ShouldNotBeNil)
})
err := async.Run(context.Background(), f1)
require.Error(t, err)
<-copyCtx.Done()
require.Error(t, copyCtx.Err())
})

Convey("Given a cancellable context", t, func() {
t.Run("cancel children when cancellable context is canceled", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())
Convey("When cancelled", func() {
cancel()
Convey("It should cancel its children as well", func() {
var childCtx context.Context
f1 := func(ctx context.Context) error {
childCtx = ctx
return nil
}
err := async.Run(ctx, f1)
So(err, ShouldBeNil)
<-childCtx.Done()
So(childCtx.Err(), ShouldNotBeNil)
})
})
cancel()

var childCtx context.Context
f1 := func(ctx context.Context) error {
childCtx = ctx
return nil
}

err := async.Run(ctx, f1)
require.Nil(t, err)
<-childCtx.Done()
require.Error(t, childCtx.Err())
})
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/StudioSol/async

go 1.13

require github.com/stretchr/testify v1.4.0
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
150 changes: 79 additions & 71 deletions runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,85 +9,93 @@ import (
"github.com/stretchr/testify/require"
)

func TestRunner_AllErrors(t *testing.T) {
expectErr := errors.New("fail")
runner := NewRunner(func(context.Context) error {
return expectErr
}).WaitErrors()
err := runner.Run(context.Background())
require.Equal(t, expectErr, err)
require.Len(t, runner.AllErrors(), 1)
require.Equal(t, expectErr, runner.AllErrors()[0])
}
func TestRunner(t *testing.T) {
t.Run("AllErrors", func(t *testing.T) {
expectErr := errors.New("fail")
runner := NewRunner(func(context.Context) error {
return expectErr
}).WaitErrors()

func TestRunner_WaitErrors(t *testing.T) {
expectErrOne := errors.New("fail")
expectErrTwo := errors.New("fail")
runner := NewRunner(func(context.Context) error {
return expectErrOne
}, func(context.Context) error {
return expectErrTwo
}).WaitErrors()
err := runner.Run(context.Background())
require.False(t, err != expectErrOne && err != expectErrTwo)
require.Len(t, runner.AllErrors(), 2)
}
err := runner.Run(context.Background())
require.Equal(t, expectErr, err)
require.Len(t, runner.AllErrors(), 1)
require.Equal(t, expectErr, runner.AllErrors()[0])
})

t.Run("WaitErrors", func(t *testing.T) {
expectErrOne := errors.New("fail")
expectErrTwo := errors.New("fail")
runner := NewRunner(func(context.Context) error {
return expectErrOne
}, func(context.Context) error {
return expectErrTwo
}).WaitErrors()

func TestRunner_Run(t *testing.T) {
calledFist := false
calledSecond := false
runner := NewRunner(func(context.Context) error {
calledFist = true
return nil
}, func(context.Context) error {
calledSecond = true
return nil
err := runner.Run(context.Background())
require.False(t, err != expectErrOne && err != expectErrTwo)
require.Len(t, runner.AllErrors(), 2)
})
err := runner.Run(context.Background())
require.Nil(t, err)
require.True(t, calledFist)
require.True(t, calledSecond)
}

func TestRunner_WithLimit(t *testing.T) {
order := 1
runner := NewRunner(func(context.Context) error {
require.Equal(t, 1, order)
order++
return nil
}, func(context.Context) error {
require.Equal(t, 2, order)
order++
return nil
}).WithLimit(1)
err := runner.Run(context.Background())
require.Nil(t, err)
}
t.Run("Run", func(t *testing.T) {
calledFist := false
calledSecond := false
runner := NewRunner(func(context.Context) error {
calledFist = true
return nil
}, func(context.Context) error {
calledSecond = true
return nil
})

err := runner.Run(context.Background())
require.Nil(t, err)
require.True(t, calledFist)
require.True(t, calledSecond)
})

func TestRunner_ContextCancelled(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
t.Run("WithLimit", func(t *testing.T) {
order := 1
runner := NewRunner(func(context.Context) error {
require.Equal(t, 1, order)
order++
return nil
}, func(context.Context) error {
require.Equal(t, 2, order)
order++
return nil
}).WithLimit(1)

start := time.Now()
runner := NewRunner(func(context.Context) error {
cancel()
time.Sleep(time.Minute)
return nil
err := runner.Run(context.Background())
require.Nil(t, err)
})
err := runner.Run(ctx)
require.True(t, time.Since(start) < time.Minute)
require.Equal(t, context.Canceled, err)
}

func TestRunner_ContextTimeout(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
t.Run("ContexCancelled", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())

start := time.Now()
runner := NewRunner(func(context.Context) error {
cancel()
time.Sleep(time.Minute)
return nil
})

err := runner.Run(ctx)
require.True(t, time.Since(start) < time.Minute)
require.Equal(t, context.Canceled, err)
})

t.Run("ContextTimeout", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

start := time.Now()
runner := NewRunner(func(context.Context) error {
time.Sleep(time.Minute)
return nil
})

start := time.Now()
runner := NewRunner(func(context.Context) error {
time.Sleep(time.Minute)
return nil
err := runner.Run(ctx)
require.True(t, time.Since(start) < time.Minute)
require.Equal(t, context.DeadlineExceeded, err)
})
err := runner.Run(ctx)
require.True(t, time.Since(start) < time.Minute)
require.Equal(t, context.DeadlineExceeded, err)
}

0 comments on commit 8beb845

Please sign in to comment.