Skip to content

Commit

Permalink
goroutine: rename functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrre committed Jun 17, 2024
1 parent 5e14bb4 commit 82ff90b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
13 changes: 6 additions & 7 deletions goroutine/goroutine.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,20 @@ import (
"github.com/pierrre/go-libs/panichandle"
)

// Go executes a function in a new goroutine.
func Go(ctx context.Context, f func(ctx context.Context)) {
// Start executes a function in a new goroutine.
func Start(ctx context.Context, f func(ctx context.Context)) {
go func() {
defer panichandle.Recover(ctx)
f(ctx)
}()
}

// GoWait executes a function in a new goroutine.
//
// Wait executes a function in a new goroutine and allows to wait until it terminates.
// It returns a function that blocks until the goroutine is terminated.
// The caller must call this function.
func GoWait(ctx context.Context, f func(ctx context.Context)) (wait func()) {
func Wait(ctx context.Context, f func(ctx context.Context)) (wait func()) {
ch := make(chan struct{})
Go(ctx, func(ctx context.Context) {
Start(ctx, func(ctx context.Context) {
defer close(ch)
f(ctx)
})
Expand All @@ -37,7 +36,7 @@ func GoWait(ctx context.Context, f func(ctx context.Context)) (wait func()) {
// It calls [sync.WaitGroup.Add] before starting it, and [sync.WaitGroup.Done] when the goroutine is terminated.
func WaitGroup(ctx context.Context, wg *sync.WaitGroup, f func(ctx context.Context)) {
wg.Add(1)
Go(ctx, func(ctx context.Context) {
Start(ctx, func(ctx context.Context) {
defer wg.Done()
f(ctx)
})
Expand Down
16 changes: 8 additions & 8 deletions goroutine/goroutine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,43 @@ import (
"github.com/pierrre/assert"
)

func TestGo(t *testing.T) {
func TestStart(t *testing.T) {
ctx := context.Background()
var called int64
done := make(chan struct{})
Go(ctx, func(ctx context.Context) {
Start(ctx, func(ctx context.Context) {
atomic.AddInt64(&called, 1)
close(done)
})
<-done
assert.Equal(t, called, 1)
}

func TestGoAllocs(t *testing.T) {
func TestStartAllocs(t *testing.T) {
ctx := context.Background()
done := make(chan struct{})
assert.AllocsPerRun(t, 100, func() {
Go(ctx, func(ctx context.Context) {
Start(ctx, func(ctx context.Context) {
done <- struct{}{}
})
<-done
}, 2)
}

func TestGoWait(t *testing.T) {
func TestWait(t *testing.T) {
ctx := context.Background()
var called int64
wait := GoWait(ctx, func(ctx context.Context) {
wait := Wait(ctx, func(ctx context.Context) {
atomic.AddInt64(&called, 1)
})
wait()
assert.Equal(t, called, 1)
}

func TestGoWaitAllocs(t *testing.T) {
func TestWaitAllocs(t *testing.T) {
ctx := context.Background()
assert.AllocsPerRun(t, 100, func() {
wait := GoWait(ctx, func(ctx context.Context) {})
wait := Wait(ctx, func(ctx context.Context) {})
wait()
}, 4)
}
Expand Down

0 comments on commit 82ff90b

Please sign in to comment.