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