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

Use *_test packages to make examples copy-pasteable #124

Merged
merged 2 commits into from
Nov 12, 2023
Merged
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
3 changes: 3 additions & 0 deletions iter/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package iter

var DefaultMaxGoroutines = defaultMaxGoroutines
30 changes: 16 additions & 14 deletions iter/iter_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package iter
package iter_test

import (
"fmt"
"strconv"
"sync/atomic"
"testing"

"github.com/sourcegraph/conc/iter"

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

func ExampleIterator() {
input := []int{1, 2, 3, 4}
iterator := Iterator[int]{
iterator := iter.Iterator[int]{
MaxGoroutines: len(input) / 2,
}

Expand All @@ -32,7 +34,7 @@ func TestIterator(t *testing.T) {
t.Run("safe for reuse", func(t *testing.T) {
t.Parallel()

iterator := Iterator[int]{MaxGoroutines: 999}
iterator := iter.Iterator[int]{MaxGoroutines: 999}

// iter.Concurrency > numInput case that updates iter.Concurrency
iterator.ForEachIdx([]int{1, 2, 3}, func(i int, t *int) {})
Expand All @@ -43,12 +45,12 @@ func TestIterator(t *testing.T) {
t.Run("allows more than defaultMaxGoroutines() concurrent tasks", func(t *testing.T) {
t.Parallel()

wantConcurrency := 2 * defaultMaxGoroutines()
wantConcurrency := 2 * iter.DefaultMaxGoroutines()

maxConcurrencyHit := make(chan struct{})

tasks := make([]int, wantConcurrency)
iterator := Iterator[int]{MaxGoroutines: wantConcurrency}
iterator := iter.Iterator[int]{MaxGoroutines: wantConcurrency}

var concurrentTasks atomic.Int64
iterator.ForEach(tasks, func(t *int) {
Expand Down Expand Up @@ -77,7 +79,7 @@ func TestForEachIdx(t *testing.T) {
t.Parallel()
f := func() {
ints := []int{}
ForEachIdx(ints, func(i int, val *int) {
iter.ForEachIdx(ints, func(i int, val *int) {
panic("this should never be called")
})
}
Expand All @@ -88,7 +90,7 @@ func TestForEachIdx(t *testing.T) {
t.Parallel()
f := func() {
ints := []int{1}
ForEachIdx(ints, func(i int, val *int) {
iter.ForEachIdx(ints, func(i int, val *int) {
panic("super bad thing happened")
})
}
Expand All @@ -98,7 +100,7 @@ func TestForEachIdx(t *testing.T) {
t.Run("mutating inputs is fine", func(t *testing.T) {
t.Parallel()
ints := []int{1, 2, 3, 4, 5}
ForEachIdx(ints, func(i int, val *int) {
iter.ForEachIdx(ints, func(i int, val *int) {
*val += 1
})
require.Equal(t, []int{2, 3, 4, 5, 6}, ints)
Expand All @@ -107,7 +109,7 @@ func TestForEachIdx(t *testing.T) {
t.Run("huge inputs", func(t *testing.T) {
t.Parallel()
ints := make([]int, 10000)
ForEachIdx(ints, func(i int, val *int) {
iter.ForEachIdx(ints, func(i int, val *int) {
*val = i
})
expected := make([]int, 10000)
Expand All @@ -125,7 +127,7 @@ func TestForEach(t *testing.T) {
t.Parallel()
f := func() {
ints := []int{}
ForEach(ints, func(val *int) {
iter.ForEach(ints, func(val *int) {
panic("this should never be called")
})
}
Expand All @@ -136,7 +138,7 @@ func TestForEach(t *testing.T) {
t.Parallel()
f := func() {
ints := []int{1}
ForEach(ints, func(val *int) {
iter.ForEach(ints, func(val *int) {
panic("super bad thing happened")
})
}
Expand All @@ -146,7 +148,7 @@ func TestForEach(t *testing.T) {
t.Run("mutating inputs is fine", func(t *testing.T) {
t.Parallel()
ints := []int{1, 2, 3, 4, 5}
ForEach(ints, func(val *int) {
iter.ForEach(ints, func(val *int) {
*val += 1
})
require.Equal(t, []int{2, 3, 4, 5, 6}, ints)
Expand All @@ -155,7 +157,7 @@ func TestForEach(t *testing.T) {
t.Run("huge inputs", func(t *testing.T) {
t.Parallel()
ints := make([]int, 10000)
ForEach(ints, func(val *int) {
iter.ForEach(ints, func(val *int) {
*val = 1
})
expected := make([]int, 10000)
Expand All @@ -171,7 +173,7 @@ func BenchmarkForEach(b *testing.B) {
b.Run(strconv.Itoa(count), func(b *testing.B) {
ints := make([]int, count)
for i := 0; i < b.N; i++ {
ForEach(ints, func(i *int) {
iter.ForEach(ints, func(i *int) {
*i = 0
})
}
Expand Down
30 changes: 16 additions & 14 deletions iter/map_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package iter
package iter_test

import (
"errors"
"fmt"
"testing"

"github.com/sourcegraph/conc/iter"

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

func ExampleMapper() {
input := []int{1, 2, 3, 4}
mapper := Mapper[int, bool]{
mapper := iter.Mapper[int, bool]{
MaxGoroutines: len(input) / 2,
}

Expand All @@ -27,7 +29,7 @@ func TestMap(t *testing.T) {
t.Parallel()
f := func() {
ints := []int{}
Map(ints, func(val *int) int {
iter.Map(ints, func(val *int) int {
panic("this should never be called")
})
}
Expand All @@ -38,7 +40,7 @@ func TestMap(t *testing.T) {
t.Parallel()
f := func() {
ints := []int{1}
Map(ints, func(val *int) int {
iter.Map(ints, func(val *int) int {
panic("super bad thing happened")
})
}
Expand All @@ -48,7 +50,7 @@ func TestMap(t *testing.T) {
t.Run("mutating inputs is fine, though not recommended", func(t *testing.T) {
t.Parallel()
ints := []int{1, 2, 3, 4, 5}
Map(ints, func(val *int) int {
iter.Map(ints, func(val *int) int {
*val += 1
return 0
})
Expand All @@ -58,7 +60,7 @@ func TestMap(t *testing.T) {
t.Run("basic increment", func(t *testing.T) {
t.Parallel()
ints := []int{1, 2, 3, 4, 5}
res := Map(ints, func(val *int) int {
res := iter.Map(ints, func(val *int) int {
return *val + 1
})
require.Equal(t, []int{2, 3, 4, 5, 6}, res)
Expand All @@ -68,7 +70,7 @@ func TestMap(t *testing.T) {
t.Run("huge inputs", func(t *testing.T) {
t.Parallel()
ints := make([]int, 10000)
res := Map(ints, func(val *int) int {
res := iter.Map(ints, func(val *int) int {
return 1
})
expected := make([]int, 10000)
Expand All @@ -86,7 +88,7 @@ func TestMapErr(t *testing.T) {
t.Parallel()
f := func() {
ints := []int{}
res, err := MapErr(ints, func(val *int) (int, error) {
res, err := iter.MapErr(ints, func(val *int) (int, error) {
panic("this should never be called")
})
require.NoError(t, err)
Expand All @@ -99,7 +101,7 @@ func TestMapErr(t *testing.T) {
t.Parallel()
f := func() {
ints := []int{1}
_, _ = MapErr(ints, func(val *int) (int, error) {
_, _ = iter.MapErr(ints, func(val *int) (int, error) {
panic("super bad thing happened")
})
}
Expand All @@ -109,7 +111,7 @@ func TestMapErr(t *testing.T) {
t.Run("mutating inputs is fine, though not recommended", func(t *testing.T) {
t.Parallel()
ints := []int{1, 2, 3, 4, 5}
res, err := MapErr(ints, func(val *int) (int, error) {
res, err := iter.MapErr(ints, func(val *int) (int, error) {
*val += 1
return 0, nil
})
Expand All @@ -121,7 +123,7 @@ func TestMapErr(t *testing.T) {
t.Run("basic increment", func(t *testing.T) {
t.Parallel()
ints := []int{1, 2, 3, 4, 5}
res, err := MapErr(ints, func(val *int) (int, error) {
res, err := iter.MapErr(ints, func(val *int) (int, error) {
return *val + 1, nil
})
require.NoError(t, err)
Expand All @@ -135,7 +137,7 @@ func TestMapErr(t *testing.T) {
t.Run("error is propagated", func(t *testing.T) {
t.Parallel()
ints := []int{1, 2, 3, 4, 5}
res, err := MapErr(ints, func(val *int) (int, error) {
res, err := iter.MapErr(ints, func(val *int) (int, error) {
if *val == 3 {
return 0, err1
}
Expand All @@ -149,7 +151,7 @@ func TestMapErr(t *testing.T) {
t.Run("multiple errors are propagated", func(t *testing.T) {
t.Parallel()
ints := []int{1, 2, 3, 4, 5}
res, err := MapErr(ints, func(val *int) (int, error) {
res, err := iter.MapErr(ints, func(val *int) (int, error) {
if *val == 3 {
return 0, err1
}
Expand All @@ -167,7 +169,7 @@ func TestMapErr(t *testing.T) {
t.Run("huge inputs", func(t *testing.T) {
t.Parallel()
ints := make([]int, 10000)
res := Map(ints, func(val *int) int {
res := iter.Map(ints, func(val *int) int {
return 1
})
expected := make([]int, 10000)
Expand Down
28 changes: 15 additions & 13 deletions panics/panics_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package panics
package panics_test

import (
"errors"
Expand All @@ -7,12 +7,14 @@ import (
"sync"
"testing"

"github.com/sourcegraph/conc/panics"

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

func ExampleCatcher() {
var pc Catcher
var pc panics.Catcher
i := 0
pc.Try(func() { i += 1 })
pc.Try(func() { panic("abort!") })
Expand All @@ -28,7 +30,7 @@ func ExampleCatcher() {
}

func ExampleCatcher_callers() {
var pc Catcher
var pc panics.Catcher
pc.Try(func() { panic("mayday!") })

recovered := pc.Recovered()
Expand All @@ -50,9 +52,9 @@ func ExampleCatcher_callers() {
// Output:
// github.com/sourcegraph/conc/panics.(*Catcher).tryRecover
// runtime.gopanic
// github.com/sourcegraph/conc/panics.ExampleCatcher_callers.func1
// github.com/sourcegraph/conc/panics_test.ExampleCatcher_callers.func1
// github.com/sourcegraph/conc/panics.(*Catcher).Try
// github.com/sourcegraph/conc/panics.ExampleCatcher_callers
// github.com/sourcegraph/conc/panics_test.ExampleCatcher_callers
// testing.runExample
// testing.runExamples
// testing.(*M).Run
Expand All @@ -63,7 +65,7 @@ func ExampleCatcher_callers() {

func ExampleCatcher_error() {
helper := func() error {
var pc Catcher
var pc panics.Catcher
pc.Try(func() { panic(errors.New("error")) })
return pc.Recovered().AsError()
}
Expand All @@ -88,7 +90,7 @@ func TestCatcher(t *testing.T) {

t.Run("error", func(t *testing.T) {
t.Parallel()
var pc Catcher
var pc panics.Catcher
pc.Try(func() { panic(err1) })
recovered := pc.Recovered()
require.ErrorIs(t, recovered.AsError(), err1)
Expand All @@ -101,30 +103,30 @@ func TestCatcher(t *testing.T) {
})

t.Run("not error", func(t *testing.T) {
var pc Catcher
var pc panics.Catcher
pc.Try(func() { panic("definitely not an error") })
recovered := pc.Recovered()
require.NotErrorIs(t, recovered.AsError(), err1)
require.Nil(t, errors.Unwrap(recovered.AsError()))
})

t.Run("repanic panics", func(t *testing.T) {
var pc Catcher
var pc panics.Catcher
pc.Try(func() { panic(err1) })
require.Panics(t, pc.Repanic)
})

t.Run("repanic does not panic without child panic", func(t *testing.T) {
t.Parallel()
var pc Catcher
var pc panics.Catcher
pc.Try(func() { _ = 1 })
require.NotPanics(t, pc.Repanic)
})

t.Run("is goroutine safe", func(t *testing.T) {
t.Parallel()
var wg sync.WaitGroup
var pc Catcher
var pc panics.Catcher
for i := 0; i < 100; i++ {
i := i
wg.Add(1)
Expand All @@ -148,7 +150,7 @@ func TestRecoveredAsError(t *testing.T) {
t.Run("as error is nil", func(t *testing.T) {
t.Parallel()
fn := func() error {
var c Catcher
var c panics.Catcher
c.Try(func() {})
return c.Recovered().AsError()
}
Expand All @@ -159,7 +161,7 @@ func TestRecoveredAsError(t *testing.T) {
t.Run("as error is not nil", func(t *testing.T) {
t.Parallel()
fn := func() error {
var c Catcher
var c panics.Catcher
c.Try(func() { panic("oh dear!") })
return c.Recovered().AsError()
}
Expand Down
Loading