-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_group_test.go
More file actions
158 lines (127 loc) · 3.72 KB
/
Copy patherror_group_test.go
File metadata and controls
158 lines (127 loc) · 3.72 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package error_group
import (
"errors"
"github.com/jgroeneveld/trial/assert"
"math/rand"
"reflect"
"strings"
"sync"
"testing"
"time"
)
func TestErrorGroup_Add(t *testing.T) {
eg := NewErrorGroup()
var wg sync.WaitGroup
numToAdd := 100000
maxRoutines := 1000
guard := make(chan struct{}, maxRoutines)
for i := 0; i < numToAdd; i++ {
guard <- struct{}{}
wg.Add(1)
go func() {
<-guard
eg.Add(errors.New(generateRandomString(20)))
wg.Done()
}()
}
wg.Wait()
t.Run("verify Add() operations were successful and Len() returns correct value", func(t *testing.T) {
assert.Equal(t, eg.Len(), numToAdd)
})
}
func TestErrorGroup_All(t *testing.T) {
firstMessage := "first message"
lastMessage := "last message"
middleMessage := "middle message"
eg := NewErrorGroup()
eg.Add(errors.New(firstMessage))
numToAdd := 10
for i := 0; i < numToAdd; i++ {
eg.Add(errors.New(middleMessage))
}
eg.Add(errors.New(lastMessage))
allErrors := eg.All()
t.Run("verify All() returns the correct first error message", func(t *testing.T) {
assert.Equal(t, firstMessage, allErrors[0].Error())
})
t.Run("verify All() returns the correct middle messages", func(t *testing.T) {
assert.Equal(t, lastMessage, allErrors[len(allErrors)-1].Error())
})
t.Run("verify All() returns the correct last message", func(t *testing.T) {
for i := 1; i < len(allErrors)-1; i++ {
assert.Equal(t, middleMessage, allErrors[i].Error())
}
})
t.Run("verify All() returns a new slice that is not affected by Add()", func(t *testing.T) {
eg.Add(errors.New(generateRandomString(10)))
assert.Equal(t, len(allErrors), 12)
})
}
func TestErrorGroup_Error(t *testing.T) {
eg := NewErrorGroup()
first := "first message"
last := "last message"
eg.Add(errors.New(first))
eg.Add(errors.New(last))
t.Run("verify Error() returns a correctly formatted error string", func(t *testing.T) {
errString := eg.Error()
assert.Equal(t, strings.Join([]string{first, last}, "\n"), errString)
})
t.Run("verify Error() returns the empty string when there are no errors", func(t *testing.T) {
other := NewErrorGroup()
assert.Equal(t, "", other.Error())
})
}
func TestErrorGroup_First(t *testing.T) {
eg := NewErrorGroup()
first := "first message"
last := "last message"
eg.Add(errors.New(first))
eg.Add(errors.New(last))
t.Run("verify First() returns the correct error string", func(t *testing.T) {
assert.Equal(t, first, eg.First().Error())
})
}
func TestErrorGroup_Last(t *testing.T) {
eg := NewErrorGroup()
first := "first message"
last := "last message"
eg.Add(errors.New(first))
eg.Add(errors.New(last))
t.Run("verify Last() returns the correct error string", func(t *testing.T) {
assert.Equal(t, last, eg.Last().Error())
})
}
func TestErrorGroup_Len(t *testing.T) {
eg := NewErrorGroup()
toAdd := 10
for i := 0; i < toAdd; i++ {
eg.Add(errors.New(generateRandomString(10)))
}
t.Run("verify Len() returns the correct number of errors", func(t *testing.T) {
assert.Equal(t, toAdd, eg.Len())
})
}
func TestErrorGroup_ToError(t *testing.T) {
eg := NewErrorGroup()
first := "first message"
last := "last message"
eg.Add(errors.New(first))
eg.Add(errors.New(last))
t.Run("verify ToError() returns the correctly formatted error message", func(t *testing.T) {
assert.True(t, reflect.DeepEqual(errors.New(eg.Error()), eg.ToError()))
})
t.Run("verify ToError() returns nil when there are no errors", func(t *testing.T) {
other := NewErrorGroup()
assert.Nil(t, other.ToError())
})
}
func generateRandomString(n int) string {
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
rand.Seed(time.Now().UnixNano())
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}