2016-06-22 15:27:47 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2016-06-22 15:27:47 -04:00
|
|
|
|
2016-08-31 14:39:08 -04:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
2016-06-22 15:27:47 -04:00
|
|
|
|
2018-01-17 10:24:46 -05:00
|
|
|
// WARNING:
|
|
|
|
//
|
|
|
|
// Expected source line number is hard coded, 32, in the
|
|
|
|
// following test. Adding new code before this test or changing its
|
|
|
|
// position will cause the line number to change and the test to FAIL
|
|
|
|
// Tests getSource().
|
|
|
|
func TestGetSource(t *testing.T) {
|
|
|
|
currentSource := func() string { return getSource() }
|
|
|
|
gotSource := currentSource()
|
|
|
|
// Hard coded line number, 32, in the "expectedSource" value
|
|
|
|
expectedSource := "[namespace-lock_test.go:32:TestGetSource()]"
|
|
|
|
if gotSource != expectedSource {
|
|
|
|
t.Errorf("expected : %s, got : %s", expectedSource, gotSource)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-22 15:27:47 -04:00
|
|
|
// Tests functionality provided by namespace lock.
|
|
|
|
func TestNamespaceLockTest(t *testing.T) {
|
2018-01-23 22:34:09 -05:00
|
|
|
isDistXL := false
|
|
|
|
initNSLock(isDistXL)
|
2016-06-22 15:27:47 -04:00
|
|
|
// List of test cases.
|
|
|
|
testCases := []struct {
|
2017-08-31 14:29:22 -04:00
|
|
|
lk func(s1, s2, s3 string, t time.Duration) bool
|
2016-08-31 14:39:08 -04:00
|
|
|
unlk func(s1, s2, s3 string)
|
2017-08-31 14:29:22 -04:00
|
|
|
rlk func(s1, s2, s3 string, t time.Duration) bool
|
2016-08-31 14:39:08 -04:00
|
|
|
runlk func(s1, s2, s3 string)
|
2016-06-22 15:27:47 -04:00
|
|
|
lkCount int
|
|
|
|
lockedRefCount uint
|
|
|
|
unlockedRefCount uint
|
|
|
|
shouldPass bool
|
|
|
|
}{
|
|
|
|
{
|
2016-12-10 19:15:12 -05:00
|
|
|
lk: globalNSMutex.Lock,
|
|
|
|
unlk: globalNSMutex.Unlock,
|
2016-06-22 15:27:47 -04:00
|
|
|
lockedRefCount: 1,
|
|
|
|
unlockedRefCount: 0,
|
|
|
|
shouldPass: true,
|
|
|
|
},
|
|
|
|
{
|
2016-12-10 19:15:12 -05:00
|
|
|
rlk: globalNSMutex.RLock,
|
|
|
|
runlk: globalNSMutex.RUnlock,
|
2016-06-22 15:27:47 -04:00
|
|
|
lockedRefCount: 4,
|
|
|
|
unlockedRefCount: 2,
|
|
|
|
shouldPass: true,
|
|
|
|
},
|
|
|
|
{
|
2016-12-10 19:15:12 -05:00
|
|
|
rlk: globalNSMutex.RLock,
|
|
|
|
runlk: globalNSMutex.RUnlock,
|
2016-06-22 15:27:47 -04:00
|
|
|
lockedRefCount: 1,
|
|
|
|
unlockedRefCount: 0,
|
|
|
|
shouldPass: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run all test cases.
|
|
|
|
|
|
|
|
// Write lock tests.
|
|
|
|
testCase := testCases[0]
|
2017-08-31 14:29:22 -04:00
|
|
|
if !testCase.lk("a", "b", "c", 60*time.Second) { // lock once.
|
|
|
|
t.Fatalf("Failed to acquire lock")
|
|
|
|
}
|
2016-12-10 19:15:12 -05:00
|
|
|
nsLk, ok := globalNSMutex.lockMap[nsParam{"a", "b"}]
|
2016-06-22 15:27:47 -04:00
|
|
|
if !ok && testCase.shouldPass {
|
|
|
|
t.Errorf("Lock in map missing.")
|
|
|
|
}
|
2016-10-19 12:27:36 -04:00
|
|
|
// Validate locked ref count.
|
2016-06-22 15:27:47 -04:00
|
|
|
if testCase.lockedRefCount != nsLk.ref && testCase.shouldPass {
|
|
|
|
t.Errorf("Test %d fails, expected to pass. Wanted ref count is %d, got %d", 1, testCase.lockedRefCount, nsLk.ref)
|
|
|
|
}
|
2016-08-31 14:39:08 -04:00
|
|
|
testCase.unlk("a", "b", "c") // unlock once.
|
2016-06-22 15:27:47 -04:00
|
|
|
if testCase.unlockedRefCount != nsLk.ref && testCase.shouldPass {
|
|
|
|
t.Errorf("Test %d fails, expected to pass. Wanted ref count is %d, got %d", 1, testCase.unlockedRefCount, nsLk.ref)
|
|
|
|
}
|
2016-12-10 19:15:12 -05:00
|
|
|
_, ok = globalNSMutex.lockMap[nsParam{"a", "b"}]
|
2016-06-22 15:27:47 -04:00
|
|
|
if ok && !testCase.shouldPass {
|
|
|
|
t.Errorf("Lock map found after unlock.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read lock tests.
|
|
|
|
testCase = testCases[1]
|
2017-08-31 14:29:22 -04:00
|
|
|
if !testCase.rlk("a", "b", "c", 60*time.Second) { // lock once.
|
|
|
|
t.Fatalf("Failed to acquire first read lock")
|
|
|
|
}
|
|
|
|
if !testCase.rlk("a", "b", "c", 60*time.Second) { // lock second time.
|
|
|
|
t.Fatalf("Failed to acquire second read lock")
|
|
|
|
}
|
|
|
|
if !testCase.rlk("a", "b", "c", 60*time.Second) { // lock third time.
|
|
|
|
t.Fatalf("Failed to acquire third read lock")
|
|
|
|
}
|
|
|
|
if !testCase.rlk("a", "b", "c", 60*time.Second) { // lock fourth time.
|
|
|
|
t.Fatalf("Failed to acquire fourth read lock")
|
|
|
|
}
|
2016-12-10 19:15:12 -05:00
|
|
|
nsLk, ok = globalNSMutex.lockMap[nsParam{"a", "b"}]
|
2016-06-22 15:27:47 -04:00
|
|
|
if !ok && testCase.shouldPass {
|
|
|
|
t.Errorf("Lock in map missing.")
|
|
|
|
}
|
2016-10-19 12:27:36 -04:00
|
|
|
// Validate locked ref count.
|
2016-06-22 15:27:47 -04:00
|
|
|
if testCase.lockedRefCount != nsLk.ref && testCase.shouldPass {
|
|
|
|
t.Errorf("Test %d fails, expected to pass. Wanted ref count is %d, got %d", 1, testCase.lockedRefCount, nsLk.ref)
|
|
|
|
}
|
2016-08-31 14:39:08 -04:00
|
|
|
|
|
|
|
testCase.runlk("a", "b", "c") // unlock once.
|
|
|
|
testCase.runlk("a", "b", "c") // unlock second time.
|
2016-06-22 15:27:47 -04:00
|
|
|
if testCase.unlockedRefCount != nsLk.ref && testCase.shouldPass {
|
|
|
|
t.Errorf("Test %d fails, expected to pass. Wanted ref count is %d, got %d", 2, testCase.unlockedRefCount, nsLk.ref)
|
|
|
|
}
|
2016-12-10 19:15:12 -05:00
|
|
|
_, ok = globalNSMutex.lockMap[nsParam{"a", "b"}]
|
2016-06-22 15:27:47 -04:00
|
|
|
if !ok && testCase.shouldPass {
|
|
|
|
t.Errorf("Lock map not found.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read lock 0 ref count.
|
|
|
|
testCase = testCases[2]
|
2017-08-31 14:29:22 -04:00
|
|
|
if !testCase.rlk("a", "c", "d", 60*time.Second) { // lock once.
|
|
|
|
t.Fatalf("Failed to acquire read lock")
|
|
|
|
}
|
2016-06-22 15:27:47 -04:00
|
|
|
|
2016-12-10 19:15:12 -05:00
|
|
|
nsLk, ok = globalNSMutex.lockMap[nsParam{"a", "c"}]
|
2016-06-22 15:27:47 -04:00
|
|
|
if !ok && testCase.shouldPass {
|
|
|
|
t.Errorf("Lock in map missing.")
|
|
|
|
}
|
2016-10-19 12:27:36 -04:00
|
|
|
// Validate locked ref count.
|
2016-06-22 15:27:47 -04:00
|
|
|
if testCase.lockedRefCount != nsLk.ref && testCase.shouldPass {
|
|
|
|
t.Errorf("Test %d fails, expected to pass. Wanted ref count is %d, got %d", 3, testCase.lockedRefCount, nsLk.ref)
|
|
|
|
}
|
2016-08-31 14:39:08 -04:00
|
|
|
testCase.runlk("a", "c", "d") // unlock once.
|
2016-06-22 15:27:47 -04:00
|
|
|
if testCase.unlockedRefCount != nsLk.ref && testCase.shouldPass {
|
|
|
|
t.Errorf("Test %d fails, expected to pass. Wanted ref count is %d, got %d", 3, testCase.unlockedRefCount, nsLk.ref)
|
|
|
|
}
|
2016-12-10 19:15:12 -05:00
|
|
|
_, ok = globalNSMutex.lockMap[nsParam{"a", "c"}]
|
2016-06-22 15:27:47 -04:00
|
|
|
if ok && !testCase.shouldPass {
|
|
|
|
t.Errorf("Lock map not found.")
|
|
|
|
}
|
|
|
|
}
|
2016-08-31 14:39:08 -04:00
|
|
|
|
2017-08-31 14:29:22 -04:00
|
|
|
func TestNamespaceLockTimedOut(t *testing.T) {
|
2018-01-23 22:34:09 -05:00
|
|
|
isDistXL := false
|
|
|
|
initNSLock(isDistXL)
|
2017-08-31 14:29:22 -04:00
|
|
|
// Get write lock
|
|
|
|
if !globalNSMutex.Lock("my-bucket", "my-object", "abc", 60*time.Second) {
|
|
|
|
t.Fatalf("Failed to acquire lock")
|
2016-08-31 14:39:08 -04:00
|
|
|
}
|
|
|
|
|
2017-08-31 14:29:22 -04:00
|
|
|
// Second attempt for write lock on same resource should time out
|
|
|
|
locked := globalNSMutex.Lock("my-bucket", "my-object", "def", 1*time.Second)
|
|
|
|
if locked {
|
|
|
|
t.Fatalf("Should not have acquired lock")
|
2016-08-31 14:39:08 -04:00
|
|
|
}
|
|
|
|
|
2017-08-31 14:29:22 -04:00
|
|
|
// Read lock on same resource should also time out
|
|
|
|
locked = globalNSMutex.RLock("my-bucket", "my-object", "def", 1*time.Second)
|
|
|
|
if locked {
|
|
|
|
t.Fatalf("Should not have acquired read lock while write lock is active")
|
|
|
|
}
|
2016-08-31 14:39:08 -04:00
|
|
|
|
2017-08-31 14:29:22 -04:00
|
|
|
// Release write lock
|
|
|
|
globalNSMutex.Unlock("my-bucket", "my-object", "abc")
|
2016-08-31 14:39:08 -04:00
|
|
|
|
2017-08-31 14:29:22 -04:00
|
|
|
// Get read lock
|
|
|
|
if !globalNSMutex.RLock("my-bucket", "my-object", "ghi", 60*time.Second) {
|
|
|
|
t.Fatalf("Failed to acquire read lock")
|
|
|
|
}
|
2016-08-31 14:39:08 -04:00
|
|
|
|
2017-08-31 14:29:22 -04:00
|
|
|
// Write lock on same resource should time out
|
|
|
|
locked = globalNSMutex.Lock("my-bucket", "my-object", "klm", 1*time.Second)
|
|
|
|
if locked {
|
|
|
|
t.Fatalf("Should not have acquired lock")
|
2016-08-31 14:39:08 -04:00
|
|
|
}
|
|
|
|
|
2017-08-31 14:29:22 -04:00
|
|
|
// 2nd read lock should be just fine
|
|
|
|
if !globalNSMutex.RLock("my-bucket", "my-object", "nop", 60*time.Second) {
|
|
|
|
t.Fatalf("Failed to acquire second read lock")
|
|
|
|
}
|
2016-08-31 14:39:08 -04:00
|
|
|
|
2017-08-31 14:29:22 -04:00
|
|
|
// Release both read locks
|
|
|
|
globalNSMutex.RUnlock("my-bucket", "my-object", "ghi")
|
|
|
|
globalNSMutex.RUnlock("my-bucket", "my-object", "nop")
|
2016-10-19 12:27:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests functionality to forcefully unlock locks.
|
|
|
|
func TestNamespaceForceUnlockTest(t *testing.T) {
|
2018-01-23 22:34:09 -05:00
|
|
|
isDistXL := false
|
|
|
|
initNSLock(isDistXL)
|
2016-10-19 12:27:36 -04:00
|
|
|
// Create lock.
|
2016-12-10 19:15:12 -05:00
|
|
|
lock := globalNSMutex.NewNSLock("bucket", "object")
|
2017-08-31 14:29:22 -04:00
|
|
|
if lock.GetLock(newDynamicTimeout(60*time.Second, time.Second)) != nil {
|
|
|
|
t.Fatalf("Failed to get lock")
|
|
|
|
}
|
2016-10-19 12:27:36 -04:00
|
|
|
// Forcefully unlock lock.
|
2016-12-10 19:15:12 -05:00
|
|
|
globalNSMutex.ForceUnlock("bucket", "object")
|
2016-10-19 12:27:36 -04:00
|
|
|
|
|
|
|
ch := make(chan struct{}, 1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// Try to claim lock again.
|
2016-12-10 19:15:12 -05:00
|
|
|
anotherLock := globalNSMutex.NewNSLock("bucket", "object")
|
2017-08-31 14:29:22 -04:00
|
|
|
if anotherLock.GetLock(newDynamicTimeout(60*time.Second, time.Second)) != nil {
|
|
|
|
t.Fatalf("Failed to get lock")
|
|
|
|
}
|
|
|
|
// And signal success.
|
2016-10-19 12:27:36 -04:00
|
|
|
ch <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
2016-10-20 19:09:19 -04:00
|
|
|
case <-ch:
|
|
|
|
// Signalled so all is fine.
|
|
|
|
break
|
2016-10-19 12:27:36 -04:00
|
|
|
|
2016-10-20 19:09:19 -04:00
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
// In case we hit the time out, the lock has not been cleared.
|
|
|
|
t.Errorf("Lock not cleared.")
|
2016-10-19 12:27:36 -04:00
|
|
|
}
|
2016-08-31 14:39:08 -04:00
|
|
|
|
2016-10-19 12:27:36 -04:00
|
|
|
// Clean up lock.
|
2016-12-10 19:15:12 -05:00
|
|
|
globalNSMutex.ForceUnlock("bucket", "object")
|
2016-08-31 14:39:08 -04:00
|
|
|
}
|