| 
									
										
										
										
											2016-06-23 03:27:47 +08: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-19 07:23:42 +08:00
										 |  |  | package cmd | 
					
						
							| 
									
										
										
										
											2016-06-23 03:27:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-01 02:39:08 +08:00
										 |  |  | import ( | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2016-06-23 03:27:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-17 23:24:46 +08: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-23 03:27:47 +08:00
										 |  |  | // Tests functionality provided by namespace lock.
 | 
					
						
							|  |  |  | func TestNamespaceLockTest(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2018-01-24 11:34:09 +08:00
										 |  |  | 	isDistXL := false | 
					
						
							|  |  |  | 	initNSLock(isDistXL) | 
					
						
							| 
									
										
										
										
											2016-06-23 03:27:47 +08:00
										 |  |  | 	// List of test cases.
 | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							| 
									
										
										
										
											2017-09-01 02:29:22 +08:00
										 |  |  | 		lk               func(s1, s2, s3 string, t time.Duration) bool | 
					
						
							| 
									
										
										
										
											2016-09-01 02:39:08 +08:00
										 |  |  | 		unlk             func(s1, s2, s3 string) | 
					
						
							| 
									
										
										
										
											2017-09-01 02:29:22 +08:00
										 |  |  | 		rlk              func(s1, s2, s3 string, t time.Duration) bool | 
					
						
							| 
									
										
										
										
											2016-09-01 02:39:08 +08:00
										 |  |  | 		runlk            func(s1, s2, s3 string) | 
					
						
							| 
									
										
										
										
											2016-06-23 03:27:47 +08:00
										 |  |  | 		lockedRefCount   uint | 
					
						
							|  |  |  | 		unlockedRefCount uint | 
					
						
							|  |  |  | 		shouldPass       bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2016-12-11 08:15:12 +08:00
										 |  |  | 			lk:               globalNSMutex.Lock, | 
					
						
							|  |  |  | 			unlk:             globalNSMutex.Unlock, | 
					
						
							| 
									
										
										
										
											2016-06-23 03:27:47 +08:00
										 |  |  | 			lockedRefCount:   1, | 
					
						
							|  |  |  | 			unlockedRefCount: 0, | 
					
						
							|  |  |  | 			shouldPass:       true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2016-12-11 08:15:12 +08:00
										 |  |  | 			rlk:              globalNSMutex.RLock, | 
					
						
							|  |  |  | 			runlk:            globalNSMutex.RUnlock, | 
					
						
							| 
									
										
										
										
											2016-06-23 03:27:47 +08:00
										 |  |  | 			lockedRefCount:   4, | 
					
						
							|  |  |  | 			unlockedRefCount: 2, | 
					
						
							|  |  |  | 			shouldPass:       true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2016-12-11 08:15:12 +08:00
										 |  |  | 			rlk:              globalNSMutex.RLock, | 
					
						
							|  |  |  | 			runlk:            globalNSMutex.RUnlock, | 
					
						
							| 
									
										
										
										
											2016-06-23 03:27:47 +08:00
										 |  |  | 			lockedRefCount:   1, | 
					
						
							|  |  |  | 			unlockedRefCount: 0, | 
					
						
							|  |  |  | 			shouldPass:       true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Run all test cases.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Write lock tests.
 | 
					
						
							|  |  |  | 	testCase := testCases[0] | 
					
						
							| 
									
										
										
										
											2017-09-01 02:29:22 +08:00
										 |  |  | 	if !testCase.lk("a", "b", "c", 60*time.Second) { // lock once.
 | 
					
						
							|  |  |  | 		t.Fatalf("Failed to acquire lock") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-12-11 08:15:12 +08:00
										 |  |  | 	nsLk, ok := globalNSMutex.lockMap[nsParam{"a", "b"}] | 
					
						
							| 
									
										
										
										
											2016-06-23 03:27:47 +08:00
										 |  |  | 	if !ok && testCase.shouldPass { | 
					
						
							|  |  |  | 		t.Errorf("Lock in map missing.") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-20 00:27:36 +08:00
										 |  |  | 	// Validate locked ref count.
 | 
					
						
							| 
									
										
										
										
											2016-06-23 03:27:47 +08: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-09-01 02:39:08 +08:00
										 |  |  | 	testCase.unlk("a", "b", "c") // unlock once.
 | 
					
						
							| 
									
										
										
										
											2016-06-23 03:27:47 +08: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-11 08:15:12 +08:00
										 |  |  | 	_, ok = globalNSMutex.lockMap[nsParam{"a", "b"}] | 
					
						
							| 
									
										
										
										
											2016-06-23 03:27:47 +08:00
										 |  |  | 	if ok && !testCase.shouldPass { | 
					
						
							|  |  |  | 		t.Errorf("Lock map found after unlock.") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Read lock tests.
 | 
					
						
							|  |  |  | 	testCase = testCases[1] | 
					
						
							| 
									
										
										
										
											2017-09-01 02:29:22 +08: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-11 08:15:12 +08:00
										 |  |  | 	nsLk, ok = globalNSMutex.lockMap[nsParam{"a", "b"}] | 
					
						
							| 
									
										
										
										
											2016-06-23 03:27:47 +08:00
										 |  |  | 	if !ok && testCase.shouldPass { | 
					
						
							|  |  |  | 		t.Errorf("Lock in map missing.") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-20 00:27:36 +08:00
										 |  |  | 	// Validate locked ref count.
 | 
					
						
							| 
									
										
										
										
											2016-06-23 03:27:47 +08: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-09-01 02:39:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	testCase.runlk("a", "b", "c") // unlock once.
 | 
					
						
							|  |  |  | 	testCase.runlk("a", "b", "c") // unlock second time.
 | 
					
						
							| 
									
										
										
										
											2016-06-23 03:27:47 +08: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-11 08:15:12 +08:00
										 |  |  | 	_, ok = globalNSMutex.lockMap[nsParam{"a", "b"}] | 
					
						
							| 
									
										
										
										
											2016-06-23 03:27:47 +08:00
										 |  |  | 	if !ok && testCase.shouldPass { | 
					
						
							|  |  |  | 		t.Errorf("Lock map not found.") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Read lock 0 ref count.
 | 
					
						
							|  |  |  | 	testCase = testCases[2] | 
					
						
							| 
									
										
										
										
											2017-09-01 02:29:22 +08:00
										 |  |  | 	if !testCase.rlk("a", "c", "d", 60*time.Second) { // lock once.
 | 
					
						
							|  |  |  | 		t.Fatalf("Failed to acquire read lock") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-06-23 03:27:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-11 08:15:12 +08:00
										 |  |  | 	nsLk, ok = globalNSMutex.lockMap[nsParam{"a", "c"}] | 
					
						
							| 
									
										
										
										
											2016-06-23 03:27:47 +08:00
										 |  |  | 	if !ok && testCase.shouldPass { | 
					
						
							|  |  |  | 		t.Errorf("Lock in map missing.") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-20 00:27:36 +08:00
										 |  |  | 	// Validate locked ref count.
 | 
					
						
							| 
									
										
										
										
											2016-06-23 03:27:47 +08: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-09-01 02:39:08 +08:00
										 |  |  | 	testCase.runlk("a", "c", "d") // unlock once.
 | 
					
						
							| 
									
										
										
										
											2016-06-23 03:27:47 +08: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-11 08:15:12 +08:00
										 |  |  | 	_, ok = globalNSMutex.lockMap[nsParam{"a", "c"}] | 
					
						
							| 
									
										
										
										
											2016-06-23 03:27:47 +08:00
										 |  |  | 	if ok && !testCase.shouldPass { | 
					
						
							|  |  |  | 		t.Errorf("Lock map not found.") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-01 02:39:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-01 02:29:22 +08:00
										 |  |  | func TestNamespaceLockTimedOut(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2018-01-24 11:34:09 +08:00
										 |  |  | 	isDistXL := false | 
					
						
							|  |  |  | 	initNSLock(isDistXL) | 
					
						
							| 
									
										
										
										
											2017-09-01 02:29:22 +08:00
										 |  |  | 	// Get write lock
 | 
					
						
							|  |  |  | 	if !globalNSMutex.Lock("my-bucket", "my-object", "abc", 60*time.Second) { | 
					
						
							|  |  |  | 		t.Fatalf("Failed to acquire lock") | 
					
						
							| 
									
										
										
										
											2016-09-01 02:39:08 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-01 02:29:22 +08: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-09-01 02:39:08 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-01 02:29:22 +08: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-09-01 02:39:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-01 02:29:22 +08:00
										 |  |  | 	// Release write lock
 | 
					
						
							|  |  |  | 	globalNSMutex.Unlock("my-bucket", "my-object", "abc") | 
					
						
							| 
									
										
										
										
											2016-09-01 02:39:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-01 02:29:22 +08:00
										 |  |  | 	// Get read lock
 | 
					
						
							|  |  |  | 	if !globalNSMutex.RLock("my-bucket", "my-object", "ghi", 60*time.Second) { | 
					
						
							|  |  |  | 		t.Fatalf("Failed to acquire read lock") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-09-01 02:39:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-01 02:29:22 +08: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-09-01 02:39:08 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-01 02:29:22 +08: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-09-01 02:39:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-01 02:29:22 +08:00
										 |  |  | 	// Release both read locks
 | 
					
						
							|  |  |  | 	globalNSMutex.RUnlock("my-bucket", "my-object", "ghi") | 
					
						
							|  |  |  | 	globalNSMutex.RUnlock("my-bucket", "my-object", "nop") | 
					
						
							| 
									
										
										
										
											2016-10-20 00:27:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Tests functionality to forcefully unlock locks.
 | 
					
						
							|  |  |  | func TestNamespaceForceUnlockTest(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2018-01-24 11:34:09 +08:00
										 |  |  | 	isDistXL := false | 
					
						
							|  |  |  | 	initNSLock(isDistXL) | 
					
						
							| 
									
										
										
										
											2016-10-20 00:27:36 +08:00
										 |  |  | 	// Create lock.
 | 
					
						
							| 
									
										
										
										
											2016-12-11 08:15:12 +08:00
										 |  |  | 	lock := globalNSMutex.NewNSLock("bucket", "object") | 
					
						
							| 
									
										
										
										
											2017-09-01 02:29:22 +08:00
										 |  |  | 	if lock.GetLock(newDynamicTimeout(60*time.Second, time.Second)) != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed to get lock") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-20 00:27:36 +08:00
										 |  |  | 	// Forcefully unlock lock.
 | 
					
						
							| 
									
										
										
										
											2016-12-11 08:15:12 +08:00
										 |  |  | 	globalNSMutex.ForceUnlock("bucket", "object") | 
					
						
							| 
									
										
										
										
											2016-10-20 00:27:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ch := make(chan struct{}, 1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	go func() { | 
					
						
							|  |  |  | 		// Try to claim lock again.
 | 
					
						
							| 
									
										
										
										
											2016-12-11 08:15:12 +08:00
										 |  |  | 		anotherLock := globalNSMutex.NewNSLock("bucket", "object") | 
					
						
							| 
									
										
										
										
											2017-09-01 02:29:22 +08:00
										 |  |  | 		if anotherLock.GetLock(newDynamicTimeout(60*time.Second, time.Second)) != nil { | 
					
						
							| 
									
										
										
										
											2018-10-24 00:44:20 +08:00
										 |  |  | 			t.Errorf("Failed to get lock") | 
					
						
							|  |  |  | 			return | 
					
						
							| 
									
										
										
										
											2017-09-01 02:29:22 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		// And signal success.
 | 
					
						
							| 
									
										
										
										
											2016-10-20 00:27:36 +08:00
										 |  |  | 		ch <- struct{}{} | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	select { | 
					
						
							| 
									
										
										
										
											2016-10-21 07:09:19 +08:00
										 |  |  | 	case <-ch: | 
					
						
							| 
									
										
										
										
											2018-06-29 07:02:02 +08:00
										 |  |  | 		// Signaled so all is fine.
 | 
					
						
							| 
									
										
										
										
											2016-10-21 07:09:19 +08:00
										 |  |  | 		break | 
					
						
							| 
									
										
										
										
											2016-10-20 00:27:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-21 07:09:19 +08: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-20 00:27:36 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-09-01 02:39:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-20 00:27:36 +08:00
										 |  |  | 	// Clean up lock.
 | 
					
						
							| 
									
										
										
										
											2016-12-11 08:15:12 +08:00
										 |  |  | 	globalNSMutex.ForceUnlock("bucket", "object") | 
					
						
							| 
									
										
										
										
											2016-09-01 02:39:08 +08:00
										 |  |  | } |