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