Chromium Code Reviews| Index: go/src/infra/gae/libs/wrapper/featurebreaker.go |
| diff --git a/go/src/infra/gae/libs/wrapper/featurebreaker.go b/go/src/infra/gae/libs/wrapper/featurebreaker.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..27ed17b48c11767bb6ca23f67cb5fbde8dc767e6 |
| --- /dev/null |
| +++ b/go/src/infra/gae/libs/wrapper/featurebreaker.go |
| @@ -0,0 +1,98 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package wrapper |
| + |
| +import ( |
| + "runtime" |
| + "strings" |
| + "sync" |
| +) |
| + |
| +// FeatureBreaker allows a fake implementation to set and unset broken features. |
| +// A feature is the Name of some methog on the fake. So if you had: |
|
Vadim Sh.
2015/05/24 19:01:57
typo: methog
iannucci
2015/05/24 20:04:31
Done
|
| +// var fake interface{ FeatureBreaker, MCSingleReadWriter } = ... |
| +// |
| +// you could do: |
| +// fake.SetBrokenFeatures("Add", "Set") |
| +// |
| +// and then |
| +// fake.Add(...) and fake.Set(...) |
| +// |
| +// would return an error (likely memcache.ErrServerError in this case), as if |
| +// the service were disconnected or broken. |
| +type FeatureBreaker interface { |
| + SetBrokenFeatures(feature ...string) |
| + UnsetBrokenFeatures(feature ...string) |
| +} |
| + |
| +// BrokenFeatures implements the FeatureBreaker interface, and is suitable for |
| +// embedding within a fake service. |
| +type BrokenFeatures struct { |
| + lock sync.Mutex |
| + |
| + broken map[string]struct{} |
| + err error |
| +} |
| + |
| +// NewBrokenFeatures creates an embeddable *BrokenFeatures which is set to |
| +// return err for a given broken feature. |
| +func NewBrokenFeatures(err error) *BrokenFeatures { |
| + return &BrokenFeatures{broken: map[string]struct{}{}, err: err} |
| +} |
| + |
| +// SetBrokenFeatures allows you to specify an MCSingleReadWriter function name |
| +// to cause it to return memcache.ErrServerError. e.g. |
| +// |
| +// m.SetBrokenFeatures("Add") |
| +// |
| +// would return memcache.ErrServerError. You can reverse this by calling |
| +// UnsetBrokenFeatures("Add"). |
| +func (b *BrokenFeatures) SetBrokenFeatures(feature ...string) { |
| + b.lock.Lock() |
| + defer b.lock.Unlock() |
| + for _, f := range feature { |
| + b.broken[f] = struct{}{} |
| + } |
| +} |
| + |
| +// UnsetBrokenFeatures is the inverse of SetBrokenFeatures. |
| +func (b *BrokenFeatures) UnsetBrokenFeatures(feature ...string) { |
| + b.lock.Lock() |
| + defer b.lock.Unlock() |
| + for _, f := range feature { |
| + delete(b.broken, f) |
| + } |
| +} |
| + |
| +// IsBroken is to be called internally by the fake service on every |
| +// publically-facing method. If it returns an error, the fake should return |
|
Vadim Sh.
2015/05/24 19:01:57
explicitly from the body of this method?
iannucci
2015/05/24 20:04:31
Added example
|
| +// the error. |
| +func (b *BrokenFeatures) IsBroken() error { |
| + b.lock.Lock() |
| + defer b.lock.Unlock() |
| + if len(b.broken) == 0 { |
| + return nil |
| + } |
| + off := 1 |
| + |
| + for { |
| + fn, _, _, _ := runtime.Caller(off) |
| + name := runtime.FuncForPC(fn).Name() |
| + toks := strings.Split(name, ".") |
| + name = toks[len(toks)-1] |
| + if name == "IsBroken" { |
| + // Allow users to override IsBroken |
| + // ITS MAGIC!!!!! |
| + off++ |
| + continue |
| + } |
| + if _, ok := b.broken[name]; ok { |
| + return b.err |
| + } |
| + break |
| + } |
| + |
| + return nil |
| +} |