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 memlock | 5 package memlock |
6 | 6 |
7 import ( | 7 import ( |
8 "fmt" | 8 "fmt" |
9 "runtime" | 9 "runtime" |
10 "testing" | 10 "testing" |
11 "time" | 11 "time" |
12 | 12 |
13 "infra/gae/libs/gae" | 13 "infra/gae/libs/gae" |
| 14 "infra/gae/libs/gae/filters/featureBreaker" |
14 "infra/gae/libs/gae/memory" | 15 "infra/gae/libs/gae/memory" |
15 | 16 |
16 "github.com/luci/luci-go/common/clock" | 17 "github.com/luci/luci-go/common/clock" |
17 "github.com/luci/luci-go/common/clock/testclock" | 18 "github.com/luci/luci-go/common/clock/testclock" |
18 . "github.com/smartystreets/goconvey/convey" | 19 . "github.com/smartystreets/goconvey/convey" |
19 "golang.org/x/net/context" | 20 "golang.org/x/net/context" |
20 ) | 21 ) |
21 | 22 |
22 func init() { | 23 func init() { |
23 delay = time.Millisecond | 24 delay = time.Millisecond |
24 memcacheLockTime = time.Millisecond * 4 | 25 memcacheLockTime = time.Millisecond * 4 |
25 } | 26 } |
26 | 27 |
27 func TestSimple(t *testing.T) { | 28 func TestSimple(t *testing.T) { |
28 // TODO(riannucci): Mock time.After so that we don't have to delay for r
eal. | 29 // TODO(riannucci): Mock time.After so that we don't have to delay for r
eal. |
29 | 30 |
30 const key = memlockKeyPrefix + "testkey" | 31 const key = memlockKeyPrefix + "testkey" |
31 | 32 |
32 Convey("basic locking", t, func() { | 33 Convey("basic locking", t, func() { |
33 start := time.Date(1986, time.October, 26, 1, 20, 00, 00, time.U
TC) | 34 start := time.Date(1986, time.October, 26, 1, 20, 00, 00, time.U
TC) |
34 ctx, clk := testclock.UseTime(context.Background(), start) | 35 ctx, clk := testclock.UseTime(context.Background(), start) |
35 blocker := make(chan struct{}) | 36 blocker := make(chan struct{}) |
36 clk.SetTimerCallback(func(clock.Timer) { | 37 clk.SetTimerCallback(func(clock.Timer) { |
37 clk.Add(delay) | 38 clk.Add(delay) |
38 select { | 39 select { |
39 case blocker <- struct{}{}: | 40 case blocker <- struct{}{}: |
40 default: | 41 default: |
41 } | 42 } |
42 }) | 43 }) |
43 » » ctx = memory.Use(ctx) | 44 » » ctx, fb := featureBreaker.FilterMC(memory.Use(ctx), nil) |
44 » » mc := gae.GetMC(ctx).(interface { | 45 » » mc := gae.GetMC(ctx) |
45 » » » gae.Testable | |
46 » » » gae.Memcache | |
47 » » }) | |
48 | 46 |
49 Convey("fails to acquire when memcache is down", func() { | 47 Convey("fails to acquire when memcache is down", func() { |
50 » » » mc.BreakFeatures(nil, "Add") | 48 » » » fb.BreakFeatures(nil, "Add") |
51 err := TryWithLock(ctx, "testkey", "id", func(check func
() bool) error { | 49 err := TryWithLock(ctx, "testkey", "id", func(check func
() bool) error { |
52 // should never reach here | 50 // should never reach here |
53 So(false, ShouldBeTrue) | 51 So(false, ShouldBeTrue) |
54 return nil | 52 return nil |
55 }) | 53 }) |
56 So(err, ShouldEqual, ErrFailedToLock) | 54 So(err, ShouldEqual, ErrFailedToLock) |
57 }) | 55 }) |
58 | 56 |
59 Convey("returns the inner error", func() { | 57 Convey("returns the inner error", func() { |
60 toRet := fmt.Errorf("sup") | 58 toRet := fmt.Errorf("sup") |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 clk.Add(memcacheLockTime) | 100 clk.Add(memcacheLockTime) |
103 waitFalse() | 101 waitFalse() |
104 }) | 102 }) |
105 | 103 |
106 Convey("or because it was stolen", func(
) { | 104 Convey("or because it was stolen", func(
) { |
107 mc.Set(mc.NewItem(key).SetValue(
[]byte("wat"))) | 105 mc.Set(mc.NewItem(key).SetValue(
[]byte("wat"))) |
108 waitFalse() | 106 waitFalse() |
109 }) | 107 }) |
110 | 108 |
111 Convey("or because of service issues", f
unc() { | 109 Convey("or because of service issues", f
unc() { |
112 » » » » » » mc.BreakFeatures(nil, "CompareAn
dSwap") | 110 » » » » » » fb.BreakFeatures(nil, "CompareAn
dSwap") |
113 waitFalse() | 111 waitFalse() |
114 }) | 112 }) |
115 }) | 113 }) |
116 return nil | 114 return nil |
117 }) | 115 }) |
118 So(err, ShouldBeNil) | 116 So(err, ShouldBeNil) |
119 }) | 117 }) |
120 | 118 |
121 Convey("an empty context id is an error", func() { | 119 Convey("an empty context id is an error", func() { |
122 So(TryWithLock(ctx, "testkey", "", nil), ShouldEqual, Er
rEmptyClientID) | 120 So(TryWithLock(ctx, "testkey", "", nil), ShouldEqual, Er
rEmptyClientID) |
123 }) | 121 }) |
124 }) | 122 }) |
125 } | 123 } |
OLD | NEW |