github.com/timandy/routine@v1.1.4-0.20240507073150-e4a3e1fe2ba5/runtime_test.go (about) 1 package routine 2 3 import ( 4 "fmt" 5 "os" 6 "reflect" 7 "runtime" 8 "sync" 9 "sync/atomic" 10 "testing" 11 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestGetgp(t *testing.T) { 16 gp0 := getgp() 17 runtime.GC() 18 assert.NotNil(t, gp0) 19 // 20 runTest(t, func() { 21 gp := getgp() 22 runtime.GC() 23 assert.NotNil(t, gp) 24 assert.NotEqual(t, gp0, gp) 25 }) 26 } 27 28 func TestGetg0(t *testing.T) { 29 runTest(t, func() { 30 g0 := getg0() 31 runtime.GC() 32 stackguard0 := reflect.ValueOf(g0).FieldByName("stackguard0") 33 assert.Greater(t, stackguard0.Uint(), uint64(0)) 34 }) 35 } 36 37 func TestGetgt(t *testing.T) { 38 fmt.Println("*** GOOS:", runtime.GOOS, "***") 39 fmt.Println("*** GOARCH:", runtime.GOARCH, "***") 40 if GOARM := os.Getenv("GOARM"); len(GOARM) > 0 { 41 fmt.Println("*** GOARM:", GOARM, "***") 42 } 43 if GOMIPS := os.Getenv("GOMIPS"); len(GOMIPS) > 0 { 44 fmt.Println("*** GOMIPS:", GOMIPS, "***") 45 } 46 // 47 gt := getgt() 48 runtime.GC() 49 assert.Equal(t, "g", gt.Name()) 50 // 51 numField := gt.NumField() 52 // 53 fmt.Println("#numField:", numField) 54 fmt.Println("#offsetGoid:", offsetGoid) 55 fmt.Println("#offsetPaniconfault:", offsetPaniconfault) 56 fmt.Println("#offsetGopc:", offsetGopc) 57 fmt.Println("#offsetLabels:", offsetLabels) 58 // 59 assert.Greater(t, numField, 20) 60 assert.Greater(t, int(offsetGoid), 0) 61 assert.Greater(t, int(offsetPaniconfault), 0) 62 assert.Greater(t, int(offsetGopc), 0) 63 assert.Greater(t, int(offsetLabels), 0) 64 // 65 runTest(t, func() { 66 tt := getgt() 67 runtime.GC() 68 assert.Equal(t, numField, tt.NumField()) 69 assert.Equal(t, offsetGoid, offset(tt, "goid")) 70 assert.Equal(t, offsetPaniconfault, offset(tt, "paniconfault")) 71 assert.Equal(t, offsetGopc, offset(tt, "gopc")) 72 assert.Equal(t, offsetLabels, offset(tt, "labels")) 73 }) 74 } 75 76 func runTest(t *testing.T, fun func()) { 77 var count int32 78 wg := &sync.WaitGroup{} 79 wg.Add(10) 80 for i := 0; i < 10; i++ { 81 go func() { 82 for j := 0; j < 10; j++ { 83 fun() 84 } 85 atomic.AddInt32(&count, 1) 86 wg.Done() 87 }() 88 } 89 wg.Wait() 90 assert.Equal(t, 10, int(count)) 91 }