|
| 1 | +// |
| 2 | +// Copyright 2021 Layotto Authors |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +package consul |
| 15 | + |
| 16 | +import ( |
| 17 | + "github.com/golang/mock/gomock" |
| 18 | + "github.com/hashicorp/consul/api" |
| 19 | + "github.com/stretchr/testify/assert" |
| 20 | + "mosn.io/layotto/components/lock" |
| 21 | + "mosn.io/layotto/components/pkg/utils" |
| 22 | + "mosn.io/pkg/log" |
| 23 | + "testing" |
| 24 | +) |
| 25 | + |
| 26 | +const resouseId = "resoure_1" |
| 27 | +const lockOwerA = "p1" |
| 28 | +const lockOwerB = "p2" |
| 29 | +const expireTime = 5 |
| 30 | + |
| 31 | +//A lock A unlock |
| 32 | +func TestConsulLock_TryLock(t *testing.T) { |
| 33 | + //mock |
| 34 | + ctrl := gomock.NewController(t) |
| 35 | + client := utils.NewMockConsulClient(ctrl) |
| 36 | + factory := utils.NewMockSessionFactory(ctrl) |
| 37 | + kv := utils.NewMockConsulKV(ctrl) |
| 38 | + |
| 39 | + comp := NewConsulLock(log.DefaultLogger) |
| 40 | + cfg := lock.Metadata{ |
| 41 | + Properties: make(map[string]string), |
| 42 | + } |
| 43 | + cfg.Properties["address"] = "127.0.0.1:8500" |
| 44 | + err := comp.Init(cfg) |
| 45 | + comp.client = client |
| 46 | + comp.sessionFactory = factory |
| 47 | + comp.kv = kv |
| 48 | + factory.EXPECT().Create(&api.SessionEntry{TTL: getTTL(expireTime), LockDelay: 0, Behavior: "delete"}, nil). |
| 49 | + Return("session1", nil, nil).Times(1) |
| 50 | + factory.EXPECT().Destroy("session1", nil).Return(nil, nil).Times(1) |
| 51 | + kv.EXPECT().Acquire(&api.KVPair{Key: resouseId, Value: []byte(lockOwerA), Session: "session1"}, nil). |
| 52 | + Return(true, nil, nil).Times(1) |
| 53 | + kv.EXPECT().Release(&api.KVPair{Key: resouseId, Value: []byte(lockOwerA), Session: "session1"}, nil). |
| 54 | + Return(true, nil, nil).Times(1) |
| 55 | + |
| 56 | + tryLock, err := comp.TryLock(&lock.TryLockRequest{ |
| 57 | + ResourceId: resouseId, |
| 58 | + LockOwner: lockOwerA, |
| 59 | + Expire: expireTime, |
| 60 | + }) |
| 61 | + |
| 62 | + assert.NoError(t, err) |
| 63 | + assert.Equal(t, true, tryLock.Success) |
| 64 | + |
| 65 | + unlock, err := comp.Unlock(&lock.UnlockRequest{ |
| 66 | + ResourceId: resouseId, |
| 67 | + LockOwner: lockOwerA, |
| 68 | + }) |
| 69 | + |
| 70 | + assert.NoError(t, err) |
| 71 | + assert.Equal(t, lock.SUCCESS, unlock.Status) |
| 72 | + |
| 73 | +} |
| 74 | + |
| 75 | +//A lock B lock |
| 76 | +func TestConsulLock_ALock_BLock(t *testing.T) { |
| 77 | + |
| 78 | + //mock |
| 79 | + ctrl := gomock.NewController(t) |
| 80 | + client := utils.NewMockConsulClient(ctrl) |
| 81 | + factory := utils.NewMockSessionFactory(ctrl) |
| 82 | + kv := utils.NewMockConsulKV(ctrl) |
| 83 | + |
| 84 | + comp := NewConsulLock(log.DefaultLogger) |
| 85 | + cfg := lock.Metadata{ |
| 86 | + Properties: make(map[string]string), |
| 87 | + } |
| 88 | + cfg.Properties["address"] = "127.0.0.1:8500" |
| 89 | + err := comp.Init(cfg) |
| 90 | + comp.client = client |
| 91 | + comp.sessionFactory = factory |
| 92 | + comp.kv = kv |
| 93 | + factory.EXPECT().Create(&api.SessionEntry{TTL: getTTL(expireTime), LockDelay: 0, Behavior: "delete"}, nil). |
| 94 | + Return("session1", nil, nil).Times(1) |
| 95 | + factory.EXPECT().Create(&api.SessionEntry{TTL: getTTL(expireTime), LockDelay: 0, Behavior: "delete"}, nil). |
| 96 | + Return("session2", nil, nil).Times(1) |
| 97 | + kv.EXPECT().Acquire(&api.KVPair{Key: resouseId, Value: []byte(lockOwerA), Session: "session1"}, nil). |
| 98 | + Return(true, nil, nil).Times(1) |
| 99 | + kv.EXPECT().Acquire(&api.KVPair{Key: resouseId, Value: []byte(lockOwerB), Session: "session2"}, nil). |
| 100 | + Return(false, nil, nil).Times(1) |
| 101 | + |
| 102 | + tryLock, _ := comp.TryLock(&lock.TryLockRequest{ |
| 103 | + ResourceId: resouseId, |
| 104 | + LockOwner: lockOwerA, |
| 105 | + Expire: expireTime, |
| 106 | + }) |
| 107 | + |
| 108 | + assert.NoError(t, err) |
| 109 | + assert.Equal(t, true, tryLock.Success) |
| 110 | + |
| 111 | + bLock, _ := comp.TryLock(&lock.TryLockRequest{ |
| 112 | + ResourceId: resouseId, |
| 113 | + LockOwner: lockOwerB, |
| 114 | + Expire: expireTime, |
| 115 | + }) |
| 116 | + |
| 117 | + assert.NoError(t, err) |
| 118 | + assert.Equal(t, false, bLock.Success) |
| 119 | + |
| 120 | +} |
| 121 | + |
| 122 | +// A lock B unlock A unlock |
| 123 | +func TestConsulLock_ALock_BUnlock(t *testing.T) { |
| 124 | + //mock |
| 125 | + ctrl := gomock.NewController(t) |
| 126 | + client := utils.NewMockConsulClient(ctrl) |
| 127 | + factory := utils.NewMockSessionFactory(ctrl) |
| 128 | + kv := utils.NewMockConsulKV(ctrl) |
| 129 | + |
| 130 | + comp := NewConsulLock(log.DefaultLogger) |
| 131 | + cfg := lock.Metadata{ |
| 132 | + Properties: make(map[string]string), |
| 133 | + } |
| 134 | + cfg.Properties["address"] = "127.0.0.1:8500" |
| 135 | + err := comp.Init(cfg) |
| 136 | + comp.client = client |
| 137 | + comp.sessionFactory = factory |
| 138 | + comp.kv = kv |
| 139 | + factory.EXPECT().Create(&api.SessionEntry{TTL: getTTL(expireTime), LockDelay: 0, Behavior: "delete"}, nil). |
| 140 | + Return("session1", nil, nil).Times(1) |
| 141 | + factory.EXPECT().Destroy("session1", nil).Return(nil, nil).Times(1) |
| 142 | + kv.EXPECT().Acquire(&api.KVPair{Key: resouseId, Value: []byte(lockOwerA), Session: "session1"}, nil). |
| 143 | + Return(true, nil, nil).Times(1) |
| 144 | + kv.EXPECT().Release(&api.KVPair{Key: resouseId, Value: []byte(lockOwerA), Session: "session1"}, nil). |
| 145 | + Return(true, nil, nil).Times(1) |
| 146 | + |
| 147 | + tryLock, _ := comp.TryLock(&lock.TryLockRequest{ |
| 148 | + ResourceId: resouseId, |
| 149 | + LockOwner: lockOwerA, |
| 150 | + Expire: expireTime, |
| 151 | + }) |
| 152 | + |
| 153 | + assert.NoError(t, err) |
| 154 | + assert.Equal(t, true, tryLock.Success) |
| 155 | + |
| 156 | + unlock, _ := comp.Unlock(&lock.UnlockRequest{ |
| 157 | + ResourceId: resouseId, |
| 158 | + LockOwner: lockOwerB, |
| 159 | + }) |
| 160 | + |
| 161 | + assert.NoError(t, err) |
| 162 | + assert.Equal(t, lock.LOCK_UNEXIST, unlock.Status) |
| 163 | + |
| 164 | + unlock2, err := comp.Unlock(&lock.UnlockRequest{ |
| 165 | + ResourceId: resouseId, |
| 166 | + LockOwner: lockOwerA, |
| 167 | + }) |
| 168 | + |
| 169 | + assert.NoError(t, err) |
| 170 | + assert.Equal(t, lock.SUCCESS, unlock2.Status) |
| 171 | +} |
0 commit comments