Skip to content

Commit b4a85b8

Browse files
committed
fix(mutex): use different mutex for store
1 parent c67415c commit b4a85b8

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

store.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package gock
22

3+
import (
4+
"sync"
5+
)
6+
7+
// storeMutex is used interally for store synchronization.
8+
var storeMutex = &sync.RWMutex{}
9+
310
// mocks is internally used to store registered mocks.
411
var mocks = []Mock{}
512

@@ -23,11 +30,15 @@ func Register(mock Mock) {
2330

2431
// GetAll returns the current stack of registed mocks.
2532
func GetAll() []Mock {
33+
storeMutex.RLock()
34+
defer storeMutex.RUnlock()
2635
return mocks
2736
}
2837

2938
// Exists checks if the given Mock is already registered.
3039
func Exists(m Mock) bool {
40+
storeMutex.RLock()
41+
defer storeMutex.RUnlock()
3142
for _, mock := range mocks {
3243
if mock == m {
3344
return true
@@ -40,23 +51,25 @@ func Exists(m Mock) bool {
4051
func Remove(m Mock) {
4152
for i, mock := range mocks {
4253
if mock == m {
43-
mutex.Lock()
54+
storeMutex.Lock()
4455
mocks = append(mocks[:i], mocks[i+1:]...)
45-
mutex.Unlock()
56+
storeMutex.Unlock()
4657
}
4758
}
4859
}
4960

5061
// Flush flushes the current stack of registered mocks.
5162
func Flush() {
52-
mutex.Lock()
53-
defer mutex.Unlock()
63+
storeMutex.Lock()
64+
defer storeMutex.Unlock()
5465
mocks = []Mock{}
5566
}
5667

5768
// Pending returns an slice of pending mocks.
5869
func Pending() []Mock {
5970
Clean()
71+
storeMutex.RLock()
72+
defer storeMutex.RUnlock()
6073
return mocks
6174
}
6275

@@ -72,8 +85,8 @@ func IsPending() bool {
7285

7386
// Clean cleans the mocks store removing disabled or obsolete mocks.
7487
func Clean() {
75-
mutex.Lock()
76-
defer mutex.Unlock()
88+
storeMutex.Lock()
89+
defer storeMutex.Unlock()
7790

7891
buf := []Mock{}
7992
for _, mock := range mocks {

0 commit comments

Comments
 (0)