| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package lazyslot | 5 package lazyslot |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "sync" | 8 "sync" |
| 9 "testing" | 9 "testing" |
| 10 "time" | 10 "time" |
| 11 | 11 |
| 12 "golang.org/x/net/context" |
| 13 |
| 12 "github.com/luci/luci-go/common/clock/testclock" | 14 "github.com/luci/luci-go/common/clock/testclock" |
| 13 | 15 |
| 14 . "github.com/smartystreets/goconvey/convey" | 16 . "github.com/smartystreets/goconvey/convey" |
| 15 "golang.org/x/net/context" | |
| 16 ) | 17 ) |
| 17 | 18 |
| 18 func TestLazySlot(t *testing.T) { | 19 func TestLazySlot(t *testing.T) { |
| 19 Convey("Blocking mode works", t, func() { | 20 Convey("Blocking mode works", t, func() { |
| 20 c, clk := newContext() | 21 c, clk := newContext() |
| 21 | 22 |
| 23 lock := sync.Mutex{} |
| 22 counter := 0 | 24 counter := 0 |
| 25 |
| 23 s := Slot{ | 26 s := Slot{ |
| 24 Fetcher: func(c context.Context, prev Value) (Value, err
or) { | 27 Fetcher: func(c context.Context, prev Value) (Value, err
or) { |
| 28 lock.Lock() |
| 29 defer lock.Unlock() |
| 25 counter++ | 30 counter++ |
| 26 return Value{counter, clk.Now().Add(time.Second)
}, nil | 31 return Value{counter, clk.Now().Add(time.Second)
}, nil |
| 27 }, | 32 }, |
| 28 } | 33 } |
| 29 | 34 |
| 30 // Initial fetch. | 35 // Initial fetch. |
| 31 » » So(s.Peek(), ShouldResemble, Value{}) | 36 » » So(s.current, ShouldBeNil) |
| 32 v, err := s.Get(c) | 37 v, err := s.Get(c) |
| 33 So(err, ShouldBeNil) | 38 So(err, ShouldBeNil) |
| 34 So(v.Value.(int), ShouldEqual, 1) | 39 So(v.Value.(int), ShouldEqual, 1) |
| 35 So(s.Peek().Value.(int), ShouldEqual, 1) | |
| 36 | 40 |
| 37 // Still fresh. | 41 // Still fresh. |
| 38 v, err = s.Get(c) | 42 v, err = s.Get(c) |
| 39 So(err, ShouldBeNil) | 43 So(err, ShouldBeNil) |
| 40 So(v.Value.(int), ShouldEqual, 1) | 44 So(v.Value.(int), ShouldEqual, 1) |
| 41 | 45 |
| 42 // Expires and refreshed. | 46 // Expires and refreshed. |
| 43 clk.Add(5 * time.Second) | 47 clk.Add(5 * time.Second) |
| 44 v, err = s.Get(c) | 48 v, err = s.Get(c) |
| 45 So(err, ShouldBeNil) | 49 So(err, ShouldBeNil) |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 So(func() { s.Get(c) }, ShouldPanicWith, "omg") | 120 So(func() { s.Get(c) }, ShouldPanicWith, "omg") |
| 117 | 121 |
| 118 // Doesn't deadlock. | 122 // Doesn't deadlock. |
| 119 s.Fetcher = func(c context.Context, prev Value) (Value, error) { | 123 s.Fetcher = func(c context.Context, prev Value) (Value, error) { |
| 120 return Value{2, clk.Now().Add(time.Second)}, nil | 124 return Value{2, clk.Now().Add(time.Second)}, nil |
| 121 } | 125 } |
| 122 v, err = s.Get(c) | 126 v, err = s.Get(c) |
| 123 So(err, ShouldBeNil) | 127 So(err, ShouldBeNil) |
| 124 So(v.Value.(int), ShouldEqual, 2) | 128 So(v.Value.(int), ShouldEqual, 2) |
| 125 }) | 129 }) |
| 130 |
| 131 Convey("Checks for nil", t, func(conv C) { |
| 132 c, clk := newContext() |
| 133 s := Slot{ |
| 134 Fetcher: func(c context.Context, prev Value) (Value, err
or) { |
| 135 return Value{nil, clk.Now().Add(time.Second)}, n
il |
| 136 }, |
| 137 } |
| 138 So(func() { s.Get(c) }, ShouldPanicWith, "lazyslot.Slot Fetcher
returned nil value") |
| 139 }) |
| 126 } | 140 } |
| 127 | 141 |
| 128 func newContext() (context.Context, testclock.TestClock) { | 142 func newContext() (context.Context, testclock.TestClock) { |
| 129 return testclock.UseTime(context.Background(), time.Unix(1442270520, 0)) | 143 return testclock.UseTime(context.Background(), time.Unix(1442270520, 0)) |
| 130 } | 144 } |
| OLD | NEW |