Skip to content

Commit

Permalink
added cacher mock and stub
Browse files Browse the repository at this point in the history
  • Loading branch information
ssd04 committed Mar 21, 2024
1 parent 615f939 commit abff3c0
Show file tree
Hide file tree
Showing 2 changed files with 302 additions and 0 deletions.
159 changes: 159 additions & 0 deletions testscommon/cacherMock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package testscommon

import (
"sync"
)

// CacherMock -
type CacherMock struct {
mut sync.RWMutex
dataMap map[string]interface{}
mutAddedDataHandlers sync.RWMutex
addedDataHandlers []func(key []byte, val interface{})
}

// NewCacherMock -
func NewCacherMock() *CacherMock {
return &CacherMock{
dataMap: make(map[string]interface{}),
addedDataHandlers: make([]func(key []byte, val interface{}), 0),
}
}

// Clear -
func (cacher *CacherMock) Clear() {
cacher.mut.Lock()
defer cacher.mut.Unlock()

cacher.dataMap = make(map[string]interface{})
}

// Put -
func (cacher *CacherMock) Put(key []byte, value interface{}, _ int) (evicted bool) {
cacher.mut.Lock()
defer cacher.mut.Unlock()

cacher.dataMap[string(key)] = value
cacher.callAddedDataHandlers(key, value)

return false
}

func (cacher *CacherMock) callAddedDataHandlers(key []byte, val interface{}) {
cacher.mutAddedDataHandlers.RLock()
for _, handler := range cacher.addedDataHandlers {
go handler(key, val)
}
cacher.mutAddedDataHandlers.RUnlock()
}

// Get -
func (cacher *CacherMock) Get(key []byte) (value interface{}, ok bool) {
cacher.mut.RLock()
defer cacher.mut.RUnlock()

val, ok := cacher.dataMap[string(key)]

return val, ok
}

// Has -
func (cacher *CacherMock) Has(key []byte) bool {
cacher.mut.RLock()
defer cacher.mut.RUnlock()

_, ok := cacher.dataMap[string(key)]

return ok
}

// Peek -
func (cacher *CacherMock) Peek(key []byte) (value interface{}, ok bool) {
cacher.mut.RLock()
defer cacher.mut.RUnlock()

val, ok := cacher.dataMap[string(key)]

return val, ok
}

// HasOrAdd -
func (cacher *CacherMock) HasOrAdd(key []byte, value interface{}, _ int) (has, added bool) {
cacher.mut.Lock()
defer cacher.mut.Unlock()

_, has = cacher.dataMap[string(key)]
if has {
return true, false
}

cacher.dataMap[string(key)] = value
cacher.callAddedDataHandlers(key, value)
return false, true
}

// Remove -
func (cacher *CacherMock) Remove(key []byte) {
cacher.mut.Lock()
defer cacher.mut.Unlock()

delete(cacher.dataMap, string(key))
}

// Keys -
func (cacher *CacherMock) Keys() [][]byte {
cacher.mut.RLock()
defer cacher.mut.RUnlock()

keys := make([][]byte, len(cacher.dataMap))
idx := 0
for k := range cacher.dataMap {
keys[idx] = []byte(k)
idx++
}

return keys
}

// Len -
func (cacher *CacherMock) Len() int {
cacher.mut.RLock()
defer cacher.mut.RUnlock()

return len(cacher.dataMap)
}

// SizeInBytesContained -
func (cacher *CacherMock) SizeInBytesContained() uint64 {
return 0
}

// MaxSize -
func (cacher *CacherMock) MaxSize() int {
return 10000
}

// RegisterHandler -
func (cacher *CacherMock) RegisterHandler(handler func(key []byte, value interface{}), _ string) {
if handler == nil {
return
}

cacher.mutAddedDataHandlers.Lock()
cacher.addedDataHandlers = append(cacher.addedDataHandlers, handler)
cacher.mutAddedDataHandlers.Unlock()
}

// UnRegisterHandler -
func (cacher *CacherMock) UnRegisterHandler(string) {
}

