Make fake_etcd_client threadsafe

This commit is contained in:
Kouhei Ueno
2014-08-04 00:26:33 +09:00
parent 3f9ec452e4
commit 4799b546c9

View File

@@ -19,6 +19,7 @@ package tools
import ( import (
"errors" "errors"
"fmt" "fmt"
"sync"
"github.com/coreos/go-etcd/etcd" "github.com/coreos/go-etcd/etcd"
) )
@@ -40,6 +41,7 @@ type FakeEtcdClient struct {
Data map[string]EtcdResponseWithError Data map[string]EtcdResponseWithError
DeletedKeys []string DeletedKeys []string
expectNotFoundGetSet map[string]struct{} expectNotFoundGetSet map[string]struct{}
sync.Mutex
Err error Err error
t TestLogger t TestLogger
Ix int Ix int
@@ -89,11 +91,17 @@ func (f *FakeEtcdClient) generateIndex() uint64 {
} }
func (f *FakeEtcdClient) AddChild(key, data string, ttl uint64) (*etcd.Response, error) { func (f *FakeEtcdClient) AddChild(key, data string, ttl uint64) (*etcd.Response, error) {
f.Mutex.Lock()
defer f.Mutex.Unlock()
f.Ix = f.Ix + 1 f.Ix = f.Ix + 1
return f.Set(fmt.Sprintf("%s/%d", key, f.Ix), data, ttl) return f.setLocked(fmt.Sprintf("%s/%d", key, f.Ix), data, ttl)
} }
func (f *FakeEtcdClient) Get(key string, sort, recursive bool) (*etcd.Response, error) { func (f *FakeEtcdClient) Get(key string, sort, recursive bool) (*etcd.Response, error) {
f.Mutex.Lock()
defer f.Mutex.Unlock()
result := f.Data[key] result := f.Data[key]
if result.R == nil { if result.R == nil {
if _, ok := f.expectNotFoundGetSet[key]; !ok { if _, ok := f.expectNotFoundGetSet[key]; !ok {
@@ -110,7 +118,7 @@ func (f *FakeEtcdClient) nodeExists(key string) bool {
return ok && result.R != nil && result.R.Node != nil return ok && result.R != nil && result.R.Node != nil
} }
func (f *FakeEtcdClient) Set(key, value string, ttl uint64) (*etcd.Response, error) { func (f *FakeEtcdClient) setLocked(key, value string, ttl uint64) (*etcd.Response, error) {
if f.Err != nil { if f.Err != nil {
return nil, f.Err return nil, f.Err
} }
@@ -146,6 +154,13 @@ func (f *FakeEtcdClient) Set(key, value string, ttl uint64) (*etcd.Response, err
return result.R, nil return result.R, nil
} }
func (f *FakeEtcdClient) Set(key, value string, ttl uint64) (*etcd.Response, error) {
f.Mutex.Lock()
defer f.Mutex.Unlock()
return f.setLocked(key, value, ttl)
}
func (f *FakeEtcdClient) CompareAndSwap(key, value string, ttl uint64, prevValue string, prevIndex uint64) (*etcd.Response, error) { func (f *FakeEtcdClient) CompareAndSwap(key, value string, ttl uint64, prevValue string, prevIndex uint64) (*etcd.Response, error) {
if f.Err != nil { if f.Err != nil {
return nil, f.Err return nil, f.Err
@@ -160,6 +175,9 @@ func (f *FakeEtcdClient) CompareAndSwap(key, value string, ttl uint64, prevValue
return nil, errors.New("Either prevValue or prevIndex must be specified.") return nil, errors.New("Either prevValue or prevIndex must be specified.")
} }
f.Mutex.Lock()
defer f.Mutex.Unlock()
if !f.nodeExists(key) { if !f.nodeExists(key) {
return nil, EtcdErrorNotFound return nil, EtcdErrorNotFound
} }
@@ -174,15 +192,18 @@ func (f *FakeEtcdClient) CompareAndSwap(key, value string, ttl uint64, prevValue
return nil, EtcdErrorTestFailed return nil, EtcdErrorTestFailed
} }
return f.Set(key, value, ttl) return f.setLocked(key, value, ttl)
} }
func (f *FakeEtcdClient) Create(key, value string, ttl uint64) (*etcd.Response, error) { func (f *FakeEtcdClient) Create(key, value string, ttl uint64) (*etcd.Response, error) {
f.Mutex.Lock()
defer f.Mutex.Unlock()
if f.nodeExists(key) { if f.nodeExists(key) {
return nil, EtcdErrorNodeExist return nil, EtcdErrorNodeExist
} }
return f.Set(key, value, ttl) return f.setLocked(key, value, ttl)
} }
func (f *FakeEtcdClient) Delete(key string, recursive bool) (*etcd.Response, error) { func (f *FakeEtcdClient) Delete(key string, recursive bool) (*etcd.Response, error) {