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 514337d commit 5774cb0
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions 03_singleton/singleton_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
package singleton

import "testing"
import (
"sync"
"testing"
)

const count = 100

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

0 comments on commit 5774cb0

Please sign in to comment.