github.com/searKing/golang/go@v1.2.117/runtime/goroutine/lock_test.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package goroutine_test
     6  
     7  import (
     8  	"fmt"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/searKing/golang/go/runtime/goroutine"
    13  )
    14  
    15  func TestLock(t *testing.T) {
    16  	oldDebug := goroutine.DebugGoroutines
    17  	goroutine.DebugGoroutines = true
    18  	defer func() { goroutine.DebugGoroutines = oldDebug }()
    19  
    20  	g := goroutine.NewLock()
    21  	g.MustCheck()
    22  
    23  	sawPanic := make(chan any)
    24  	go func() {
    25  		defer func() { sawPanic <- recover() }()
    26  		g.MustCheck() // should panic
    27  	}()
    28  	e := <-sawPanic
    29  	if e == nil {
    30  		t.Fatal("did not see panic from check in other goroutine")
    31  	}
    32  	if !strings.Contains(fmt.Sprint(e), "wrong goroutine") {
    33  		t.Errorf("expected on see panic about running on the wrong goroutine; got %v", e)
    34  	}
    35  }