github.com/binbinly/pkg@v0.0.11-0.20240321014439-f4fbf666eb0f/util/runtime_test.go (about)

     1  package util
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestSafeGo(t *testing.T) {
    12  	var (
    13  		err   any
    14  		trace []byte
    15  	)
    16  	rcb := func(e any, s []byte) {
    17  		err = e
    18  		trace = s
    19  	}
    20  	SafeGo(testFn2, rcb)
    21  	time.Sleep(5 * time.Millisecond)
    22  	assert.Equal(t, "fn1", err)
    23  	assert.Equal(t, true, bytes.Contains(trace, []byte("panic")))
    24  }
    25  
    26  var (
    27  	testFn1 = func() {
    28  		panic("fn1")
    29  	}
    30  	testFn2 = func() {
    31  		testFn1()
    32  	}
    33  )