| 
									
										
										
										
											2016-06-23 03:27:47 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2019-04-10 02:39:42 +08:00
										 |  |  |  * MinIO Cloud Storage, (C) 2016 MinIO, Inc. | 
					
						
							| 
									
										
										
										
											2016-06-23 03:27:47 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * 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
 | 
					
						
							| 
									
										
										
										
											2019-11-14 04:17:45 +08:00
										 |  |  | 	expectedSource := "[namespace-lock_test.go:32:TestGetSource()]" | 
					
						
							| 
									
										
										
										
											2018-01-17 23:24:46 +08:00
										 |  |  | 	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 | 
					
						
							| 
									
										
										
										
											2019-11-14 04:17:45 +08:00
										 |  |  | 	nsMutex := newNSLock(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 | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-11-14 04:17:45 +08:00
										 |  |  | 			lk:               nsMutex.Lock, | 
					
						
							|  |  |  | 			unlk:             nsMutex.Unlock, | 
					
						
							| 
									
										
										
										
											2016-06-23 03:27:47 +08:00
										 |  |  | 			lockedRefCount:   1, | 
					
						
							|  |  |  | 			unlockedRefCount: 0, | 
					
						
							|  |  |  | 			shouldPass:       true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-11-14 04:17:45 +08:00
										 |  |  | 			rlk:              nsMutex.RLock, | 
					
						
							|  |  |  | 			runlk:            nsMutex.RUnlock, | 
					
						
							| 
									
										
										
										
											2016-06-23 03:27:47 +08:00
										 |  |  | 			lockedRefCount:   4, | 
					
						
							|  |  |  | 			unlockedRefCount: 2, | 
					
						
							|  |  |  | 			shouldPass:       true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-11-14 04:17:45 +08:00
										 |  |  | 			rlk:              nsMutex.RLock, | 
					
						
							|  |  |  | 			runlk:            nsMutex.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") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-14 04:17:45 +08:00
										 |  |  | 	nsLk, ok := nsMutex.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) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-14 04:17:45 +08:00
										 |  |  | 	_, ok = nsMutex.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") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-14 04:17:45 +08:00
										 |  |  | 	nsLk, ok = nsMutex.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) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-14 04:17:45 +08:00
										 |  |  | 	_, ok = nsMutex.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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-14 04:17:45 +08:00
										 |  |  | 	nsLk, ok = nsMutex.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) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-14 04:17:45 +08:00
										 |  |  | 	_, ok = nsMutex.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 | 
					
						
							| 
									
										
										
										
											2019-11-14 04:17:45 +08:00
										 |  |  | 	nsMutex := newNSLock(isDistXL) | 
					
						
							| 
									
										
										
										
											2017-09-01 02:29:22 +08:00
										 |  |  | 	// Get write lock
 | 
					
						
							| 
									
										
										
										
											2019-11-14 04:17:45 +08:00
										 |  |  | 	if !nsMutex.Lock("my-bucket", "my-object", "abc", 60*time.Second) { | 
					
						
							| 
									
										
										
										
											2017-09-01 02:29:22 +08:00
										 |  |  | 		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
 | 
					
						
							| 
									
										
										
										
											2019-11-14 04:17:45 +08:00
										 |  |  | 	locked := nsMutex.Lock("my-bucket", "my-object", "def", 1*time.Second) | 
					
						
							| 
									
										
										
										
											2017-09-01 02:29:22 +08:00
										 |  |  | 	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
 | 
					
						
							| 
									
										
										
										
											2019-11-14 04:17:45 +08:00
										 |  |  | 	locked = nsMutex.RLock("my-bucket", "my-object", "def", 1*time.Second) | 
					
						
							| 
									
										
										
										
											2017-09-01 02:29:22 +08:00
										 |  |  | 	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
 | 
					
						
							| 
									
										
										
										
											2019-11-14 04:17:45 +08:00
										 |  |  | 	nsMutex.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
 | 
					
						
							| 
									
										
										
										
											2019-11-14 04:17:45 +08:00
										 |  |  | 	if !nsMutex.RLock("my-bucket", "my-object", "ghi", 60*time.Second) { | 
					
						
							| 
									
										
										
										
											2017-09-01 02:29:22 +08:00
										 |  |  | 		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
 | 
					
						
							| 
									
										
										
										
											2019-11-14 04:17:45 +08:00
										 |  |  | 	locked = nsMutex.Lock("my-bucket", "my-object", "klm", 1*time.Second) | 
					
						
							| 
									
										
										
										
											2017-09-01 02:29:22 +08:00
										 |  |  | 	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
 | 
					
						
							| 
									
										
										
										
											2019-11-14 04:17:45 +08:00
										 |  |  | 	if !nsMutex.RLock("my-bucket", "my-object", "nop", 60*time.Second) { | 
					
						
							| 
									
										
										
										
											2017-09-01 02:29:22 +08:00
										 |  |  | 		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
 | 
					
						
							| 
									
										
										
										
											2019-11-14 04:17:45 +08:00
										 |  |  | 	nsMutex.RUnlock("my-bucket", "my-object", "ghi") | 
					
						
							|  |  |  | 	nsMutex.RUnlock("my-bucket", "my-object", "nop") | 
					
						
							| 
									
										
										
										
											2016-09-01 02:39:08 +08:00
										 |  |  | } |