| 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 |
| 19 . "github.com/luci/luci-go/common/testing/assertions" |
| 16 . "github.com/smartystreets/goconvey/convey" | 20 . "github.com/smartystreets/goconvey/convey" |
| 17 ) | 21 ) |
| 18 | 22 |
| 19 func TestLazySlot(t *testing.T) { | 23 func TestLazySlot(t *testing.T) { |
| 20 Convey("Blocking mode works", t, func() { | 24 Convey("Blocking mode works", t, func() { |
| 21 c, clk := newContext() | 25 c, clk := newContext() |
| 22 | 26 |
| 23 lock := sync.Mutex{} | 27 lock := sync.Mutex{} |
| 24 counter := 0 | 28 counter := 0 |
| 25 | 29 |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 | 134 |
| 131 Convey("Checks for nil", t, func(conv C) { | 135 Convey("Checks for nil", t, func(conv C) { |
| 132 c, clk := newContext() | 136 c, clk := newContext() |
| 133 s := Slot{ | 137 s := Slot{ |
| 134 Fetcher: func(c context.Context, prev Value) (Value, err
or) { | 138 Fetcher: func(c context.Context, prev Value) (Value, err
or) { |
| 135 return Value{nil, clk.Now().Add(time.Second)}, n
il | 139 return Value{nil, clk.Now().Add(time.Second)}, n
il |
| 136 }, | 140 }, |
| 137 } | 141 } |
| 138 So(func() { s.Get(c) }, ShouldPanicWith, "lazyslot.Slot Fetcher
returned nil value") | 142 So(func() { s.Get(c) }, ShouldPanicWith, "lazyslot.Slot Fetcher
returned nil value") |
| 139 }) | 143 }) |
| 144 |
| 145 Convey("Does retries", t, func(conv C) { |
| 146 c, clk := newContext() |
| 147 |
| 148 clk.SetTimerCallback(func(d time.Duration, t clock.Timer) { |
| 149 if testclock.HasTags(t, "retry") { |
| 150 clk.Add(d) |
| 151 } |
| 152 }) |
| 153 |
| 154 lock := sync.Mutex{} |
| 155 calls := 0 |
| 156 |
| 157 s := Slot{ |
| 158 RetryFactory: retry.Default, |
| 159 Fetcher: func(c context.Context, prev Value) (Value, err
or) { |
| 160 lock.Lock() |
| 161 defer lock.Unlock() |
| 162 calls++ |
| 163 return Value{}, fmt.Errorf("omg, error") |
| 164 }, |
| 165 } |
| 166 _, err := s.Get(c) |
| 167 So(err, ShouldErrLike, "context deadline exceeded") |
| 168 So(calls, ShouldEqual, 8) // depends on retry config and deadlin
e |
| 169 }) |
| 170 |
| 171 Convey("Panics in retries", t, func(conv C) { |
| 172 c, clk := newContext() |
| 173 |
| 174 clk.SetTimerCallback(func(d time.Duration, t clock.Timer) { |
| 175 if testclock.HasTags(t, "retry") { |
| 176 clk.Add(d) |
| 177 } |
| 178 }) |
| 179 |
| 180 lock := sync.Mutex{} |
| 181 calls := 0 |
| 182 |
| 183 s := Slot{ |
| 184 RetryFactory: retry.Default, |
| 185 Fetcher: func(c context.Context, prev Value) (Value, err
or) { |
| 186 lock.Lock() |
| 187 defer lock.Unlock() |
| 188 calls++ |
| 189 if calls == 4 { |
| 190 panic("omg, panic") |
| 191 } |
| 192 return Value{}, fmt.Errorf("omg, error") |
| 193 }, |
| 194 } |
| 195 So(func() { s.Get(c) }, ShouldPanicWith, "omg, panic") |
| 196 So(calls, ShouldEqual, 4) |
| 197 }) |
| 140 } | 198 } |
| 141 | 199 |
| 142 func newContext() (context.Context, testclock.TestClock) { | 200 func newContext() (context.Context, testclock.TestClock) { |
| 143 return testclock.UseTime(context.Background(), time.Unix(1442270520, 0)) | 201 return testclock.UseTime(context.Background(), time.Unix(1442270520, 0)) |
| 144 } | 202 } |
| OLD | NEW |