Skip to content

Commit f994e1d

Browse files
committed
add more tests
1 parent b5dcadd commit f994e1d

File tree

3 files changed

+98
-9
lines changed

3 files changed

+98
-9
lines changed

core/breaker/breaker.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import (
55
"fmt"
66
"strings"
77
"sync"
8-
"time"
98

109
"github.com/tal-tech/go-zero/core/mathx"
1110
"github.com/tal-tech/go-zero/core/proc"
1211
"github.com/tal-tech/go-zero/core/stat"
1312
"github.com/tal-tech/go-zero/core/stringx"
13+
"github.com/tal-tech/go-zero/core/timex"
1414
)
1515

1616
const (
@@ -195,23 +195,23 @@ type errorWindow struct {
195195

196196
func (ew *errorWindow) add(reason string) {
197197
ew.lock.Lock()
198-
ew.reasons[ew.index] = fmt.Sprintf("%s %s", time.Now().Format(timeFormat), reason)
198+
ew.reasons[ew.index] = fmt.Sprintf("%s %s", timex.Time().Format(timeFormat), reason)
199199
ew.index = (ew.index + 1) % numHistoryReasons
200200
ew.count = mathx.MinInt(ew.count+1, numHistoryReasons)
201201
ew.lock.Unlock()
202202
}
203203

204204
func (ew *errorWindow) String() string {
205-
var builder strings.Builder
205+
var reasons []string
206206

207207
ew.lock.Lock()
208-
for i := ew.index + ew.count - 1; i >= ew.index; i-- {
209-
builder.WriteString(ew.reasons[i%numHistoryReasons])
210-
builder.WriteByte('\n')
208+
// reverse order
209+
for i := ew.index - 1; i >= ew.index-ew.count; i-- {
210+
reasons = append(reasons, ew.reasons[(i+numHistoryReasons)%numHistoryReasons])
211211
}
212212
ew.lock.Unlock()
213213

214-
return builder.String()
214+
return strings.Join(reasons, "\n")
215215
}
216216

217217
type promiseWithReason struct {

core/breaker/breaker_test.go

+89
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package breaker
22

33
import (
44
"errors"
5+
"fmt"
56
"strconv"
7+
"strings"
68
"testing"
79

810
"github.com/stretchr/testify/assert"
@@ -33,6 +35,84 @@ func TestLogReason(t *testing.T) {
3335
assert.Equal(t, numHistoryReasons, errs.count)
3436
}
3537

38+
func TestErrorWindow(t *testing.T) {
39+
tests := []struct {
40+
name string
41+
reasons []string
42+
}{
43+
{
44+
name: "no error",
45+
},
46+
{
47+
name: "one error",
48+
reasons: []string{"foo"},
49+
},
50+
{
51+
name: "two errors",
52+
reasons: []string{"foo", "bar"},
53+
},
54+
{
55+
name: "five errors",
56+
reasons: []string{"first", "second", "third", "fourth", "fifth"},
57+
},
58+
{
59+
name: "six errors",
60+
reasons: []string{"first", "second", "third", "fourth", "fifth", "sixth"},
61+
},
62+
}
63+
64+
for _, test := range tests {
65+
t.Run(test.name, func(t *testing.T) {
66+
var ew errorWindow
67+
for _, reason := range test.reasons {
68+
ew.add(reason)
69+
}
70+
var reasons []string
71+
if len(test.reasons) > numHistoryReasons {
72+
reasons = test.reasons[len(test.reasons)-numHistoryReasons:]
73+
} else {
74+
reasons = test.reasons
75+
}
76+
for _, reason := range reasons {
77+
assert.True(t, strings.Contains(ew.String(), reason), fmt.Sprintf("actual: %s", ew.String()))
78+
}
79+
})
80+
}
81+
}
82+
83+
func TestPromiseWithReason(t *testing.T) {
84+
tests := []struct {
85+
name string
86+
reason string
87+
expect string
88+
}{
89+
{
90+
name: "success",
91+
},
92+
{
93+
name: "success",
94+
reason: "fail",
95+
expect: "fail",
96+
},
97+
}
98+
99+
for _, test := range tests {
100+
t.Run(test.name, func(t *testing.T) {
101+
promise := promiseWithReason{
102+
promise: new(mockedPromise),
103+
errWin: new(errorWindow),
104+
}
105+
if len(test.reason) == 0 {
106+
promise.Accept()
107+
} else {
108+
promise.Reject(test.reason)
109+
}
110+
111+
assert.True(t, strings.Contains(promise.errWin.String(), test.expect))
112+
})
113+
}
114+
}
115+
36116
func BenchmarkGoogleBreaker(b *testing.B) {
37117
br := NewBreaker()
38118
for i := 0; i < b.N; i++ {
@@ -41,3 +121,12 @@ func BenchmarkGoogleBreaker(b *testing.B) {
41121
})
42122
}
43123
}
124+
125+
type mockedPromise struct {
126+
}
127+
128+
func (m *mockedPromise) Accept() {
129+
}
130+
131+
func (m *mockedPromise) Reject() {
132+
}

core/mr/mapreduce.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ func MapReduceWithSource(source <-chan interface{}, mapper MapperFunc, reducer R
127127
drain(collector)
128128
}()
129129

130-
go executeMappers(func(item interface{}, writer Writer) {
131-
mapper(item, writer, cancel)
130+
go executeMappers(func(item interface{}, w Writer) {
131+
mapper(item, w, cancel)
132132
}, source, collector, done.Done(), options.workers)
133133

134134
value, ok := <-output

0 commit comments

Comments
 (0)