You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/mr/mapreduce_test.go

609 lines
13 KiB
Go

package mr
4 years ago
import (
"context"
4 years ago
"errors"
"io"
4 years ago
"log"
"runtime"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.uber.org/goleak"
4 years ago
)
var errDummy = errors.New("dummy")
func init() {
log.SetOutput(io.Discard)
4 years ago
}
func TestFinish(t *testing.T) {
defer goleak.VerifyNone(t)
4 years ago
var total uint32
err := Finish(func() error {
atomic.AddUint32(&total, 2)
return nil
}, func() error {
atomic.AddUint32(&total, 3)
return nil
}, func() error {
atomic.AddUint32(&total, 5)
return nil
})
assert.Equal(t, uint32(10), atomic.LoadUint32(&total))
assert.Nil(t, err)
}
func TestFinishNone(t *testing.T) {
defer goleak.VerifyNone(t)
4 years ago
assert.Nil(t, Finish())
}
func TestFinishVoidNone(t *testing.T) {
defer goleak.VerifyNone(t)
4 years ago
FinishVoid()
}
func TestFinishErr(t *testing.T) {
defer goleak.VerifyNone(t)
4 years ago
var total uint32
err := Finish(func() error {
atomic.AddUint32(&total, 2)
return nil
}, func() error {
atomic.AddUint32(&total, 3)
return errDummy
}, func() error {
atomic.AddUint32(&total, 5)
return nil
})
assert.Equal(t, errDummy, err)
}
func TestFinishVoid(t *testing.T) {
defer goleak.VerifyNone(t)
4 years ago
var total uint32
FinishVoid(func() {
atomic.AddUint32(&total, 2)
}, func() {
atomic.AddUint32(&total, 3)
}, func() {
atomic.AddUint32(&total, 5)
})
assert.Equal(t, uint32(10), atomic.LoadUint32(&total))
}
func TestForEach(t *testing.T) {
const tasks = 1000
t.Run("all", func(t *testing.T) {
defer goleak.VerifyNone(t)
var count uint32
ForEach(func(source chan<- int) {
for i := 0; i < tasks; i++ {
source <- i
}
}, func(item int) {
atomic.AddUint32(&count, 1)
}, WithWorkers(-1))
assert.Equal(t, tasks, int(count))
})
t.Run("odd", func(t *testing.T) {
defer goleak.VerifyNone(t)
var count uint32
ForEach(func(source chan<- int) {
for i := 0; i < tasks; i++ {
source <- i
}
}, func(item int) {
if item%2 == 0 {
atomic.AddUint32(&count, 1)
}
})
assert.Equal(t, tasks/2, int(count))
})
t.Run("all", func(t *testing.T) {
defer goleak.VerifyNone(t)
assert.PanicsWithValue(t, "foo", func() {
ForEach(func(source chan<- int) {
for i := 0; i < tasks; i++ {
source <- i
}
}, func(item int) {
panic("foo")
})
})
})
}
func TestGeneratePanic(t *testing.T) {
defer goleak.VerifyNone(t)
t.Run("all", func(t *testing.T) {
assert.PanicsWithValue(t, "foo", func() {
ForEach(func(source chan<- int) {
panic("foo")
}, func(item int) {
})
})
})
}
4 years ago
func TestMapperPanic(t *testing.T) {
defer goleak.VerifyNone(t)
const tasks = 1000
var run int32
t.Run("all", func(t *testing.T) {
assert.PanicsWithValue(t, "foo", func() {
_, _ = MapReduce(func(source chan<- int) {
for i := 0; i < tasks; i++ {
4 years ago
source <- i
}
}, func(item int, writer Writer[int], cancel func(error)) {
atomic.AddInt32(&run, 1)
panic("foo")
}, func(pipe <-chan int, writer Writer[int], cancel func(error)) {
})
4 years ago
})
assert.True(t, atomic.LoadInt32(&run) < tasks/2)
})
4 years ago
}
func TestMapReduce(t *testing.T) {
defer goleak.VerifyNone(t)
4 years ago
tests := []struct {
name string
mapper MapperFunc[int, int]
reducer ReducerFunc[int, int]
4 years ago
expectErr error
expectValue int
4 years ago
}{
{
name: "simple",
4 years ago
expectErr: nil,
expectValue: 30,
},
{
name: "cancel with error",
mapper: func(v int, writer Writer[int], cancel func(error)) {
4 years ago
if v%3 == 0 {
cancel(errDummy)
}
writer.Write(v * v)
},
expectErr: errDummy,
},
{
name: "cancel with nil",
mapper: func(v int, writer Writer[int], cancel func(error)) {
4 years ago
if v%3 == 0 {
cancel(nil)
}
writer.Write(v * v)
},
expectErr: ErrCancelWithNil,
4 years ago
},
{
name: "cancel with more",
reducer: func(pipe <-chan int, writer Writer[int], cancel func(error)) {
4 years ago
var result int
for item := range pipe {
result += item
4 years ago
if result > 10 {
cancel(errDummy)
}
}
writer.Write(result)
},
expectErr: errDummy,
},
}
t.Run("MapReduce", func(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.mapper == nil {
test.mapper = func(v int, writer Writer[int], cancel func(error)) {
writer.Write(v * v)
4 years ago
}
}
if test.reducer == nil {
test.reducer = func(pipe <-chan int, writer Writer[int], cancel func(error)) {
var result int
for item := range pipe {
result += item
}
writer.Write(result)
}
4 years ago
}
value, err := MapReduce(func(source chan<- int) {
for i := 1; i < 5; i++ {
source <- i
}
}, test.mapper, test.reducer, WithWorkers(runtime.NumCPU()))
4 years ago
assert.Equal(t, test.expectErr, err)
assert.Equal(t, test.expectValue, value)
})
}
})
4 years ago
t.Run("MapReduce", func(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.mapper == nil {
test.mapper = func(v int, writer Writer[int], cancel func(error)) {
writer.Write(v * v)
}
}
if test.reducer == nil {
test.reducer = func(pipe <-chan int, writer Writer[int], cancel func(error)) {
var result int
for item := range pipe {
result += item
}
writer.Write(result)
}
}
source := make(chan int)
go func() {
for i := 1; i < 5; i++ {
source <- i
}
close(source)
}()
value, err := MapReduceChan(source, test.mapper, test.reducer, WithWorkers(-1))
assert.Equal(t, test.expectErr, err)
assert.Equal(t, test.expectValue, value)
})
}
})
}
3 years ago
func TestMapReduceWithReduerWriteMoreThanOnce(t *testing.T) {
defer goleak.VerifyNone(t)
3 years ago
assert.Panics(t, func() {
MapReduce(func(source chan<- int) {
3 years ago
for i := 0; i < 10; i++ {
source <- i
}
}, func(item int, writer Writer[int], cancel func(error)) {
3 years ago
writer.Write(item)
}, func(pipe <-chan int, writer Writer[string], cancel func(error)) {
3 years ago
drain(pipe)
writer.Write("one")
writer.Write("two")
})
})
}
4 years ago
func TestMapReduceVoid(t *testing.T) {
defer goleak.VerifyNone(t)
4 years ago
var value uint32
tests := []struct {
name string
mapper MapperFunc[int, int]
reducer VoidReducerFunc[int]
4 years ago
expectValue uint32
expectErr error
}{
{
name: "simple",
4 years ago
expectValue: 30,
expectErr: nil,
},
{
name: "cancel with error",
mapper: func(v int, writer Writer[int], cancel func(error)) {
4 years ago
if v%3 == 0 {
cancel(errDummy)
}
writer.Write(v * v)
},
expectErr: errDummy,
},
{
name: "cancel with nil",
mapper: func(v int, writer Writer[int], cancel func(error)) {
4 years ago
if v%3 == 0 {
cancel(nil)
}
writer.Write(v * v)
},
expectErr: ErrCancelWithNil,
},
{
name: "cancel with more",
reducer: func(pipe <-chan int, cancel func(error)) {
4 years ago
for item := range pipe {
result := atomic.AddUint32(&value, uint32(item))
4 years ago
if result > 10 {
cancel(errDummy)
}
}
},
expectErr: errDummy,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
4 years ago
atomic.StoreUint32(&value, 0)
if test.mapper == nil {
test.mapper = func(v int, writer Writer[int], cancel func(error)) {
4 years ago
writer.Write(v * v)
}
}
if test.reducer == nil {
test.reducer = func(pipe <-chan int, cancel func(error)) {
4 years ago
for item := range pipe {
atomic.AddUint32(&value, uint32(item))
4 years ago
}
}
}
err := MapReduceVoid(func(source chan<- int) {
4 years ago
for i := 1; i < 5; i++ {
source <- i
}
}, test.mapper, test.reducer)
assert.Equal(t, test.expectErr, err)
if err == nil {
assert.Equal(t, test.expectValue, atomic.LoadUint32(&value))
}
})
}
}
func TestMapReduceVoidWithDelay(t *testing.T) {
defer goleak.VerifyNone(t)
4 years ago
var result []int
err := MapReduceVoid(func(source chan<- int) {
4 years ago
source <- 0
source <- 1
}, func(i int, writer Writer[int], cancel func(error)) {
4 years ago
if i == 0 {
time.Sleep(time.Millisecond * 50)
}
writer.Write(i)
}, func(pipe <-chan int, cancel func(error)) {
4 years ago
for item := range pipe {
i := item
4 years ago
result = append(result, i)
}
})
assert.Nil(t, err)
assert.Equal(t, 2, len(result))
assert.Equal(t, 1, result[0])
assert.Equal(t, 0, result[1])
}
func TestMapReducePanic(t *testing.T) {
defer goleak.VerifyNone(t)
assert.Panics(t, func() {
_, _ = MapReduce(func(source chan<- int) {
source <- 0
source <- 1
}, func(i int, writer Writer[int], cancel func(error)) {
writer.Write(i)
}, func(pipe <-chan int, writer Writer[int], cancel func(error)) {
for range pipe {
panic("panic")
}
})
4 years ago
})
}
4 years ago
func TestMapReducePanicOnce(t *testing.T) {
defer goleak.VerifyNone(t)
assert.Panics(t, func() {
_, _ = MapReduce(func(source chan<- int) {
for i := 0; i < 100; i++ {
source <- i
}
}, func(i int, writer Writer[int], cancel func(error)) {
if i == 0 {
panic("foo")
}
writer.Write(i)
}, func(pipe <-chan int, writer Writer[int], cancel func(error)) {
for range pipe {
panic("bar")
}
})
})
4 years ago
}
func TestMapReducePanicBothMapperAndReducer(t *testing.T) {
defer goleak.VerifyNone(t)
assert.Panics(t, func() {
_, _ = MapReduce(func(source chan<- int) {
source <- 0
source <- 1
}, func(item int, writer Writer[int], cancel func(error)) {
panic("foo")
}, func(pipe <-chan int, writer Writer[int], cancel func(error)) {
panic("bar")
})
4 years ago
})
}
func TestMapReduceVoidCancel(t *testing.T) {
defer goleak.VerifyNone(t)
4 years ago
var result []int
err := MapReduceVoid(func(source chan<- int) {
4 years ago
source <- 0
source <- 1
}, func(i int, writer Writer[int], cancel func(error)) {
4 years ago
if i == 1 {
cancel(errors.New("anything"))
}
writer.Write(i)
}, func(pipe <-chan int, cancel func(error)) {
4 years ago
for item := range pipe {
i := item
4 years ago
result = append(result, i)
}
})
assert.NotNil(t, err)
assert.Equal(t, "anything", err.Error())
}
func TestMapReduceVoidCancelWithRemains(t *testing.T) {
defer goleak.VerifyNone(t)
var done int32
4 years ago
var result []int
err := MapReduceVoid(func(source chan<- int) {
4 years ago
for i := 0; i < defaultWorkers*2; i++ {
source <- i
}
atomic.AddInt32(&done, 1)
}, func(i int, writer Writer[int], cancel func(error)) {
4 years ago
if i == defaultWorkers/2 {
cancel(errors.New("anything"))
}
writer.Write(i)
}, func(pipe <-chan int, cancel func(error)) {
4 years ago
for item := range pipe {
result = append(result, item)
4 years ago
}
})
assert.NotNil(t, err)
assert.Equal(t, "anything", err.Error())
assert.Equal(t, int32(1), done)
4 years ago
}
func TestMapReduceWithoutReducerWrite(t *testing.T) {
defer goleak.VerifyNone(t)
uids := []int{1, 2, 3}
res, err := MapReduce(func(source chan<- int) {
for _, uid := range uids {
source <- uid
}
}, func(item int, writer Writer[int], cancel func(error)) {
writer.Write(item)
}, func(pipe <-chan int, writer Writer[int], cancel func(error)) {
drain(pipe)
// not calling writer.Write(...), should not panic
})
assert.Equal(t, ErrReduceNoOutput, err)
assert.Equal(t, 0, res)
}
func TestMapReduceVoidPanicInReducer(t *testing.T) {
defer goleak.VerifyNone(t)
const message = "foo"
assert.Panics(t, func() {
var done int32
_ = MapReduceVoid(func(source chan<- int) {
for i := 0; i < defaultWorkers*2; i++ {
source <- i
}
atomic.AddInt32(&done, 1)
}, func(i int, writer Writer[int], cancel func(error)) {
writer.Write(i)
}, func(pipe <-chan int, cancel func(error)) {
panic(message)
}, WithWorkers(1))
})
}
func TestForEachWithContext(t *testing.T) {
defer goleak.VerifyNone(t)
var done int32
ctx, cancel := context.WithCancel(context.Background())
ForEach(func(source chan<- int) {
for i := 0; i < defaultWorkers*2; i++ {
source <- i
}
atomic.AddInt32(&done, 1)
}, func(i int) {
if i == defaultWorkers/2 {
cancel()
}
}, WithContext(ctx))
}
func TestMapReduceWithContext(t *testing.T) {
defer goleak.VerifyNone(t)
var done int32
var result []int
ctx, cancel := context.WithCancel(context.Background())
err := MapReduceVoid(func(source chan<- int) {
for i := 0; i < defaultWorkers*2; i++ {
source <- i
}
atomic.AddInt32(&done, 1)
}, func(i int, writer Writer[int], c func(error)) {
if i == defaultWorkers/2 {
cancel()
}
writer.Write(i)
}, func(pipe <-chan int, cancel func(error)) {
for item := range pipe {
i := item
result = append(result, i)
}
}, WithContext(ctx))
assert.NotNil(t, err)
assert.Equal(t, context.DeadlineExceeded, err)
}
4 years ago
func BenchmarkMapReduce(b *testing.B) {
b.ReportAllocs()
mapper := func(v int64, writer Writer[int64], cancel func(error)) {
writer.Write(v * v)
4 years ago
}
reducer := func(input <-chan int64, writer Writer[int64], cancel func(error)) {
4 years ago
var result int64
for v := range input {
result += v
4 years ago
}
writer.Write(result)
}
for i := 0; i < b.N; i++ {
MapReduce(func(input chan<- int64) {
4 years ago
for j := 0; j < 2; j++ {
input <- int64(j)
}
}, mapper, reducer)
}
}