| 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 |
|
| 20 |
"github.com/nats-io/nats.go" |
| 21 |
) |
| 22 |
|
| 23 |
//lint:file-ignore SA1019 Ignore deprecation warnings for EncodedConn |
| 24 |
|
| 25 |
// newGobEncodedConn wraps an existing *nats.Conn as a Gob-encoded connection. |
| 26 |
func newGobEncodedConn(tl testing.TB, nc *nats.Conn) *nats.EncodedConn { |
| 27 |
ec, err := nats.NewEncodedConn(nc, nats.GOB_ENCODER) |
| 28 |
if err != nil { |
| 29 |
tl.Fatalf("Failed to create an encoded connection: %v\n", err) |
| 30 |
} |
| 31 |
return ec |
| 32 |
} |
| 33 |
|
| 34 |
func TestEncBuiltinGobMarshalString(t *testing.T) { |
| 35 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 36 |
ec := newGobEncodedConn(t, nc) |
| 37 |
ch := make(chan bool) |
| 38 |
|
| 39 |
testString := "Hello World!" |
| 40 |
|
| 41 |
ec.Subscribe("gob_string", func(s string) { |
| 42 |
if s != testString { |
| 43 |
t.Fatalf("Received test string of '%s', wanted '%s'", s, testString) |
| 44 |
} |
| 45 |
ch <- true |
| 46 |
}) |
| 47 |
ec.Publish("gob_string", testString) |
| 48 |
if e := Wait(ch); e != nil { |
| 49 |
t.Fatal("Did not receive the message") |
| 50 |
} |
| 51 |
}) |
| 52 |
} |
| 53 |
|
| 54 |
func TestEncBuiltinGobMarshalInt(t *testing.T) { |
| 55 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 56 |
ec := newGobEncodedConn(t, nc) |
| 57 |
ch := make(chan bool) |
| 58 |
|
| 59 |
testN := 22 |
| 60 |
|
| 61 |
ec.Subscribe("gob_int", func(n int) { |
| 62 |
if n != testN { |
| 63 |
t.Fatalf("Received test int of '%d', wanted '%d'", n, testN) |
| 64 |
} |
| 65 |
ch <- true |
| 66 |
}) |
| 67 |
ec.Publish("gob_int", testN) |
| 68 |
if e := Wait(ch); e != nil { |
| 69 |
t.Fatal("Did not receive the message") |
| 70 |
} |
| 71 |
}) |
| 72 |
} |
| 73 |
|
| 74 |
func TestEncBuiltinGobMarshalStruct(t *testing.T) { |
| 75 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 76 |
ec := newGobEncodedConn(t, nc) |
| 77 |
ch := make(chan bool) |
| 78 |
|
| 79 |
me := &person{Name: "derek", Age: 22, Address: "140 New Montgomery St"} |
| 80 |
me.Children = make(map[string]*person) |
| 81 |
|
| 82 |
me.Children["sam"] = &person{Name: "sam", Age: 19, Address: "140 New Montgomery St"} |
| 83 |
me.Children["meg"] = &person{Name: "meg", Age: 17, Address: "140 New Montgomery St"} |
| 84 |
|
| 85 |
me.Assets = make(map[string]uint) |
| 86 |
me.Assets["house"] = 1000 |
| 87 |
me.Assets["car"] = 100 |
| 88 |
|
| 89 |
ec.Subscribe("gob_struct", func(p *person) { |
| 90 |
if !reflect.DeepEqual(p, me) { |
| 91 |
t.Fatalf("Did not receive the correct struct response") |
| 92 |
} |
| 93 |
ch <- true |
| 94 |
}) |
| 95 |
|
| 96 |
ec.Publish("gob_struct", me) |
| 97 |
if e := Wait(ch); e != nil { |
| 98 |
t.Fatal("Did not receive the message") |
| 99 |
} |
| 100 |
}) |
| 101 |
} |
| 102 |
|
| 103 |
func BenchmarkPublishGobStruct(b *testing.B) { |
| 104 |
b.StopTimer() |
| 105 |
withServerB(b, func(b *testing.B, nc *nats.Conn) { |
| 106 |
ec := newGobEncodedConn(b, nc) |
| 107 |
defer ec.Close() |
| 108 |
ch := make(chan bool) |
| 109 |
|
| 110 |
me := &person{Name: "derek", Age: 22, Address: "140 New Montgomery St"} |
| 111 |
me.Children = make(map[string]*person) |
| 112 |
me.Children["sam"] = &person{Name: "sam", Age: 19, Address: "140 New Montgomery St"} |
| 113 |
me.Children["meg"] = &person{Name: "meg", Age: 17, Address: "140 New Montgomery St"} |
| 114 |
|
| 115 |
ec.Subscribe("gob_struct", func(p *person) { |
| 116 |
if !reflect.DeepEqual(p, me) { |
| 117 |
b.Fatalf("Did not receive the correct struct response") |
| 118 |
} |
| 119 |
ch <- true |
| 120 |
}) |
| 121 |
|
| 122 |
b.StartTimer() |
| 123 |
for range b.N { |
| 124 |
ec.Publish("gob_struct", me) |
| 125 |
if e := Wait(ch); e != nil { |
| 126 |
b.Fatal("Did not receive the message") |
| 127 |
} |
| 128 |
} |
| 129 |
}) |
| 130 |
} |