~juju-qa/ubuntu/xenial/juju/2.0-rc2

« back to all changes in this revision

Viewing changes to src/github.com/juju/utils/cache/cache_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-09-30 14:39:30 UTC
  • mfrom: (1.8.1)
  • Revision ID: nicholas.skaggs@canonical.com-20160930143930-vwwhrefh6ftckccy
import upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
241
241
        c.Assert(total, gc.Equals, N)
242
242
}
243
243
 
 
244
func (*suite) TestSingleFlight(c *gc.C) {
 
245
        p := cache.New(time.Minute)
 
246
        start := make(chan struct{})
 
247
        var wg sync.WaitGroup
 
248
 
 
249
        wg.Add(1)
 
250
        go func() {
 
251
                defer wg.Done()
 
252
                x, err := p.Get("x", func() (interface{}, error) {
 
253
                        start <- struct{}{}
 
254
                        <-start
 
255
                        return 99, nil
 
256
                })
 
257
                c.Check(x, gc.Equals, 99)
 
258
                c.Check(err, gc.Equals, nil)
 
259
 
 
260
        }()
 
261
        // Wait for the fetch to start.
 
262
        <-start
 
263
        wg.Add(1)
 
264
        go func() {
 
265
                defer wg.Done()
 
266
                x, err := p.Get("x", func() (interface{}, error) {
 
267
                        c.Errorf("fetch function unexpectedly called with inflight request")
 
268
                        return 55, nil
 
269
                })
 
270
                c.Check(x, gc.Equals, 99)
 
271
                c.Check(err, gc.Equals, nil)
 
272
        }()
 
273
 
 
274
        // Check that we can still get other values while the
 
275
        // other fetches are in progress.
 
276
        y, err := p.Get("y", func() (interface{}, error) {
 
277
                return 88, nil
 
278
        })
 
279
        c.Check(y, gc.Equals, 88)
 
280
        c.Check(err, gc.Equals, nil)
 
281
 
 
282
        // Let the original fetch proceed, which should let the other one
 
283
        // succeed too, but sleep for a little bit to let the second goroutine
 
284
        // actually initiate its request.
 
285
        time.Sleep(time.Millisecond)
 
286
        start <- struct{}{}
 
287
        wg.Wait()
 
288
}
 
289
 
244
290
var errUnexpectedFetch = errgo.New("fetch called unexpectedly")
245
291
 
246
292
func fetchError(err error) func() (interface{}, error) {