| 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 "fmt" |
| 8 "sync" | 9 "sync" |
| 9 "testing" | 10 "testing" |
| 10 "time" | 11 "time" |
| 11 | 12 |
| 12 "golang.org/x/net/context" | 13 "golang.org/x/net/context" |
| 13 | 14 |
| 15 "github.com/luci/luci-go/common/clock" |
| 14 "github.com/luci/luci-go/common/clock/testclock" | 16 "github.com/luci/luci-go/common/clock/testclock" |
| 17 "github.com/luci/luci-go/common/retry" |
| 15 | 18 |
| 16 . "github.com/luci/luci-go/common/testing/assertions" | 19 . "github.com/luci/luci-go/common/testing/assertions" |
| 17 . "github.com/smartystreets/goconvey/convey" | 20 . "github.com/smartystreets/goconvey/convey" |
| 18 ) | 21 ) |
| 19 | 22 |
| 20 func TestLazySlot(t *testing.T) { | 23 func TestLazySlot(t *testing.T) { |
| 21 Convey("Blocking mode works", t, func() { | 24 Convey("Blocking mode works", t, func() { |
| 22 c, clk := newContext() | 25 c, clk := newContext() |
| 23 | 26 |
| 24 lock := sync.Mutex{} | 27 lock := sync.Mutex{} |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 Convey("Checks for nil", t, func(conv C) { | 136 Convey("Checks for nil", t, func(conv C) { |
| 134 c, clk := newContext() | 137 c, clk := newContext() |
| 135 s := Slot{ | 138 s := Slot{ |
| 136 Fetcher: func(c context.Context, prev Value) (Value, err
or) { | 139 Fetcher: func(c context.Context, prev Value) (Value, err
or) { |
| 137 return Value{nil, clk.Now().Add(time.Second)}, n
il | 140 return Value{nil, clk.Now().Add(time.Second)}, n
il |
| 138 }, | 141 }, |
| 139 } | 142 } |
| 140 _, err := s.Get(c) | 143 _, err := s.Get(c) |
| 141 So(err, ShouldErrLike, "lazyslot.Slot Fetcher returned nil value
") | 144 So(err, ShouldErrLike, "lazyslot.Slot Fetcher returned nil value
") |
| 142 }) | 145 }) |
| 146 |
| 147 Convey("Does retries", t, func(conv C) { |
| 148 c, clk := newContext() |
| 149 |
| 150 clk.SetTimerCallback(func(d time.Duration, t clock.Timer) { |
| 151 if testclock.HasTags(t, "retry") { |
| 152 clk.Add(d) |
| 153 } |
| 154 }) |
| 155 |
| 156 lock := sync.Mutex{} |
| 157 calls := 0 |
| 158 |
| 159 s := Slot{ |
| 160 RetryFactory: retry.Default, |
| 161 Fetcher: func(c context.Context, prev Value) (Value, err
or) { |
| 162 lock.Lock() |
| 163 calls++ |
| 164 lock.Unlock() |
| 165 return Value{}, fmt.Errorf("omg, error") |
| 166 }, |
| 167 } |
| 168 _, err := s.Get(c) |
| 169 So(err, ShouldErrLike, "context deadline exceeded") |
| 170 So(calls, ShouldEqual, 8) // depends on retry config and deadlin
e |
| 171 }) |
| 143 } | 172 } |
| 144 | 173 |
| 145 func newContext() (context.Context, testclock.TestClock) { | 174 func newContext() (context.Context, testclock.TestClock) { |
| 146 return testclock.UseTime(context.Background(), time.Unix(1442270520, 0)) | 175 return testclock.UseTime(context.Background(), time.Unix(1442270520, 0)) |
| 147 } | 176 } |
| OLD | NEW |