241
241
c.Assert(total, gc.Equals, N)
244
func (*suite) TestSingleFlight(c *gc.C) {
245
p := cache.New(time.Minute)
246
start := make(chan struct{})
247
var wg sync.WaitGroup
252
x, err := p.Get("x", func() (interface{}, error) {
257
c.Check(x, gc.Equals, 99)
258
c.Check(err, gc.Equals, nil)
261
// Wait for the fetch to start.
266
x, err := p.Get("x", func() (interface{}, error) {
267
c.Errorf("fetch function unexpectedly called with inflight request")
270
c.Check(x, gc.Equals, 99)
271
c.Check(err, gc.Equals, nil)
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) {
279
c.Check(y, gc.Equals, 88)
280
c.Check(err, gc.Equals, nil)
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)
244
290
var errUnexpectedFetch = errgo.New("fetch called unexpectedly")
246
292
func fetchError(err error) func() (interface{}, error) {