Skip to content

Commit

Permalink
go.mod: update toolchain base to 1.22
Browse files Browse the repository at this point in the history
- Use math/rand/v2 where useful.
- Remove the "shuffled" helper from the tests.
  • Loading branch information
creachadair committed Aug 17, 2024
1 parent fb5fb44 commit 9d7d7e2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 19 deletions.
15 changes: 2 additions & 13 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"io"
"log"
"math/rand"
"math/rand/v2"
"strings"
"time"

Expand Down Expand Up @@ -106,17 +106,6 @@ func ExampleGroup_Limit() {
// Max active ≤ 4: true
}

func shuffled(n int) []int {
vs := make([]int, n)
for i := range vs {
vs[i] = i + 1
}
rand.Shuffle(n, func(i, j int) {
vs[i], vs[j] = vs[j], vs[i]
})
return vs
}

type slowReader struct {
n int
d time.Duration
Expand Down Expand Up @@ -171,7 +160,7 @@ func ExampleCollector() {
})

const numTasks = 25
input := shuffled(500)
input := rand.Perm(500)

// Start a bunch of tasks to find elements in the input...
g := taskgroup.New(nil)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/creachadair/taskgroup

go 1.21
go 1.22

require github.com/fortytw2/leaktest v1.3.0
10 changes: 5 additions & 5 deletions taskgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package taskgroup_test
import (
"context"
"errors"
"math/rand"
"math/rand/v2"
"reflect"
"sync"
"sync/atomic"
Expand All @@ -17,7 +17,7 @@ import (
const numTasks = 64

// randms returns a random duration of up to n milliseconds.
func randms(n int) time.Duration { return time.Duration(rand.Intn(n)) * time.Millisecond }
func randms(n int) time.Duration { return time.Duration(rand.IntN(n)) * time.Millisecond }

// busyWork returns a Task that does nothing for n ms and returns err.
func busyWork(n int, err error) taskgroup.Task {
Expand Down Expand Up @@ -265,7 +265,7 @@ func TestCollector(t *testing.T) {
var sum int
c := taskgroup.NewCollector(func(v int) { sum += v })

vs := shuffled(15)
vs := rand.Perm(15)
g := taskgroup.New(nil)

for i, v := range vs {
Expand Down Expand Up @@ -295,7 +295,7 @@ func TestCollector_Report(t *testing.T) {
c := taskgroup.NewCollector(func(v int) { sum += v })

g := taskgroup.New(nil).Go(c.Report(func(report func(v int)) error {
for _, v := range shuffled(10) {
for _, v := range rand.Perm(10) {
report(v)
}
return nil
Expand All @@ -304,7 +304,7 @@ func TestCollector_Report(t *testing.T) {
if err := g.Wait(); err != nil {
t.Errorf("Unexpected error from group: %v", err)
}
if want := (10 * 11) / 2; sum != want {
if want := (9 * 10) / 2; sum != want {
t.Errorf("Final result: got %d, want %d", sum, want)
}
}
Expand Down

0 comments on commit 9d7d7e2

Please sign in to comment.