File: race_test.go

package info (click to toggle)
golang-google-protobuf 1.36.7-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 14,996 kB
  • sloc: sh: 94; makefile: 4
file content (47 lines) | stat: -rw-r--r-- 1,237 bytes parent folder | download | duplicates (2)
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
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package race_test

import (
	"sync"
	"testing"

	"google.golang.org/protobuf/proto"

	epb "google.golang.org/protobuf/internal/testprotos/race/extender"
	mpb "google.golang.org/protobuf/internal/testprotos/race/message"
)

// There must be no other test in this package as we are testing global
// initialization which only happens once per binary.
// It tests that it is safe to initialize the descriptor of a message and
// the descriptor of an extendee of that message in parallel (i.e. no data race).
func TestConcurrentInitialization(t *testing.T) {
	var wg sync.WaitGroup
	wg.Add(2)
	go func() {
		defer wg.Done()
		m := &mpb.MyMessage{
			I32: proto.Int32(int32(42)),
		}
		// This initializes the descriptor.
		_, err := proto.Marshal(m)
		if err != nil {
			t.Errorf("proto.Marshal(): %v", err)
		}
	}()
	go func() {
		defer wg.Done()
		m := &epb.OtherMessage{
			I32: proto.Int32(int32(42)),
		}
		// This initializes the descriptor including the extension.
		_, err := proto.Marshal(m)
		if err != nil {
			t.Errorf("proto.Marshal(): %v", err)
		}
	}()
	wg.Wait()
}