// IsInterfaceNil returns true if there is no value under the interface
func (cacher *CacherMock) IsInterfaceNil() bool {
return cacher == nil
}

// Close -
func (cacher *CacherMock) Close() error {
return nil
}
143 changes: 143 additions & 0 deletions testscommon/cacherStub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package testscommon

// CacherStub -
type CacherStub struct {
ClearCalled func()
PutCalled func(key []byte, value interface{}, sizeInBytes int) (evicted bool)
GetCalled func(key []byte) (value interface{}, ok bool)
HasCalled func(key []byte) bool
PeekCalled func(key []byte) (value interface{}, ok bool)
HasOrAddCalled func(key []byte, value interface{}, sizeInBytes int) (has, added bool)
RemoveCalled func(key []byte)
RemoveOldestCalled func()
KeysCalled func() [][]byte
LenCalled func() int
MaxSizeCalled func() int
RegisterHandlerCalled func(func(key []byte, value interface{}))
UnRegisterHandlerCalled func(id string)
CloseCalled func() error
}

// NewCacherStub -
func NewCacherStub() *CacherStub {
return &CacherStub{}
}

// Clear -
func (cacher *CacherStub) Clear() {
if cacher.ClearCalled != nil {
cacher.ClearCalled()
}
}

// Put -
func (cacher *CacherStub) Put(key []byte, value interface{}, sizeInBytes int) (evicted bool) {
if cacher.PutCalled != nil {
return cacher.PutCalled(key, value, sizeInBytes)
}

return false
}

// Get -
func (cacher *CacherStub) Get(key []byte) (value interface{}, ok bool) {
if cacher.GetCalled != nil {
return cacher.GetCalled(key)
}

return nil, false
}

// Has -
func (cacher *CacherStub) Has(key []byte) bool {
if cacher.HasCalled != nil {
return cacher.HasCalled(key)
}

return false
}

// Peek -
func (cacher *CacherStub) Peek(key []byte) (value interface{}, ok bool) {
if cacher.PeekCalled != nil {
return cacher.PeekCalled(key)
}

return nil, false
}

// HasOrAdd -
func (cacher *CacherStub) HasOrAdd(key []byte, value interface{}, sizeInBytes int) (has, added bool) {
if cacher.HasOrAddCalled != nil {
return cacher.HasOrAddCalled(key, value, sizeInBytes)
}

return false, false
}

// Remove -
func (cacher *CacherStub) Remove(key []byte) {
if cacher.RemoveCalled != nil {
cacher.RemoveCalled(key)
}
}

// Keys -
func (cacher *CacherStub) Keys() [][]byte {
if cacher.KeysCalled != nil {
return cacher.KeysCalled()
}

return make([][]byte, 0)
}

// Len -
func (cacher *CacherStub) Len() int {
if cacher.LenCalled != nil {
return cacher.LenCalled()
}

return 0
}

// SizeInBytesContained -
func (cacher *CacherStub) SizeInBytesContained() uint64 {
return 0
}

// MaxSize -
func (cacher *CacherStub) MaxSize() int {
if cacher.MaxSizeCalled != nil {
return cacher.MaxSizeCalled()
}

return 0
}

// RegisterHandler -
func (cacher *CacherStub) RegisterHandler(handler func(key []byte, value interface{}), _ string) {
if cacher.RegisterHandlerCalled != nil {
cacher.RegisterHandlerCalled(handler)
}
}

// UnRegisterHandler -
func (cacher *CacherStub) UnRegisterHandler(id string) {
if cacher.UnRegisterHandlerCalled != nil {
cacher.UnRegisterHandlerCalled(id)
}
}

// IsInterfaceNil returns true if there is no value under the interface
func (cacher *CacherStub) IsInterfaceNil() bool {
return cacher == nil
}

// Close -
func (cacher *CacherStub) Close() error {
if cacher.CloseCalled != nil {
return cacher.CloseCalled()
}

return nil
}

0 comments on commit abff3c0

Please sign in to comment.