gitee.com/quant1x/gox@v1.21.2/encoding/binary/struc/test_pack_init/pack_init_test.go (about)

     1  package test_pack_init
     2  
     3  import (
     4  	"bytes"
     5  	"gitee.com/quant1x/gox/encoding/binary/struc"
     6  	"sync"
     7  	"testing"
     8  )
     9  
    10  type Example struct {
    11  	I int `struc:int`
    12  }
    13  
    14  // TestParallelPack checks whether Pack is goroutine-safe. Run it with -race flag.
    15  // Keep it as a single test in package since it is likely to be triggered on initialization
    16  // of global objects reported as a data race by race detector.
    17  func TestParallelPack(t *testing.T) {
    18  	var wg sync.WaitGroup
    19  	val := Example{}
    20  	for i := 0; i < 2; i++ {
    21  		wg.Add(1)
    22  		go func() {
    23  			defer wg.Done()
    24  			var buf bytes.Buffer
    25  			_ = struc.Pack(&buf, &val)
    26  		}()
    27  	}
    28  	wg.Wait()
    29  }