MCPcopy
hub / github.com/golang/protobuf / TestConcurrentMarshal

Function TestConcurrentMarshal

proto/proto_test.go:2090–2114  ·  view source on GitHub ↗

TestConcurrentMarshal makes sure that it is safe to marshal same message in multiple goroutines concurrently.

(t *testing.T)

Source from the content-addressed store, hash-verified

2088// TestConcurrentMarshal makes sure that it is safe to marshal
2089// same message in multiple goroutines concurrently.
2090func TestConcurrentMarshal(t *testing.T) {
2091 pb := initGoTest(true)
2092 const N = 100
2093 b := make([][]byte, N)
2094
2095 var wg sync.WaitGroup
2096 for i := 0; i < N; i++ {
2097 wg.Add(1)
2098 go func(i int) {
2099 defer wg.Done()
2100 var err error
2101 b[i], err = proto.Marshal(pb)
2102 if err != nil {
2103 t.Errorf("marshal error: %v", err)
2104 }
2105 }(i)
2106 }
2107
2108 wg.Wait()
2109 for i := 1; i < N; i++ {
2110 if !bytes.Equal(b[0], b[i]) {
2111 t.Errorf("concurrent marshal result not same: b[0] = %v, b[%d] = %v", b[0], i, b[i])
2112 }
2113 }
2114}
2115
2116func TestInvalidUTF8(t *testing.T) {
2117 const invalidUTF8 = "\xde\xad\xbe\xef\x80\x00\xff"

Callers

nothing calls this directly

Calls 2

MarshalFunction · 0.92
initGoTestFunction · 0.85

Tested by

no test coverage detected