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

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

Issue 1152383003: Simple memory testing for gae/wrapper (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@better_context_lite
Patch Set: fix capitalization Created 5 years, 6 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 memory
6
7 import (
8 "fmt"
9 "infra/gae/libs/wrapper"
M-A Ruel 2015/05/27 20:14:47 goimports really generated that? That would be hig
iannucci 2015/05/27 21:36:22 yeah it really did :(. I think it's because it thi
10 "math/rand"
11 "sync"
12
13 "golang.org/x/net/context"
14
15 "appengine/datastore"
16 )
17
18 type memContextObj interface {
19 sync.Locker
20 canApplyTxn(m memContextObj) bool
21 applyTxn(rnd *rand.Rand, m memContextObj)
22
23 endTxn()
24 mkTxn(*datastore.TransactionOptions) (memContextObj, error)
25 }
26
27 type memContext []memContextObj
28
29 func newMemContext() memContext {
30 return memContext{
31 newTaskQueueData(),
32 newDataStoreData(),
33 }
34 }
35
36 var memContextIndices = map[string]int{
37 "TQ": 0,
38 "DS": 1,
39 }
40
41 func (m memContext) Get(item string) memContextObj {
42 if i, ok := memContextIndices[item]; !ok {
M-A Ruel 2015/05/27 20:14:47 if i, ok := memContextIndices[item]; ok { return
iannucci 2015/05/27 21:36:22 done
43 panic(fmt.Errorf("wrapper/memory: cannot get context item %q", i tem))
44 } else {
45 return m[i]
46 }
47 }
48
49 func (m memContext) Lock() {
50 for _, itm := range m {
51 itm.Lock()
52 }
53 }
54
55 func (m memContext) Unlock() {
56 for i := len(m) - 1; i >= 0; i-- {
57 m[i].Unlock()
58 }
59 }
60
61 func (m memContext) endTxn() {
62 for _, itm := range m {
63 itm.endTxn()
64 }
65 }
66
67 func (m memContext) mkTxn(o *datastore.TransactionOptions) (memContext, error) {
68 ret := make(memContext, len(m))
69 for i, itm := range m {
70 newItm, err := itm.mkTxn(o)
71 if err != nil {
72 return nil, err
73 }
74 ret[i] = newItm
75 }
76 return ret, nil
77 }
78
79 func (m memContext) canApplyTxn(txnCtx memContext) bool {
80 for i := range m {
81 if !m[i].canApplyTxn(txnCtx[i]) {
82 return false
83 }
84 }
85 return true
86 }
87
88 func (m memContext) applyTxn(rnd *rand.Rand, txnCtx memContext) {
89 for i := range m {
90 m[i].applyTxn(rnd, txnCtx[i])
91 }
92 }
93
94 // Enable adds a new memory context to c. This new memory context will have
95 // a zeroed state.
96 func Enable(c context.Context) context.Context {
97 return context.WithValue(
98 context.WithValue(c, memContextKey, newMemContext()),
99 giContextKey, &globalInfoData{})
100 }
101
102 // Use calls ALL of this packages Use* methods on c. This enables all
103 // gae/wrapper Get* api's.
104 func Use(c context.Context) context.Context {
105 return UseTQ(UseDS(UseMC(UseGI(c))))
106 }
107
108 func cur(c context.Context) (p memContext) {
109 p, _ = c.Value(memContextKey).(memContext)
110 return
111 }
112
113 type memContextKeyType int
114
115 var memContextKey memContextKeyType
116
117 // weird stuff
118
119 // RunInTransaction is here because it's really a service-wide transaction, not
120 // just in the datastore. TaskQueue behaves differently in a transaction in
121 // a couple ways, for example.
M-A Ruel 2015/05/27 20:14:47 FWIU, it's really more that TaskQueue implicitly d
iannucci 2015/05/27 21:36:22 it also isn't allowed to have a Name parameter in
122 //
123 // It really should have been appengine.Context.RunInTransaction(func(tc...)),
124 // but because it's not, this method is on dsImpl instead to mirror the official
125 // API.
126 //
127 // The fake implementation also differs from the real implementation because the
128 // fake TaskQueue is NOT backed by the fake Datastore. This is done to make the
129 // test-access API for TaskQueue better (instead of trying to reconstitute the
130 // state of the task queue from a bunch of datastore accesses).
131 func (d *dsImpl) RunInTransaction(f func(context.Context) error, o *datastore.Tr ansactionOptions) error {
132 curMC := cur(d.c)
133
134 txnMC, err := curMC.mkTxn(o)
135 if err != nil {
136 return err
137 }
138
139 defer func() {
140 txnMC.Lock()
141 defer txnMC.Unlock()
142
143 txnMC.endTxn()
144 }()
145
146 if err = f(context.WithValue(d.c, memContextKey, txnMC)); err != nil {
147 return err
148 }
149
150 txnMC.Lock()
151 defer txnMC.Unlock()
152
153 if curMC.canApplyTxn(txnMC) {
154 curMC.applyTxn(wrapper.GetMathRand(d.c), txnMC)
155 } else {
156 return datastore.ErrConcurrentTransaction
157 }
158 return nil
159 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698