MCPcopy Index your code
hub / github.com/sourcegraph/conc / TestResultErrorGroup

Function TestResultErrorGroup

pool/result_error_pool_test.go:14–131  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

12)
13
14func TestResultErrorGroup(t *testing.T) {
15 t.Parallel()
16
17 err1 := errors.New("err1")
18 err2 := errors.New("err2")
19
20 t.Run("panics on configuration after init", func(t *testing.T) {
21 t.Run("before wait", func(t *testing.T) {
22 t.Parallel()
23 g := NewWithResults[int]().WithErrors()
24 g.Go(func() (int, error) { return 0, nil })
25 require.Panics(t, func() { g.WithMaxGoroutines(10) })
26 })
27
28 t.Run("after wait", func(t *testing.T) {
29 t.Parallel()
30 g := NewWithResults[int]().WithErrors()
31 g.Go(func() (int, error) { return 0, nil })
32 _, _ = g.Wait()
33 require.Panics(t, func() { g.WithMaxGoroutines(10) })
34 })
35 })
36
37 t.Run("wait returns no error if no errors", func(t *testing.T) {
38 t.Parallel()
39 g := NewWithResults[int]().WithErrors()
40 g.Go(func() (int, error) { return 1, nil })
41 res, err := g.Wait()
42 require.NoError(t, err)
43 require.Equal(t, []int{1}, res)
44 })
45
46 t.Run("wait error if func returns error", func(t *testing.T) {
47 t.Parallel()
48 g := NewWithResults[int]().WithErrors()
49 g.Go(func() (int, error) { return 0, err1 })
50 res, err := g.Wait()
51 require.Len(t, res, 0) // errored value is ignored
52 require.ErrorIs(t, err, err1)
53 })
54
55 t.Run("WithCollectErrored", func(t *testing.T) {
56 t.Parallel()
57 g := NewWithResults[int]().WithErrors().WithCollectErrored()
58 g.Go(func() (int, error) { return 0, err1 })
59 res, err := g.Wait()
60 require.Len(t, res, 1) // errored value is collected
61 require.ErrorIs(t, err, err1)
62 })
63
64 t.Run("WithFirstError", func(t *testing.T) {
65 t.Parallel()
66 g := NewWithResults[int]().WithErrors().WithFirstError()
67 synchronizer := make(chan struct{})
68 g.Go(func() (int, error) {
69 <-synchronizer
70 // This test has an intrinsic race condition that can be reproduced
71 // by adding a `defer time.Sleep(time.Second)` before the `defer

Callers

nothing calls this directly

Calls 6

WithErrorsMethod · 0.45
GoMethod · 0.45
WithMaxGoroutinesMethod · 0.45
WaitMethod · 0.45
WithCollectErroredMethod · 0.45
WithFirstErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…