...

Source file src/google.golang.org/protobuf/internal/race_test/file_desc_init/race_test.go

Documentation: google.golang.org/protobuf/internal/race_test/file_desc_init

     1  // Copyright 2024 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package race_test
     6  
     7  import (
     8  	"sync"
     9  	"testing"
    10  
    11  	"google.golang.org/protobuf/proto"
    12  
    13  	epb "google.golang.org/protobuf/internal/testprotos/race/extender"
    14  	mpb "google.golang.org/protobuf/internal/testprotos/race/message"
    15  )
    16  
    17  // There must be no other test in this package as we are testing global
    18  // initialization which only happens once per binary.
    19  // It tests that it is safe to initialize the descriptor of a message and
    20  // the descriptor of an extendee of that message in parallel (i.e. no data race).
    21  func TestConcurrentInitialization(t *testing.T) {
    22  	var wg sync.WaitGroup
    23  	wg.Add(2)
    24  	go func() {
    25  		defer wg.Done()
    26  		m := &mpb.MyMessage{
    27  			I32: proto.Int32(int32(42)),
    28  		}
    29  		// This initializes the descriptor.
    30  		_, err := proto.Marshal(m)
    31  		if err != nil {
    32  			t.Errorf("proto.Marshal(): %v", err)
    33  		}
    34  	}()
    35  	go func() {
    36  		defer wg.Done()
    37  		m := &epb.OtherMessage{
    38  			I32: proto.Int32(int32(42)),
    39  		}
    40  		// This initializes the descriptor including the extension.
    41  		_, err := proto.Marshal(m)
    42  		if err != nil {
    43  			t.Errorf("proto.Marshal(): %v", err)
    44  		}
    45  	}()
    46  	wg.Wait()
    47  }
    48  

View as plain text