Skip to content

Commit

Permalink
add test (zeromicro#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
knight0zh authored Sep 22, 2020
1 parent 9f9c24c commit 17a0908
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions core/syncx/sharedcalls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,38 @@ func TestExclusiveCallDoDupSuppress(t *testing.T) {
}
}

func TestExclusiveCallDoDiffDupSuppress(t *testing.T) {
g := NewSharedCalls()
broadcast := make(chan struct{})
var calls int32
tests := []string{"e", "a", "e", "a", "b", "c", "b", "a", "c", "d", "b", "c", "d"}

var wg sync.WaitGroup
for _, key := range tests {
wg.Add(1)
go func(k string) {
<-broadcast // get all goroutines ready
_, err := g.Do(k, func() (interface{}, error) {
atomic.AddInt32(&calls, 1)
time.Sleep(10 * time.Millisecond)
return nil, nil
})
if err != nil {
t.Errorf("Do error: %v", err)
}
wg.Done()
}(key)
}

time.Sleep(100 * time.Millisecond) // let goroutines above block
close(broadcast)
wg.Wait()

if got := atomic.LoadInt32(&calls); got != 5 { // five letters
t.Errorf("number of calls = %d; want 5", got)
}
}

func TestExclusiveCallDoExDupSuppress(t *testing.T) {
g := NewSharedCalls()
c := make(chan string)
Expand Down

0 comments on commit 17a0908

Please sign in to comment.