MCPcopy
hub / github.com/dropbox/godropbox / TestGetWithCheckFunc

Method TestGetWithCheckFunc

lockstore/map_test.go:129–221  ·  view source on GitHub ↗
(c *C)

Source from the content-addressed store, hash-verified

127}
128
129func (s *LockingMapSuite) TestGetWithCheckFunc(c *C) {
130 checkerReturn := make(chan bool, 2)
131 checkerCalls := new(int32)
132 l := NewLockingMap(LockingMapOptions{
133 LockStoreOptions: LockStoreOptions{
134 Granularity: PerKeyGranularity,
135 },
136 ValueCheckFunc: func(key string, value interface{}) bool {
137 atomic.AddInt32(checkerCalls, 1)
138 return <-checkerReturn
139 },
140 })
141
142 // add value OK
143 c.Assert(l.Add("foo", 1), Equals, true)
144
145 // Value valid
146 checkerReturn <- true
147 val, ok := l.Get("foo")
148 c.Assert(ok, Equals, true)
149 c.Assert(val, Equals, 1)
150 c.Assert(atomic.LoadInt32(checkerCalls), Equals, int32(1))
151
152 // Value invalid
153 checkerReturn <- false
154 val, ok = l.Get("foo")
155 c.Assert(ok, Equals, false)
156 c.Assert(val, Equals, nil)
157 c.Assert(atomic.LoadInt32(checkerCalls), Equals, int32(2))
158
159 // Checker now returns true, value is visible again (this is weird
160 // but technically correct)
161 checkerReturn <- true
162 val, ok = l.Get("foo")
163 c.Assert(ok, Equals, true)
164 c.Assert(val, Equals, 1)
165 c.Assert(atomic.LoadInt32(checkerCalls), Equals, int32(3))
166
167 // Add with valid value - nothing added
168 checkerReturn <- true
169 c.Assert(l.Add("foo", 2), Equals, false)
170 c.Assert(atomic.LoadInt32(checkerCalls), Equals, int32(4))
171
172 // Value valid, still original 1
173 checkerReturn <- true
174 val, ok = l.Get("foo")
175 c.Assert(ok, Equals, true)
176 c.Assert(val, Equals, 1)
177 c.Assert(atomic.LoadInt32(checkerCalls), Equals, int32(5))
178
179 // Add with invalid value - adds
180 checkerReturn <- false
181 c.Assert(l.Add("foo", 2), Equals, true)
182 c.Assert(atomic.LoadInt32(checkerCalls), Equals, int32(6))
183
184 // Value valid, is now new 2
185 checkerReturn <- true
186 val, ok = l.Get("foo")

Callers

nothing calls this directly

Calls 4

AddMethod · 0.95
GetMethod · 0.95
AddOrGetMethod · 0.95
NewLockingMapFunction · 0.70

Tested by

no test coverage detected