github.com/cs3org/reva/v2@v2.27.7/pkg/storage/utils/sync/mutex_test.go (about)

     1  // Copyright 2018-2022 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 sync
    20  
    21  import (
    22  	"fmt"
    23  	"runtime"
    24  	"testing"
    25  )
    26  
    27  func HammerMutex(m *NamedRWMutex, loops int, c chan bool) {
    28  	for i := 0; i < loops; i++ {
    29  		id := fmt.Sprintf("%v", i)
    30  		m.Lock(id)
    31  		m.Unlock(id)
    32  	}
    33  	c <- true
    34  }
    35  
    36  func TestNamedRWMutex(t *testing.T) {
    37  	if n := runtime.SetMutexProfileFraction(1); n != 0 {
    38  		t.Logf("got mutexrate %d expected 0", n)
    39  	}
    40  	defer runtime.SetMutexProfileFraction(0)
    41  	m := NewNamedRWMutex()
    42  	c := make(chan bool)
    43  	r := 10
    44  
    45  	for i := 0; i < r; i++ {
    46  		go HammerMutex(&m, 2000, c)
    47  	}
    48  	for i := 0; i < r; i++ {
    49  		<-c
    50  	}
    51  }