mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-12-23 13:57:12 +00:00
Introduce a locking inmem storage for unit tests that are doing concurrent things
This commit is contained in:
58
logical/locking_inmem_storage.go
Normal file
58
logical/locking_inmem_storage.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package logical
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// LockingInmemStorage implements Storage and stores all data in memory.
|
||||
type LockingInmemStorage struct {
|
||||
sync.RWMutex
|
||||
|
||||
Data map[string]*StorageEntry
|
||||
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
func (s *LockingInmemStorage) List(prefix string) ([]string, error) {
|
||||
s.once.Do(s.init)
|
||||
|
||||
s.RLock()
|
||||
defer s.RUnlock()
|
||||
|
||||
var result []string
|
||||
for k, _ := range s.Data {
|
||||
if strings.HasPrefix(k, prefix) {
|
||||
result = append(result, k)
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *LockingInmemStorage) Get(key string) (*StorageEntry, error) {
|
||||
s.once.Do(s.init)
|
||||
s.RLock()
|
||||
defer s.RUnlock()
|
||||
return s.Data[key], nil
|
||||
}
|
||||
|
||||
func (s *LockingInmemStorage) Put(entry *StorageEntry) error {
|
||||
s.once.Do(s.init)
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
s.Data[entry.Key] = entry
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *LockingInmemStorage) Delete(k string) error {
|
||||
s.once.Do(s.init)
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
delete(s.Data, k)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *LockingInmemStorage) init() {
|
||||
s.Data = make(map[string]*StorageEntry)
|
||||
}
|
||||
Reference in New Issue
Block a user