| 1 |
// Copyright 2019-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 |
//go:build !race && !skip_no_race_tests |
| 15 |
|
| 16 |
package test |
| 17 |
|
| 18 |
import ( |
| 19 |
"bytes" |
| 20 |
"context" |
| 21 |
"crypto/rand" |
| 22 |
"fmt" |
| 23 |
"io" |
| 24 |
"strings" |
| 25 |
"testing" |
| 26 |
"time" |
| 27 |
|
| 28 |
"github.com/nats-io/nats.go" |
| 29 |
testservice "github.com/synadia-io/orbit.go/ntf-client" |
| 30 |
) |
| 31 |
|
| 32 |
func TestNoRaceObjectContextOpt(t *testing.T) { |
| 33 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 34 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 35 |
expectOk(t, err) |
| 36 |
|
| 37 |
obs, err := js.CreateObjectStore(&nats.ObjectStoreConfig{Bucket: "OBJS"}) |
| 38 |
expectOk(t, err) |
| 39 |
|
| 40 |
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 41 |
time.AfterFunc(100*time.Millisecond, cancel) |
| 42 |
|
| 43 |
start := time.Now() |
| 44 |
_, err = obs.Put(&nats.ObjectMeta{Name: "TEST"}, &slow{1000}, nats.Context(ctx)) |
| 45 |
expectErr(t, err) |
| 46 |
if delta := time.Since(start); delta > time.Second { |
| 47 |
t.Fatalf("Cancel took too long: %v", delta) |
| 48 |
} |
| 49 |
si, err := js.StreamInfo("OBJ_OBJS") |
| 50 |
expectOk(t, err) |
| 51 |
if si.State.Msgs != 0 { |
| 52 |
t.Fatalf("Expected no messages after canceling put, got %+v", si.State) |
| 53 |
} |
| 54 |
|
| 55 |
// Now put a large object in there. |
| 56 |
blob := make([]byte, 16*1024*1024) |
| 57 |
rand.Read(blob) |
| 58 |
_, err = obs.PutBytes("BLOB", blob) |
| 59 |
expectOk(t, err) |
| 60 |
|
| 61 |
ctx, cancel = context.WithTimeout(context.Background(), 2*time.Second) |
| 62 |
time.AfterFunc(10*time.Millisecond, cancel) |
| 63 |
|
| 64 |
start = time.Now() |
| 65 |
_, err = obs.GetBytes("BLOB", nats.Context(ctx)) |
| 66 |
expectErr(t, err) |
| 67 |
if delta := time.Since(start); delta > 2500*time.Millisecond { |
| 68 |
t.Fatalf("Cancel took too long: %v", delta) |
| 69 |
} |
| 70 |
}) |
| 71 |
} |
| 72 |
|
| 73 |
type slow struct{ n int } |
| 74 |
|
| 75 |
func (sr *slow) Read(p []byte) (n int, err error) { |
| 76 |
if sr.n <= 0 { |
| 77 |
return 0, io.EOF |
| 78 |
} |
| 79 |
sr.n-- |
| 80 |
time.Sleep(10 * time.Millisecond) |
| 81 |
p[0] = 'A' |
| 82 |
return 1, nil |
| 83 |
} |
| 84 |
|
| 85 |
func TestNoRaceObjectDoublePut(t *testing.T) { |
| 86 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 87 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 88 |
expectOk(t, err) |
| 89 |
|
| 90 |
obs, err := js.CreateObjectStore(&nats.ObjectStoreConfig{Bucket: "OBJS"}) |
| 91 |
expectOk(t, err) |
| 92 |
|
| 93 |
_, err = obs.PutBytes("A", bytes.Repeat([]byte("A"), 1_000_000)) |
| 94 |
expectOk(t, err) |
| 95 |
|
| 96 |
_, err = obs.PutBytes("A", bytes.Repeat([]byte("a"), 20_000_000)) |
| 97 |
expectOk(t, err) |
| 98 |
|
| 99 |
_, err = obs.GetBytes("A") |
| 100 |
expectOk(t, err) |
| 101 |
}) |
| 102 |
} |
| 103 |
|
| 104 |
func TestNoRaceJetStreamConsumerSlowConsumer(t *testing.T) { |
| 105 |
// This test fails many times, need to look harder at the imbalance. |
| 106 |
t.SkipNow() |
| 107 |
|
| 108 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 109 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 110 |
if err != nil { |
| 111 |
t.Fatalf("Unexpected error: %v", err) |
| 112 |
} |
| 113 |
|
| 114 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 115 |
Name: "PENDING_TEST", |
| 116 |
Subjects: []string{"js.p"}, |
| 117 |
Storage: nats.MemoryStorage, |
| 118 |
}) |
| 119 |
if err != nil { |
| 120 |
t.Fatalf("stream create failed: %v", err) |
| 121 |
} |
| 122 |
|
| 123 |
// Override default handler for test. |
| 124 |
nc.SetErrorHandler(func(_ *nats.Conn, _ *nats.Subscription, _ error) {}) |
| 125 |
|
| 126 |
// Queue up 1M small messages. |
| 127 |
toSend := uint64(1000000) |
| 128 |
for i := uint64(0); i < toSend; i++ { |
| 129 |
nc.Publish("js.p", []byte("ok")) |
| 130 |
} |
| 131 |
nc.Flush() |
| 132 |
|
| 133 |
str, err := js.StreamInfo("PENDING_TEST") |
| 134 |
if err != nil { |
| 135 |
t.Fatal(err) |
| 136 |
} |
| 137 |
|
| 138 |
if nm := str.State.Msgs; nm != toSend { |
| 139 |
t.Fatalf("Expected to have stored all %d msgs, got only %d", toSend, nm) |
| 140 |
} |
| 141 |
|
| 142 |
var received uint64 |
| 143 |
done := make(chan bool, 1) |
| 144 |
|
| 145 |
js.Subscribe("js.p", func(m *nats.Msg) { |
| 146 |
received++ |
| 147 |
if received >= toSend { |
| 148 |
done <- true |
| 149 |
} |
| 150 |
meta, err := m.Metadata() |
| 151 |
if err != nil { |
| 152 |
t.Fatalf("could not get message metadata: %s", err) |
| 153 |
} |
| 154 |
if meta.Sequence.Stream != received { |
| 155 |
t.Errorf("Missed a sequence, was expecting %d but got %d, last error: '%v'", received, meta.Sequence.Stream, nc.LastError()) |
| 156 |
nc.Close() |
| 157 |
} |
| 158 |
m.Ack() |
| 159 |
}) |
| 160 |
|
| 161 |
select { |
| 162 |
case <-time.After(5 * time.Second): |
| 163 |
t.Fatalf("Failed to get all %d messages, only got %d", toSend, received) |
| 164 |
case <-done: |
| 165 |
} |
| 166 |
}) |
| 167 |
} |
| 168 |
|
| 169 |
func TestNoRaceJetStreamPushFlowControlHeartbeats_SubscribeSync(t *testing.T) { |
| 170 |
errHandler := nats.ErrorHandler(func(c *nats.Conn, sub *nats.Subscription, err error) { |
| 171 |
t.Logf("WARN: %s", err) |
| 172 |
}) |
| 173 |
|
| 174 |
c := newTester(t) |
| 175 |
inst := c.CreateServer(t, true) |
| 176 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 177 |
|
| 178 |
nc := dialInstance(t, inst, errHandler) |
| 179 |
c.WaitForJetStream(t, nc) |
| 180 |
|
| 181 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 182 |
if err != nil { |
| 183 |
t.Fatalf("Unexpected error: %v", err) |
| 184 |
} |
| 185 |
|
| 186 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 187 |
Name: "TEST", |
| 188 |
Subjects: []string{"foo"}, |
| 189 |
}) |
| 190 |
if err != nil { |
| 191 |
t.Fatalf("Unexpected error: %v", err) |
| 192 |
} |
| 193 |
|
| 194 |
// Burst and try to hit the flow control limit of the server. |
| 195 |
const totalMsgs = 16536 |
| 196 |
payload := strings.Repeat("A", 1024) |
| 197 |
for i := 0; i < totalMsgs; i++ { |
| 198 |
if _, err := js.Publish("foo", []byte(fmt.Sprintf("i:%d/", i)+payload)); err != nil { |
| 199 |
t.Fatal(err) |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
hbTimer := 100 * time.Millisecond |
| 204 |
sub, err := js.SubscribeSync("foo", |
| 205 |
nats.AckWait(30*time.Second), |
| 206 |
nats.MaxDeliver(1), |
| 207 |
nats.EnableFlowControl(), |
| 208 |
nats.IdleHeartbeat(hbTimer), |
| 209 |
) |
| 210 |
if err != nil { |
| 211 |
t.Fatal(err) |
| 212 |
} |
| 213 |
defer sub.Unsubscribe() |
| 214 |
|
| 215 |
info, err := sub.ConsumerInfo() |
| 216 |
if err != nil { |
| 217 |
t.Fatal(err) |
| 218 |
} |
| 219 |
if !info.Config.FlowControl { |
| 220 |
t.Fatal("Expected Flow Control to be enabled") |
| 221 |
} |
| 222 |
if info.Config.Heartbeat != hbTimer { |
| 223 |
t.Errorf("Expected %v, got: %v", hbTimer, info.Config.Heartbeat) |
| 224 |
} |
| 225 |
|
| 226 |
m, err := sub.NextMsg(1 * time.Second) |
| 227 |
if err != nil { |
| 228 |
t.Fatalf("Error getting next message: %v", err) |
| 229 |
} |
| 230 |
meta, err := m.Metadata() |
| 231 |
if err != nil { |
| 232 |
t.Fatal(err) |
| 233 |
} |
| 234 |
if meta.NumPending > totalMsgs { |
| 235 |
t.Logf("WARN: More pending messages than expected (%v), got: %v", totalMsgs, meta.NumPending) |
| 236 |
} |
| 237 |
err = m.Ack() |
| 238 |
if err != nil { |
| 239 |
t.Fatal(err) |
| 240 |
} |
| 241 |
|
| 242 |
recvd := 1 |
| 243 |
timeout := time.Now().Add(10 * time.Second) |
| 244 |
for time.Now().Before(timeout) { |
| 245 |
m, err := sub.NextMsg(1 * time.Second) |
| 246 |
if err != nil { |
| 247 |
t.Fatalf("Error getting next message: %v", err) |
| 248 |
} |
| 249 |
if len(m.Data) == 0 { |
| 250 |
t.Fatalf("Unexpected empty message: %+v", m) |
| 251 |
} |
| 252 |
|
| 253 |
if err := m.AckSync(); err != nil { |
| 254 |
t.Fatalf("Error on ack message: %v", err) |
| 255 |
} |
| 256 |
recvd++ |
| 257 |
|
| 258 |
if recvd == totalMsgs { |
| 259 |
break |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
t.Run("idle heartbeats", func(t *testing.T) { |
| 264 |
// Delay to get a few heartbeats. |
| 265 |
time.Sleep(4 * hbTimer) |
| 266 |
|
| 267 |
timeout = time.Now().Add(5 * time.Second) |
| 268 |
for time.Now().Before(timeout) { |
| 269 |
msg, err := sub.NextMsg(200 * time.Millisecond) |
| 270 |
if err != nil { |
| 271 |
if err == nats.ErrTimeout { |
| 272 |
// If timeout, ok to stop checking for the test. |
| 273 |
break |
| 274 |
} |
| 275 |
t.Fatal(err) |
| 276 |
} |
| 277 |
if len(msg.Data) == 0 { |
| 278 |
t.Fatalf("Unexpected empty message: %+v", m) |
| 279 |
} |
| 280 |
|
| 281 |
recvd++ |
| 282 |
meta, err := msg.Metadata() |
| 283 |
if err != nil { |
| 284 |
t.Fatal(err) |
| 285 |
} |
| 286 |
if meta.NumPending == 0 { |
| 287 |
break |
| 288 |
} |
| 289 |
} |
| 290 |
if recvd > totalMsgs { |
| 291 |
t.Logf("WARN: Received more messages than expected (%v), got: %v", totalMsgs, recvd) |
| 292 |
} |
| 293 |
}) |
| 294 |
|
| 295 |
t.Run("with context", func(t *testing.T) { |
| 296 |
sub, err := js.SubscribeSync("foo", |
| 297 |
nats.AckWait(30*time.Second), |
| 298 |
nats.Durable("bar"), |
| 299 |
nats.EnableFlowControl(), |
| 300 |
nats.IdleHeartbeat(hbTimer), |
| 301 |
) |
| 302 |
if err != nil { |
| 303 |
t.Fatal(err) |
| 304 |
} |
| 305 |
defer sub.Unsubscribe() |
| 306 |
|
| 307 |
info, err = sub.ConsumerInfo() |
| 308 |
if err != nil { |
| 309 |
t.Fatal(err) |
| 310 |
} |
| 311 |
if !info.Config.FlowControl { |
| 312 |
t.Fatal("Expected Flow Control to be enabled") |
| 313 |
} |
| 314 |
|
| 315 |
recvd = 0 |
| 316 |
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 317 |
defer cancel() |
| 318 |
for { |
| 319 |
select { |
| 320 |
case <-ctx.Done(): |
| 321 |
t.Fatal(ctx.Err()) |
| 322 |
default: |
| 323 |
} |
| 324 |
|
| 325 |
m, err := sub.NextMsgWithContext(ctx) |
| 326 |
if err != nil { |
| 327 |
t.Fatalf("Error getting next message: %v", err) |
| 328 |
} |
| 329 |
if len(m.Data) == 0 { |
| 330 |
t.Fatalf("Unexpected empty message: %+v", m) |
| 331 |
} |
| 332 |
|
| 333 |
if err := m.Ack(); err != nil { |
| 334 |
t.Fatalf("Error on ack message: %v", err) |
| 335 |
} |
| 336 |
recvd++ |
| 337 |
|
| 338 |
if recvd >= totalMsgs { |
| 339 |
break |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
// Delay to get a few heartbeats. |
| 344 |
time.Sleep(4 * hbTimer) |
| 345 |
ctx, cancel = context.WithTimeout(context.Background(), time.Second) |
| 346 |
defer cancel() |
| 347 |
FOR_LOOP: |
| 348 |
for { |
| 349 |
select { |
| 350 |
case <-ctx.Done(): |
| 351 |
if ctx.Err() == context.DeadlineExceeded { |
| 352 |
break FOR_LOOP |
| 353 |
} |
| 354 |
default: |
| 355 |
} |
| 356 |
|
| 357 |
msg, err := sub.NextMsgWithContext(ctx) |
| 358 |
if err != nil { |
| 359 |
if err == context.DeadlineExceeded { |
| 360 |
break |
| 361 |
} |
| 362 |
t.Fatal(err) |
| 363 |
} |
| 364 |
if len(msg.Data) == 0 { |
| 365 |
t.Fatalf("Unexpected empty message: %+v", m) |
| 366 |
} |
| 367 |
recvd++ |
| 368 |
meta, err := msg.Metadata() |
| 369 |
if err != nil { |
| 370 |
t.Fatal(err) |
| 371 |
} |
| 372 |
if meta.NumPending == 0 { |
| 373 |
break |
| 374 |
} |
| 375 |
} |
| 376 |
if recvd > totalMsgs { |
| 377 |
t.Logf("WARN: Received more messages than expected (%v), got: %v", totalMsgs, recvd) |
| 378 |
} |
| 379 |
}) |
| 380 |
} |
| 381 |
|
| 382 |
func TestNoRaceJetStreamPushFlowControlHeartbeats_SubscribeAsync(t *testing.T) { |
| 383 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 384 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 385 |
if err != nil { |
| 386 |
t.Fatalf("Unexpected error: %v", err) |
| 387 |
} |
| 388 |
|
| 389 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 390 |
Name: "TEST", |
| 391 |
Subjects: []string{"foo"}, |
| 392 |
}) |
| 393 |
if err != nil { |
| 394 |
t.Fatalf("Unexpected error: %v", err) |
| 395 |
} |
| 396 |
|
| 397 |
// Burst and try to hit the flow control limit of the server. |
| 398 |
const totalMsgs = 16536 |
| 399 |
payload := strings.Repeat("A", 1024) |
| 400 |
for i := 0; i < totalMsgs; i++ { |
| 401 |
if _, err := js.Publish("foo", []byte(payload)); err != nil { |
| 402 |
t.Fatal(err) |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
recvd := make(chan *nats.Msg, totalMsgs) |
| 407 |
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 408 |
defer cancel() |
| 409 |
|
| 410 |
errCh := make(chan error) |
| 411 |
hbTimer := 100 * time.Millisecond |
| 412 |
sub, err := js.Subscribe("foo", func(msg *nats.Msg) { |
| 413 |
if len(msg.Data) == 0 { |
| 414 |
errCh <- fmt.Errorf("Unexpected empty message: %+v", msg) |
| 415 |
} |
| 416 |
recvd <- msg |
| 417 |
|
| 418 |
if len(recvd) == totalMsgs { |
| 419 |
cancel() |
| 420 |
} |
| 421 |
}, nats.EnableFlowControl(), nats.IdleHeartbeat(hbTimer)) |
| 422 |
if err != nil { |
| 423 |
t.Fatal(err) |
| 424 |
} |
| 425 |
defer sub.Unsubscribe() |
| 426 |
|
| 427 |
info, err := sub.ConsumerInfo() |
| 428 |
if err != nil { |
| 429 |
t.Fatal(err) |
| 430 |
} |
| 431 |
if !info.Config.FlowControl { |
| 432 |
t.Fatal("Expected Flow Control to be enabled") |
| 433 |
} |
| 434 |
if info.Config.Heartbeat != hbTimer { |
| 435 |
t.Errorf("Expected %v, got: %v", hbTimer, info.Config.Heartbeat) |
| 436 |
} |
| 437 |
|
| 438 |
<-ctx.Done() |
| 439 |
|
| 440 |
got := len(recvd) |
| 441 |
expected := totalMsgs |
| 442 |
if got != expected { |
| 443 |
t.Errorf("Expected %v, got: %v", expected, got) |
| 444 |
} |
| 445 |
|
| 446 |
// Wait for a couple of heartbeats to arrive and confirm there is no error. |
| 447 |
select { |
| 448 |
case <-time.After(1 * time.Second): |
| 449 |
case err := <-errCh: |
| 450 |
t.Fatal(err) |
| 451 |
} |
| 452 |
}) |
| 453 |
} |
| 454 |
|
| 455 |
func TestNoRaceJetStreamPushFlowControlHeartbeats_ChanSubscribe(t *testing.T) { |
| 456 |
errHandler := nats.ErrorHandler(func(c *nats.Conn, sub *nats.Subscription, err error) { |
| 457 |
t.Logf("WARN: %s : %v", err, sub.Subject) |
| 458 |
}) |
| 459 |
|
| 460 |
c := newTester(t) |
| 461 |
inst := c.CreateServer(t, true) |
| 462 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 463 |
|
| 464 |
nc := dialInstance(t, inst, errHandler) |
| 465 |
c.WaitForJetStream(t, nc) |
| 466 |
|
| 467 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 468 |
if err != nil { |
| 469 |
t.Fatalf("Unexpected error: %v", err) |
| 470 |
} |
| 471 |
|
| 472 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 473 |
Name: "TEST", |
| 474 |
Subjects: []string{"foo"}, |
| 475 |
}) |
| 476 |
if err != nil { |
| 477 |
t.Fatalf("Unexpected error: %v", err) |
| 478 |
} |
| 479 |
|
| 480 |
// Burst and try to hit the flow control limit of the server. |
| 481 |
const totalMsgs = 16536 |
| 482 |
payload := strings.Repeat("A", 1024) |
| 483 |
for i := 0; i < totalMsgs; i++ { |
| 484 |
if _, err := js.Publish("foo", []byte(fmt.Sprintf("i:%d/", i)+payload)); err != nil { |
| 485 |
t.Fatal(err) |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
hbTimer := 100 * time.Millisecond |
| 490 |
mch := make(chan *nats.Msg, 16536) |
| 491 |
sub, err := js.ChanSubscribe("foo", mch, |
| 492 |
nats.AckWait(30*time.Second), |
| 493 |
nats.MaxDeliver(1), |
| 494 |
nats.EnableFlowControl(), |
| 495 |
nats.IdleHeartbeat(hbTimer), |
| 496 |
) |
| 497 |
if err != nil { |
| 498 |
t.Fatal(err) |
| 499 |
} |
| 500 |
defer sub.Unsubscribe() |
| 501 |
|
| 502 |
info, err := sub.ConsumerInfo() |
| 503 |
if err != nil { |
| 504 |
t.Fatal(err) |
| 505 |
} |
| 506 |
if !info.Config.FlowControl { |
| 507 |
t.Fatal("Expected Flow Control to be enabled") |
| 508 |
} |
| 509 |
if info.Config.Heartbeat != hbTimer { |
| 510 |
t.Errorf("Expected %v, got: %v", hbTimer, info.Config.Heartbeat) |
| 511 |
} |
| 512 |
|
| 513 |
getNextMsg := func(mch chan *nats.Msg, timeout time.Duration) (*nats.Msg, error) { |
| 514 |
t.Helper() |
| 515 |
select { |
| 516 |
case m := <-mch: |
| 517 |
return m, nil |
| 518 |
case <-time.After(timeout): |
| 519 |
return nil, nats.ErrTimeout |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
m, err := getNextMsg(mch, 1*time.Second) |
| 524 |
if err != nil { |
| 525 |
t.Fatalf("Error getting next message: %v", err) |
| 526 |
} |
| 527 |
meta, err := m.Metadata() |
| 528 |
if err != nil { |
| 529 |
t.Fatal(err) |
| 530 |
} |
| 531 |
if meta.NumPending > totalMsgs { |
| 532 |
t.Logf("WARN: More pending messages than expected (%v), got: %v", totalMsgs, meta.NumPending) |
| 533 |
} |
| 534 |
err = m.Ack() |
| 535 |
if err != nil { |
| 536 |
t.Fatal(err) |
| 537 |
} |
| 538 |
|
| 539 |
recvd := 1 |
| 540 |
ctx, done := context.WithTimeout(context.Background(), 10*time.Second) |
| 541 |
defer done() |
| 542 |
|
| 543 |
Loop: |
| 544 |
for { |
| 545 |
select { |
| 546 |
case <-ctx.Done(): |
| 547 |
break Loop |
| 548 |
case m := <-mch: |
| 549 |
if err != nil { |
| 550 |
t.Fatalf("Error getting next message: %v", err) |
| 551 |
} |
| 552 |
if len(m.Data) == 0 { |
| 553 |
t.Fatalf("Unexpected empty message: %+v", m) |
| 554 |
} |
| 555 |
|
| 556 |
if err := m.Ack(); err != nil { |
| 557 |
t.Fatalf("Error on ack message: %v", err) |
| 558 |
} |
| 559 |
recvd++ |
| 560 |
|
| 561 |
if recvd == totalMsgs { |
| 562 |
done() |
| 563 |
} |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
t.Run("idle heartbeats", func(t *testing.T) { |
| 568 |
// Delay to get a few heartbeats. |
| 569 |
time.Sleep(4 * hbTimer) |
| 570 |
|
| 571 |
ctx, done := context.WithTimeout(context.Background(), 1*time.Second) |
| 572 |
defer done() |
| 573 |
Loop: |
| 574 |
for { |
| 575 |
select { |
| 576 |
case <-ctx.Done(): |
| 577 |
break Loop |
| 578 |
case msg := <-mch: |
| 579 |
if err != nil { |
| 580 |
if err == nats.ErrTimeout { |
| 581 |
// If timeout, ok to stop checking for the test. |
| 582 |
break Loop |
| 583 |
} |
| 584 |
t.Fatal(err) |
| 585 |
} |
| 586 |
if len(msg.Data) == 0 { |
| 587 |
t.Fatalf("Unexpected empty message: %+v", m) |
| 588 |
} |
| 589 |
|
| 590 |
recvd++ |
| 591 |
meta, err := msg.Metadata() |
| 592 |
if err != nil { |
| 593 |
t.Fatal(err) |
| 594 |
} |
| 595 |
if meta.NumPending == 0 { |
| 596 |
break Loop |
| 597 |
} |
| 598 |
} |
| 599 |
} |
| 600 |
if recvd > totalMsgs { |
| 601 |
t.Logf("WARN: Received more messages than expected (%v), got: %v", totalMsgs, recvd) |
| 602 |
} |
| 603 |
}) |
| 604 |
} |
| 605 |
|
| 606 |
func TestNoRaceJetStreamPushFlowControl_SubscribeAsyncAndChannel(t *testing.T) { |
| 607 |
errCh := make(chan error) |
| 608 |
errHandler := nats.ErrorHandler(func(c *nats.Conn, sub *nats.Subscription, err error) { |
| 609 |
errCh <- err |
| 610 |
}) |
| 611 |
|
| 612 |
c := newTester(t) |
| 613 |
inst := c.CreateServer(t, true) |
| 614 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 615 |
|
| 616 |
nc := dialInstance(t, inst, errHandler) |
| 617 |
c.WaitForJetStream(t, nc) |
| 618 |
|
| 619 |
const totalMsgs = 10_000 |
| 620 |
|
| 621 |
js, err := nc.JetStream(nats.PublishAsyncMaxPending(totalMsgs)) |
| 622 |
if err != nil { |
| 623 |
t.Fatalf("Unexpected error: %v", err) |
| 624 |
} |
| 625 |
|
| 626 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 627 |
Name: "TEST", |
| 628 |
Subjects: []string{"foo"}, |
| 629 |
}) |
| 630 |
if err != nil { |
| 631 |
t.Fatalf("Unexpected error: %v", err) |
| 632 |
} |
| 633 |
go func() { |
| 634 |
payload := strings.Repeat("O", 4096) |
| 635 |
for i := 0; i < totalMsgs; i++ { |
| 636 |
js.PublishAsync("foo", []byte(payload)) |
| 637 |
} |
| 638 |
}() |
| 639 |
|
| 640 |
// Small channel that blocks and then buffered channel that can deliver all |
| 641 |
// messages without blocking. |
| 642 |
recvd := make(chan *nats.Msg, 64) |
| 643 |
delivered := make(chan *nats.Msg, totalMsgs) |
| 644 |
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) |
| 645 |
defer cancel() |
| 646 |
|
| 647 |
// Dispatch channel consumer |
| 648 |
go func() { |
| 649 |
for m := range recvd { |
| 650 |
select { |
| 651 |
case <-ctx.Done(): |
| 652 |
return |
| 653 |
default: |
| 654 |
} |
| 655 |
|
| 656 |
delivered <- m |
| 657 |
if len(delivered) == totalMsgs { |
| 658 |
cancel() |
| 659 |
return |
| 660 |
} |
| 661 |
} |
| 662 |
}() |
| 663 |
|
| 664 |
sub, err := js.Subscribe("foo", func(msg *nats.Msg) { |
| 665 |
// Cause bottleneck by having channel block when full |
| 666 |
// because of work taking long. |
| 667 |
recvd <- msg |
| 668 |
}, nats.EnableFlowControl(), nats.IdleHeartbeat(5*time.Second)) |
| 669 |
|
| 670 |
if err != nil { |
| 671 |
t.Fatal(err) |
| 672 |
} |
| 673 |
defer sub.Unsubscribe() |
| 674 |
|
| 675 |
// Set this lower then normal to make sure we do not exceed bytes pending with FC turned on. |
| 676 |
sub.SetPendingLimits(totalMsgs, 4*1024*1024) // This matches server window for flowcontrol. |
| 677 |
|
| 678 |
info, err := sub.ConsumerInfo() |
| 679 |
if err != nil { |
| 680 |
t.Fatal(err) |
| 681 |
} |
| 682 |
if !info.Config.FlowControl { |
| 683 |
t.Fatal("Expected Flow Control to be enabled") |
| 684 |
} |
| 685 |
<-ctx.Done() |
| 686 |
|
| 687 |
got := len(delivered) |
| 688 |
expected := totalMsgs |
| 689 |
if got != expected { |
| 690 |
t.Errorf("Expected %d messages, got: %d", expected, got) |
| 691 |
} |
| 692 |
|
| 693 |
// Wait for a couple of heartbeats to arrive and confirm there is no error. |
| 694 |
select { |
| 695 |
case <-time.After(1 * time.Second): |
| 696 |
case err := <-errCh: |
| 697 |
t.Errorf("error handler: %v", err) |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
func TestNoRaceJetStreamChanSubscribeStall(t *testing.T) { |
| 702 |
accounts := `accounts: { |
| 703 |
JS: { |
| 704 |
jetstream: enabled |
| 705 |
users: [ {user: pc, password: foo} ] |
| 706 |
}, |
| 707 |
}` |
| 708 |
c := newTester(t) |
| 709 |
inst := c.CreateServer(t, true, |
| 710 |
testservice.WithAccounts(accounts), |
| 711 |
testservice.WithAuthorization("no_auth_user: pc"), |
| 712 |
testservice.WithSystemAccount(noSystemAccountBody()), |
| 713 |
) |
| 714 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 715 |
|
| 716 |
nc := dialInstance(t, inst) |
| 717 |
c.WaitForJetStream(t, nc) |
| 718 |
|
| 719 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 720 |
if err != nil { |
| 721 |
t.Fatalf("Unexpected error: %v", err) |
| 722 |
} |
| 723 |
|
| 724 |
if _, err = js.AddStream(&nats.StreamConfig{Name: "STALL"}); err != nil { |
| 725 |
t.Fatalf("Unexpected error: %v", err) |
| 726 |
} |
| 727 |
|
| 728 |
_, err = js.StreamInfo("STALL") |
| 729 |
if err != nil { |
| 730 |
t.Fatalf("stream lookup failed: %v", err) |
| 731 |
} |
| 732 |
|
| 733 |
msg := []byte(strings.Repeat("A", 512)) |
| 734 |
toSend := 100_000 |
| 735 |
for i := 0; i < toSend; i++ { |
| 736 |
// Use plain NATS here for speed. |
| 737 |
if _, err := js.PublishAsync("STALL", msg); err != nil { |
| 738 |
t.Fatalf("Unexpected error: %v", err) |
| 739 |
} |
| 740 |
} |
| 741 |
select { |
| 742 |
case <-js.PublishAsyncComplete(): |
| 743 |
case <-time.After(5 * time.Second): |
| 744 |
t.Fatalf("Timeout waiting for messages") |
| 745 |
} |
| 746 |
nc.Flush() |
| 747 |
|
| 748 |
batch := 100 |
| 749 |
msgs := make(chan *nats.Msg, batch-2) |
| 750 |
sub, err := js.ChanSubscribe("STALL", msgs, |
| 751 |
nats.Durable("dlc"), |
| 752 |
nats.EnableFlowControl(), |
| 753 |
nats.IdleHeartbeat(5*time.Second), |
| 754 |
nats.MaxAckPending(batch-2), |
| 755 |
) |
| 756 |
if err != nil { |
| 757 |
t.Fatalf("Unexpected error: %v", err) |
| 758 |
} |
| 759 |
defer sub.Unsubscribe() |
| 760 |
|
| 761 |
for received := 0; received < toSend; { |
| 762 |
select { |
| 763 |
case m := <-msgs: |
| 764 |
received++ |
| 765 |
meta, _ := m.Metadata() |
| 766 |
if meta.Sequence.Consumer != uint64(received) { |
| 767 |
t.Fatalf("Missed something, wanted %d but got %d", received, meta.Sequence.Consumer) |
| 768 |
} |
| 769 |
m.Ack() |
| 770 |
case <-time.After(time.Second): |
| 771 |
t.Fatalf("Timeout waiting for messages, last received was %d", received) |
| 772 |
} |
| 773 |
} |
| 774 |
} |