Debian Codegraph @

test/protobuf_test.go

raw
1 // Copyright 2015-2026 The NATS Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package test
15
16 import (
17 "errors"
18 "reflect"
19 "testing"
20 "time"
21
22 "github.com/nats-io/nats.go"
23
24 "github.com/nats-io/nats.go/encoders/protobuf"
25 pb "github.com/nats-io/nats.go/encoders/protobuf/testdata"
26 )
27
28 //lint:file-ignore SA1019 Ignore deprecation warnings for EncodedConn
29
30 // newProtoEncodedConn wraps an existing *nats.Conn as a Protobuf-encoded connection.
31 func newProtoEncodedConn(tl testing.TB, nc *nats.Conn) *nats.EncodedConn {
32 ec, err := nats.NewEncodedConn(nc, protobuf.PROTOBUF_ENCODER)
33 if err != nil {
34 tl.Fatalf("Failed to create an encoded connection: %v\n", err)
35 }
36 return ec
37 }
38
39 func TestEncProtoMarshalStruct(t *testing.T) {
40 withServer(t, func(t *testing.T, nc *nats.Conn) {
41 ec := newProtoEncodedConn(t, nc)
42
43 me := &pb.Person{Name: "derek", Age: 22, Address: "140 New Montgomery St"}
44 me.Children = make(map[string]*pb.Person)
45
46 me.Children["sam"] = &pb.Person{Name: "sam", Age: 19, Address: "140 New Montgomery St"}
47 me.Children["meg"] = &pb.Person{Name: "meg", Age: 17, Address: "140 New Montgomery St"}
48
49 ch := make(chan error, 1)
50 ec.Subscribe("protobuf_test", func(p *pb.Person) {
51 var err error
52 if !reflect.DeepEqual(p.ProtoReflect(), me.ProtoReflect()) {
53 err = errors.New("Did not receive the correct protobuf response")
54 }
55 ch <- err
56 })
57
58 ec.Publish("protobuf_test", me)
59 select {
60 case e := <-ch:
61 if e != nil {
62 t.Fatal(e.Error())
63 }
64 case <-time.After(time.Second):
65 t.Fatal("Failed to receive message")
66 }
67 })
68 }
69
70 func TestEncProtoNilRequest(t *testing.T) {
71 withServer(t, func(t *testing.T, nc *nats.Conn) {
72 ec := newProtoEncodedConn(t, nc)
73
74 testPerson := &pb.Person{Name: "Anatolii", Age: 25, Address: "Ukraine, Nikolaev"}
75
76 // Subscribe with empty interface shouldn't fail on empty message
77 ec.Subscribe("nil_test", func(_, reply string, _ any) {
78 ec.Publish(reply, testPerson)
79 })
80
81 resp := new(pb.Person)
82
83 // Request with nil argument shouldn't fail
84 err := ec.Request("nil_test", nil, resp, 100*time.Millisecond)
85 ec.Flush()
86
87 if err != nil {
88 t.Error("Fail to send empty message via encoded proto connection")
89 }
90
91 if !reflect.DeepEqual(testPerson.ProtoReflect(), resp.ProtoReflect()) {
92 t.Error("Fail to receive encoded response")
93 }
94 })
95 }
96
97 func BenchmarkProtobufMarshalStruct(b *testing.B) {
98 me := &pb.Person{Name: "derek", Age: 22, Address: "140 New Montgomery St"}
99 me.Children = make(map[string]*pb.Person)
100
101 me.Children["sam"] = &pb.Person{Name: "sam", Age: 19, Address: "140 New Montgomery St"}
102 me.Children["meg"] = &pb.Person{Name: "meg", Age: 17, Address: "140 New Montgomery St"}
103
104 encoder := &protobuf.ProtobufEncoder{}
105 for n := 0; n < b.N; n++ {
106 if _, err := encoder.Encode("protobuf_test", me); err != nil {
107 b.Fatal("Couldn't serialize object", err)
108 }
109 }
110 }
111
112 func BenchmarkPublishProtobufStruct(b *testing.B) {
113 b.StopTimer()
114 withServerB(b, func(b *testing.B, nc *nats.Conn) {
115 ec := newProtoEncodedConn(b, nc)
116 defer ec.Close()
117 ch := make(chan bool)
118
119 me := &pb.Person{Name: "derek", Age: 22, Address: "140 New Montgomery St"}
120 me.Children = make(map[string]*pb.Person)
121 me.Children["sam"] = &pb.Person{Name: "sam", Age: 19, Address: "140 New Montgomery St"}
122 me.Children["meg"] = &pb.Person{Name: "meg", Age: 17, Address: "140 New Montgomery St"}
123
124 ec.Subscribe("protobuf_test", func(p *pb.Person) {
125 if !reflect.DeepEqual(p.ProtoReflect(), me.ProtoReflect()) {
126 b.Fatalf("Did not receive the correct protobuf response")
127 }
128 ch <- true
129 })
130
131 b.StartTimer()
132 for range b.N {
133 ec.Publish("protobuf_test", me)
134 if e := Wait(ch); e != nil {
135 b.Fatal("Did not receive the message")
136 }
137 }
138 })
139 }