...

Source file src/github.com/go-stack/stack/stack-go19_test.go

Documentation: github.com/go-stack/stack

     1  // +build go1.9
     2  
     3  package stack_test
     4  
     5  import (
     6  	"runtime"
     7  	"testing"
     8  
     9  	"github.com/go-stack/stack"
    10  )
    11  
    12  func TestCallerInlinedPanic(t *testing.T) {
    13  	t.Parallel()
    14  
    15  	var line int
    16  
    17  	defer func() {
    18  		if recover() != nil {
    19  			var pcs [32]uintptr
    20  			n := runtime.Callers(1, pcs[:])
    21  			frames := runtime.CallersFrames(pcs[:n])
    22  			// count frames to runtime.sigpanic
    23  			panicIdx := 0
    24  			for {
    25  				f, more := frames.Next()
    26  				if f.Function == "runtime.sigpanic" {
    27  					break
    28  				}
    29  				panicIdx++
    30  				if !more {
    31  					t.Fatal("no runtime.sigpanic entry on the stack")
    32  				}
    33  			}
    34  
    35  			c := stack.Caller(panicIdx)
    36  			if got, want := c.Frame().Function, "runtime.sigpanic"; got != want {
    37  				t.Errorf("sigpanic frame: got name == %v, want name == %v", got, want)
    38  			}
    39  
    40  			c1 := stack.Caller(panicIdx + 1)
    41  			if got, want := c1.Frame().Function, "github.com/go-stack/stack_test.inlinablePanic"; got != want {
    42  				t.Errorf("TestCallerInlinedPanic frame: got name == %v, want name == %v", got, want)
    43  			}
    44  			if got, want := c1.Frame().Line, line; got != want {
    45  				t.Errorf("TestCallerInlinedPanic frame: got line == %v, want line == %v", got, want)
    46  			}
    47  		}
    48  	}()
    49  
    50  	doPanic(t, &line)
    51  	t.Fatal("failed to panic")
    52  }
    53  
    54  func doPanic(t *testing.T, panicLine *int) {
    55  	_, _, line, ok := runtime.Caller(0)
    56  	*panicLine = line + 11 // adjust to match line of panic below
    57  	if !ok {
    58  		t.Fatal("runtime.Caller(0) failed")
    59  	}
    60  	inlinablePanic()
    61  }
    62  
    63  func inlinablePanic() {
    64  	// Initiate a sigpanic.
    65  	var x *uintptr
    66  	_ = *x
    67  }
    68  

View as plain text