| 1 |
// Copyright 2018-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 |
"fmt" |
| 19 |
"sync" |
| 20 |
"sync/atomic" |
| 21 |
"testing" |
| 22 |
"time" |
| 23 |
|
| 24 |
"github.com/nats-io/nats.go" |
| 25 |
testservice "github.com/synadia-io/orbit.go/ntf-client" |
| 26 |
) |
| 27 |
|
| 28 |
// Drain can be very useful for graceful shutdown of subscribers. |
| 29 |
// Especially queue subscribers. |
| 30 |
func TestDrain(t *testing.T) { |
| 31 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 32 |
done := make(chan bool) |
| 33 |
received := int32(0) |
| 34 |
expected := int32(100) |
| 35 |
|
| 36 |
cb := func(_ *nats.Msg) { |
| 37 |
time.Sleep(time.Millisecond) |
| 38 |
rcvd := atomic.AddInt32(&received, 1) |
| 39 |
if rcvd >= expected { |
| 40 |
done <- true |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
sub, err := nc.Subscribe("foo", cb) |
| 45 |
if err != nil { |
| 46 |
t.Fatalf("Error creating subscription; %v", err) |
| 47 |
} |
| 48 |
|
| 49 |
for i := int32(0); i < expected; i++ { |
| 50 |
nc.Publish("foo", []byte("Don't forget about me")) |
| 51 |
} |
| 52 |
|
| 53 |
sub.Drain() |
| 54 |
if !sub.IsDraining() { |
| 55 |
t.Fatalf("Expected to be draining") |
| 56 |
} |
| 57 |
select { |
| 58 |
case <-done: |
| 59 |
case <-time.After(5 * time.Second): |
| 60 |
r := atomic.LoadInt32(&received) |
| 61 |
if r != expected { |
| 62 |
t.Fatalf("Did not receive all messages: %d of %d", r, expected) |
| 63 |
} |
| 64 |
} |
| 65 |
time.Sleep(100 * time.Millisecond) |
| 66 |
if sub.IsDraining() { |
| 67 |
t.Fatalf("Expected to be done draining") |
| 68 |
} |
| 69 |
}) |
| 70 |
} |
| 71 |
|
| 72 |
func TestDrainQueueSub(t *testing.T) { |
| 73 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 74 |
done := make(chan bool) |
| 75 |
received := int32(0) |
| 76 |
expected := int32(4096) |
| 77 |
numSubs := int32(32) |
| 78 |
|
| 79 |
checkDone := func() int32 { |
| 80 |
rcvd := atomic.AddInt32(&received, 1) |
| 81 |
if rcvd >= expected { |
| 82 |
done <- true |
| 83 |
} |
| 84 |
return rcvd |
| 85 |
} |
| 86 |
|
| 87 |
callback := func(m *nats.Msg) { |
| 88 |
rcvd := checkDone() |
| 89 |
if rcvd%3 == 0 { |
| 90 |
m.Sub.Drain() |
| 91 |
nc.QueueSubscribe("foo", "bar", func(m *nats.Msg) { checkDone() }) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
for i := int32(0); i < numSubs; i++ { |
| 96 |
if _, err := nc.QueueSubscribe("foo", "bar", callback); err != nil { |
| 97 |
t.Fatalf("Error creating subscription; %v", err) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
for i := int32(0); i < expected; i++ { |
| 102 |
nc.Publish("foo", []byte("Don't forget about me")) |
| 103 |
} |
| 104 |
|
| 105 |
select { |
| 106 |
case <-done: |
| 107 |
case <-time.After(5 * time.Second): |
| 108 |
r := atomic.LoadInt32(&received) |
| 109 |
if r != expected { |
| 110 |
t.Fatalf("Did not receive all messages: %d of %d", r, expected) |
| 111 |
} |
| 112 |
} |
| 113 |
}) |
| 114 |
} |
| 115 |
|
| 116 |
func TestDrainUnSubs(t *testing.T) { |
| 117 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 118 |
num := 100 |
| 119 |
subs := make([]*nats.Subscription, num) |
| 120 |
|
| 121 |
for i := 0; i < num; i++ { |
| 122 |
sub, err := nc.Subscribe("foo", func(_ *nats.Msg) {}) |
| 123 |
if err != nil { |
| 124 |
t.Fatalf("Error creating subscription; %v", err) |
| 125 |
} |
| 126 |
subs[i] = sub |
| 127 |
} |
| 128 |
|
| 129 |
if numSubs := nc.NumSubscriptions(); numSubs != num { |
| 130 |
t.Fatalf("Expected %d subscriptions, got %d", num, numSubs) |
| 131 |
} |
| 132 |
for i := 0; i < num; i++ { |
| 133 |
subs[i].Unsubscribe() |
| 134 |
} |
| 135 |
if numSubs := nc.NumSubscriptions(); numSubs != 0 { |
| 136 |
t.Fatalf("Expected no subscriptions, got %d", numSubs) |
| 137 |
} |
| 138 |
|
| 139 |
for i := 0; i < num; i++ { |
| 140 |
sub, err := nc.Subscribe("foo", func(_ *nats.Msg) {}) |
| 141 |
if err != nil { |
| 142 |
t.Fatalf("Error creating subscription; %v", err) |
| 143 |
} |
| 144 |
subs[i] = sub |
| 145 |
} |
| 146 |
|
| 147 |
if numSubs := nc.NumSubscriptions(); numSubs != num { |
| 148 |
t.Fatalf("Expected %d subscriptions, got %d", num, numSubs) |
| 149 |
} |
| 150 |
for i := 0; i < num; i++ { |
| 151 |
subs[i].Drain() |
| 152 |
} |
| 153 |
waitFor(t, 2*time.Second, 10*time.Millisecond, func() error { |
| 154 |
if numSubs := nc.NumSubscriptions(); numSubs != 0 { |
| 155 |
return fmt.Errorf("Expected no subscriptions, got %d", numSubs) |
| 156 |
} |
| 157 |
return nil |
| 158 |
}) |
| 159 |
}) |
| 160 |
} |
| 161 |
|
| 162 |
func TestDrainSlowSubscriber(t *testing.T) { |
| 163 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 164 |
received := int32(0) |
| 165 |
|
| 166 |
sub, err := nc.Subscribe("foo", func(_ *nats.Msg) { |
| 167 |
atomic.AddInt32(&received, 1) |
| 168 |
time.Sleep(100 * time.Millisecond) |
| 169 |
}) |
| 170 |
if err != nil { |
| 171 |
t.Fatalf("Error creating subscription; %v", err) |
| 172 |
} |
| 173 |
|
| 174 |
total := 10 |
| 175 |
for i := 0; i < total; i++ { |
| 176 |
nc.Publish("foo", []byte("Slow Slow")) |
| 177 |
} |
| 178 |
nc.Flush() |
| 179 |
|
| 180 |
pmsgs, _, _ := sub.Pending() |
| 181 |
if pmsgs != total && pmsgs != total-1 { |
| 182 |
t.Fatalf("Expected most messages to be pending, but got %d vs %d", pmsgs, total) |
| 183 |
} |
| 184 |
sub.Drain() |
| 185 |
|
| 186 |
waitFor(t, 2*time.Second, 100*time.Millisecond, func() error { |
| 187 |
_, _, err := sub.Pending() |
| 188 |
if err != nats.ErrBadSubscription { |
| 189 |
return errors.New("Still valid") |
| 190 |
} |
| 191 |
r := int(atomic.LoadInt32(&received)) |
| 192 |
if r != total { |
| 193 |
t.Fatalf("Did not receive all messages, got %d vs %d", r, total) |
| 194 |
} |
| 195 |
return nil |
| 196 |
}) |
| 197 |
}) |
| 198 |
} |
| 199 |
|
| 200 |
func TestDrainConnection(t *testing.T) { |
| 201 |
withServerInstance(t, func(t *testing.T, _ *nats.Conn, inst *testservice.Instance) { |
| 202 |
done := make(chan bool) |
| 203 |
rdone := make(chan bool) |
| 204 |
|
| 205 |
closed := func(_ *nats.Conn) { |
| 206 |
done <- true |
| 207 |
} |
| 208 |
|
| 209 |
nc, err := nats.Connect(inst.Servers[0].URL, nats.ClosedHandler(closed)) |
| 210 |
if err != nil { |
| 211 |
t.Fatalf("Failed to create default connection: %v", err) |
| 212 |
} |
| 213 |
defer nc.Close() |
| 214 |
|
| 215 |
nc2, err := nats.Connect(inst.Servers[0].URL) |
| 216 |
if err != nil { |
| 217 |
t.Fatalf("Failed to create default connection: %v", err) |
| 218 |
} |
| 219 |
defer nc2.Close() |
| 220 |
|
| 221 |
received := int32(0) |
| 222 |
responses := int32(0) |
| 223 |
expected := int32(50) |
| 224 |
sleep := 10 * time.Millisecond |
| 225 |
|
| 226 |
_, err = nc2.Subscribe("bar", func(_ *nats.Msg) { |
| 227 |
r := atomic.AddInt32(&responses, 1) |
| 228 |
if r == expected { |
| 229 |
rdone <- true |
| 230 |
} |
| 231 |
}) |
| 232 |
if err != nil { |
| 233 |
t.Fatalf("Error creating subscription for responses: %v", err) |
| 234 |
} |
| 235 |
|
| 236 |
sub, err := nc.Subscribe("foo", func(m *nats.Msg) { |
| 237 |
time.Sleep(sleep) |
| 238 |
atomic.AddInt32(&received, 1) |
| 239 |
if err := nc.Publish(m.Reply, []byte("Stop bugging me")); err != nil { |
| 240 |
t.Errorf("Publisher received an error sending response: %v", err) |
| 241 |
} |
| 242 |
}) |
| 243 |
if err != nil { |
| 244 |
t.Fatalf("Error creating subscription; %v", err) |
| 245 |
} |
| 246 |
|
| 247 |
for i := int32(0); i < expected; i++ { |
| 248 |
nc.PublishRequest("foo", "bar", []byte("Slow Slow")) |
| 249 |
} |
| 250 |
|
| 251 |
drainStart := time.Now() |
| 252 |
nc.Drain() |
| 253 |
|
| 254 |
if err := sub.Unsubscribe(); err == nil { |
| 255 |
t.Fatalf("Expected to receive an error on Unsubscribe after drain") |
| 256 |
} |
| 257 |
if _, err := nc.Subscribe("foo", func(_ *nats.Msg) {}); err == nil { |
| 258 |
t.Fatalf("Expected to receive an error on new Subscription after drain") |
| 259 |
} |
| 260 |
|
| 261 |
if err := nc.Publish("baz", []byte("Slow Slow")); err != nil { |
| 262 |
t.Fatalf("Expected to not receive an error on Publish after drain, got %v", err) |
| 263 |
} |
| 264 |
|
| 265 |
select { |
| 266 |
case <-done: |
| 267 |
if time.Since(drainStart) < (sleep * time.Duration(expected)) { |
| 268 |
t.Fatalf("Drain exited too soon") |
| 269 |
} |
| 270 |
r := atomic.LoadInt32(&received) |
| 271 |
if r != expected { |
| 272 |
t.Fatalf("Did not receive all messages from Drain, %d vs %d", r, expected) |
| 273 |
} |
| 274 |
case <-time.After(2 * time.Second): |
| 275 |
t.Fatalf("Timeout waiting for closed state for connection") |
| 276 |
} |
| 277 |
|
| 278 |
select { |
| 279 |
case <-rdone: |
| 280 |
r := atomic.LoadInt32(&responses) |
| 281 |
if r != expected { |
| 282 |
t.Fatalf("Did not receive all responses, %d vs %d", r, expected) |
| 283 |
} |
| 284 |
case <-time.After(2 * time.Second): |
| 285 |
t.Fatalf("Timeout waiting for all the responses") |
| 286 |
} |
| 287 |
}) |
| 288 |
} |
| 289 |
|
| 290 |
func TestDrainConnectionAutoUnsub(t *testing.T) { |
| 291 |
withServerInstance(t, func(t *testing.T, _ *nats.Conn, inst *testservice.Instance) { |
| 292 |
errCount := int32(0) |
| 293 |
received := int32(0) |
| 294 |
expected := int32(10) |
| 295 |
|
| 296 |
done := make(chan bool) |
| 297 |
|
| 298 |
closed := func(_ *nats.Conn) { |
| 299 |
done <- true |
| 300 |
} |
| 301 |
errCb := func(_ *nats.Conn, _ *nats.Subscription, _ error) { |
| 302 |
atomic.AddInt32(&errCount, 1) |
| 303 |
} |
| 304 |
|
| 305 |
nc, err := nats.Connect(inst.Servers[0].URL, |
| 306 |
nats.ErrorHandler(errCb), |
| 307 |
nats.ClosedHandler(closed)) |
| 308 |
if err != nil { |
| 309 |
t.Fatalf("Failed to create default connection: %v", err) |
| 310 |
} |
| 311 |
defer nc.Close() |
| 312 |
|
| 313 |
sub, err := nc.Subscribe("foo", func(_ *nats.Msg) { |
| 314 |
time.Sleep(10 * time.Millisecond) |
| 315 |
atomic.AddInt32(&received, 1) |
| 316 |
}) |
| 317 |
if err != nil { |
| 318 |
t.Fatalf("Error creating subscription; %v", err) |
| 319 |
} |
| 320 |
|
| 321 |
sub.AutoUnsubscribe(int(expected)) |
| 322 |
|
| 323 |
for i := 0; i < 50; i++ { |
| 324 |
nc.Publish("foo", []byte("Only 10 please!")) |
| 325 |
} |
| 326 |
nc.Flush() |
| 327 |
|
| 328 |
time.Sleep(10 * time.Millisecond) |
| 329 |
nc.Drain() |
| 330 |
|
| 331 |
select { |
| 332 |
case <-done: |
| 333 |
errs := atomic.LoadInt32(&errCount) |
| 334 |
if errs > 0 { |
| 335 |
t.Fatalf("Did not expect any errors, got %d", errs) |
| 336 |
} |
| 337 |
r := atomic.LoadInt32(&received) |
| 338 |
if r != expected { |
| 339 |
t.Fatalf("Did not receive all messages from Drain, %d vs %d", r, expected) |
| 340 |
} |
| 341 |
case <-time.After(2 * time.Second): |
| 342 |
t.Fatalf("Timeout waiting for closed state for connection") |
| 343 |
} |
| 344 |
}) |
| 345 |
} |
| 346 |
|
| 347 |
func TestDrainConnLastError(t *testing.T) { |
| 348 |
withServerInstance(t, func(t *testing.T, _ *nats.Conn, inst *testservice.Instance) { |
| 349 |
done := make(chan bool, 1) |
| 350 |
closedCb := func(_ *nats.Conn) { |
| 351 |
done <- true |
| 352 |
} |
| 353 |
|
| 354 |
nc, err := nats.Connect(inst.Servers[0].URL, |
| 355 |
nats.ClosedHandler(closedCb), |
| 356 |
nats.DrainTimeout(time.Millisecond)) |
| 357 |
if err != nil { |
| 358 |
t.Fatalf("Failed to create default connection: %v", err) |
| 359 |
} |
| 360 |
defer nc.Close() |
| 361 |
|
| 362 |
nc.SetErrorHandler(func(_ *nats.Conn, _ *nats.Subscription, _ error) {}) |
| 363 |
|
| 364 |
wg := sync.WaitGroup{} |
| 365 |
wg.Add(1) |
| 366 |
if _, err := nc.Subscribe("foo", func(_ *nats.Msg) { |
| 367 |
time.Sleep(100 * time.Millisecond) |
| 368 |
wg.Done() |
| 369 |
}); err != nil { |
| 370 |
t.Fatalf("Error creating subscription; %v", err) |
| 371 |
} |
| 372 |
|
| 373 |
if err := nc.Publish("foo", []byte("msg")); err != nil { |
| 374 |
t.Fatalf("Error on publish: %v", err) |
| 375 |
} |
| 376 |
if err := nc.Drain(); err != nil { |
| 377 |
t.Fatalf("Error on drain: %v", err) |
| 378 |
} |
| 379 |
|
| 380 |
select { |
| 381 |
case <-done: |
| 382 |
if e := nc.LastError(); e == nil || e != nats.ErrDrainTimeout { |
| 383 |
t.Fatalf("Expected last error to be set to %v, got %v", nats.ErrDrainTimeout, e) |
| 384 |
} |
| 385 |
case <-time.After(2 * time.Second): |
| 386 |
t.Fatalf("Timeout waiting for closed state for connection") |
| 387 |
} |
| 388 |
|
| 389 |
wg.Wait() |
| 390 |
}) |
| 391 |
} |
| 392 |
|
| 393 |
func TestDrainConnDuringReconnect(t *testing.T) { |
| 394 |
withServerInstance(t, func(t *testing.T, _ *nats.Conn, inst *testservice.Instance) { |
| 395 |
done := make(chan bool, 1) |
| 396 |
closedCb := func(_ *nats.Conn) { |
| 397 |
done <- true |
| 398 |
} |
| 399 |
|
| 400 |
nc, err := nats.Connect(inst.Servers[0].URL, |
| 401 |
nats.ClosedHandler(closedCb), |
| 402 |
nats.DrainTimeout(20*time.Millisecond)) |
| 403 |
if err != nil { |
| 404 |
t.Fatalf("Failed to create default connection: %v", err) |
| 405 |
} |
| 406 |
defer nc.Close() |
| 407 |
|
| 408 |
// Shutdown the server. |
| 409 |
inst.StopServer(t, inst.Servers[0]) |
| 410 |
|
| 411 |
waitFor(t, time.Second, 10*time.Millisecond, func() error { |
| 412 |
if nc.IsReconnecting() { |
| 413 |
return nil |
| 414 |
} |
| 415 |
return errors.New("Not reconnecting yet") |
| 416 |
}) |
| 417 |
|
| 418 |
if err := nc.Drain(); err != nats.ErrConnectionReconnecting { |
| 419 |
t.Fatalf("Unexpected error on drain: %v", err) |
| 420 |
} |
| 421 |
|
| 422 |
select { |
| 423 |
case <-done: |
| 424 |
case <-time.After(2 * time.Second): |
| 425 |
t.Fatalf("Timeout waiting for closed state for connection") |
| 426 |
} |
| 427 |
}) |
| 428 |
} |
| 429 |
|
| 430 |
func TestDrainClosedHandlerRace(t *testing.T) { |
| 431 |
withServerInstance(t, func(t *testing.T, _ *nats.Conn, inst *testservice.Instance) { |
| 432 |
nc, err := nats.Connect(inst.Servers[0].URL, nats.ClosedHandler(func(_ *nats.Conn) {})) |
| 433 |
if err != nil { |
| 434 |
t.Fatalf("Unexpected error: %v", err) |
| 435 |
} |
| 436 |
defer nc.Close() |
| 437 |
|
| 438 |
if err := nc.Drain(); err != nil { |
| 439 |
t.Fatalf("Unexpected error on drain: %v", err) |
| 440 |
} |
| 441 |
go func() { |
| 442 |
nc.SetClosedHandler(nil) |
| 443 |
}() |
| 444 |
time.Sleep(500 * time.Millisecond) |
| 445 |
}) |
| 446 |
} |