| 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 |
| 16 . "github.com/luci/luci-go/common/testing/assertions" |
| 14 . "github.com/smartystreets/goconvey/convey" | 17 . "github.com/smartystreets/goconvey/convey" |
| 15 "golang.org/x/net/context" | |
| 16 ) | 18 ) |
| 17 | 19 |
| 18 func TestLazySlot(t *testing.T) { | 20 func TestLazySlot(t *testing.T) { |
| 19 Convey("Blocking mode works", t, func() { | 21 Convey("Blocking mode works", t, func() { |
| 20 c, clk := newContext() | 22 c, clk := newContext() |
| 21 | 23 |
| 24 lock := sync.Mutex{} |
| 22 counter := 0 | 25 counter := 0 |
| 26 |
| 23 s := Slot{ | 27 s := Slot{ |
| 24 Fetcher: func(c context.Context, prev Value) (Value, err
or) { | 28 Fetcher: func(c context.Context, prev Value) (Value, err
or) { |
| 29 lock.Lock() |
| 30 defer lock.Unlock() |
| 25 counter++ | 31 counter++ |
| 26 return Value{counter, clk.Now().Add(time.Second)
}, nil | 32 return Value{counter, clk.Now().Add(time.Second)
}, nil |
| 27 }, | 33 }, |
| 28 } | 34 } |
| 29 | 35 |
| 30 // Initial fetch. | 36 // Initial fetch. |
| 31 » » So(s.Peek(), ShouldResemble, Value{}) | 37 » » So(s.current, ShouldBeNil) |
| 32 v, err := s.Get(c) | 38 v, err := s.Get(c) |
| 33 So(err, ShouldBeNil) | 39 So(err, ShouldBeNil) |
| 34 So(v.Value.(int), ShouldEqual, 1) | 40 So(v.Value.(int), ShouldEqual, 1) |
| 35 So(s.Peek().Value.(int), ShouldEqual, 1) | |
| 36 | 41 |
| 37 // Still fresh. | 42 // Still fresh. |
| 38 v, err = s.Get(c) | 43 v, err = s.Get(c) |
| 39 So(err, ShouldBeNil) | 44 So(err, ShouldBeNil) |
| 40 So(v.Value.(int), ShouldEqual, 1) | 45 So(v.Value.(int), ShouldEqual, 1) |
| 41 | 46 |
| 42 // Expires and refreshed. | 47 // Expires and refreshed. |
| 43 clk.Add(5 * time.Second) | 48 clk.Add(5 * time.Second) |
| 44 v, err = s.Get(c) | 49 v, err = s.Get(c) |
| 45 So(err, ShouldBeNil) | 50 So(err, ShouldBeNil) |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 } | 111 } |
| 107 v, err := s.Get(c) | 112 v, err := s.Get(c) |
| 108 So(err, ShouldBeNil) | 113 So(err, ShouldBeNil) |
| 109 So(v.Value.(int), ShouldEqual, 1) | 114 So(v.Value.(int), ShouldEqual, 1) |
| 110 | 115 |
| 111 // Make it expire. Start panicing fetch. | 116 // Make it expire. Start panicing fetch. |
| 112 clk.Add(5 * time.Second) | 117 clk.Add(5 * time.Second) |
| 113 s.Fetcher = func(c context.Context, prev Value) (Value, error) { | 118 s.Fetcher = func(c context.Context, prev Value) (Value, error) { |
| 114 panic("omg") | 119 panic("omg") |
| 115 } | 120 } |
| 116 » » So(func() { s.Get(c) }, ShouldPanicWith, "omg") | 121 » » _, err = s.Get(c) |
| 122 » » So(err, ShouldErrLike, "panic caught in lazyslot.Slot Fetcher -
omg") |
| 117 | 123 |
| 118 // Doesn't deadlock. | 124 // Doesn't deadlock. |
| 119 s.Fetcher = func(c context.Context, prev Value) (Value, error) { | 125 s.Fetcher = func(c context.Context, prev Value) (Value, error) { |
| 120 return Value{2, clk.Now().Add(time.Second)}, nil | 126 return Value{2, clk.Now().Add(time.Second)}, nil |
| 121 } | 127 } |
| 122 v, err = s.Get(c) | 128 v, err = s.Get(c) |
| 123 So(err, ShouldBeNil) | 129 So(err, ShouldBeNil) |
| 124 So(v.Value.(int), ShouldEqual, 2) | 130 So(v.Value.(int), ShouldEqual, 2) |
| 125 }) | 131 }) |
| 132 |
| 133 Convey("Checks for nil", t, func(conv C) { |
| 134 c, clk := newContext() |
| 135 s := Slot{ |
| 136 Fetcher: func(c context.Context, prev Value) (Value, err
or) { |
| 137 return Value{nil, clk.Now().Add(time.Second)}, n
il |
| 138 }, |
| 139 } |
| 140 _, err := s.Get(c) |
| 141 So(err, ShouldErrLike, "lazyslot.Slot Fetcher returned nil value
") |
| 142 }) |
| 126 } | 143 } |
| 127 | 144 |
| 128 func newContext() (context.Context, testclock.TestClock) { | 145 func newContext() (context.Context, testclock.TestClock) { |
| 129 return testclock.UseTime(context.Background(), time.Unix(1442270520, 0)) | 146 return testclock.UseTime(context.Background(), time.Unix(1442270520, 0)) |
| 130 } | 147 } |
| OLD | NEW |