Skip to content

Commit ef73f66

Browse files
razertorysenghoo
authored andcommitted
update: 拆分出并发的单例测试
1 parent 5774cb0 commit ef73f66

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

03_singleton/singleton_test.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,31 @@ import (
55
"testing"
66
)
77

8-
const count = 100
8+
const parCount = 100
99

1010
func TestSingleton(t *testing.T) {
11+
ins1 := GetInstance()
12+
ins2 := GetInstance()
13+
if ins1 != ins2 {
14+
t.Fatal("instance is not equal")
15+
}
16+
}
17+
18+
func TestParallelSingleton(t *testing.T) {
1119
wg := sync.WaitGroup{}
12-
wg.Add(count)
13-
instances := [count]*Singleton{}
14-
for i := 0; i < count; i++ {
20+
wg.Add(parCount)
21+
instances := [parCount]*Singleton{}
22+
for i := 0; i < parCount; i++ {
1523
go func(index int) {
1624
instances[index] = GetInstance()
1725
wg.Done()
1826
}(i)
1927
}
2028
wg.Wait()
21-
for i := 1; i < count; i++ {
29+
for i := 1; i < parCount; i++ {
2230
if instances[i] != instances[i-1] {
2331
t.Fatal("instance is not equal")
2432
}
2533
}
2634
}
35+

0 commit comments

Comments
 (0)