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

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: 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
« common/lazyslot/lazyslot.go ('K') | « 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..c8b50a2d5596a6cd93d6c13d15701b05766ad85b 100644
--- a/common/lazyslot/lazyslot_test.go
+++ b/common/lazyslot/lazyslot_test.go
@@ -9,30 +9,35 @@ import (
"testing"
"time"
+ "golang.org/x/net/context"
+
"github.com/luci/luci-go/common/clock/testclock"
+ . "github.com/luci/luci-go/common/testing/assertions"
. "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)
@@ -113,7 +118,8 @@ func TestLazySlot(t *testing.T) {
s.Fetcher = func(c context.Context, prev Value) (Value, error) {
panic("omg")
}
- So(func() { s.Get(c) }, ShouldPanicWith, "omg")
+ _, err = s.Get(c)
+ So(err, ShouldErrLike, "panic caught in lazyslot.Slot Fetcher - omg")
// Doesn't deadlock.
s.Fetcher = func(c context.Context, prev Value) (Value, error) {
@@ -123,6 +129,17 @@ 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
+ },
+ }
+ _, err := s.Get(c)
+ So(err, ShouldErrLike, "lazyslot.Slot Fetcher returned nil value")
+ })
}
func newContext() (context.Context, testclock.TestClock) {
« common/lazyslot/lazyslot.go ('K') | « common/lazyslot/lazyslot.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698