github.com/cs3org/reva/v2@v2.27.7/pkg/storage/utils/filelocks/filelocks_test.go (about) 1 // Copyright 2018-2021 CERN 2 // 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 // 15 // In applying this license, CERN does not waive the privileges and immunities 16 // granted to it by virtue of its status as an Intergovernmental Organization 17 // or submit itself to any jurisdiction. 18 19 package filelocks_test 20 21 import ( 22 "sync" 23 "testing" 24 25 "github.com/cs3org/reva/v2/pkg/storage/utils/filelocks" 26 "github.com/stretchr/testify/assert" 27 "github.com/test-go/testify/require" 28 ) 29 30 func TestAcquireWriteLock(t *testing.T) { 31 file, fin, _ := filelocks.FileFactory() 32 defer fin() 33 34 filelocks.SetMaxLockCycles(90) 35 filelocks.SetLockCycleDurationFactor(3) 36 37 var wg sync.WaitGroup 38 for i := 0; i < 10; i++ { 39 wg.Add(1) 40 go func() { 41 defer wg.Done() 42 43 l, err := filelocks.AcquireWriteLock(file) 44 assert.Nil(t, err) 45 require.NotNil(t, l) 46 47 defer func() { 48 err = filelocks.ReleaseLock(l) 49 assert.Nil(t, err) 50 }() 51 52 assert.Equal(t, true, l.Locked()) 53 assert.Equal(t, false, l.RLocked()) 54 }() 55 } 56 57 wg.Wait() 58 } 59 60 func TestAcquireReadLock(t *testing.T) { 61 file, fin, _ := filelocks.FileFactory() 62 defer fin() 63 64 filelocks.SetMaxLockCycles(90) 65 filelocks.SetLockCycleDurationFactor(3) 66 67 var wg sync.WaitGroup 68 for i := 0; i < 10; i++ { 69 wg.Add(1) 70 go func() { 71 defer wg.Done() 72 73 l, err := filelocks.AcquireReadLock(file) 74 assert.Nil(t, err) 75 require.NotNil(t, l) 76 77 defer func() { 78 err = filelocks.ReleaseLock(l) 79 assert.Nil(t, err) 80 }() 81 82 assert.Equal(t, false, l.Locked()) 83 assert.Equal(t, true, l.RLocked()) 84 85 }() 86 } 87 88 wg.Wait() 89 } 90 91 /* This negative test is flaky as 8000 goroutines are not enough to trigger this in ci 92 func TestAcquireReadLockFail(t *testing.T) { 93 file, fin, _ := filelocks.FileFactory() 94 defer fin() 95 96 filelocks.SetMaxLockCycles(1) 97 filelocks.SetLockCycleDurationFactor(1) 98 99 // create a channel big enough for all waiting groups 100 errors := make(chan error, 8000) 101 var wg sync.WaitGroup 102 for i := 0; i < 8000; i++ { 103 wg.Add(1) 104 go func() { 105 defer wg.Done() 106 107 l, err := filelocks.AcquireReadLock(file) 108 if err != nil { 109 // collect the error in a channel 110 errors <- err 111 return 112 } 113 err = filelocks.ReleaseLock(l) 114 assert.Nil(t, err) 115 }() 116 } 117 118 // at least one error should have occurred 119 assert.NotNil(t, <-errors) 120 121 wg.Wait() 122 } 123 */ 124 125 func TestReleaseLock(t *testing.T) { 126 file, fin, _ := filelocks.FileFactory() 127 defer fin() 128 129 l1, err := filelocks.AcquireWriteLock(file) 130 assert.Equal(t, true, l1.Locked()) 131 assert.Nil(t, err) 132 133 err = filelocks.ReleaseLock(l1) 134 assert.Nil(t, err) 135 assert.Equal(t, false, l1.Locked()) 136 }