Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug singleflight test #547

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions internal/singleflight/singleflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package singleflight

import (
"context"
"fmt"
"sync"
)

Expand Down Expand Up @@ -53,11 +52,9 @@ func New(size int) Group {
// Do returns a set of the cache of the first return value from function as interface{}, shared flg as bool, and err as error when the function is called multiple times in an instant
func (g *group) Do(ctx context.Context, key string, fn func() (interface{}, error)) (v interface{}, shared bool, err error) {
g.mu.RLock()
fmt.Println(g.m[key])
if c, ok := g.m[key]; ok {
g.mu.RUnlock()
c.dups++
fmt.Println("waiting")
c.wg.Wait()
return c.val, true, c.err
}
Expand All @@ -71,12 +68,10 @@ func (g *group) Do(ctx context.Context, key string, fn func() (interface{}, erro
g.mu.Unlock()

c.val, c.err = fn()
fmt.Println("release")
c.wg.Done()

g.mu.Lock()
delete(g.m, key)
g.mu.Unlock()
fmt.Println("delete")
return c.val, c.dups > 0, c.err
}
218 changes: 126 additions & 92 deletions internal/singleflight/singleflight_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package singleflight

import (
"context"
"fmt"
"reflect"
"strconv"
"sync"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -115,6 +115,12 @@ func Test_group_Do(t *testing.T) {
type fields struct {
m map[string]*call
}
type util struct {
mu *sync.Mutex
wg *sync.WaitGroup
cond *sync.Cond
condWaitFn func()
}
type want struct {
wantV interface{}
wantShared bool
Expand All @@ -124,10 +130,8 @@ func Test_group_Do(t *testing.T) {
name string
args args
fields fields
util util
want want
cond *sync.Cond
mu *sync.Mutex
wg *sync.WaitGroup
checkFunc func(want, interface{}, bool, error) error
beforeFunc func(Group, args)
afterFunc func(args)
Expand All @@ -145,92 +149,113 @@ func Test_group_Do(t *testing.T) {
return nil
}
tests := []test{
// TODO test cases
/*
{
name: "test_case_1",
args: args {
ctx: nil,
key: "",
fn: nil,
},
fields: fields {
mu: sync.RWMutex{},
m: nil,
},
want: want{},
checkFunc: defaultCheckFunc,
},
*/

func() test {
mu := new(sync.Mutex)
cond := sync.NewCond(mu)
cnt := uint32(0)
wg := new(sync.WaitGroup)
var calledCnt uint32

var (
mu = new(sync.Mutex)
cond = sync.NewCond(mu)
wg = new(sync.WaitGroup)
condWaitFn = func() {
mu.Lock()
defer mu.Unlock()
cond.Wait()
}
)

return test{
mu: mu,
cond: cond,
wg: wg,
name: "returns (v, shared, nil) when Do is called with another key",
fields: fields{
m: make(map[string]*call),
},
util: util{
mu: mu,
cond: cond,
wg: wg,
condWaitFn: condWaitFn,
},
args: args{
ctx: context.Background(),
key: "req_1",
ctx: context.Background(),
fn: func() (interface{}, error) {
atomic.AddUint32(&cnt, 1)
atomic.AddUint32(&calledCnt, 1)
return "res_1", nil
},
},
fields: fields{
m: make(map[string]*call, 2),
},
want: want{
wantV: "res_1",
wantShared: false,
err: nil,
},
beforeFunc: func(g Group, args args) {
wg.Add(1)
go func() {
mu.Lock()
defer mu.Unlock()
cond.Wait()
g.Do(context.Background(), "req_2", func() (interface{}, error) {
gcnt := 10
ch := make(chan struct{}, gcnt)

for i := 0; i < gcnt; i++ {
wg.Add(1)
go func(i int) {
ch <- struct{}{}
defer wg.Done()
atomic.AddUint32(&cnt, 1)
return "res_2", nil
})
}()
condWaitFn()

g.Do(context.Background(), strconv.Itoa(i), func() (interface{}, error) {
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
time.Sleep(time.Nanosecond * 100)
atomic.AddUint32(&calledCnt, 1)
return "vdaas/vald", nil
})
}(i)
}

for i := 0; i < gcnt; i++ {
<-ch
}
close(ch)
},
checkFunc: func(want, interface{}, bool, error) error {
if got, want := int(atomic.LoadUint32(&cnt)), 2; got != want {
return errors.Errorf("cnt got = %d, want = %d", got, want)
checkFunc: func(w want, gotV interface{}, gotShared bool, err error) error {
if got, want := int(atomic.LoadUint32(&calledCnt)), 11; got != want {
return errors.Errorf("calledCnt got = %d, want = %d", got, want)
}

if err := defaultCheckFunc(w, gotV, gotShared, err); err != nil {
return err
}
return nil
},
}
}(),

func() test {
mu := new(sync.Mutex)
cond := sync.NewCond(mu)
cnt := uint32(0)
wg := new(sync.WaitGroup)
var calledCnt uint32

var (
mu = new(sync.Mutex)
cond = sync.NewCond(mu)
wg = new(sync.WaitGroup)
condWaitFn = func() {
mu.Lock()
defer mu.Unlock()
cond.Wait()
}
)

return test{
name: "returns (v, shared, nil) when Do is called with same key",
mu: mu,
cond: cond,
wg: wg,
args: args{
ctx: context.Background(),
key: "req_1",
ctx: context.Background(),
fn: func() (interface{}, error) {
fmt.Println("args")
atomic.AddUint32(&cnt, 1)
atomic.AddUint32(&calledCnt, 1)
return "res_1", nil
},
},
fields: fields{
m: make(map[string]*call, 2),
m: make(map[string]*call),
},
util: util{
mu: mu,
cond: cond,
wg: wg,
condWaitFn: condWaitFn,
},
want: want{
wantV: "res_1",
Expand All @@ -239,35 +264,43 @@ func Test_group_Do(t *testing.T) {
},
beforeFunc: func(g Group, args args) {
wg.Add(1)
ch := make(chan struct{})
go func() {
g.Do(context.Background(), "req_1", func() (interface{}, error) {
ch <- struct{}{}
fmt.Println("test")
defer wg.Done()
time.Sleep(time.Second * 10)
return "res_1", nil
defer wg.Done()
g.Do(context.Background(), args.key, func() (interface{}, error) {
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
time.Sleep(3 * time.Second)
return args.fn()
})
}()
<- ch
for i := 0; i < 10; i++ {

gcnt := 10
ch := make(chan struct{}, gcnt)

for i := 0; i < gcnt; i++ {
wg.Add(1)
go func(i int) {
mu.Lock()
defer mu.Unlock()
cond.Wait()
go func() {
ch <- struct{}{}
defer wg.Done()
fmt.Println(i)
g.Do(context.Background(), "req_1", func() (interface{}, error) {
atomic.AddUint32(&cnt, 1)
return "res_1", nil
condWaitFn()

g.Do(context.Background(), args.key, func() (interface{}, error) {
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
atomic.AddUint32(&calledCnt, 1)
return "vdaas/vald", nil
})
}(i)
}()
}

for i := 0; i < gcnt; i++ {
<-ch
}
close(ch)
},
checkFunc: func(want, interface{}, bool, error) error {
if got, want := int(atomic.LoadUint32(&cnt)), 1; got != want {
return errors.Errorf("cnt got = %d, want = %d", got, want)
checkFunc: func(w want, gotV interface{}, gotShared bool, err error) error {
if got, want := int(atomic.LoadUint32(&calledCnt)), 1; got != want {
return errors.Errorf("calledCnt got = %d, want = %d", got, want)
}

if err := defaultCheckFunc(w, gotV, gotShared, err); err != nil {
return err
}
return nil
},
Expand All @@ -284,6 +317,7 @@ func Test_group_Do(t *testing.T) {
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}

g := &group{
m: test.fields.m,
}
Expand All @@ -292,24 +326,24 @@ func Test_group_Do(t *testing.T) {
test.beforeFunc(g, test.args)
}

var gotV interface{}
var gotShared bool
var err error
test.wg.Add(1)
var (
gotV interface{}
gotShared bool
err error
)

test.util.wg.Add(1)
go func() {
test.mu.Lock()
defer test.mu.Unlock()
test.cond.Wait()
defer test.wg.Done()
gotV, gotShared, err = g.Do(test.args.ctx, test.args.key, test.args.fn)
defer test.util.wg.Done()
gotV, gotShared, err = g.Do(context.Background(), test.args.key, test.args.fn)
}()
time.Sleep(time.Second)
test.cond.Broadcast()
test.wg.Wait()

test.util.cond.Broadcast()
test.util.wg.Wait()

if err := test.checkFunc(test.want, gotV, gotShared, err); err != nil {
tt.Errorf("error = %v", err)
}

})
}
}