mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2024-11-26 21:56:02 +03:00
35 lines
577 B
Go
35 lines
577 B
Go
package singleton
|
|
|
|
import (
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
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(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 < parCount; i++ {
|
|
if instances[i] != instances[i-1] {
|
|
t.Fatal("instance is not equal")
|
|
}
|
|
}
|
|
}
|