-
Notifications
You must be signed in to change notification settings - Fork 0
/
singletone_test.go
72 lines (55 loc) · 1.84 KB
/
singletone_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package singleton_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/marksartdev/go-patterns/pkg/creational/singleton"
)
func BenchmarkGetFullInstance(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = singleton.GetFullInstance()
}
}
func BenchmarkGetConditionalInstance(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = singleton.GetConditionalInstance()
}
}
func BenchmarkGetAtomicInstance(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = singleton.GetAtomicInstance()
}
}
func BenchmarkGetOnceInstance(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = singleton.GetOnceInstance()
}
}
func TestGetFullInstance(t *testing.T) {
boilerInstance1 := singleton.GetFullInstance()
boilerInstance2 := singleton.GetFullInstance()
assertInstances(t, boilerInstance1, boilerInstance2)
}
func TestGetConditionalInstance(t *testing.T) {
boilerInstance1 := singleton.GetConditionalInstance()
boilerInstance2 := singleton.GetConditionalInstance()
assertInstances(t, boilerInstance1, boilerInstance2)
}
func TestGetAtomicInstance(t *testing.T) {
boilerInstance1 := singleton.GetAtomicInstance()
boilerInstance2 := singleton.GetAtomicInstance()
assertInstances(t, boilerInstance1, boilerInstance2)
}
func TestGetOnceInstance(t *testing.T) {
boilerInstance1 := singleton.GetOnceInstance()
boilerInstance2 := singleton.GetOnceInstance()
assertInstances(t, boilerInstance1, boilerInstance2)
}
func assertInstances(t *testing.T, boilerInstance1, boilerInstance2 singleton.ChocolateBoiler) {
assert.Equal(t, boilerInstance1, boilerInstance2)
assert.Equal(t, "Filling ...", boilerInstance1.Fill())
assert.Equal(t, "", boilerInstance2.Fill())
assert.Equal(t, "Boiling ...", boilerInstance1.Boil())
assert.Equal(t, "", boilerInstance2.Boil())
assert.Equal(t, "Draining ...", boilerInstance1.Drain())
assert.Equal(t, "", boilerInstance2.Drain())
}