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

Function TestRacyMarshal

proto/text_test.go:691–731  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

689}
690
691func TestRacyMarshal(t *testing.T) {
692 // This test should be run with the race detector.
693
694 any := &pb2.MyMessage{Count: proto.Int32(47), Name: proto.String("David")}
695 proto.SetExtension(any, pb2.E_Ext_Text, proto.String("bar"))
696 b, err := proto.Marshal(any)
697 if err != nil {
698 panic(err)
699 }
700 m := &pb3.Message{
701 Name: "David",
702 ResultCount: 47,
703 Anything: &anypb.Any{TypeUrl: "type.googleapis.com/" + proto.MessageName(any), Value: b},
704 }
705
706 wantText := proto.MarshalTextString(m)
707 wantBytes, err := proto.Marshal(m)
708 if err != nil {
709 t.Fatalf("proto.Marshal error: %v", err)
710 }
711
712 var wg sync.WaitGroup
713 defer wg.Wait()
714 wg.Add(20)
715 for i := 0; i < 10; i++ {
716 go func() {
717 defer wg.Done()
718 got := proto.MarshalTextString(m)
719 if got != wantText {
720 t.Errorf("proto.MarshalTextString = %q, want %q", got, wantText)
721 }
722 }()
723 go func() {
724 defer wg.Done()
725 got, err := proto.Marshal(m)
726 if !bytes.Equal(got, wantBytes) || err != nil {
727 t.Errorf("proto.Marshal = (%x, %v), want (%x, nil)", got, err, wantBytes)
728 }
729 }()
730 }
731}
732
733type UnmarshalTextTest struct {
734 in string

Callers

nothing calls this directly

Calls 6

Int32Function · 0.92
StringFunction · 0.92
SetExtensionFunction · 0.92
MarshalFunction · 0.92
MessageNameFunction · 0.92
MarshalTextStringFunction · 0.92

Tested by

no test coverage detected