Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1370)

Unified Diff: go/src/infra/gae/libs/gae/brokenfeatures.go

Issue 1240573002: Reland: Refactor current GAE abstraction library to be free of the SDK* (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: expand coverage range to fit 32bit test expectations Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: go/src/infra/gae/libs/gae/brokenfeatures.go
diff --git a/go/src/infra/gae/libs/wrapper/brokenfeatures.go b/go/src/infra/gae/libs/gae/brokenfeatures.go
similarity index 75%
rename from go/src/infra/gae/libs/wrapper/brokenfeatures.go
rename to go/src/infra/gae/libs/gae/brokenfeatures.go
index 4e4524b203bf57efc8e3af5150761512ce4b1282..65bf231c38bfa3af21f523812b2784d1cc561b0a 100644
--- a/go/src/infra/gae/libs/wrapper/brokenfeatures.go
+++ b/go/src/infra/gae/libs/gae/brokenfeatures.go
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-package wrapper
+package gae
import (
"errors"
@@ -34,8 +34,8 @@ type FeatureBreaker interface {
UnbreakFeatures(feature ...string)
}
-// ErrBrokenFeaturesBroken is returned from IsBroken when BrokenFeatures itself
-// isn't working correctly.
+// ErrBrokenFeaturesBroken is returned from RunIfNotBroken when BrokenFeatures
+// itself isn't working correctly.
var ErrBrokenFeaturesBroken = errors.New("brokenFeatures: Unable to retrieve caller information")
// BrokenFeatures implements the FeatureBreaker interface, and is suitable for
@@ -81,14 +81,14 @@ func (b *BrokenFeatures) UnbreakFeatures(feature ...string) {
}
}
-// IsBroken is to be called internally by the fake service on every
-// publically-facing method. If it returns an error, the fake should return
-// the error.
+// RunIfNotBroken is to be called internally by the fake service on every
+// publically-facing method. If it returns an error, the fake should return the
+// error.
//
// Example:
// type MyService struct { BrokenFeatures }
// func (ms *MyService) Thingy() error {
-// if err := ms.IsBroken(); err != nil {
+// if err := ms.RunIfNotBroken(); err != nil {
// return err
// }
// ...
@@ -97,23 +97,25 @@ func (b *BrokenFeatures) UnbreakFeatures(feature ...string) {
// You can now do ms.SetBrokenFeatures("Thingy"), and Thingy will return an
// error.
//
-// Note that IsBroken will keep walking the stack until it finds the first
-// publically-exported method, which will allow you to put the IsBroken call
-// in an internal helper method of your service implementation.
+// Note that RunIfNotBroken will keep walking the stack until it finds the
+// first publically-exported method, which will allow you to put the
+// RunIfNotBroken call in an internal helper method of your service
+// implementation.
//
-// Additionaly, IsBroken allows a very primitive form of overriding; it walks
-// the stack until it finds the first method which is not called "IsBroken".
-// This allows the embedding struct to call into BrokenFeatures.IsBroken from
-// another IsBroken function, and still have it behave correctly.
-func (b *BrokenFeatures) IsBroken() error {
+// Additionaly, RunIfNotBroken allows a very primitive form of overriding; it
+// walks the stack until it finds the first method which is not called
+// "RunIfNotBroken". This allows the embedding struct to call into
+// BrokenFeatures.RunIfNotBroken from another RunIfNotBroken function, and
+// still have it behave correctly.
+func (b *BrokenFeatures) RunIfNotBroken(f func() error) error {
if b.noBrokenFeatures() {
- return nil
+ return f()
}
var name string
for off := 1; ; off++ { // offset of 1 skips ourselves by default
// TODO(riannucci): Profile this to see if it's having an adverse
- // performance impact ont tests.
+ // performance impact on tests.
fn, _, _, ok := runtime.Caller(off)
if !ok {
return ErrBrokenFeaturesBroken
@@ -127,17 +129,19 @@ func (b *BrokenFeatures) IsBroken() error {
// !IsLower, and afaik, in unicode-land they're not direct opposites.
continue
}
- if name == "IsBroken" {
- // Allow users to override IsBroken, keep walking until we see a function
- // which is named differently than IsBroken.
+ if name == "RunIfNotBroken" {
+ // Allow users to override RunIfNotBroken, keep walking until we see
+ // a function which is named differently than RunIfNotBroken.
continue
}
break
}
b.lock.Lock()
+ err, ok := b.broken[name]
defer b.lock.Unlock()
- if err, ok := b.broken[name]; ok {
+
+ if ok {
if err != nil {
return err
}
@@ -147,7 +151,7 @@ func (b *BrokenFeatures) IsBroken() error {
return fmt.Errorf("feature %q is broken", name)
}
- return nil
+ return f()
}
func (b *BrokenFeatures) noBrokenFeatures() bool {
« no previous file with comments | « go/src/infra/gae/epservice/example/service_list.go ('k') | go/src/infra/gae/libs/gae/brokenfeatures_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698