github.com/liangmanlin/routine@v1.1.0/g/g_test.go (about)

     1  // Copyright 2022 TimAndy. All rights reserved.
     2  // Licensed under the Apache-2.0 license that can be found in the LICENSE file.
     3  
     4  package g
     5  
     6  import (
     7  	"github.com/stretchr/testify/assert"
     8  	"reflect"
     9  	"runtime"
    10  	"sync"
    11  	"testing"
    12  )
    13  
    14  func TestGetgp(t *testing.T) {
    15  	gp0 := getgp()
    16  	runtime.GC()
    17  	assert.NotNil(t, gp0)
    18  	//
    19  	runTest(t, func() {
    20  		gp := getgp()
    21  		runtime.GC()
    22  		assert.NotNil(t, gp)
    23  		assert.NotEqual(t, gp0, gp)
    24  	})
    25  }
    26  
    27  func TestGetg0(t *testing.T) {
    28  	runTest(t, func() {
    29  		g0 := getg0()
    30  		runtime.GC()
    31  		stackguard0 := reflect.ValueOf(g0).FieldByName("stackguard0")
    32  		assert.Greater(t, stackguard0.Uint(), uint64(0))
    33  	})
    34  }
    35  
    36  func TestGetgt(t *testing.T) {
    37  	runTest(t, func() {
    38  		gt := getgt()
    39  		runtime.GC()
    40  		assert.Equal(t, "g", gt.Name())
    41  		//
    42  		assert.Greater(t, gt.NumField(), 20)
    43  	})
    44  }
    45  
    46  func runTest(t *testing.T, fun func()) {
    47  	run := false
    48  	wg := &sync.WaitGroup{}
    49  	wg.Add(1)
    50  	go func() {
    51  		fun()
    52  		run = true
    53  		wg.Done()
    54  	}()
    55  	wg.Wait()
    56  	assert.True(t, run)
    57  }