Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Convey #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ test:
done

lint:
go get -u github.com/golangci/golangci-lint/cmd/golangci-lint
go get github.com/golangci/golangci-lint/cmd/golangci-lint@v1.32.0
golangci-lint run --enable golint --enable gocyclo
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())
})
}
16 changes: 16 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
module github.com/StudioSol/async

go 1.13

require (
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6 // indirect
github.com/bombsimon/wsl/v2 v2.0.0 // indirect
github.com/coreos/go-etcd v2.0.0+incompatible // indirect
github.com/cpuguy83/go-md2man v1.0.10 // indirect
github.com/go-lintpack/lintpack v0.5.2 // indirect
github.com/golangci/golangci-lint v1.32.0 // indirect
github.com/klauspost/cpuid v1.2.0 // indirect
github.com/securego/gosec v0.0.0-20200103095621-79fbf3af8d83 // indirect
github.com/stretchr/testify v1.6.1
github.com/ugorji/go v1.1.4 // indirect
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8 // indirect
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77 // indirect
sourcegraph.com/sqs/pbtypes v0.0.0-20180604144634-d3ebe8f20ae4 // indirect
)
Loading