File tree 2 files changed +15
-1
lines changed
2 files changed +15
-1
lines changed Original file line number Diff line number Diff line change 1
1
package singleton
2
2
3
3
type singleton struct {
4
- calls int
4
+ calls int
5
+ creations int
5
6
}
6
7
7
8
var instance * singleton
8
9
9
10
func GetInstance () * singleton {
10
11
if instance == nil {
11
12
instance = new (singleton )
13
+ instance .creations = 1
12
14
}
13
15
instance .calls ++
14
16
return instance
@@ -17,3 +19,7 @@ func GetInstance() *singleton {
17
19
func (s * singleton ) NumberOfCalls () int {
18
20
return s .calls
19
21
}
22
+
23
+ func (s * singleton ) NumberOfCreations () int {
24
+ return s .creations
25
+ }
Original file line number Diff line number Diff line change @@ -5,6 +5,10 @@ import "testing"
5
5
func TestGetInstance (t * testing.T ) {
6
6
firstInstance := GetInstance ()
7
7
8
+ if firstInstance .NumberOfCreations () != 1 {
9
+ t .Error ("expected just one number of creations" )
10
+ }
11
+
8
12
secondInstance := GetInstance ()
9
13
if firstInstance != secondInstance {
10
14
t .Error ("expected same instance" )
@@ -14,4 +18,8 @@ func TestGetInstance(t *testing.T) {
14
18
if thirdInstance .NumberOfCalls () != 3 {
15
19
t .Error ("expected three calls" )
16
20
}
21
+
22
+ if firstInstance .NumberOfCreations () != 1 {
23
+ t .Error ("expected just one number of creations" )
24
+ }
17
25
}
You can’t perform that action at this time.
0 commit comments