Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(442)

Unified Diff: common/lazyslot/lazyslot_test.go

Issue 1750023002: common/lazyslot: Simplify code a little bit. (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-go@master
Patch Set: don't trap panics Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « common/lazyslot/lazyslot.go ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: common/lazyslot/lazyslot_test.go
diff --git a/common/lazyslot/lazyslot_test.go b/common/lazyslot/lazyslot_test.go
index c221590cde340eb9d37c92d1e03ad741a5ec16f1..e46f69f25921b7b95ee934d604054a4d7762baa8 100644
--- a/common/lazyslot/lazyslot_test.go
+++ b/common/lazyslot/lazyslot_test.go
@@ -9,30 +9,34 @@ import (
"testing"
"time"
+ "golang.org/x/net/context"
+
"github.com/luci/luci-go/common/clock/testclock"
. "github.com/smartystreets/goconvey/convey"
- "golang.org/x/net/context"
)
func TestLazySlot(t *testing.T) {
Convey("Blocking mode works", t, func() {
c, clk := newContext()
+ lock := sync.Mutex{}
counter := 0
+
s := Slot{
Fetcher: func(c context.Context, prev Value) (Value, error) {
+ lock.Lock()
+ defer lock.Unlock()
counter++
return Value{counter, clk.Now().Add(time.Second)}, nil
},
}
// Initial fetch.
- So(s.Peek(), ShouldResemble, Value{})
+ So(s.current, ShouldBeNil)
v, err := s.Get(c)
So(err, ShouldBeNil)
So(v.Value.(int), ShouldEqual, 1)
- So(s.Peek().Value.(int), ShouldEqual, 1)
// Still fresh.
v, err = s.Get(c)
@@ -123,6 +127,16 @@ func TestLazySlot(t *testing.T) {
So(err, ShouldBeNil)
So(v.Value.(int), ShouldEqual, 2)
})
+
+ Convey("Checks for nil", t, func(conv C) {
+ c, clk := newContext()
+ s := Slot{
+ Fetcher: func(c context.Context, prev Value) (Value, error) {
+ return Value{nil, clk.Now().Add(time.Second)}, nil
+ },
+ }
+ So(func() { s.Get(c) }, ShouldPanicWith, "lazyslot.Slot Fetcher returned nil value")
+ })
}
func newContext() (context.Context, testclock.TestClock) {
« no previous file with comments | « common/lazyslot/lazyslot.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698