blob: 9bb484cb252010f4bd490eacc9538919a6ae0a8f [file] [log] [blame]
Joe Tsaib9365042019-03-19 14:14:29 -07001// Copyright 2019 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
5package legacy
6
7import (
8 "sync"
9 "testing"
10
11 "github.com/golang/protobuf/v2/reflect/protoreflect"
12)
13
14type (
15 MessageA struct {
16 A1 *MessageA `protobuf:"bytes,1,req,name=a1"`
17 A2 *MessageB `protobuf:"bytes,2,req,name=a2"`
18 A3 Enum `protobuf:"varint,3,opt,name=a3,enum=legacy.Enum"`
19 }
20 MessageB struct {
21 B1 *MessageA `protobuf:"bytes,1,req,name=b1"`
22 B2 *MessageB `protobuf:"bytes,2,req,name=b2"`
23 B3 Enum `protobuf:"varint,3,opt,name=b3,enum=legacy.Enum"`
24 }
25 Enum int32
26)
27
28// TestConcurrentInit tests that concurrent wrapping of multiple legacy types
29// results in the exact same descriptor being created.
30func TestConcurrentInit(t *testing.T) {
31 const numParallel = 5
32 var messageATypes [numParallel]protoreflect.MessageType
33 var messageBTypes [numParallel]protoreflect.MessageType
Joe Tsai0fc49f82019-05-01 12:29:25 -070034 var enumDescs [numParallel]protoreflect.EnumDescriptor
Joe Tsaib9365042019-03-19 14:14:29 -070035
36 // Concurrently load message and enum types.
37 var wg sync.WaitGroup
38 for i := 0; i < numParallel; i++ {
39 i := i
40 wg.Add(3)
41 go func() {
42 defer wg.Done()
43 messageATypes[i] = Export{}.MessageTypeOf((*MessageA)(nil))
44 }()
45 go func() {
46 defer wg.Done()
47 messageBTypes[i] = Export{}.MessageTypeOf((*MessageB)(nil))
48 }()
49 go func() {
50 defer wg.Done()
Joe Tsai0fc49f82019-05-01 12:29:25 -070051 enumDescs[i] = Export{}.EnumDescriptorOf(Enum(0))
Joe Tsaib9365042019-03-19 14:14:29 -070052 }()
53 }
54 wg.Wait()
55
56 var (
57 wantMTA = messageATypes[0]
Joe Tsai0fc49f82019-05-01 12:29:25 -070058 wantMDA = messageATypes[0].Descriptor().Fields().ByNumber(1).Message()
Joe Tsaib9365042019-03-19 14:14:29 -070059 wantMTB = messageBTypes[0]
Joe Tsai0fc49f82019-05-01 12:29:25 -070060 wantMDB = messageBTypes[0].Descriptor().Fields().ByNumber(2).Message()
61 wantED = messageATypes[0].Descriptor().Fields().ByNumber(3).Enum()
Joe Tsaib9365042019-03-19 14:14:29 -070062 )
63
64 for _, gotMT := range messageATypes[1:] {
65 if gotMT != wantMTA {
66 t.Error("MessageType(MessageA) mismatch")
67 }
Joe Tsai0fc49f82019-05-01 12:29:25 -070068 if gotMDA := gotMT.Descriptor().Fields().ByNumber(1).Message(); gotMDA != wantMDA {
Joe Tsaib9365042019-03-19 14:14:29 -070069 t.Error("MessageDescriptor(MessageA) mismatch")
70 }
Joe Tsai0fc49f82019-05-01 12:29:25 -070071 if gotMDB := gotMT.Descriptor().Fields().ByNumber(2).Message(); gotMDB != wantMDB {
Joe Tsaib9365042019-03-19 14:14:29 -070072 t.Error("MessageDescriptor(MessageB) mismatch")
73 }
Joe Tsai0fc49f82019-05-01 12:29:25 -070074 if gotED := gotMT.Descriptor().Fields().ByNumber(3).Enum(); gotED != wantED {
Joe Tsaib9365042019-03-19 14:14:29 -070075 t.Error("EnumDescriptor(Enum) mismatch")
76 }
77 }
78 for _, gotMT := range messageBTypes[1:] {
79 if gotMT != wantMTB {
80 t.Error("MessageType(MessageB) mismatch")
81 }
Joe Tsai0fc49f82019-05-01 12:29:25 -070082 if gotMDA := gotMT.Descriptor().Fields().ByNumber(1).Message(); gotMDA != wantMDA {
Joe Tsaib9365042019-03-19 14:14:29 -070083 t.Error("MessageDescriptor(MessageA) mismatch")
84 }
Joe Tsai0fc49f82019-05-01 12:29:25 -070085 if gotMDB := gotMT.Descriptor().Fields().ByNumber(2).Message(); gotMDB != wantMDB {
Joe Tsaib9365042019-03-19 14:14:29 -070086 t.Error("MessageDescriptor(MessageB) mismatch")
87 }
Joe Tsai0fc49f82019-05-01 12:29:25 -070088 if gotED := gotMT.Descriptor().Fields().ByNumber(3).Enum(); gotED != wantED {
Joe Tsaib9365042019-03-19 14:14:29 -070089 t.Error("EnumDescriptor(Enum) mismatch")
90 }
91 }
Joe Tsai0fc49f82019-05-01 12:29:25 -070092 for _, gotED := range enumDescs[1:] {
93 if gotED != wantED {
Joe Tsaib9365042019-03-19 14:14:29 -070094 t.Error("EnumType(Enum) mismatch")
95 }
96 }
97}