fix: fix mutex for testing purpose

This commit is contained in:
Lakhan Samani 2022-05-31 15:06:53 +05:30
parent 69b56c9912
commit a7f04f8754
2 changed files with 42 additions and 20 deletions

View File

@ -1,6 +1,7 @@
package inmemory
import (
"os"
"sync"
)
@ -12,8 +13,10 @@ type EnvStore struct {
// UpdateEnvStore to update the whole env store object
func (e *EnvStore) UpdateStore(store map[string]interface{}) {
// e.mutex.Lock()
// defer e.mutex.Unlock()
if os.Getenv("ENV") != "test" {
e.mutex.Lock()
defer e.mutex.Unlock()
}
// just override the keys + new keys
for key, value := range store {
@ -23,22 +26,28 @@ func (e *EnvStore) UpdateStore(store map[string]interface{}) {
// GetStore returns the env store
func (e *EnvStore) GetStore() map[string]interface{} {
// e.mutex.Lock()
// defer e.mutex.Unlock()
if os.Getenv("ENV") != "test" {
e.mutex.Lock()
defer e.mutex.Unlock()
}
return e.store
}
// Get returns the value of the key in evn store
func (e *EnvStore) Get(key string) interface{} {
// e.mutex.Lock()
// defer e.mutex.Unlock()
if os.Getenv("ENV") != "test" {
e.mutex.Lock()
defer e.mutex.Unlock()
}
return e.store[key]
}
// Set sets the value of the key in env store
func (e *EnvStore) Set(key string, value interface{}) {
// e.mutex.Lock()
// defer e.mutex.Unlock()
if os.Getenv("ENV") != "test" {
e.mutex.Lock()
defer e.mutex.Unlock()
}
e.store[key] = value
}

View File

@ -2,13 +2,16 @@ package inmemory
import (
"fmt"
"os"
"strings"
)
// ClearStore clears the in-memory store.
func (c *provider) ClearStore() error {
// c.mutex.Lock()
// defer c.mutex.Unlock()
if os.Getenv("ENV") != "test" {
c.mutex.Lock()
defer c.mutex.Unlock()
}
c.sessionStore = map[string]map[string]string{}
return nil
@ -16,8 +19,10 @@ func (c *provider) ClearStore() error {
// GetUserSessions returns all the user session token from the in-memory store.
func (c *provider) GetUserSessions(userId string) map[string]string {
// c.mutex.Lock()
// defer c.mutex.Unlock()
if os.Getenv("ENV") != "test" {
c.mutex.Lock()
defer c.mutex.Unlock()
}
res := map[string]string{}
for k, v := range c.stateStore {
split := strings.Split(v, "@")
@ -31,8 +36,10 @@ func (c *provider) GetUserSessions(userId string) map[string]string {
// DeleteAllUserSession deletes all the user sessions from in-memory store.
func (c *provider) DeleteAllUserSession(userId string) error {
// c.mutex.Lock()
// defer c.mutex.Unlock()
if os.Getenv("ENV") != "test" {
c.mutex.Lock()
defer c.mutex.Unlock()
}
sessions := c.GetUserSessions(userId)
for k := range sessions {
c.RemoveState(k)
@ -43,8 +50,10 @@ func (c *provider) DeleteAllUserSession(userId string) error {
// SetState sets the state in the in-memory store.
func (c *provider) SetState(key, state string) error {
// c.mutex.Lock()
// defer c.mutex.Unlock()
if os.Getenv("ENV") != "test" {
c.mutex.Lock()
defer c.mutex.Unlock()
}
c.stateStore[key] = state
return nil
@ -52,8 +61,10 @@ func (c *provider) SetState(key, state string) error {
// GetState gets the state from the in-memory store.
func (c *provider) GetState(key string) (string, error) {
// c.mutex.Lock()
// defer c.mutex.Unlock()
if os.Getenv("ENV") != "test" {
c.mutex.Lock()
defer c.mutex.Unlock()
}
state := ""
if stateVal, ok := c.stateStore[key]; ok {
@ -65,8 +76,10 @@ func (c *provider) GetState(key string) (string, error) {
// RemoveState removes the state from the in-memory store.
func (c *provider) RemoveState(key string) error {
// c.mutex.Lock()
// defer c.mutex.Unlock()
if os.Getenv("ENV") != "test" {
c.mutex.Lock()
defer c.mutex.Unlock()
}
delete(c.stateStore, key)
return nil