| 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" |
| 9 "runtime" | 13 "runtime" |
| 10 "testing" | 14 "testing" |
| 11 "time" | 15 "time" |
| 12 | 16 |
| 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" | |
| 18 . "github.com/smartystreets/goconvey/convey" | 17 . "github.com/smartystreets/goconvey/convey" |
| 19 "golang.org/x/net/context" | 18 "golang.org/x/net/context" |
| 19 |
| 20 "appengine/memcache" |
| 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 = memory.Use(ctx) |
| 44 » » mc := gae.GetMC(ctx).(interface { | 45 » » mc := wrapper.GetMC(ctx).(interface { |
| 45 » » » gae.Testable | 46 » » » wrapper.Testable |
| 46 » » » gae.Memcache | 47 » » » wrapper.MCSingleReadWriter |
| 47 }) | 48 }) |
| 48 | 49 |
| 49 Convey("fails to acquire when memcache is down", func() { | 50 Convey("fails to acquire when memcache is down", func() { |
| 50 mc.BreakFeatures(nil, "Add") | 51 mc.BreakFeatures(nil, "Add") |
| 51 err := TryWithLock(ctx, "testkey", "id", func(check func
() bool) error { | 52 err := TryWithLock(ctx, "testkey", "id", func(check func
() bool) error { |
| 52 // should never reach here | 53 // should never reach here |
| 53 So(false, ShouldBeTrue) | 54 So(false, ShouldBeTrue) |
| 54 return nil | 55 return nil |
| 55 }) | 56 }) |
| 56 So(err, ShouldEqual, ErrFailedToLock) | 57 So(err, ShouldEqual, ErrFailedToLock) |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 }) | 98 }) |
| 98 | 99 |
| 99 Convey("but sometimes we might lose it", func()
{ | 100 Convey("but sometimes we might lose it", func()
{ |
| 100 Convey("because it was evicted", func()
{ | 101 Convey("because it was evicted", func()
{ |
| 101 mc.Delete(key) | 102 mc.Delete(key) |
| 102 clk.Add(memcacheLockTime) | 103 clk.Add(memcacheLockTime) |
| 103 waitFalse() | 104 waitFalse() |
| 104 }) | 105 }) |
| 105 | 106 |
| 106 Convey("or because it was stolen", func(
) { | 107 Convey("or because it was stolen", func(
) { |
| 107 » » » » » » mc.Set(mc.NewItem(key).SetValue(
[]byte("wat"))) | 108 » » » » » » mc.Set(&memcache.Item{Key: key,
Value: []byte("wat")}) |
| 108 waitFalse() | 109 waitFalse() |
| 109 }) | 110 }) |
| 110 | 111 |
| 111 Convey("or because of service issues", f
unc() { | 112 Convey("or because of service issues", f
unc() { |
| 112 mc.BreakFeatures(nil, "CompareAn
dSwap") | 113 mc.BreakFeatures(nil, "CompareAn
dSwap") |
| 113 waitFalse() | 114 waitFalse() |
| 114 }) | 115 }) |
| 115 }) | 116 }) |
| 116 return nil | 117 return nil |
| 117 }) | 118 }) |
| 118 So(err, ShouldBeNil) | 119 So(err, ShouldBeNil) |
| 119 }) | 120 }) |
| 120 | 121 |
| 121 Convey("an empty context id is an error", func() { | 122 Convey("an empty context id is an error", func() { |
| 122 So(TryWithLock(ctx, "testkey", "", nil), ShouldEqual, Er
rEmptyClientID) | 123 So(TryWithLock(ctx, "testkey", "", nil), ShouldEqual, Er
rEmptyClientID) |
| 123 }) | 124 }) |
| 124 }) | 125 }) |
| 125 } | 126 } |
| OLD | NEW |