github.com/pygolin/runtime@v0.0.0-20201208210830-a62e3cd39798/threading_test.go (about)

     1  // Copyright 2016 Google Inc. All Rights Reserved.
     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  package runtime
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func TestRecursiveMutex(t *testing.T) {
    22  	var m recursiveMutex
    23  	f := NewRootFrame()
    24  	m.Lock(f)
    25  	m.Lock(f)
    26  	m.Unlock(f)
    27  	m.Unlock(f)
    28  }
    29  
    30  func TestRecursiveMutexUnlockedTooManyTimes(t *testing.T) {
    31  	var m recursiveMutex
    32  	f := NewRootFrame()
    33  	m.Lock(f)
    34  	m.Unlock(f)
    35  	oldLogFatal := logFatal
    36  	logFatal = func(msg string) { panic(msg) }
    37  	defer func() {
    38  		logFatal = oldLogFatal
    39  		if e := recover(); e == nil {
    40  			t.Error("Unlock didn't call logFatal")
    41  		}
    42  	}()
    43  	m.Unlock(f)
    44  }
    45  
    46  func TestRecursiveMutexUnlockFrameMismatch(t *testing.T) {
    47  	var m recursiveMutex
    48  	m.Lock(NewRootFrame())
    49  	oldLogFatal := logFatal
    50  	logFatal = func(msg string) { panic(msg) }
    51  	defer func() {
    52  		logFatal = oldLogFatal
    53  		if e := recover(); e == nil {
    54  			t.Error("Unlock didn't call logFatal")
    55  		}
    56  	}()
    57  	m.Unlock(NewRootFrame())
    58  }