github.com/insolar/vanilla@v0.0.0-20201023172447-248fdf805322/throw/stacktrace_test.go (about)

     1  // Copyright 2020 Insolar Network Ltd.
     2  // All rights reserved.
     3  // This material is licensed under the Insolar License version 1.0,
     4  // available at https://github.com/insolar/assured-ledger/blob/master/LICENSE.md.
     5  
     6  package throw
     7  
     8  import (
     9  	"runtime"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestCaptureStack(t *testing.T) {
    16  	// TODO proper tests
    17  	// fmt.Printf("%s\n==============\n", captureStackByDebug(0, false))
    18  	// fmt.Printf("%s\n==============\n", captureStackByDebug(1, false))
    19  	// fmt.Printf("%s\n==============\n", captureStackByDebug(0, true))
    20  	// fmt.Printf("%s\n==============\n", captureStackByDebug(99, true))
    21  	//
    22  	// fmt.Println()
    23  	//
    24  	// fmt.Printf("%s\n==============\n", captureStackByCallers(0, false))
    25  	// fmt.Printf("%s\n==============\n", captureStackByCallers(1, false))
    26  	// fmt.Printf("%s\n==============\n", captureStackByCallers(0, true))
    27  
    28  	s := string(captureStack(0, false))
    29  	require.Contains(t, s, "TestCaptureStack")
    30  	require.Contains(t, s, "created by testing.(*T).Run")
    31  
    32  	s = string(captureStack(0, true))
    33  	require.Contains(t, s, "TestCaptureStack")
    34  	require.NotContains(t, s, "created by testing.(*T).Run")
    35  
    36  	s = string(captureStack(99, true))
    37  	require.NotContains(t, string(captureStack(99, true)), "TestCaptureStack")
    38  	require.Contains(t, s, "created by testing.(*T).Run")
    39  }
    40  
    41  func BenchmarkCaptureStack(b *testing.B) {
    42  	b.Run("captureStackByDebug-full", func(b *testing.B) {
    43  		for i := b.N; i > 0; i-- {
    44  			v := captureStackByDebug(0, false)
    45  			runtime.KeepAlive(v)
    46  		}
    47  	})
    48  
    49  	b.Run("captureStackByCallers-full", func(b *testing.B) {
    50  		for i := b.N; i > 0; i-- {
    51  			v := captureStackByCallers(0, false)
    52  			runtime.KeepAlive(v)
    53  		}
    54  	})
    55  
    56  	b.Run("captureStackByDebug-top", func(b *testing.B) {
    57  		for i := b.N; i > 0; i-- {
    58  			v := captureStackByDebug(0, true)
    59  			runtime.KeepAlive(v)
    60  		}
    61  	})
    62  
    63  	b.Run("captureStackByCallers-top", func(b *testing.B) {
    64  		for i := b.N; i > 0; i-- {
    65  			v := captureStackByCallers(0, true)
    66  			runtime.KeepAlive(v)
    67  		}
    68  	})
    69  }