File tree Expand file tree Collapse file tree 1 file changed +19
-6
lines changed Expand file tree Collapse file tree 1 file changed +19
-6
lines changed Original file line number Diff line number Diff line change 1
1
package gock
2
2
3
+ import (
4
+ "sync"
5
+ )
6
+
7
+ // storeMutex is used interally for store synchronization.
8
+ var storeMutex = & sync.RWMutex {}
9
+
3
10
// mocks is internally used to store registered mocks.
4
11
var mocks = []Mock {}
5
12
@@ -23,11 +30,15 @@ func Register(mock Mock) {
23
30
24
31
// GetAll returns the current stack of registed mocks.
25
32
func GetAll () []Mock {
33
+ storeMutex .RLock ()
34
+ defer storeMutex .RUnlock ()
26
35
return mocks
27
36
}
28
37
29
38
// Exists checks if the given Mock is already registered.
30
39
func Exists (m Mock ) bool {
40
+ storeMutex .RLock ()
41
+ defer storeMutex .RUnlock ()
31
42
for _ , mock := range mocks {
32
43
if mock == m {
33
44
return true
@@ -40,23 +51,25 @@ func Exists(m Mock) bool {
40
51
func Remove (m Mock ) {
41
52
for i , mock := range mocks {
42
53
if mock == m {
43
- mutex .Lock ()
54
+ storeMutex .Lock ()
44
55
mocks = append (mocks [:i ], mocks [i + 1 :]... )
45
- mutex .Unlock ()
56
+ storeMutex .Unlock ()
46
57
}
47
58
}
48
59
}
49
60
50
61
// Flush flushes the current stack of registered mocks.
51
62
func Flush () {
52
- mutex .Lock ()
53
- defer mutex .Unlock ()
63
+ storeMutex .Lock ()
64
+ defer storeMutex .Unlock ()
54
65
mocks = []Mock {}
55
66
}
56
67
57
68
// Pending returns an slice of pending mocks.
58
69
func Pending () []Mock {
59
70
Clean ()
71
+ storeMutex .RLock ()
72
+ defer storeMutex .RUnlock ()
60
73
return mocks
61
74
}
62
75
@@ -72,8 +85,8 @@ func IsPending() bool {
72
85
73
86
// Clean cleans the mocks store removing disabled or obsolete mocks.
74
87
func Clean () {
75
- mutex .Lock ()
76
- defer mutex .Unlock ()
88
+ storeMutex .Lock ()
89
+ defer storeMutex .Unlock ()
77
90
78
91
buf := []Mock {}
79
92
for _ , mock := range mocks {
You can’t perform that action at this time.
0 commit comments