~evanj/errgroupcount

ref: 45e72d8ba8fcca5ae47d06e98648bb2047cfadc6 errgroupcount/errgroupcount_test.go -rw-r--r-- 2.6 KiB
45e72d8bEvan M Jones Feat(*): Project init. Breaking into its own module, outside 1 year, 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package errgroupcount_test

import (
	"errors"
	"testing"
	"time"

	"git.evanjon.es/i/errgroupcount"
	"github.com/stretchr/testify/assert"
)

var (
	ErrOne = errors.New("this is the first error")
	ErrTwo = errors.New("this is the second error")
)

func TestBasic(t *testing.T) {
	t.Parallel()
	eg := errgroupcount.Group{}
	eg.Go(func() error { return nil })
	eg.Go(func() error { return nil })
	eg.Go(func() error { return nil })
	assert.Equal(t, nil, eg.WaitCount(3))
}

func TestZero(t *testing.T) {
	t.Parallel()
	eg := errgroupcount.Group{}
	assert.Equal(t, nil, eg.WaitCount(0))
	assert.Equal(t, nil, eg.WaitCount(-1))
}

func TestBasicError(t *testing.T) {
	t.Parallel()
	eg := errgroupcount.Group{}
	eg.Go(func() error { return ErrOne })
	assert.Equal(t, ErrOne, eg.WaitCount(1))
}

func TestBasicFirst(t *testing.T) {
	t.Parallel()
	eg := errgroupcount.Group{}
	eg.Go(func() error { return ErrOne })
	eg.Go(func() error {
		time.Sleep(time.Millisecond * 100) // We can assume eg obtains ErrOne first.
		return ErrTwo
	})
	assert.Equal(t, ErrOne, eg.WaitCount(1))
}

func TestIsError(t *testing.T) {
	t.Parallel()
	eg := errgroupcount.Group{}
	eg.Go(func() error { return ErrOne })
	eg.Go(func() error { return nil })
	assert.Equal(t, ErrOne, eg.WaitCount(1))
}

func TestNoError(t *testing.T) {
	t.Parallel()
	eg := errgroupcount.Group{}
	eg.Go(func() error { return ErrOne })
	eg.Go(func() error { return nil })
	assert.Equal(t, nil, eg.WaitCount(2))
}

func TestIsErrorTwo(t *testing.T) {
	t.Parallel()
	eg := errgroupcount.Group{}
	eg.Go(func() error { return ErrOne })
	eg.Go(func() error { return ErrOne })
	eg.Go(func() error { return ErrOne })
	eg.Go(func() error { return ErrOne })
	eg.Go(func() error { return ErrOne })
	eg.Go(func() error { return ErrOne })
	eg.Go(func() error { return ErrOne })
	assert.Equal(t, ErrOne, eg.WaitCount(7))
}

func TestIsErrorThree(t *testing.T) {
	t.Parallel()
	eg := errgroupcount.Group{}
	eg.Go(func() error { return ErrOne })
	eg.Go(func() error { return ErrOne })
	eg.Go(func() error { return ErrOne })
	eg.Go(func() error { return ErrOne })
	eg.Go(func() error { return ErrOne })
	eg.Go(func() error { return ErrOne })
	eg.Go(func() error { return ErrOne })
	assert.Equal(t, nil, eg.WaitCount(8))
}

func TestLots(t *testing.T) {
	t.Parallel()
	eg := errgroupcount.Group{}
	top := 1000
	for x := 0; x < top/2; x++ {
		eg.Go(func() error { return ErrOne })
	}
	for x := 0; x < top/2; x++ {
		eg.Go(func() error { return nil })
	}
	assert.Equal(t, ErrOne, eg.WaitCount(top/2))
	assert.Equal(t, nil, eg.WaitCount(top/2+1))
	assert.Equal(t, nil, eg.WaitCount(top))
}