Skip to content

Commit

Permalink
update: 拆分出并发的单例测试
Browse files Browse the repository at this point in the history
  • Loading branch information
razertory authored and senghoo committed Mar 30, 2020
1 parent 5774cb0 commit ef73f66
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions 03_singleton/singleton_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,31 @@ import (
"testing"
)

const count = 100
const parCount = 100

func TestSingleton(t *testing.T) {
ins1 := GetInstance()
ins2 := GetInstance()
if ins1 != ins2 {
t.Fatal("instance is not equal")
}
}

func TestParallelSingleton(t *testing.T) {
wg := sync.WaitGroup{}
wg.Add(count)
instances := [count]*Singleton{}
for i := 0; i < count; i++ {
wg.Add(parCount)
instances := [parCount]*Singleton{}
for i := 0; i < parCount; i++ {
go func(index int) {
instances[index] = GetInstance()
wg.Done()
}(i)
}
wg.Wait()
for i := 1; i < count; i++ {
for i := 1; i < parCount; i++ {
if instances[i] != instances[i-1] {
t.Fatal("instance is not equal")
}
}
}

0 comments on commit ef73f66

Please sign in to comment.