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

View File

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