github.com/cyub/threadlocal@v0.0.0-20220816024249-5db4997a97f4/thread_test.go (about)

     1  package threadlocal
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  	"strconv"
     7  	"strings"
     8  	"sync"
     9  	"testing"
    10  )
    11  
    12  func TestGid(t *testing.T) {
    13  	var wg sync.WaitGroup
    14  	n := 100
    15  	wg.Add(100)
    16  	for i := 0; i < n; i++ {
    17  		go func() {
    18  			defer wg.Done()
    19  			i := Gid()
    20  			j := extractGidFromStack()
    21  			if i != j {
    22  				t.Fatal(fmt.Sprintf("%d != %d", i, j))
    23  			}
    24  		}()
    25  	}
    26  	wg.Wait()
    27  }
    28  
    29  func extractGidFromStack() int {
    30  	var buf [128]byte
    31  	n := runtime.Stack(buf[:], false)
    32  	stack := strings.Split(string(buf[:n]), " ")
    33  	id, _ := strconv.Atoi(stack[1])
    34  	return id
    35  }