| 1 |
// Copyright 2013-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 |
"testing" |
| 18 |
"time" |
| 19 |
|
| 20 |
"github.com/nats-io/nats.go" |
| 21 |
testservice "github.com/synadia-io/orbit.go/ntf-client" |
| 22 |
) |
| 23 |
|
| 24 |
//lint:file-ignore SA1019 Ignore deprecation warnings for EncodedConn |
| 25 |
|
| 26 |
// newEncodedConn wraps an existing *nats.Conn as a DEFAULT_ENCODER conn. |
| 27 |
func newEncodedConn(t *testing.T, nc *nats.Conn) *nats.EncodedConn { |
| 28 |
t.Helper() |
| 29 |
ec, err := nats.NewEncodedConn(nc, nats.DEFAULT_ENCODER) |
| 30 |
if err != nil { |
| 31 |
t.Fatalf("Failed to create an encoded connection: %v\n", err) |
| 32 |
} |
| 33 |
return ec |
| 34 |
} |
| 35 |
|
| 36 |
func TestBadChan(t *testing.T) { |
| 37 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 38 |
ec := newEncodedConn(t, nc) |
| 39 |
|
| 40 |
if err := ec.BindSendChan("foo", "not a chan"); err == nil { |
| 41 |
t.Fatalf("Expected an Error when sending a non-channel") |
| 42 |
} |
| 43 |
|
| 44 |
if _, err := ec.BindRecvChan("foo", "not a chan"); err == nil { |
| 45 |
t.Fatalf("Expected an Error when sending a non-channel") |
| 46 |
} |
| 47 |
|
| 48 |
if err := ec.BindSendChan("foo", "not a chan"); err != nats.ErrChanArg { |
| 49 |
t.Fatalf("Expected an ErrChanArg when sending a non-channel") |
| 50 |
} |
| 51 |
|
| 52 |
if _, err := ec.BindRecvChan("foo", "not a chan"); err != nats.ErrChanArg { |
| 53 |
t.Fatalf("Expected an ErrChanArg when sending a non-channel") |
| 54 |
} |
| 55 |
}) |
| 56 |
} |
| 57 |
|
| 58 |
func TestSimpleSendChan(t *testing.T) { |
| 59 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 60 |
ec := newEncodedConn(t, nc) |
| 61 |
|
| 62 |
recv := make(chan bool) |
| 63 |
|
| 64 |
numSent := int32(22) |
| 65 |
ch := make(chan int32) |
| 66 |
|
| 67 |
if err := ec.BindSendChan("foo", ch); err != nil { |
| 68 |
t.Fatalf("Failed to bind to a send channel: %v", err) |
| 69 |
} |
| 70 |
|
| 71 |
ec.Subscribe("foo", func(num int32) { |
| 72 |
if num != numSent { |
| 73 |
t.Fatalf("Failed to receive correct value: %d vs %d", num, numSent) |
| 74 |
} |
| 75 |
recv <- true |
| 76 |
}) |
| 77 |
|
| 78 |
ch <- numSent |
| 79 |
|
| 80 |
if e := Wait(recv); e != nil { |
| 81 |
if ec.LastError() != nil { |
| 82 |
e = ec.LastError() |
| 83 |
} |
| 84 |
t.Fatalf("Did not receive the message: %s", e) |
| 85 |
} |
| 86 |
close(ch) |
| 87 |
}) |
| 88 |
} |
| 89 |
|
| 90 |
func TestFailedChannelSend(t *testing.T) { |
| 91 |
withServerInstance(t, func(t *testing.T, nc *nats.Conn, inst *testservice.Instance) { |
| 92 |
// First half of the test uses the helper's connection. |
| 93 |
ec := newEncodedConn(t, nc) |
| 94 |
ch := make(chan bool) |
| 95 |
wch := make(chan bool) |
| 96 |
|
| 97 |
nc.Opts.AsyncErrorCB = func(_ *nats.Conn, _ *nats.Subscription, _ error) { |
| 98 |
wch <- true |
| 99 |
} |
| 100 |
|
| 101 |
if err := ec.BindSendChan("foo", ch); err != nil { |
| 102 |
t.Fatalf("Failed to bind to a send channel: %v", err) |
| 103 |
} |
| 104 |
|
| 105 |
nc.Flush() |
| 106 |
|
| 107 |
go func() { |
| 108 |
time.Sleep(100 * time.Millisecond) |
| 109 |
nc.Close() |
| 110 |
}() |
| 111 |
|
| 112 |
func() { |
| 113 |
for { |
| 114 |
select { |
| 115 |
case ch <- true: |
| 116 |
case <-wch: |
| 117 |
return |
| 118 |
case <-time.After(time.Second): |
| 119 |
t.Fatal("Failed to get async error cb") |
| 120 |
} |
| 121 |
} |
| 122 |
}() |
| 123 |
|
| 124 |
// Second half: open a fresh connection and try a too-big payload. |
| 125 |
nc2 := dialInstance(t, inst) |
| 126 |
ec2 := newEncodedConn(t, nc2) |
| 127 |
bch := make(chan []byte) |
| 128 |
|
| 129 |
nc2.Opts.AsyncErrorCB = func(_ *nats.Conn, _ *nats.Subscription, _ error) { |
| 130 |
wch <- true |
| 131 |
} |
| 132 |
|
| 133 |
if err := ec2.BindSendChan("foo", bch); err != nil { |
| 134 |
t.Fatalf("Failed to bind to a send channel: %v", err) |
| 135 |
} |
| 136 |
|
| 137 |
buf := make([]byte, 2*1024*1024) |
| 138 |
bch <- buf |
| 139 |
|
| 140 |
if e := Wait(wch); e != nil { |
| 141 |
t.Fatal("Failed to call async err handler") |
| 142 |
} |
| 143 |
}) |
| 144 |
} |
| 145 |
|
| 146 |
func TestSimpleRecvChan(t *testing.T) { |
| 147 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 148 |
ec := newEncodedConn(t, nc) |
| 149 |
|
| 150 |
numSent := int32(22) |
| 151 |
ch := make(chan int32) |
| 152 |
|
| 153 |
if _, err := ec.BindRecvChan("foo", ch); err != nil { |
| 154 |
t.Fatalf("Failed to bind to a receive channel: %v", err) |
| 155 |
} |
| 156 |
|
| 157 |
ec.Publish("foo", numSent) |
| 158 |
|
| 159 |
select { |
| 160 |
case num := <-ch: |
| 161 |
if num != numSent { |
| 162 |
t.Fatalf("Failed to receive correct value: %d vs %d", num, numSent) |
| 163 |
} |
| 164 |
case <-time.After(1 * time.Second): |
| 165 |
t.Fatalf("Failed to receive a value, timed-out") |
| 166 |
} |
| 167 |
close(ch) |
| 168 |
}) |
| 169 |
} |
| 170 |
|
| 171 |
func TestQueueRecvChan(t *testing.T) { |
| 172 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 173 |
ec := newEncodedConn(t, nc) |
| 174 |
|
| 175 |
numSent := int32(22) |
| 176 |
ch := make(chan int32) |
| 177 |
|
| 178 |
if _, err := ec.BindRecvQueueChan("foo", "bar", ch); err != nil { |
| 179 |
t.Fatalf("Failed to bind to a queue receive channel: %v", err) |
| 180 |
} |
| 181 |
|
| 182 |
ec.Publish("foo", numSent) |
| 183 |
|
| 184 |
select { |
| 185 |
case num := <-ch: |
| 186 |
if num != numSent { |
| 187 |
t.Fatalf("Failed to receive correct value: %d vs %d", num, numSent) |
| 188 |
} |
| 189 |
case <-time.After(1 * time.Second): |
| 190 |
t.Fatalf("Failed to receive a value, timed-out") |
| 191 |
} |
| 192 |
close(ch) |
| 193 |
}) |
| 194 |
} |
| 195 |
|
| 196 |
func TestDecoderErrRecvChan(t *testing.T) { |
| 197 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 198 |
ec := newEncodedConn(t, nc) |
| 199 |
wch := make(chan bool) |
| 200 |
|
| 201 |
nc.Opts.AsyncErrorCB = func(_ *nats.Conn, _ *nats.Subscription, _ error) { |
| 202 |
wch <- true |
| 203 |
} |
| 204 |
|
| 205 |
ch := make(chan *int32) |
| 206 |
|
| 207 |
if _, err := ec.BindRecvChan("foo", ch); err != nil { |
| 208 |
t.Fatalf("Failed to bind to a recv channel: %v", err) |
| 209 |
} |
| 210 |
|
| 211 |
ec.Publish("foo", "Hello World") |
| 212 |
|
| 213 |
if e := Wait(wch); e != nil { |
| 214 |
t.Fatal("Failed to call async err handler") |
| 215 |
} |
| 216 |
}) |
| 217 |
} |
| 218 |
|
| 219 |
func TestRecvChanPanicOnClosedChan(t *testing.T) { |
| 220 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 221 |
ec := newEncodedConn(t, nc) |
| 222 |
|
| 223 |
ch := make(chan int) |
| 224 |
|
| 225 |
if _, err := ec.BindRecvChan("foo", ch); err != nil { |
| 226 |
t.Fatalf("Failed to bind to a recv channel: %v", err) |
| 227 |
} |
| 228 |
|
| 229 |
close(ch) |
| 230 |
ec.Publish("foo", 22) |
| 231 |
ec.Flush() |
| 232 |
}) |
| 233 |
} |
| 234 |
|
| 235 |
func TestRecvChanAsyncLeakGoRoutines(t *testing.T) { |
| 236 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 237 |
ec := newEncodedConn(t, nc) |
| 238 |
|
| 239 |
ec.Flush() |
| 240 |
|
| 241 |
base := getStableNumGoroutine(t) |
| 242 |
|
| 243 |
ch := make(chan int) |
| 244 |
|
| 245 |
if _, err := ec.BindRecvChan("foo", ch); err != nil { |
| 246 |
t.Fatalf("Failed to bind to a recv channel: %v", err) |
| 247 |
} |
| 248 |
|
| 249 |
close(ch) |
| 250 |
|
| 251 |
ec.Publish("foo", 22) |
| 252 |
ec.Flush() |
| 253 |
|
| 254 |
checkNoGoroutineLeak(t, base, "closing channel") |
| 255 |
}) |
| 256 |
} |
| 257 |
|
| 258 |
func TestRecvChanLeakGoRoutines(t *testing.T) { |
| 259 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 260 |
ec := newEncodedConn(t, nc) |
| 261 |
|
| 262 |
ec.Flush() |
| 263 |
|
| 264 |
base := getStableNumGoroutine(t) |
| 265 |
|
| 266 |
ch := make(chan int) |
| 267 |
|
| 268 |
sub, err := ec.BindRecvChan("foo", ch) |
| 269 |
if err != nil { |
| 270 |
t.Fatalf("Failed to bind to a recv channel: %v", err) |
| 271 |
} |
| 272 |
sub.Unsubscribe() |
| 273 |
|
| 274 |
checkNoGoroutineLeak(t, base, "Unsubscribe()") |
| 275 |
}) |
| 276 |
} |
| 277 |
|
| 278 |
func TestRecvChanMultipleMessages(t *testing.T) { |
| 279 |
// Make sure we can receive more than one message (#25 fix regression of #22). |
| 280 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 281 |
ec := newEncodedConn(t, nc) |
| 282 |
|
| 283 |
size := 10 |
| 284 |
ch := make(chan int, size) |
| 285 |
|
| 286 |
if _, err := ec.BindRecvChan("foo", ch); err != nil { |
| 287 |
t.Fatalf("Failed to bind to a recv channel: %v", err) |
| 288 |
} |
| 289 |
|
| 290 |
for i := 0; i < size; i++ { |
| 291 |
ec.Publish("foo", 22) |
| 292 |
} |
| 293 |
ec.Flush() |
| 294 |
time.Sleep(10 * time.Millisecond) |
| 295 |
|
| 296 |
if lch := len(ch); lch != size { |
| 297 |
t.Fatalf("Expected %d messages queued, got %d.", size, lch) |
| 298 |
} |
| 299 |
}) |
| 300 |
} |
| 301 |
|
| 302 |
func BenchmarkPublishSpeedViaChan(b *testing.B) { |
| 303 |
b.StopTimer() |
| 304 |
withServerB(b, func(b *testing.B, nc *nats.Conn) { |
| 305 |
ec, err := nats.NewEncodedConn(nc, nats.DEFAULT_ENCODER) |
| 306 |
if err != nil { |
| 307 |
b.Fatalf("Failed creating encoded connection: %v\n", err) |
| 308 |
} |
| 309 |
defer ec.Close() |
| 310 |
|
| 311 |
ch := make(chan int32, 1024) |
| 312 |
if err := ec.BindSendChan("foo", ch); err != nil { |
| 313 |
b.Fatalf("Failed to bind to a send channel: %v\n", err) |
| 314 |
} |
| 315 |
|
| 316 |
b.StartTimer() |
| 317 |
num := int32(22) |
| 318 |
for range b.N { |
| 319 |
ch <- num |
| 320 |
} |
| 321 |
nc.Flush() |
| 322 |
b.StopTimer() |
| 323 |
}) |
| 324 |
} |