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

Side by Side Diff: go/src/infra/gae/libs/wrapper/featurebreaker.go

Issue 1151473003: Better attempt at an appengine wrapper. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: typo Created 5 years, 7 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package wrapper
6
7 import (
8 "runtime"
9 "strings"
10 "sync"
11 )
12
13 // FeatureBreaker allows a fake implementation to set and unset broken features.
14 // 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
15 // var fake interface{ FeatureBreaker, MCSingleReadWriter } = ...
16 //
17 // you could do:
18 // fake.SetBrokenFeatures("Add", "Set")
19 //
20 // and then
21 // fake.Add(...) and fake.Set(...)
22 //
23 // would return an error (likely memcache.ErrServerError in this case), as if
24 // the service were disconnected or broken.
25 type FeatureBreaker interface {
26 SetBrokenFeatures(feature ...string)
27 UnsetBrokenFeatures(feature ...string)
28 }
29
30 // BrokenFeatures implements the FeatureBreaker interface, and is suitable for
31 // embedding within a fake service.
32 type BrokenFeatures struct {
33 lock sync.Mutex
34
35 broken map[string]struct{}
36 err error
37 }
38
39 // NewBrokenFeatures creates an embeddable *BrokenFeatures which is set to
40 // return err for a given broken feature.
41 func NewBrokenFeatures(err error) *BrokenFeatures {
42 return &BrokenFeatures{broken: map[string]struct{}{}, err: err}
43 }
44
45 // SetBrokenFeatures allows you to specify an MCSingleReadWriter function name
46 // to cause it to return memcache.ErrServerError. e.g.
47 //
48 // m.SetBrokenFeatures("Add")
49 //
50 // would return memcache.ErrServerError. You can reverse this by calling
51 // UnsetBrokenFeatures("Add").
52 func (b *BrokenFeatures) SetBrokenFeatures(feature ...string) {
53 b.lock.Lock()
54 defer b.lock.Unlock()
55 for _, f := range feature {
56 b.broken[f] = struct{}{}
57 }
58 }
59
60 // UnsetBrokenFeatures is the inverse of SetBrokenFeatures.
61 func (b *BrokenFeatures) UnsetBrokenFeatures(feature ...string) {
62 b.lock.Lock()
63 defer b.lock.Unlock()
64 for _, f := range feature {
65 delete(b.broken, f)
66 }
67 }
68
69 // IsBroken is to be called internally by the fake service on every
70 // 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
71 // the error.
72 func (b *BrokenFeatures) IsBroken() error {
73 b.lock.Lock()
74 defer b.lock.Unlock()
75 if len(b.broken) == 0 {
76 return nil
77 }
78 off := 1
79
80 for {
81 fn, _, _, _ := runtime.Caller(off)
82 name := runtime.FuncForPC(fn).Name()
83 toks := strings.Split(name, ".")
84 name = toks[len(toks)-1]
85 if name == "IsBroken" {
86 // Allow users to override IsBroken
87 // ITS MAGIC!!!!!
88 off++
89 continue
90 }
91 if _, ok := b.broken[name]; ok {
92 return b.err
93 }
94 break
95 }
96
97 return nil
98 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698