Debian Codegraph @

test/json_test.go

raw
1 // Copyright 2012-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 "reflect"
18 "testing"
19 "time"
20
21 "github.com/nats-io/nats.go"
22 "github.com/nats-io/nats.go/encoders/builtin"
23 )
24
25 //lint:file-ignore SA1019 Ignore deprecation warnings for EncodedConn
26
27 // newJSONEncodedConn wraps an existing *nats.Conn as a JSON-encoded connection.
28 // The wrapped conn's lifetime is managed by the helper that produced it.
29 func newJSONEncodedConn(tl testing.TB, nc *nats.Conn) *nats.EncodedConn {
30 ec, err := nats.NewEncodedConn(nc, nats.JSON_ENCODER)
31 if err != nil {
32 tl.Fatalf("Failed to create an encoded connection: %v\n", err)
33 }
34 return ec
35 }
36
37 func TestEncBuiltinJsonMarshalString(t *testing.T) {
38 withServer(t, func(t *testing.T, nc *nats.Conn) {
39 ec := newJSONEncodedConn(t, nc)
40 ch := make(chan bool)
41
42 testString := "Hello World!"
43
44 ec.Subscribe("json_string", func(s string) {
45 if s != testString {
46 t.Fatalf("Received test string of '%s', wanted '%s'", s, testString)
47 }
48 ch <- true
49 })
50 ec.Publish("json_string", testString)
51 if e := Wait(ch); e != nil {
52 t.Fatal("Did not receive the message")
53 }
54 })
55 }
56
57 func TestEncBuiltinJsonMarshalEmptyString(t *testing.T) {
58 withServer(t, func(t *testing.T, nc *nats.Conn) {
59 ec := newJSONEncodedConn(t, nc)
60 ch := make(chan bool)
61
62 ec.Subscribe("json_empty_string", func(s string) {
63 if s != "" {
64 t.Fatalf("Received test of '%v', wanted empty string", s)
65 }
66 ch <- true
67 })
68 ec.Publish("json_empty_string", "")
69 if e := Wait(ch); e != nil {
70 t.Fatal("Did not receive the message")
71 }
72 })
73 }
74
75 func TestEncBuiltinJsonMarshalInt(t *testing.T) {
76 withServer(t, func(t *testing.T, nc *nats.Conn) {
77 ec := newJSONEncodedConn(t, nc)
78 ch := make(chan bool)
79
80 testN := 22
81
82 ec.Subscribe("json_int", func(n int) {
83 if n != testN {
84 t.Fatalf("Received test int of '%d', wanted '%d'", n, testN)
85 }
86 ch <- true
87 })
88 ec.Publish("json_int", testN)
89 if e := Wait(ch); e != nil {
90 t.Fatal("Did not receive the message")
91 }
92 })
93 }
94
95 func TestEncBuiltinJsonMarshalBool(t *testing.T) {
96 withServer(t, func(t *testing.T, nc *nats.Conn) {
97 ec := newJSONEncodedConn(t, nc)
98 ch := make(chan bool)
99
100 ec.Subscribe("json_bool", func(b bool) {
101 if !b {
102 t.Fatalf("Received test of '%v', wanted 'true'", b)
103 }
104 ch <- true
105 })
106 ec.Publish("json_bool", true)
107 if e := Wait(ch); e != nil {
108 t.Fatal("Did not receive the message")
109 }
110 })
111 }
112
113 func TestEncBuiltinJsonMarshalNull(t *testing.T) {
114 withServer(t, func(t *testing.T, nc *nats.Conn) {
115 ec := newJSONEncodedConn(t, nc)
116
117 type TestType struct{}
118 ch := make(chan bool)
119
120 var testValue *TestType
121
122 ec.Subscribe("json_null", func(i any) {
123 if i != nil {
124 t.Fatalf("Received test of '%v', wanted 'nil'", i)
125 }
126 ch <- true
127 })
128 ec.Publish("json_null", testValue)
129 if e := Wait(ch); e != nil {
130 t.Fatal("Did not receive the message")
131 }
132 })
133 }
134
135 func TestEncBuiltinJsonMarshalArray(t *testing.T) {
136 withServer(t, func(t *testing.T, nc *nats.Conn) {
137 ec := newJSONEncodedConn(t, nc)
138 ch := make(chan bool)
139
140 a := []string{"a", "b", "c"}
141
142 ec.Subscribe("json_array", func(v []string) {
143 if !reflect.DeepEqual(v, a) {
144 t.Fatalf("Received test of '%v', wanted '%v'", v, a)
145 }
146 ch <- true
147 })
148 ec.Publish("json_array", a)
149 if e := Wait(ch); e != nil {
150 t.Fatal("Did not receive the message")
151 }
152 })
153 }
154
155 func TestEncBuiltinJsonMarshalEmptyArray(t *testing.T) {
156 withServer(t, func(t *testing.T, nc *nats.Conn) {
157 ec := newJSONEncodedConn(t, nc)
158 ch := make(chan bool)
159
160 var a []string
161
162 ec.Subscribe("json_empty_array", func(v []string) {
163 if !reflect.DeepEqual(v, a) {
164 t.Fatalf("Received test of '%v', wanted '%v'", v, a)
165 }
166 ch <- true
167 })
168 ec.Publish("json_empty_array", a)
169 if e := Wait(ch); e != nil {
170 t.Fatal("Did not receive the message")
171 }
172 })
173 }
174
175 type person struct {
176 Name string
177 Address string
178 Age int
179 Children map[string]*person
180 Assets map[string]uint
181 }
182
183 func TestEncBuiltinJsonMarshalStruct(t *testing.T) {
184 withServer(t, func(t *testing.T, nc *nats.Conn) {
185 ec := newJSONEncodedConn(t, nc)
186 ch := make(chan bool)
187
188 me := &person{Name: "derek", Age: 22, Address: "140 New Montgomery St"}
189 me.Children = make(map[string]*person)
190
191 me.Children["sam"] = &person{Name: "sam", Age: 19, Address: "140 New Montgomery St"}
192 me.Children["meg"] = &person{Name: "meg", Age: 17, Address: "140 New Montgomery St"}
193
194 me.Assets = make(map[string]uint)
195 me.Assets["house"] = 1000
196 me.Assets["car"] = 100
197
198 ec.Subscribe("json_struct", func(p *person) {
199 if !reflect.DeepEqual(p, me) {
200 t.Fatal("Did not receive the correct struct response")
201 }
202 ch <- true
203 })
204
205 ec.Publish("json_struct", me)
206 if e := Wait(ch); e != nil {
207 t.Fatal("Did not receive the message")
208 }
209 })
210 }
211
212 func BenchmarkJsonMarshalStruct(b *testing.B) {
213 me := &person{Name: "derek", Age: 22, Address: "140 New Montgomery St"}
214 me.Children = make(map[string]*person)
215
216 me.Children["sam"] = &person{Name: "sam", Age: 19, Address: "140 New Montgomery St"}
217 me.Children["meg"] = &person{Name: "meg", Age: 17, Address: "140 New Montgomery St"}
218
219 encoder := &builtin.JsonEncoder{}
220 for n := 0; n < b.N; n++ {
221 if _, err := encoder.Encode("json_benchmark_struct_marshal", me); err != nil {
222 b.Fatal("Couldn't serialize object", err)
223 }
224 }
225 }
226
227 func BenchmarkPublishJsonStruct(b *testing.B) {
228 b.StopTimer()
229 withServerB(b, func(b *testing.B, nc *nats.Conn) {
230 ec := newJSONEncodedConn(b, nc)
231 defer ec.Close()
232 ch := make(chan bool)
233
234 me := &person{Name: "derek", Age: 22, Address: "140 New Montgomery St"}
235 me.Children = make(map[string]*person)
236 me.Children["sam"] = &person{Name: "sam", Age: 19, Address: "140 New Montgomery St"}
237 me.Children["meg"] = &person{Name: "meg", Age: 17, Address: "140 New Montgomery St"}
238
239 ec.Subscribe("json_benchmark_struct_publish", func(p *person) {
240 if !reflect.DeepEqual(p, me) {
241 b.Fatalf("Did not receive the correct struct response")
242 }
243 ch <- true
244 })
245
246 b.StartTimer()
247 for range b.N {
248 ec.Publish("json_benchmark_struct_publish", me)
249 if e := Wait(ch); e != nil {
250 b.Fatal("Did not receive the message")
251 }
252 }
253 })
254 }
255
256 func TestEncBuiltinNotMarshableToJson(t *testing.T) {
257 je := &builtin.JsonEncoder{}
258 ch := make(chan bool)
259 _, err := je.Encode("foo", ch)
260 if err == nil {
261 t.Fatal("Expected an error when failing encoding")
262 }
263 }
264
265 func TestEncBuiltinFailedEncodedPublish(t *testing.T) {
266 withServer(t, func(t *testing.T, nc *nats.Conn) {
267 ec := newJSONEncodedConn(t, nc)
268
269 ch := make(chan bool)
270 if err := ec.Publish("foo", ch); err == nil {
271 t.Fatal("Expected an error trying to publish a channel")
272 }
273 if err := ec.PublishRequest("foo", "bar", ch); err == nil {
274 t.Fatal("Expected an error trying to publish a channel")
275 }
276 var cr chan bool
277 if err := ec.Request("foo", ch, &cr, 1*time.Second); err == nil {
278 t.Fatal("Expected an error trying to publish a channel")
279 }
280 if err := ec.LastError(); err != nil {
281 t.Fatalf("Expected LastError to be nil: %q ", err)
282 }
283 })
284 }
285
286 func TestEncBuiltinDecodeConditionals(t *testing.T) {
287 je := &builtin.JsonEncoder{}
288
289 b, err := je.Encode("foo", 22)
290 if err != nil {
291 t.Fatalf("Expected no error when encoding, got %v\n", err)
292 }
293 var foo string
294 var bar []byte
295 err = je.Decode("foo", b, &foo)
296 if err != nil {
297 t.Fatalf("Expected no error when decoding, got %v\n", err)
298 }
299 err = je.Decode("foo", b, &bar)
300 if err != nil {
301 t.Fatalf("Expected no error when decoding, got %v\n", err)
302 }
303 }