Skip to content

Commit a7f04f8

Browse files
committed
fix: fix mutex for testing purpose
1 parent 69b56c9 commit a7f04f8

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed
Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package inmemory
22

33
import (
4+
"os"
45
"sync"
56
)
67

@@ -12,8 +13,10 @@ type EnvStore struct {
1213

1314
// UpdateEnvStore to update the whole env store object
1415
func (e *EnvStore) UpdateStore(store map[string]interface{}) {
15-
// e.mutex.Lock()
16-
// defer e.mutex.Unlock()
16+
if os.Getenv("ENV") != "test" {
17+
e.mutex.Lock()
18+
defer e.mutex.Unlock()
19+
}
1720
// just override the keys + new keys
1821

1922
for key, value := range store {
@@ -23,22 +26,28 @@ func (e *EnvStore) UpdateStore(store map[string]interface{}) {
2326

2427
// GetStore returns the env store
2528
func (e *EnvStore) GetStore() map[string]interface{} {
26-
// e.mutex.Lock()
27-
// defer e.mutex.Unlock()
29+
if os.Getenv("ENV") != "test" {
30+
e.mutex.Lock()
31+
defer e.mutex.Unlock()
32+
}
2833

2934
return e.store
3035
}
3136

3237
// Get returns the value of the key in evn store
3338
func (e *EnvStore) Get(key string) interface{} {
34-
// e.mutex.Lock()
35-
// defer e.mutex.Unlock()
39+
if os.Getenv("ENV") != "test" {
40+
e.mutex.Lock()
41+
defer e.mutex.Unlock()
42+
}
3643
return e.store[key]
3744
}
3845

3946
// Set sets the value of the key in env store
4047
func (e *EnvStore) Set(key string, value interface{}) {
41-
// e.mutex.Lock()
42-
// defer e.mutex.Unlock()
48+
if os.Getenv("ENV") != "test" {
49+
e.mutex.Lock()
50+
defer e.mutex.Unlock()
51+
}
4352
e.store[key] = value
4453
}

server/memorystore/providers/inmemory/store.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,27 @@ package inmemory
22

33
import (
44
"fmt"
5+
"os"
56
"strings"
67
)
78

89
// ClearStore clears the in-memory store.
910
func (c *provider) ClearStore() error {
10-
// c.mutex.Lock()
11-
// defer c.mutex.Unlock()
11+
if os.Getenv("ENV") != "test" {
12+
c.mutex.Lock()
13+
defer c.mutex.Unlock()
14+
}
1215
c.sessionStore = map[string]map[string]string{}
1316

1417
return nil
1518
}
1619

1720
// GetUserSessions returns all the user session token from the in-memory store.
1821
func (c *provider) GetUserSessions(userId string) map[string]string {
19-
// c.mutex.Lock()
20-
// defer c.mutex.Unlock()
22+
if os.Getenv("ENV") != "test" {
23+
c.mutex.Lock()
24+
defer c.mutex.Unlock()
25+
}
2126
res := map[string]string{}
2227
for k, v := range c.stateStore {
2328
split := strings.Split(v, "@")
@@ -31,8 +36,10 @@ func (c *provider) GetUserSessions(userId string) map[string]string {
3136

3237
// DeleteAllUserSession deletes all the user sessions from in-memory store.
3338
func (c *provider) DeleteAllUserSession(userId string) error {
34-
// c.mutex.Lock()
35-
// defer c.mutex.Unlock()
39+
if os.Getenv("ENV") != "test" {
40+
c.mutex.Lock()
41+
defer c.mutex.Unlock()
42+
}
3643
sessions := c.GetUserSessions(userId)
3744
for k := range sessions {
3845
c.RemoveState(k)
@@ -43,17 +50,21 @@ func (c *provider) DeleteAllUserSession(userId string) error {
4350

4451
// SetState sets the state in the in-memory store.
4552
func (c *provider) SetState(key, state string) error {
46-
// c.mutex.Lock()
47-
// defer c.mutex.Unlock()
53+
if os.Getenv("ENV") != "test" {
54+
c.mutex.Lock()
55+
defer c.mutex.Unlock()
56+
}
4857
c.stateStore[key] = state
4958

5059
return nil
5160
}
5261

5362
// GetState gets the state from the in-memory store.
5463
func (c *provider) GetState(key string) (string, error) {
55-
// c.mutex.Lock()
56-
// defer c.mutex.Unlock()
64+
if os.Getenv("ENV") != "test" {
65+
c.mutex.Lock()
66+
defer c.mutex.Unlock()
67+
}
5768

5869
state := ""
5970
if stateVal, ok := c.stateStore[key]; ok {
@@ -65,8 +76,10 @@ func (c *provider) GetState(key string) (string, error) {
6576

6677
// RemoveState removes the state from the in-memory store.
6778
func (c *provider) RemoveState(key string) error {
68-
// c.mutex.Lock()
69-
// defer c.mutex.Unlock()
79+
if os.Getenv("ENV") != "test" {
80+
c.mutex.Lock()
81+
defer c.mutex.Unlock()
82+
}
7083
delete(c.stateStore, key)
7184

7285
return nil

0 commit comments

Comments
 (0)