Initial cloud-services repo - gateway service + pkg modules
This commit is contained in:
56
pkg/redis/tester/mock_client_pool.go
Normal file
56
pkg/redis/tester/mock_client_pool.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package tester
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"fiskerinc.com/modules/redis"
|
||||
)
|
||||
|
||||
func NewMockClientPool(args ...interface{}) redis.ClientPoolInterface {
|
||||
result := &MockClientPool{}
|
||||
|
||||
for i := range args {
|
||||
if pool, ok := args[i].(redis.Pool); ok {
|
||||
result.pool = pool
|
||||
} else if client, ok := args[i].(redis.Client); ok {
|
||||
result.client = client
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
type MockClientPool struct {
|
||||
once sync.Once
|
||||
oncePool sync.Once
|
||||
client redis.Client
|
||||
pool redis.Pool
|
||||
}
|
||||
|
||||
func (f *MockClientPool) getClient() redis.Client {
|
||||
f.once.Do(func() {
|
||||
if f.client == nil {
|
||||
f.client = NewRedisMock()
|
||||
}
|
||||
})
|
||||
|
||||
return f.client
|
||||
}
|
||||
|
||||
func (f *MockClientPool) GetPool() redis.Pool {
|
||||
f.oncePool.Do(func() {
|
||||
if f.pool == nil {
|
||||
f.pool = redis.GetMockPool()
|
||||
}
|
||||
})
|
||||
|
||||
return f.pool
|
||||
}
|
||||
|
||||
func (f *MockClientPool) SetPool(pool redis.Pool) {
|
||||
f.pool = pool
|
||||
}
|
||||
|
||||
func (f *MockClientPool) GetFromPool() redis.Client {
|
||||
return f.getClient()
|
||||
}
|
||||
Reference in New Issue
Block a user