| 1 |
// Copyright 2020-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 |
"context" |
| 18 |
"crypto/rand" |
| 19 |
"encoding/json" |
| 20 |
"errors" |
| 21 |
"fmt" |
| 22 |
"reflect" |
| 23 |
"strconv" |
| 24 |
"strings" |
| 25 |
"sync" |
| 26 |
"sync/atomic" |
| 27 |
"testing" |
| 28 |
"time" |
| 29 |
|
| 30 |
"github.com/nats-io/nats.go" |
| 31 |
"github.com/nats-io/nuid" |
| 32 |
testservice "github.com/synadia-io/orbit.go/ntf-client" |
| 33 |
) |
| 34 |
|
| 35 |
func TestJetStreamNotEnabled(t *testing.T) { |
| 36 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 37 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 38 |
if err != nil { |
| 39 |
t.Fatalf("Unexpected error: %v", err) |
| 40 |
} |
| 41 |
if _, err := js.AccountInfo(); err != nats.ErrJetStreamNotEnabled { |
| 42 |
t.Fatalf("Did not get the proper error, got %v", err) |
| 43 |
} |
| 44 |
}) |
| 45 |
} |
| 46 |
|
| 47 |
func TestJetStreamErrors(t *testing.T) { |
| 48 |
t.Run("API error", func(t *testing.T) { |
| 49 |
c := newTester(t) |
| 50 |
accounts := `accounts: { |
| 51 |
JS: { |
| 52 |
jetstream: enabled |
| 53 |
users: [ {user: dlc, password: foo} ] |
| 54 |
}, |
| 55 |
IU: { |
| 56 |
users: [ {user: rip, password: bar} ] |
| 57 |
}, |
| 58 |
}` |
| 59 |
inst := c.CreateServer(t, true, |
| 60 |
testservice.WithAccounts(accounts), |
| 61 |
testservice.WithAuthorization("no_auth_user: rip"), |
| 62 |
testservice.WithSystemAccount(noSystemAccountBody()), |
| 63 |
) |
| 64 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 65 |
|
| 66 |
// wait for JS to be ready using the JS-enabled user |
| 67 |
jsNC := dialInstance(t, inst, nats.UserInfo("dlc", "foo")) |
| 68 |
c.WaitForJetStream(t, jsNC) |
| 69 |
jsNC.Close() |
| 70 |
|
| 71 |
nc := dialInstance(t, inst) |
| 72 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 73 |
if err != nil { |
| 74 |
t.Fatalf("Unexpected error: %v", err) |
| 75 |
} |
| 76 |
|
| 77 |
_, err = js.AccountInfo() |
| 78 |
// check directly to var (backwards compatible) |
| 79 |
if err != nats.ErrJetStreamNotEnabledForAccount { |
| 80 |
t.Fatalf("Did not get the proper error, got %v", err) |
| 81 |
} |
| 82 |
|
| 83 |
// matching via errors.Is |
| 84 |
if ok := errors.Is(err, nats.ErrJetStreamNotEnabledForAccount); !ok { |
| 85 |
t.Fatal("Expected ErrJetStreamNotEnabledForAccount") |
| 86 |
} |
| 87 |
|
| 88 |
// matching wrapped via error.Is |
| 89 |
err2 := fmt.Errorf("custom error: %w", nats.ErrJetStreamNotEnabledForAccount) |
| 90 |
if ok := errors.Is(err2, nats.ErrJetStreamNotEnabledForAccount); !ok { |
| 91 |
t.Fatal("Expected wrapped ErrJetStreamNotEnabled") |
| 92 |
} |
| 93 |
|
| 94 |
// via classic type assertion. |
| 95 |
jserr, ok := err.(nats.JetStreamError) |
| 96 |
if !ok { |
| 97 |
t.Fatal("Expected a JetStreamError") |
| 98 |
} |
| 99 |
expected := nats.JSErrCodeJetStreamNotEnabledForAccount |
| 100 |
if jserr.APIError().ErrorCode != expected { |
| 101 |
t.Fatalf("Expected: %v, got: %v", expected, jserr.APIError().ErrorCode) |
| 102 |
} |
| 103 |
if jserr.APIError() == nil { |
| 104 |
t.Fatal("Expected APIError") |
| 105 |
} |
| 106 |
|
| 107 |
// matching to interface via errors.As(...) |
| 108 |
var apierr nats.JetStreamError |
| 109 |
ok = errors.As(err, &apierr) |
| 110 |
if !ok { |
| 111 |
t.Fatal("Expected a JetStreamError") |
| 112 |
} |
| 113 |
if apierr.APIError() == nil { |
| 114 |
t.Fatal("Expected APIError") |
| 115 |
} |
| 116 |
if apierr.APIError().ErrorCode != expected { |
| 117 |
t.Fatalf("Expected: %v, got: %v", expected, apierr.APIError().ErrorCode) |
| 118 |
} |
| 119 |
expectedMessage := "nats: jetstream not enabled for account" |
| 120 |
if apierr.Error() != expectedMessage { |
| 121 |
t.Fatalf("Expected: %v, got: %v", expectedMessage, apierr.Error()) |
| 122 |
} |
| 123 |
|
| 124 |
// an APIError also implements the JetStreamError interface. |
| 125 |
var _ nats.JetStreamError = &nats.APIError{} |
| 126 |
|
| 127 |
// matching arbitrary custom error via errors.Is(...) |
| 128 |
customErr := &nats.APIError{ErrorCode: expected} |
| 129 |
if ok := errors.Is(customErr, nats.ErrJetStreamNotEnabledForAccount); !ok { |
| 130 |
t.Fatal("Expected wrapped ErrJetStreamNotEnabledForAccount") |
| 131 |
} |
| 132 |
customErr = &nats.APIError{ErrorCode: 1} |
| 133 |
if ok := errors.Is(customErr, nats.ErrJetStreamNotEnabledForAccount); ok { |
| 134 |
t.Fatal("Expected to not match ErrJetStreamNotEnabled") |
| 135 |
} |
| 136 |
var cerr nats.JetStreamError |
| 137 |
if ok := errors.As(customErr, &cerr); !ok { |
| 138 |
t.Fatal("Expected custom error to be a JetStreamError") |
| 139 |
} |
| 140 |
|
| 141 |
// matching to concrete type via errors.As(...) |
| 142 |
var aerr *nats.APIError |
| 143 |
ok = errors.As(err, &aerr) |
| 144 |
if !ok { |
| 145 |
t.Fatal("Expected an APIError") |
| 146 |
} |
| 147 |
if aerr.ErrorCode != expected { |
| 148 |
t.Fatalf("Expected: %v, got: %v", expected, aerr.ErrorCode) |
| 149 |
} |
| 150 |
expectedMessage = "nats: jetstream not enabled for account" |
| 151 |
if aerr.Error() != expectedMessage { |
| 152 |
t.Fatalf("Expected: %v, got: %v", expectedMessage, apierr.Error()) |
| 153 |
} |
| 154 |
}) |
| 155 |
|
| 156 |
t.Run("test non-api error", func(t *testing.T) { |
| 157 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 158 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 159 |
if err != nil { |
| 160 |
t.Fatalf("Unexpected error: %v", err) |
| 161 |
} |
| 162 |
|
| 163 |
// stream with empty name |
| 164 |
_, err = js.AddStream(&nats.StreamConfig{}) |
| 165 |
if err == nil { |
| 166 |
t.Fatalf("Expected error, got nil") |
| 167 |
} |
| 168 |
|
| 169 |
// check directly to var (backwards compatible) |
| 170 |
if err != nats.ErrStreamNameRequired { |
| 171 |
t.Fatalf("Expected: %v; got: %v", nats.ErrInvalidStreamName, err) |
| 172 |
} |
| 173 |
|
| 174 |
// matching via errors.Is |
| 175 |
if ok := errors.Is(err, nats.ErrStreamNameRequired); !ok { |
| 176 |
t.Fatalf("Expected: %v; got: %v", nats.ErrStreamNameRequired, err) |
| 177 |
} |
| 178 |
|
| 179 |
// matching wrapped via error.Is |
| 180 |
err2 := fmt.Errorf("custom error: %w", nats.ErrStreamNameRequired) |
| 181 |
if ok := errors.Is(err2, nats.ErrStreamNameRequired); !ok { |
| 182 |
t.Fatal("Expected wrapped ErrStreamNameRequired") |
| 183 |
} |
| 184 |
|
| 185 |
// via classic type assertion. |
| 186 |
jserr, ok := err.(nats.JetStreamError) |
| 187 |
if !ok { |
| 188 |
t.Fatal("Expected a JetStreamError") |
| 189 |
} |
| 190 |
if jserr.APIError() != nil { |
| 191 |
t.Fatalf("Expected: empty APIError; got: %v", jserr.APIError()) |
| 192 |
} |
| 193 |
|
| 194 |
// matching to interface via errors.As(...) |
| 195 |
var jserr2 nats.JetStreamError |
| 196 |
ok = errors.As(err, &jserr2) |
| 197 |
if !ok { |
| 198 |
t.Fatal("Expected a JetStreamError") |
| 199 |
} |
| 200 |
if jserr2.APIError() != nil { |
| 201 |
t.Fatalf("Expected: empty APIError; got: %v", jserr2.APIError()) |
| 202 |
} |
| 203 |
expectedMessage := "nats: stream name is required" |
| 204 |
if jserr2.Error() != expectedMessage { |
| 205 |
t.Fatalf("Expected: %v, got: %v", expectedMessage, jserr2.Error()) |
| 206 |
} |
| 207 |
|
| 208 |
// matching to concrete type via errors.As(...) |
| 209 |
var aerr *nats.APIError |
| 210 |
ok = errors.As(err, &aerr) |
| 211 |
if ok { |
| 212 |
t.Fatal("Expected ErrStreamNameRequired not to map to APIError") |
| 213 |
} |
| 214 |
}) |
| 215 |
}) |
| 216 |
} |
| 217 |
|
| 218 |
func TestJetStreamPublish(t *testing.T) { |
| 219 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 220 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 221 |
if err != nil { |
| 222 |
t.Fatalf("Unexpected error: %v", err) |
| 223 |
} |
| 224 |
|
| 225 |
// Make sure we get a proper failure when no stream is present. |
| 226 |
_, err = js.Publish("foo", []byte("Hello JS")) |
| 227 |
if err != nats.ErrNoStreamResponse { |
| 228 |
t.Fatalf("Expected a no stream error but got %v", err) |
| 229 |
} |
| 230 |
|
| 231 |
si, err := js.AddStream(&nats.StreamConfig{ |
| 232 |
Name: "TEST", |
| 233 |
Subjects: []string{"test", "foo", "bar"}, |
| 234 |
}) |
| 235 |
if err != nil { |
| 236 |
t.Fatalf("Unexpected error: %v", err) |
| 237 |
} |
| 238 |
// Double check that file-based storage is default. |
| 239 |
if si.Config.Storage != nats.FileStorage { |
| 240 |
t.Fatalf("Expected FileStorage as default, got %v", si.Config.Storage) |
| 241 |
} |
| 242 |
|
| 243 |
_, err = js.StreamInfo("TEST") |
| 244 |
if err != nil { |
| 245 |
t.Fatalf("stream lookup failed: %v", err) |
| 246 |
} |
| 247 |
|
| 248 |
var pa *nats.PubAck |
| 249 |
expect := func(seq, nmsgs uint64) { |
| 250 |
t.Helper() |
| 251 |
if seq > 0 && pa == nil { |
| 252 |
t.Fatalf("Missing pubAck to test sequence %d", seq) |
| 253 |
} |
| 254 |
if pa != nil { |
| 255 |
if pa.Stream != "TEST" { |
| 256 |
t.Fatalf("Wrong stream name, expected %q, got %q", "TEST", pa.Stream) |
| 257 |
} |
| 258 |
if seq > 0 && pa.Sequence != seq { |
| 259 |
t.Fatalf("Wrong stream sequence, expected %d, got %d", seq, pa.Sequence) |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
stream, err := js.StreamInfo("TEST") |
| 264 |
if err != nil { |
| 265 |
t.Fatalf("stream lookup failed: %v", err) |
| 266 |
} |
| 267 |
if stream.State.Msgs != nmsgs { |
| 268 |
t.Fatalf("Expected %d messages, got %d", nmsgs, stream.State.Msgs) |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
msg := []byte("Hello JS") |
| 273 |
|
| 274 |
// Basic publish like NATS core. |
| 275 |
pa, err = js.Publish("foo", msg) |
| 276 |
if err != nil { |
| 277 |
t.Fatalf("Unexpected publish error: %v", err) |
| 278 |
} |
| 279 |
expect(1, 1) |
| 280 |
|
| 281 |
// Test stream expectation. |
| 282 |
pa, err = js.Publish("foo", msg, nats.ExpectStream("ORDERS")) |
| 283 |
if err == nil || !strings.Contains(err.Error(), "stream does not match") { |
| 284 |
t.Fatalf("Expected an error, got %v", err) |
| 285 |
} |
| 286 |
// Test last sequence expectation. |
| 287 |
pa, err = js.Publish("foo", msg, nats.ExpectLastSequence(10)) |
| 288 |
if err == nil || !strings.Contains(err.Error(), "wrong last sequence") { |
| 289 |
t.Fatalf("Expected an error, got %v", err) |
| 290 |
} |
| 291 |
// Messages should have been rejected. |
| 292 |
expect(0, 1) |
| 293 |
|
| 294 |
// Using PublishMsg API and accessing directly the Header map. |
| 295 |
msg2 := nats.NewMsg("foo") |
| 296 |
msg2.Header[nats.ExpectedLastSeqHdr] = []string{"10"} |
| 297 |
pa, err = js.PublishMsg(msg2) |
| 298 |
if err == nil || !strings.Contains(err.Error(), "wrong last sequence") { |
| 299 |
t.Fatalf("Expected an error, got %v", err) |
| 300 |
} |
| 301 |
expect(0, 1) |
| 302 |
|
| 303 |
// Send in a stream with a msgId |
| 304 |
pa, err = js.Publish("foo", msg, nats.MsgId("ZZZ")) |
| 305 |
if err != nil { |
| 306 |
t.Fatalf("Unexpected publish error: %v", err) |
| 307 |
} |
| 308 |
expect(2, 2) |
| 309 |
|
| 310 |
// Send in the same message with same msgId. |
| 311 |
pa, err = js.Publish("foo", msg, nats.MsgId("ZZZ")) |
| 312 |
if err != nil { |
| 313 |
t.Fatalf("Unexpected publish error: %v", err) |
| 314 |
} |
| 315 |
if pa.Sequence != 2 { |
| 316 |
t.Fatalf("Expected sequence of 2, got %d", pa.Sequence) |
| 317 |
} |
| 318 |
if !pa.Duplicate { |
| 319 |
t.Fatalf("Expected duplicate to be set") |
| 320 |
} |
| 321 |
expect(2, 2) |
| 322 |
|
| 323 |
// Now try to send one in with the wrong last msgId. |
| 324 |
pa, err = js.Publish("foo", msg, nats.ExpectLastMsgId("AAA")) |
| 325 |
if err == nil || !strings.Contains(err.Error(), "wrong last msg") { |
| 326 |
t.Fatalf("Expected an error, got %v", err) |
| 327 |
} |
| 328 |
// Make sure expected sequence works. |
| 329 |
pa, err = js.Publish("foo", msg, nats.ExpectLastSequence(22)) |
| 330 |
if err == nil || !strings.Contains(err.Error(), "wrong last sequence") { |
| 331 |
t.Fatalf("Expected an error, got %v", err) |
| 332 |
} |
| 333 |
expect(0, 2) |
| 334 |
|
| 335 |
// This should work ok. |
| 336 |
pa, err = js.Publish("foo", msg, nats.ExpectLastSequence(2)) |
| 337 |
if err != nil { |
| 338 |
t.Fatalf("Unexpected publish error: %v", err) |
| 339 |
} |
| 340 |
expect(3, 3) |
| 341 |
|
| 342 |
// JetStream Headers are case-sensitive right now, |
| 343 |
// so this will not activate the check. |
| 344 |
msg3 := nats.NewMsg("foo") |
| 345 |
msg3.Header["nats-expected-last-sequence"] = []string{"4"} |
| 346 |
pa, err = js.PublishMsg(msg3) |
| 347 |
if err != nil { |
| 348 |
t.Fatalf("Expected an error, got %v", err) |
| 349 |
} |
| 350 |
expect(4, 4) |
| 351 |
|
| 352 |
// Now test context and timeouts. |
| 353 |
// Both set should fail. |
| 354 |
ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 355 |
defer cancel() |
| 356 |
|
| 357 |
_, err = js.Publish("foo", msg, nats.AckWait(time.Second), nats.Context(ctx)) |
| 358 |
if err != nats.ErrContextAndTimeout { |
| 359 |
t.Fatalf("Expected %q, got %q", nats.ErrContextAndTimeout, err) |
| 360 |
} |
| 361 |
|
| 362 |
// Create dummy listener for timeout and context tests. |
| 363 |
sub, err := nc.SubscribeSync("baz") |
| 364 |
if err != nil { |
| 365 |
t.Fatal(err) |
| 366 |
} |
| 367 |
defer sub.Unsubscribe() |
| 368 |
|
| 369 |
_, err = js.Publish("baz", msg, nats.AckWait(time.Nanosecond)) |
| 370 |
if err != nats.ErrTimeout { |
| 371 |
t.Fatalf("Expected %q, got %q", nats.ErrTimeout, err) |
| 372 |
} |
| 373 |
|
| 374 |
go cancel() |
| 375 |
_, err = js.Publish("baz", msg, nats.Context(ctx)) |
| 376 |
if err != context.Canceled { |
| 377 |
t.Fatalf("Expected %q, got %q", context.Canceled, err) |
| 378 |
} |
| 379 |
|
| 380 |
// Test ExpectLastSequencePerSubject. Just make sure that we set the header. |
| 381 |
sub, err = nc.SubscribeSync("test") |
| 382 |
if err != nil { |
| 383 |
t.Fatalf("Error on subscribe: %v", err) |
| 384 |
} |
| 385 |
js.Publish("test", []byte("msg"), nats.ExpectLastSequencePerSubject(1)) |
| 386 |
m, err := sub.NextMsg(time.Second) |
| 387 |
if err != nil { |
| 388 |
t.Fatalf("Error on next msg: %v", err) |
| 389 |
} |
| 390 |
if m.Header.Get(nats.ExpectedLastSubjSeqHdr) != "1" { |
| 391 |
t.Fatalf("Header ExpectLastSequencePerSubject not set: %+v", m.Header) |
| 392 |
} |
| 393 |
}) |
| 394 |
} |
| 395 |
|
| 396 |
func TestPublishWithTTL(t *testing.T) { |
| 397 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 398 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 399 |
if err != nil { |
| 400 |
t.Fatalf("Unexpected error: %v", err) |
| 401 |
} |
| 402 |
|
| 403 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 404 |
Name: "foo", Subjects: []string{"FOO.*"}, MaxMsgSize: 64, AllowMsgTTL: true}) |
| 405 |
if err != nil { |
| 406 |
t.Fatalf("Unexpected error: %v", err) |
| 407 |
} |
| 408 |
|
| 409 |
ack, err := js.Publish("FOO.1", []byte("msg"), nats.MsgTTL(1*time.Second)) |
| 410 |
if err != nil { |
| 411 |
t.Fatalf("Unexpected error: %v", err) |
| 412 |
} |
| 413 |
gotMsg, err := js.GetMsg("foo", ack.Sequence) |
| 414 |
if err != nil { |
| 415 |
t.Fatalf("Unexpected error: %v", err) |
| 416 |
} |
| 417 |
if ttl := gotMsg.Header.Get("Nats-TTL"); ttl != "1s" { |
| 418 |
t.Fatalf("Expected message to have TTL header set to 1s; got: %s", ttl) |
| 419 |
} |
| 420 |
time.Sleep(1500 * time.Millisecond) |
| 421 |
_, err = js.GetMsg("foo", ack.Sequence) |
| 422 |
if !errors.Is(err, nats.ErrMsgNotFound) { |
| 423 |
t.Fatalf("Expected not found error; got: %v", err) |
| 424 |
} |
| 425 |
}) |
| 426 |
} |
| 427 |
|
| 428 |
func TestMsgDeleteMarkerMaxAge(t *testing.T) { |
| 429 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 430 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 431 |
if err != nil { |
| 432 |
t.Fatalf("Unexpected error: %v", err) |
| 433 |
} |
| 434 |
|
| 435 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 436 |
Name: "foo", Subjects: []string{"FOO.*"}, AllowMsgTTL: true, SubjectDeleteMarkerTTL: 50 * time.Second, MaxAge: 1 * time.Second}) |
| 437 |
if err != nil { |
| 438 |
t.Fatalf("Unexpected error: %v", err) |
| 439 |
} |
| 440 |
|
| 441 |
_, err = js.Publish("FOO.1", []byte("msg1")) |
| 442 |
if err != nil { |
| 443 |
t.Fatalf("Unexpected error: %v", err) |
| 444 |
} |
| 445 |
time.Sleep(1500 * time.Millisecond) |
| 446 |
gotMsg, err := js.GetLastMsg("foo", "FOO.1") |
| 447 |
if err != nil { |
| 448 |
t.Fatalf("Unexpected error: %v", err) |
| 449 |
} |
| 450 |
if ttlMarker := gotMsg.Header.Get("Nats-Marker-Reason"); ttlMarker != "MaxAge" { |
| 451 |
t.Fatalf("Expected message to have Marker-Reason header set to MaxAge; got: %s", ttlMarker) |
| 452 |
} |
| 453 |
if ttl := gotMsg.Header.Get("Nats-TTL"); ttl != "50s" { |
| 454 |
t.Fatalf("Expected message to have Nats-TTL header set to 50s; got: %s", ttl) |
| 455 |
} |
| 456 |
}) |
| 457 |
} |
| 458 |
|
| 459 |
func TestPublishAsyncWithTTL(t *testing.T) { |
| 460 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 461 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 462 |
if err != nil { |
| 463 |
t.Fatalf("Unexpected error: %v", err) |
| 464 |
} |
| 465 |
|
| 466 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 467 |
Name: "foo", Subjects: []string{"FOO.*"}, MaxMsgSize: 64, AllowMsgTTL: true}) |
| 468 |
if err != nil { |
| 469 |
t.Fatalf("Unexpected error: %v", err) |
| 470 |
} |
| 471 |
|
| 472 |
paf, err := js.PublishAsync("FOO.1", []byte("msg"), nats.MsgTTL(1*time.Second)) |
| 473 |
if err != nil { |
| 474 |
t.Fatalf("Unexpected error: %v", err) |
| 475 |
} |
| 476 |
var ack *nats.PubAck |
| 477 |
select { |
| 478 |
case ack = <-paf.Ok(): |
| 479 |
case <-time.After(5 * time.Second): |
| 480 |
t.Fatalf("Did not receive ack") |
| 481 |
} |
| 482 |
|
| 483 |
gotMsg, err := js.GetMsg("foo", ack.Sequence) |
| 484 |
if err != nil { |
| 485 |
t.Fatalf("Unexpected error: %v", err) |
| 486 |
} |
| 487 |
if ttl := gotMsg.Header.Get("Nats-TTL"); ttl != "1s" { |
| 488 |
t.Fatalf("Expected message to have TTL header set to 1s; got: %s", ttl) |
| 489 |
} |
| 490 |
time.Sleep(1500 * time.Millisecond) |
| 491 |
_, err = js.GetMsg("foo", ack.Sequence) |
| 492 |
if !errors.Is(err, nats.ErrMsgNotFound) { |
| 493 |
t.Fatalf("Expected not found error; got: %v", err) |
| 494 |
} |
| 495 |
}) |
| 496 |
} |
| 497 |
|
| 498 |
func TestJetStreamSubscribe(t *testing.T) { |
| 499 |
withJSServerInstance(t, func(t *testing.T, nc *nats.Conn, inst *testservice.Instance) { |
| 500 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 501 |
if err != nil { |
| 502 |
t.Fatalf("Unexpected error: %v", err) |
| 503 |
} |
| 504 |
|
| 505 |
expectConsumers := func(t *testing.T, expected int) { |
| 506 |
t.Helper() |
| 507 |
checkFor(t, 2*time.Second, 15*time.Millisecond, func() error { |
| 508 |
var infos []*nats.ConsumerInfo |
| 509 |
for info := range js.Consumers("TEST") { |
| 510 |
infos = append(infos, info) |
| 511 |
} |
| 512 |
if len(infos) != expected { |
| 513 |
return fmt.Errorf("Expected %d consumers, got: %d", expected, len(infos)) |
| 514 |
} |
| 515 |
return nil |
| 516 |
}) |
| 517 |
} |
| 518 |
|
| 519 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 520 |
Name: "TEST", |
| 521 |
Subjects: []string{"foo", "bar", "baz", "foo.*"}, |
| 522 |
}) |
| 523 |
if err != nil { |
| 524 |
t.Fatalf("Unexpected error: %v", err) |
| 525 |
} |
| 526 |
|
| 527 |
_, err = js.StreamInfo("TEST") |
| 528 |
if err != nil { |
| 529 |
t.Fatalf("stream lookup failed: %v", err) |
| 530 |
} |
| 531 |
|
| 532 |
// If stream name is not specified, then the subject is required. |
| 533 |
if _, err := js.SubscribeSync(""); err == nil || !strings.Contains(err.Error(), "required") { |
| 534 |
t.Fatalf("Unexpected error: %v", err) |
| 535 |
} |
| 536 |
// Check that if stream name is present, then technically the subject does not have to. |
| 537 |
sub, err := js.SubscribeSync("", nats.BindStream("TEST")) |
| 538 |
if err != nil { |
| 539 |
t.Fatalf("Unexpected error: %v", err) |
| 540 |
} |
| 541 |
initialPending, err := sub.InitialConsumerPending() |
| 542 |
if err != nil { |
| 543 |
t.Fatalf("Unexpected error: %v", err) |
| 544 |
} |
| 545 |
if initialPending != 0 { |
| 546 |
t.Fatalf("Expected no initial pending, got %d", initialPending) |
| 547 |
} |
| 548 |
sub.Unsubscribe() |
| 549 |
|
| 550 |
// Check that Queue subscribe with HB or FC fails. |
| 551 |
_, err = js.QueueSubscribeSync("foo", "wq", nats.IdleHeartbeat(time.Second)) |
| 552 |
if err == nil || !strings.Contains(err.Error(), "heartbeat") { |
| 553 |
t.Fatalf("Unexpected error: %v", err) |
| 554 |
} |
| 555 |
_, err = js.QueueSubscribeSync("foo", "wq", nats.EnableFlowControl()) |
| 556 |
if err == nil || !strings.Contains(err.Error(), "flow control") { |
| 557 |
t.Fatalf("Unexpected error: %v", err) |
| 558 |
} |
| 559 |
|
| 560 |
// Check that Queue subscribe without durable name requires queue name |
| 561 |
// to not have "." in the name. |
| 562 |
_, err = js.QueueSubscribeSync("foo", "bar.baz") |
| 563 |
if err != nats.ErrInvalidConsumerName { |
| 564 |
t.Fatalf("Unexpected error: %v", err) |
| 565 |
} |
| 566 |
|
| 567 |
msg := []byte("Hello JS") |
| 568 |
|
| 569 |
js.Publish("foo", msg) |
| 570 |
|
| 571 |
q := make(chan *nats.Msg, 4) |
| 572 |
|
| 573 |
checkSub, err := nc.SubscribeSync("ivan") |
| 574 |
if err != nil { |
| 575 |
t.Fatalf("Error on sub: %v", err) |
| 576 |
} |
| 577 |
|
| 578 |
// Now create a simple ephemeral consumer. |
| 579 |
sub1, err := js.Subscribe("foo", func(m *nats.Msg) { |
| 580 |
q <- m |
| 581 |
}, nats.DeliverSubject("ivan")) |
| 582 |
if err != nil { |
| 583 |
t.Fatalf("Unexpected error: %v", err) |
| 584 |
} |
| 585 |
defer sub1.Unsubscribe() |
| 586 |
|
| 587 |
select { |
| 588 |
case m := <-q: |
| 589 |
if _, err := m.Metadata(); err != nil { |
| 590 |
t.Fatalf("Unexpected error: %v", err) |
| 591 |
} |
| 592 |
if _, err := checkSub.NextMsg(time.Second); err != nil { |
| 593 |
t.Fatal("Wrong deliver subject") |
| 594 |
} |
| 595 |
case <-time.After(5 * time.Second): |
| 596 |
t.Fatalf("Did not receive the messages in time") |
| 597 |
} |
| 598 |
|
| 599 |
sub2, err := js.SubscribeSync("foo") |
| 600 |
if err != nil { |
| 601 |
t.Fatalf("Unexpected error: %v", err) |
| 602 |
} |
| 603 |
defer sub2.Unsubscribe() |
| 604 |
|
| 605 |
waitForPending := func(t *testing.T, sub *nats.Subscription, n int) { |
| 606 |
t.Helper() |
| 607 |
timeout := time.Now().Add(2 * time.Second) |
| 608 |
for time.Now().Before(timeout) { |
| 609 |
if msgs, _, _ := sub.Pending(); msgs == n { |
| 610 |
return |
| 611 |
} |
| 612 |
time.Sleep(10 * time.Millisecond) |
| 613 |
} |
| 614 |
msgs, _, _ := sub.Pending() |
| 615 |
t.Fatalf("Expected to receive %d messages, but got %d", n, msgs) |
| 616 |
} |
| 617 |
|
| 618 |
waitForPending(t, sub2, 1) |
| 619 |
|
| 620 |
toSend := 10 |
| 621 |
for range toSend { |
| 622 |
js.Publish("bar", msg) |
| 623 |
} |
| 624 |
|
| 625 |
done := make(chan bool, 1) |
| 626 |
var received int |
| 627 |
sub3, err := js.Subscribe("bar", func(m *nats.Msg) { |
| 628 |
received++ |
| 629 |
if received == toSend { |
| 630 |
done <- true |
| 631 |
} |
| 632 |
}) |
| 633 |
|
| 634 |
if err != nil { |
| 635 |
t.Fatalf("Unexpected error: %v", err) |
| 636 |
} |
| 637 |
expectConsumers(t, 3) |
| 638 |
defer sub3.Unsubscribe() |
| 639 |
|
| 640 |
initialPending, err = sub3.InitialConsumerPending() |
| 641 |
if err != nil { |
| 642 |
t.Fatalf("Unexpected error: %v", err) |
| 643 |
} |
| 644 |
if initialPending != 10 { |
| 645 |
t.Fatalf("Expected initial pending of 10, got %d", initialPending) |
| 646 |
} |
| 647 |
select { |
| 648 |
case <-done: |
| 649 |
case <-time.After(5 * time.Second): |
| 650 |
t.Fatalf("Did not receive all of the messages in time") |
| 651 |
} |
| 652 |
|
| 653 |
// If we are here we have received all of the messages. |
| 654 |
// We hang the ConsumerInfo option off of the subscription, so we use that to check status. |
| 655 |
// We may need to retry this check since the acks sent by the client have to be processed |
| 656 |
// on the server. |
| 657 |
checkFor(t, 10*time.Second, 100*time.Millisecond, func() error { |
| 658 |
info, _ := sub3.ConsumerInfo() |
| 659 |
if info.Config.AckPolicy != nats.AckExplicitPolicy { |
| 660 |
t.Fatalf("Expected ack explicit policy, got %q", info.Config.AckPolicy) |
| 661 |
} |
| 662 |
if info.Delivered.Consumer != uint64(toSend) { |
| 663 |
return fmt.Errorf("Expected to have received all %d messages, got %d", toSend, info.Delivered.Consumer) |
| 664 |
} |
| 665 |
if info.AckFloor.Consumer != uint64(toSend) { |
| 666 |
return fmt.Errorf("Expected to have ack'd all %d messages, got ack floor of %d", toSend, info.AckFloor.Consumer) |
| 667 |
} |
| 668 |
return nil |
| 669 |
}) |
| 670 |
sub3.Unsubscribe() |
| 671 |
sub2.Unsubscribe() |
| 672 |
sub1.Unsubscribe() |
| 673 |
expectConsumers(t, 0) |
| 674 |
|
| 675 |
// durable |
| 676 |
dname := "derek" |
| 677 |
sub, err = js.SubscribeSync("foo", nats.Durable(dname)) |
| 678 |
if err != nil { |
| 679 |
t.Fatalf("Unexpected error: %v", err) |
| 680 |
} |
| 681 |
defer sub.Unsubscribe() |
| 682 |
expectConsumers(t, 1) |
| 683 |
|
| 684 |
info, _ := sub.ConsumerInfo() |
| 685 |
if info.Config.Durable != dname { |
| 686 |
t.Fatalf("Expected durable name to be set to %q, got %q", dname, info.Config.Durable) |
| 687 |
} |
| 688 |
deliver := info.Config.DeliverSubject |
| 689 |
|
| 690 |
// Drain subscription, this will delete the consumer. |
| 691 |
go func() { |
| 692 |
time.Sleep(250 * time.Millisecond) |
| 693 |
for { |
| 694 |
if _, err := sub.NextMsg(500 * time.Millisecond); err != nil { |
| 695 |
return |
| 696 |
} |
| 697 |
} |
| 698 |
}() |
| 699 |
sub.Drain() |
| 700 |
nc.Flush() |
| 701 |
expectConsumers(t, 0) |
| 702 |
|
| 703 |
sub, err = js.SubscribeSync("foo", nats.Durable(dname)) |
| 704 |
if err != nil { |
| 705 |
t.Fatalf("Unexpected error: %v", err) |
| 706 |
} |
| 707 |
if info, err := sub.ConsumerInfo(); err != nil || info.Config.DeliverSubject == deliver { |
| 708 |
t.Fatal("Expected delivery subject to be different") |
| 709 |
} |
| 710 |
expectConsumers(t, 1) |
| 711 |
|
| 712 |
// Subscribing again with same subject and durable name is an error. |
| 713 |
if _, err := js.SubscribeSync("foo", nats.Durable(dname)); err == nil { |
| 714 |
t.Fatal("Unexpected success") |
| 715 |
} |
| 716 |
expectConsumers(t, 1) |
| 717 |
|
| 718 |
sub.Unsubscribe() |
| 719 |
expectConsumers(t, 0) |
| 720 |
|
| 721 |
sub, err = js.SubscribeSync("foo", nats.Durable(dname)) |
| 722 |
if err != nil { |
| 723 |
t.Fatalf("Unexpected error: %v", err) |
| 724 |
} |
| 725 |
defer sub.Unsubscribe() |
| 726 |
expectConsumers(t, 1) |
| 727 |
|
| 728 |
if deliver == sub.Subject { |
| 729 |
t.Fatalf("Expected delivery subject to be different then %q", deliver) |
| 730 |
} |
| 731 |
sub.Unsubscribe() |
| 732 |
expectConsumers(t, 0) |
| 733 |
|
| 734 |
// queue group on bar (durable=queue name) |
| 735 |
sub1, err = js.QueueSubscribeSync("bar", "v0") |
| 736 |
if err != nil { |
| 737 |
t.Fatalf("Unexpected error: %v", err) |
| 738 |
} |
| 739 |
defer sub1.Unsubscribe() |
| 740 |
waitForPending(t, sub1, 10) |
| 741 |
expectConsumers(t, 1) |
| 742 |
|
| 743 |
if _, err = js.QueueSubscribeSync("baz", "v0"); err == nil { |
| 744 |
t.Fatal("Unexpected success") |
| 745 |
} |
| 746 |
|
| 747 |
if _, err = js.QueueSubscribeSync("bar", "v1", nats.Durable("v0")); err == nil { |
| 748 |
t.Fatal("Unexpected success") |
| 749 |
} |
| 750 |
sub2, err = js.QueueSubscribeSync("bar", "v0", nats.Durable("otherQueueDurable")) |
| 751 |
if err != nil { |
| 752 |
t.Fatalf("Unexpected error: %v", err) |
| 753 |
} |
| 754 |
defer sub2.Unsubscribe() |
| 755 |
waitForPending(t, sub2, 10) |
| 756 |
expectConsumers(t, 2) |
| 757 |
|
| 758 |
sub1.Unsubscribe() |
| 759 |
sub2.Unsubscribe() |
| 760 |
expectConsumers(t, 0) |
| 761 |
|
| 762 |
// Now try pull based subscribers. |
| 763 |
if _, err := js.Subscribe("bar", nil); err != nats.ErrBadSubscription { |
| 764 |
t.Fatalf("Expected an error trying to create subscriber with nil callback, got %v", err) |
| 765 |
} |
| 766 |
|
| 767 |
// Since v2.7.0, we can create pull consumers with ephemerals. |
| 768 |
sub, err = js.PullSubscribe("bar", "") |
| 769 |
if err != nil { |
| 770 |
t.Fatalf("Error on subscribe: %v", err) |
| 771 |
} |
| 772 |
sub.Unsubscribe() |
| 773 |
|
| 774 |
// Pull consumer with AckNone policy |
| 775 |
sub, err = js.PullSubscribe("bar", "", nats.AckNone()) |
| 776 |
if err != nil { |
| 777 |
t.Fatalf("Error on subscribe: %v", err) |
| 778 |
} |
| 779 |
sub.Unsubscribe() |
| 780 |
|
| 781 |
// Can't specify DeliverSubject for pull subscribers |
| 782 |
_, err = js.PullSubscribe("bar", "foo", nats.DeliverSubject("baz")) |
| 783 |
if err != nats.ErrPullSubscribeToPushConsumer { |
| 784 |
t.Fatalf("Unexpected error: %v", err) |
| 785 |
} |
| 786 |
// If stream name is not specified, need the subject. |
| 787 |
_, err = js.PullSubscribe("", "rip") |
| 788 |
if err == nil || !strings.Contains(err.Error(), "required") { |
| 789 |
t.Fatalf("Unexpected error: %v", err) |
| 790 |
} |
| 791 |
// If stream provided, it should be ok. |
| 792 |
sub, err = js.PullSubscribe("", "rip", nats.BindStream("TEST")) |
| 793 |
if err != nil { |
| 794 |
t.Fatalf("Unexpected error: %v", err) |
| 795 |
} |
| 796 |
sub.Unsubscribe() |
| 797 |
|
| 798 |
batch := 5 |
| 799 |
sub, err = js.PullSubscribe("bar", "rip") |
| 800 |
if err != nil { |
| 801 |
t.Fatalf("Unexpected error: %v", err) |
| 802 |
} |
| 803 |
expectConsumers(t, 1) |
| 804 |
|
| 805 |
bmsgs, err := sub.Fetch(batch) |
| 806 |
if err != nil { |
| 807 |
t.Fatal(err) |
| 808 |
} |
| 809 |
|
| 810 |
if info, _ := sub.ConsumerInfo(); info.NumAckPending != batch || info.NumPending != uint64(batch) { |
| 811 |
t.Fatalf("Expected %d pending ack, and %d still waiting to be delivered, got %d and %d", batch, batch, info.NumAckPending, info.NumPending) |
| 812 |
} |
| 813 |
|
| 814 |
// Now go ahead and consume these and ack, but not ack+next. |
| 815 |
for i := range batch { |
| 816 |
m := bmsgs[i] |
| 817 |
err = m.AckSync() |
| 818 |
if err != nil { |
| 819 |
t.Fatal(err) |
| 820 |
} |
| 821 |
} |
| 822 |
checkFor(t, time.Second, 15*time.Millisecond, func() error { |
| 823 |
if info, _ := sub.ConsumerInfo(); info.AckFloor.Consumer != uint64(batch) { |
| 824 |
return fmt.Errorf("Expected ack floor to be %d, got %d", batch, info.AckFloor.Consumer) |
| 825 |
} |
| 826 |
return nil |
| 827 |
}) |
| 828 |
waitForPending(t, sub, 0) |
| 829 |
|
| 830 |
bmsgs, err = sub.Fetch(10, nats.MaxWait(2*time.Second)) |
| 831 |
if err != nil { |
| 832 |
t.Errorf("Unexpected error: %v", err) |
| 833 |
} |
| 834 |
got := len(bmsgs) |
| 835 |
expected := 5 |
| 836 |
if got != expected { |
| 837 |
t.Errorf("Expected: %v, got: %v", expected, got) |
| 838 |
} |
| 839 |
|
| 840 |
for _, msg := range bmsgs { |
| 841 |
msg.AckSync() |
| 842 |
} |
| 843 |
|
| 844 |
_, err = js.PullSubscribe("baz", "rip") |
| 845 |
if err != nats.ErrSubjectMismatch { |
| 846 |
t.Fatalf("Expected a %q error but got %q", nats.ErrSubjectMismatch, err) |
| 847 |
} |
| 848 |
|
| 849 |
for range toSend { |
| 850 |
js.Publish("bar", msg) |
| 851 |
} |
| 852 |
|
| 853 |
sub, err = js.PullSubscribe("bar", "rip") |
| 854 |
if err != nil { |
| 855 |
t.Fatalf("Unexpected error: %v", err) |
| 856 |
} |
| 857 |
defer sub.Unsubscribe() |
| 858 |
expectConsumers(t, 1) |
| 859 |
|
| 860 |
expected = 5 |
| 861 |
bmsgs, err = sub.Fetch(expected, nats.MaxWait(2*time.Second)) |
| 862 |
if err != nil { |
| 863 |
t.Fatal(err) |
| 864 |
} |
| 865 |
|
| 866 |
got = len(bmsgs) |
| 867 |
if got != expected { |
| 868 |
t.Errorf("Expected: %v, got: %v", expected, got) |
| 869 |
} |
| 870 |
|
| 871 |
info, err = sub.ConsumerInfo() |
| 872 |
if err != nil { |
| 873 |
t.Fatalf("Unexpected error: %v", err) |
| 874 |
} |
| 875 |
if info.NumAckPending != batch || info.NumPending != uint64(toSend-batch) { |
| 876 |
t.Fatalf("Expected ack pending of %d and pending to be %d, got %d %d", batch, toSend-batch, info.NumAckPending, info.NumPending) |
| 877 |
} |
| 878 |
|
| 879 |
// Pull subscriptions can't use NextMsg variants. |
| 880 |
if _, err := sub.NextMsg(time.Second); err != nats.ErrTypeSubscription { |
| 881 |
t.Fatalf("Expected error %q, got %v", nats.ErrTypeSubscription, err) |
| 882 |
} |
| 883 |
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 884 |
defer cancel() |
| 885 |
if _, err := sub.NextMsgWithContext(ctx); err != nats.ErrTypeSubscription { |
| 886 |
t.Fatalf("Expected error %q, got %v", nats.ErrTypeSubscription, err) |
| 887 |
} |
| 888 |
cancel() |
| 889 |
|
| 890 |
if _, err := js.SubscribeSync("baz", nats.Durable("test.durable")); err != nats.ErrInvalidConsumerName { |
| 891 |
t.Fatalf("Expected invalid durable name error") |
| 892 |
} |
| 893 |
|
| 894 |
ackWait := 1 * time.Millisecond |
| 895 |
sub, err = js.SubscribeSync("bar", nats.Durable("ack-wait"), nats.AckWait(ackWait)) |
| 896 |
if err != nil { |
| 897 |
t.Fatalf("Unexpected error: %v", err) |
| 898 |
} |
| 899 |
expectConsumers(t, 2) |
| 900 |
|
| 901 |
_, err = sub.NextMsg(1 * time.Second) |
| 902 |
if err != nil { |
| 903 |
t.Fatalf("Unexpected error: %v", err) |
| 904 |
} |
| 905 |
info, err = sub.ConsumerInfo() |
| 906 |
if err != nil { |
| 907 |
t.Fatalf("Unexpected error: %v", err) |
| 908 |
} |
| 909 |
if info.Config.AckWait != ackWait { |
| 910 |
t.Errorf("Expected %v, got %v", ackWait, info.Config.AckWait) |
| 911 |
} |
| 912 |
|
| 913 |
sub, err = js.SubscribeSync("bar", nats.Durable("consumer-name")) |
| 914 |
if err != nil { |
| 915 |
t.Fatalf("Unexpected error: %v", err) |
| 916 |
} |
| 917 |
expectConsumers(t, 3) |
| 918 |
m, err := sub.NextMsg(1 * time.Second) |
| 919 |
if err != nil { |
| 920 |
t.Fatalf("Unexpected error: %v", err) |
| 921 |
} |
| 922 |
meta, err := m.Metadata() |
| 923 |
if err != nil { |
| 924 |
t.Fatal(err) |
| 925 |
} |
| 926 |
if meta.Stream != "TEST" { |
| 927 |
t.Fatalf("Unexpected stream name, got: %v", meta.Stream) |
| 928 |
} |
| 929 |
if meta.Consumer != "consumer-name" { |
| 930 |
t.Fatalf("Unexpected consumer name, got: %v", meta.Consumer) |
| 931 |
} |
| 932 |
|
| 933 |
qsubDurable := nats.Durable("qdur-chan") |
| 934 |
mch := make(chan *nats.Msg, 16536) |
| 935 |
sub, err = js.ChanQueueSubscribe("bar", "v1", mch, qsubDurable) |
| 936 |
if err != nil { |
| 937 |
t.Fatalf("Unexpected error: %v", err) |
| 938 |
} |
| 939 |
defer sub.Unsubscribe() |
| 940 |
expectConsumers(t, 4) |
| 941 |
|
| 942 |
var a, b *nats.MsgMetadata |
| 943 |
select { |
| 944 |
case msg := <-mch: |
| 945 |
meta, err := msg.Metadata() |
| 946 |
if err != nil { |
| 947 |
t.Error(err) |
| 948 |
} |
| 949 |
a = meta |
| 950 |
case <-time.After(2 * time.Second): |
| 951 |
t.Errorf("Timeout waiting for message") |
| 952 |
} |
| 953 |
|
| 954 |
mch2 := make(chan *nats.Msg, 16536) |
| 955 |
sub, err = js.ChanQueueSubscribe("bar", "v1", mch2, qsubDurable) |
| 956 |
if err != nil { |
| 957 |
t.Fatalf("Unexpected error: %v", err) |
| 958 |
} |
| 959 |
defer sub.Unsubscribe() |
| 960 |
expectConsumers(t, 4) |
| 961 |
|
| 962 |
for range toSend { |
| 963 |
js.Publish("bar", msg) |
| 964 |
} |
| 965 |
|
| 966 |
select { |
| 967 |
case msg := <-mch2: |
| 968 |
meta, err := msg.Metadata() |
| 969 |
if err != nil { |
| 970 |
t.Error(err) |
| 971 |
} |
| 972 |
b = meta |
| 973 |
case <-time.After(2 * time.Second): |
| 974 |
t.Errorf("Timeout waiting for message") |
| 975 |
} |
| 976 |
if reflect.DeepEqual(a, b) { |
| 977 |
t.Errorf("Expected to receive different messages in stream") |
| 978 |
} |
| 979 |
|
| 980 |
expectConsumers(t, 4) |
| 981 |
|
| 982 |
sub, err = js.SubscribeSync("foo", nats.InactiveThreshold(-100*time.Millisecond)) |
| 983 |
if err == nil || !strings.Contains(err.Error(), "invalid InactiveThreshold") { |
| 984 |
t.Fatalf("Expected error about invalid option, got %v", err) |
| 985 |
} |
| 986 |
|
| 987 |
sub, err = js.SubscribeSync("foo", nats.InactiveThreshold(50*time.Millisecond)) |
| 988 |
if err != nil { |
| 989 |
t.Fatalf("Error on subscribe: %v", err) |
| 990 |
} |
| 991 |
ci, err := sub.ConsumerInfo() |
| 992 |
if err != nil { |
| 993 |
t.Fatalf("Error on consumer info: %v", err) |
| 994 |
} |
| 995 |
name := ci.Name |
| 996 |
nc.Close() |
| 997 |
|
| 998 |
time.Sleep(150 * time.Millisecond) |
| 999 |
|
| 1000 |
nc2 := dialInstance(t, inst) |
| 1001 |
defer nc2.Close() |
| 1002 |
js2, err := nc2.JetStream(nats.MaxWait(10 * time.Second)) |
| 1003 |
if err != nil { |
| 1004 |
t.Fatalf("Unexpected error: %v", err) |
| 1005 |
} |
| 1006 |
|
| 1007 |
if ci, err := js2.ConsumerInfo("TEST", name); err == nil { |
| 1008 |
t.Fatalf("Expected no consumer to exist, got %+v", ci) |
| 1009 |
} |
| 1010 |
}) |
| 1011 |
} |
| 1012 |
|
| 1013 |
func TestJetStreamSubscribe_SkipConsumerLookup(t *testing.T) { |
| 1014 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 1015 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 1016 |
if err != nil { |
| 1017 |
t.Fatalf("Unexpected error: %v", err) |
| 1018 |
} |
| 1019 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 1020 |
Name: "TEST", |
| 1021 |
Subjects: []string{"foo"}, |
| 1022 |
}) |
| 1023 |
if err != nil { |
| 1024 |
t.Fatalf("Unexpected error: %v", err) |
| 1025 |
} |
| 1026 |
|
| 1027 |
_, err = js.AddConsumer("TEST", &nats.ConsumerConfig{ |
| 1028 |
Name: "cons", |
| 1029 |
DeliverSubject: "_INBOX.foo", |
| 1030 |
AckPolicy: nats.AckExplicitPolicy, |
| 1031 |
}) |
| 1032 |
if err != nil { |
| 1033 |
t.Fatalf("Unexpected error: %v", err) |
| 1034 |
} |
| 1035 |
|
| 1036 |
// for checking whether subscribe looks up the consumer |
| 1037 |
infoSub, err := nc.SubscribeSync("$JS.API.CONSUMER.INFO.TEST.*") |
| 1038 |
if err != nil { |
| 1039 |
t.Fatalf("Unexpected error: %v", err) |
| 1040 |
} |
| 1041 |
defer infoSub.Unsubscribe() |
| 1042 |
|
| 1043 |
// for checking whether subscribe creates the consumer |
| 1044 |
createConsSub, err := nc.SubscribeSync("$JS.API.CONSUMER.CREATE.>") |
| 1045 |
if err != nil { |
| 1046 |
t.Fatalf("Unexpected error: %v", err) |
| 1047 |
} |
| 1048 |
defer createConsSub.Unsubscribe() |
| 1049 |
t.Run("use Bind to skip consumer lookup and create", func(t *testing.T) { |
| 1050 |
sub, err := js.SubscribeSync("", nats.Bind("TEST", "cons"), nats.SkipConsumerLookup(), nats.DeliverSubject("_INBOX.foo")) |
| 1051 |
if err != nil { |
| 1052 |
t.Fatalf("Unexpected error: %v", err) |
| 1053 |
} |
| 1054 |
defer sub.Unsubscribe() |
| 1055 |
if msg, err := infoSub.NextMsg(50 * time.Millisecond); err == nil { |
| 1056 |
t.Fatalf("Expected to skip consumer lookup; got message on %q", msg.Subject) |
| 1057 |
} |
| 1058 |
|
| 1059 |
if msg, err := createConsSub.NextMsg(50 * time.Millisecond); err == nil { |
| 1060 |
t.Fatalf("Expected to skip consumer create; got message on %q", msg.Subject) |
| 1061 |
} |
| 1062 |
if _, err := js.Publish("foo", []byte("msg")); err != nil { |
| 1063 |
t.Fatalf("Unexpected error: %s", err) |
| 1064 |
} |
| 1065 |
if _, err := sub.NextMsg(100 * time.Millisecond); err != nil { |
| 1066 |
t.Fatalf("Expected to receive msg; got: %s", err) |
| 1067 |
} |
| 1068 |
}) |
| 1069 |
t.Run("use Durable, skip consumer lookup but overwrite the consumer", func(t *testing.T) { |
| 1070 |
sub, err := js.SubscribeSync("foo", nats.Durable("cons"), nats.SkipConsumerLookup(), nats.DeliverSubject("_INBOX.foo")) |
| 1071 |
if err != nil { |
| 1072 |
t.Fatalf("Unexpected error: %v", err) |
| 1073 |
} |
| 1074 |
if msg, err := infoSub.NextMsg(50 * time.Millisecond); err == nil { |
| 1075 |
t.Fatalf("Expected to skip consumer lookup; got message on %q", msg.Subject) |
| 1076 |
} |
| 1077 |
|
| 1078 |
if _, err := createConsSub.NextMsg(50 * time.Millisecond); err != nil { |
| 1079 |
t.Fatalf("Expected consumer create; got: %s", err) |
| 1080 |
} |
| 1081 |
if _, err := js.Publish("foo", []byte("msg")); err != nil { |
| 1082 |
t.Fatalf("Unexpected error: %s", err) |
| 1083 |
} |
| 1084 |
if _, err := sub.NextMsg(100 * time.Millisecond); err != nil { |
| 1085 |
t.Fatalf("Expected to receive msg; got: %s", err) |
| 1086 |
} |
| 1087 |
}) |
| 1088 |
t.Run("create new consumer with Durable, skip lookup", func(t *testing.T) { |
| 1089 |
sub, err := js.SubscribeSync("foo", nats.Durable("pp"), nats.SkipConsumerLookup(), nats.DeliverSubject("_INBOX.foo1")) |
| 1090 |
if err != nil { |
| 1091 |
t.Fatalf("Unexpected error: %v", err) |
| 1092 |
} |
| 1093 |
defer sub.Unsubscribe() |
| 1094 |
if msg, err := infoSub.NextMsg(50 * time.Millisecond); err == nil { |
| 1095 |
t.Fatalf("Expected to skip consumer lookup; got message on %q", msg.Subject) |
| 1096 |
} |
| 1097 |
|
| 1098 |
if _, err := createConsSub.NextMsg(50 * time.Millisecond); err != nil { |
| 1099 |
t.Fatalf("Expected consumer create; got: %s", err) |
| 1100 |
} |
| 1101 |
if _, err := js.Publish("foo", []byte("msg")); err != nil { |
| 1102 |
t.Fatalf("Unexpected error: %s", err) |
| 1103 |
} |
| 1104 |
if _, err := sub.NextMsg(100 * time.Millisecond); err != nil { |
| 1105 |
t.Fatalf("Expected to receive msg; got: %s", err) |
| 1106 |
} |
| 1107 |
}) |
| 1108 |
t.Run("create new consumer with ConsumerName, skip lookup", func(t *testing.T) { |
| 1109 |
sub, err := js.SubscribeSync("foo", nats.ConsumerName("pp"), nats.SkipConsumerLookup(), nats.DeliverSubject("_INBOX.foo1")) |
| 1110 |
if err != nil { |
| 1111 |
t.Fatalf("Unexpected error: %v", err) |
| 1112 |
} |
| 1113 |
defer sub.Unsubscribe() |
| 1114 |
if msg, err := infoSub.NextMsg(50 * time.Millisecond); err == nil { |
| 1115 |
t.Fatalf("Expected to skip consumer lookup; got message on %q", msg.Subject) |
| 1116 |
} |
| 1117 |
|
| 1118 |
if _, err := createConsSub.NextMsg(50 * time.Millisecond); err != nil { |
| 1119 |
t.Fatalf("Expected consumer create; got: %s", err) |
| 1120 |
} |
| 1121 |
if _, err := js.Publish("foo", []byte("msg")); err != nil { |
| 1122 |
t.Fatalf("Unexpected error: %s", err) |
| 1123 |
} |
| 1124 |
if _, err := sub.NextMsg(100 * time.Millisecond); err != nil { |
| 1125 |
t.Fatalf("Expected to receive msg; got: %s", err) |
| 1126 |
} |
| 1127 |
}) |
| 1128 |
|
| 1129 |
t.Run("create ephemeral consumer, SkipConsumerLookup has no effect", func(t *testing.T) { |
| 1130 |
sub, err := js.SubscribeSync("foo", nats.SkipConsumerLookup(), nats.DeliverSubject("_INBOX.foo2")) |
| 1131 |
if err != nil { |
| 1132 |
t.Fatalf("Unexpected error: %v", err) |
| 1133 |
} |
| 1134 |
defer sub.Unsubscribe() |
| 1135 |
if msg, err := infoSub.NextMsg(50 * time.Millisecond); err == nil { |
| 1136 |
t.Fatalf("Expected to skip consumer lookup; got message on %q", msg.Subject) |
| 1137 |
} |
| 1138 |
|
| 1139 |
if _, err := createConsSub.NextMsg(50 * time.Millisecond); err != nil { |
| 1140 |
t.Fatalf("Expected consumer create; got: %s", err) |
| 1141 |
} |
| 1142 |
if _, err := js.Publish("foo", []byte("msg")); err != nil { |
| 1143 |
t.Fatalf("Unexpected error: %s", err) |
| 1144 |
} |
| 1145 |
if _, err := sub.NextMsg(100 * time.Millisecond); err != nil { |
| 1146 |
t.Fatalf("Expected to receive msg; got: %s", err) |
| 1147 |
} |
| 1148 |
}) |
| 1149 |
t.Run("attempt to update ack policy of existing consumer", func(t *testing.T) { |
| 1150 |
_, err := js.SubscribeSync("foo", nats.Durable("cons"), nats.SkipConsumerLookup(), nats.DeliverSubject("_INBOX.foo"), nats.AckAll()) |
| 1151 |
if err == nil || !strings.Contains(err.Error(), "ack policy can not be updated") { |
| 1152 |
t.Fatalf("Expected update consumer error, got: %v", err) |
| 1153 |
} |
| 1154 |
}) |
| 1155 |
}) |
| 1156 |
} |
| 1157 |
|
| 1158 |
func TestPullSubscribeFetchWithHeartbeat(t *testing.T) { |
| 1159 |
t.Skip("Since v2.10.26 server sends no responders if the consumer is deleted, we need to figure out how else to test missing heartbeats") |
| 1160 |
} |
| 1161 |
|
| 1162 |
func TestPullSubscribeConsumerDoesNotExist(t *testing.T) { |
| 1163 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 1164 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 1165 |
if err != nil { |
| 1166 |
t.Fatalf("Unexpected error: %v", err) |
| 1167 |
} |
| 1168 |
|
| 1169 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 1170 |
Name: "TEST", |
| 1171 |
Subjects: []string{"foo"}, |
| 1172 |
}) |
| 1173 |
if err != nil { |
| 1174 |
t.Fatalf("Unexpected error: %v", err) |
| 1175 |
} |
| 1176 |
|
| 1177 |
sub, err := js.PullSubscribe("foo", "") |
| 1178 |
if err != nil { |
| 1179 |
t.Fatalf("Unexpected error: %s", err) |
| 1180 |
} |
| 1181 |
defer sub.Unsubscribe() |
| 1182 |
|
| 1183 |
info, err := sub.ConsumerInfo() |
| 1184 |
if err != nil { |
| 1185 |
t.Fatalf("Unexpected error: %v", err) |
| 1186 |
} |
| 1187 |
if err := js.DeleteConsumer("TEST", info.Name); err != nil { |
| 1188 |
t.Fatalf("Unexpected error: %v", err) |
| 1189 |
} |
| 1190 |
_, err = sub.Fetch(5) |
| 1191 |
if !errors.Is(err, nats.ErrNoResponders) { |
| 1192 |
t.Fatalf("Expected no responders error; got: %v", err) |
| 1193 |
} |
| 1194 |
|
| 1195 |
msgs, err := sub.FetchBatch(5) |
| 1196 |
if err != nil { |
| 1197 |
t.Fatalf("Unexpected error: %v", err) |
| 1198 |
} |
| 1199 |
select { |
| 1200 |
case _, ok := <-msgs.Messages(): |
| 1201 |
if ok { |
| 1202 |
t.Fatalf("Expected no messages") |
| 1203 |
} |
| 1204 |
case <-time.After(time.Second): |
| 1205 |
t.Fatalf("Timeout waiting for messages") |
| 1206 |
} |
| 1207 |
|
| 1208 |
if !errors.Is(msgs.Error(), nats.ErrNoResponders) { |
| 1209 |
t.Fatalf("Expected no responders error; got: %v", msgs.Error()) |
| 1210 |
} |
| 1211 |
}) |
| 1212 |
} |
| 1213 |
|
| 1214 |
func TestPullSubscribeFetchDrain(t *testing.T) { |
| 1215 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 1216 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 1217 |
if err != nil { |
| 1218 |
t.Fatalf("Unexpected error: %v", err) |
| 1219 |
} |
| 1220 |
|
| 1221 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 1222 |
Name: "TEST", |
| 1223 |
Subjects: []string{"foo"}, |
| 1224 |
}) |
| 1225 |
if err != nil { |
| 1226 |
t.Fatalf("Unexpected error: %v", err) |
| 1227 |
} |
| 1228 |
|
| 1229 |
defer js.PurgeStream("TEST") |
| 1230 |
sub, err := js.PullSubscribe("foo", "") |
| 1231 |
if err != nil { |
| 1232 |
t.Fatalf("Unexpected error: %s", err) |
| 1233 |
} |
| 1234 |
for range 100 { |
| 1235 |
if _, err := js.Publish("foo", []byte("msg")); err != nil { |
| 1236 |
t.Fatalf("Unexpected error: %s", err) |
| 1237 |
} |
| 1238 |
} |
| 1239 |
// fill buffer with messages |
| 1240 |
cinfo, err := sub.ConsumerInfo() |
| 1241 |
if err != nil { |
| 1242 |
t.Fatalf("Unexpected error: %s", err) |
| 1243 |
} |
| 1244 |
nextSubject := fmt.Sprintf("$JS.API.CONSUMER.MSG.NEXT.TEST.%s", cinfo.Name) |
| 1245 |
replySubject := strings.Replace(sub.Subject, "*", "abc", 1) |
| 1246 |
payload := `{"batch":10,"no_wait":true}` |
| 1247 |
if err := nc.PublishRequest(nextSubject, replySubject, []byte(payload)); err != nil { |
| 1248 |
t.Fatalf("Unexpected error: %s", err) |
| 1249 |
} |
| 1250 |
time.Sleep(100 * time.Millisecond) |
| 1251 |
|
| 1252 |
sub.Drain() |
| 1253 |
msgs, err := sub.Fetch(100) |
| 1254 |
if err != nil { |
| 1255 |
t.Fatalf("Unexpected error: %s", err) |
| 1256 |
} |
| 1257 |
for _, msg := range msgs { |
| 1258 |
msg.Ack() |
| 1259 |
} |
| 1260 |
if len(msgs) != 10 { |
| 1261 |
t.Fatalf("Expected %d messages; got: %d", 10, len(msgs)) |
| 1262 |
} |
| 1263 |
|
| 1264 |
_, err = sub.Fetch(10, nats.MaxWait(100*time.Millisecond)) |
| 1265 |
if !errors.Is(err, nats.ErrSubscriptionClosed) { |
| 1266 |
t.Fatalf("Expected error: %s; got: %s", nats.ErrSubscriptionClosed, err) |
| 1267 |
} |
| 1268 |
}) |
| 1269 |
} |
| 1270 |
|
| 1271 |
func TestPullSubscribeFetchBatchWithHeartbeat(t *testing.T) { |
| 1272 |
t.Skip("Since v2.10.26 server sends no responders if the consumer is deleted, we need to figure out how else to test missing heartbeats") |
| 1273 |
} |
| 1274 |
|
| 1275 |
func TestPullSubscribeFetchBatch(t *testing.T) { |
| 1276 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 1277 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 1278 |
if err != nil { |
| 1279 |
t.Fatalf("Unexpected error: %v", err) |
| 1280 |
} |
| 1281 |
|
| 1282 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 1283 |
Name: "TEST", |
| 1284 |
Subjects: []string{"foo"}, |
| 1285 |
}) |
| 1286 |
if err != nil { |
| 1287 |
t.Fatalf("Unexpected error: %v", err) |
| 1288 |
} |
| 1289 |
|
| 1290 |
t.Run("basic fetch", func(t *testing.T) { |
| 1291 |
defer js.PurgeStream("TEST") |
| 1292 |
sub, err := js.PullSubscribe("foo", "") |
| 1293 |
if err != nil { |
| 1294 |
t.Fatalf("Unexpected error: %s", err) |
| 1295 |
} |
| 1296 |
for range 5 { |
| 1297 |
if _, err := js.Publish("foo", []byte("msg")); err != nil { |
| 1298 |
t.Fatalf("Unexpected error: %s", err) |
| 1299 |
} |
| 1300 |
} |
| 1301 |
res, err := sub.FetchBatch(10) |
| 1302 |
if err != nil { |
| 1303 |
t.Fatalf("Unexpected error: %s", err) |
| 1304 |
} |
| 1305 |
go func() { |
| 1306 |
time.Sleep(10 * time.Millisecond) |
| 1307 |
for range 5 { |
| 1308 |
js.Publish("foo", []byte("msg")) |
| 1309 |
} |
| 1310 |
}() |
| 1311 |
msgs := make([]*nats.Msg, 0) |
| 1312 |
for msg := range res.Messages() { |
| 1313 |
msgs = append(msgs, msg) |
| 1314 |
} |
| 1315 |
if res.Error() != nil { |
| 1316 |
t.Fatalf("Unexpected error: %s", res.Error()) |
| 1317 |
} |
| 1318 |
if len(msgs) != 10 { |
| 1319 |
t.Fatalf("Expected %d messages; got: %d", 10, len(msgs)) |
| 1320 |
} |
| 1321 |
}) |
| 1322 |
|
| 1323 |
t.Run("multiple concurrent fetches", func(t *testing.T) { |
| 1324 |
defer js.PurgeStream("TEST") |
| 1325 |
sub, err := js.PullSubscribe("foo", "") |
| 1326 |
if err != nil { |
| 1327 |
t.Fatalf("Unexpected error: %s", err) |
| 1328 |
} |
| 1329 |
for range 50 { |
| 1330 |
if _, err := js.Publish("foo", []byte("msg")); err != nil { |
| 1331 |
t.Fatalf("Unexpected error: %s", err) |
| 1332 |
} |
| 1333 |
} |
| 1334 |
var r1, r2, r3 nats.MessageBatch |
| 1335 |
started := &sync.WaitGroup{} |
| 1336 |
started.Add(3) |
| 1337 |
errs := make(chan error, 3) |
| 1338 |
go func() { |
| 1339 |
var err error |
| 1340 |
r1, err = sub.FetchBatch(10) |
| 1341 |
if err != nil { |
| 1342 |
errs <- err |
| 1343 |
} |
| 1344 |
started.Done() |
| 1345 |
}() |
| 1346 |
go func() { |
| 1347 |
var err error |
| 1348 |
r2, err = sub.FetchBatch(10) |
| 1349 |
if err != nil { |
| 1350 |
errs <- err |
| 1351 |
} |
| 1352 |
started.Done() |
| 1353 |
}() |
| 1354 |
go func() { |
| 1355 |
var err error |
| 1356 |
r3, err = sub.FetchBatch(10) |
| 1357 |
if err != nil { |
| 1358 |
errs <- err |
| 1359 |
} |
| 1360 |
started.Done() |
| 1361 |
}() |
| 1362 |
started.Wait() |
| 1363 |
|
| 1364 |
select { |
| 1365 |
case err := <-errs: |
| 1366 |
t.Fatalf("Error initializing fetch: %s", err) |
| 1367 |
default: |
| 1368 |
} |
| 1369 |
|
| 1370 |
var msgsReceived int |
| 1371 |
for msgsReceived < 30 { |
| 1372 |
select { |
| 1373 |
case <-r1.Messages(): |
| 1374 |
msgsReceived++ |
| 1375 |
case <-r2.Messages(): |
| 1376 |
msgsReceived++ |
| 1377 |
case <-r3.Messages(): |
| 1378 |
msgsReceived++ |
| 1379 |
case <-time.After(1 * time.Second): |
| 1380 |
t.Fatalf("Timeout waiting for incoming messages") |
| 1381 |
} |
| 1382 |
} |
| 1383 |
select { |
| 1384 |
case <-r1.Done(): |
| 1385 |
case <-time.After(1 * time.Second): |
| 1386 |
t.Fatalf("FetchBatch result channel should be closed after receiving all messages on r1") |
| 1387 |
} |
| 1388 |
select { |
| 1389 |
case <-r2.Done(): |
| 1390 |
case <-time.After(1 * time.Second): |
| 1391 |
t.Fatalf("FetchBatch result channel should be closed after receiving all messages on r2") |
| 1392 |
} |
| 1393 |
select { |
| 1394 |
case <-r3.Done(): |
| 1395 |
case <-time.After(1 * time.Second): |
| 1396 |
t.Fatalf("FetchBatch result channel should be closed after receiving all messages on r3") |
| 1397 |
} |
| 1398 |
if r1.Error() != nil { |
| 1399 |
t.Fatalf("Unexpected error: %s", r1.Error()) |
| 1400 |
} |
| 1401 |
if r2.Error() != nil { |
| 1402 |
t.Fatalf("Unexpected error: %s", r2.Error()) |
| 1403 |
} |
| 1404 |
if r3.Error() != nil { |
| 1405 |
t.Fatalf("Unexpected error: %s", r3.Error()) |
| 1406 |
} |
| 1407 |
if msgsReceived != 30 { |
| 1408 |
t.Fatalf("Expected %d messages; got: %d", 30, msgsReceived) |
| 1409 |
} |
| 1410 |
}) |
| 1411 |
|
| 1412 |
t.Run("deliver all, then consume", func(t *testing.T) { |
| 1413 |
defer js.PurgeStream("TEST") |
| 1414 |
sub, err := js.PullSubscribe("foo", "") |
| 1415 |
if err != nil { |
| 1416 |
t.Fatalf("Unexpected error: %s", err) |
| 1417 |
} |
| 1418 |
for range 5 { |
| 1419 |
if _, err := js.Publish("foo", []byte("msg")); err != nil { |
| 1420 |
t.Fatalf("Unexpected error: %s", err) |
| 1421 |
} |
| 1422 |
} |
| 1423 |
res, err := sub.FetchBatch(5) |
| 1424 |
if err != nil { |
| 1425 |
t.Fatalf("Unexpected error: %s", err) |
| 1426 |
} |
| 1427 |
time.Sleep(10 * time.Millisecond) |
| 1428 |
msgs := make([]*nats.Msg, 0) |
| 1429 |
for msg := range res.Messages() { |
| 1430 |
msgs = append(msgs, msg) |
| 1431 |
} |
| 1432 |
if res.Error() != nil { |
| 1433 |
t.Fatalf("Unexpected error: %s", res.Error()) |
| 1434 |
} |
| 1435 |
if len(msgs) != 5 { |
| 1436 |
t.Fatalf("Expected %d messages; got: %d", 5, len(msgs)) |
| 1437 |
} |
| 1438 |
}) |
| 1439 |
|
| 1440 |
t.Run("fetch with context", func(t *testing.T) { |
| 1441 |
defer js.PurgeStream("TEST") |
| 1442 |
sub, err := js.PullSubscribe("foo", "") |
| 1443 |
if err != nil { |
| 1444 |
t.Fatalf("Unexpected error: %s", err) |
| 1445 |
} |
| 1446 |
for range 5 { |
| 1447 |
if _, err := js.Publish("foo", []byte("msg")); err != nil { |
| 1448 |
t.Fatalf("Unexpected error: %s", err) |
| 1449 |
} |
| 1450 |
} |
| 1451 |
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) |
| 1452 |
defer cancel() |
| 1453 |
res, err := sub.FetchBatch(10, nats.Context(ctx)) |
| 1454 |
if err != nil { |
| 1455 |
t.Fatalf("Unexpected error: %s", err) |
| 1456 |
} |
| 1457 |
go func() { |
| 1458 |
time.Sleep(10 * time.Millisecond) |
| 1459 |
for range 5 { |
| 1460 |
js.Publish("foo", []byte("msg")) |
| 1461 |
} |
| 1462 |
}() |
| 1463 |
msgs := make([]*nats.Msg, 0) |
| 1464 |
for msg := range res.Messages() { |
| 1465 |
msgs = append(msgs, msg) |
| 1466 |
} |
| 1467 |
if res.Error() != nil { |
| 1468 |
t.Fatalf("Unexpected error: %s", res.Error()) |
| 1469 |
} |
| 1470 |
if len(msgs) != 10 { |
| 1471 |
t.Fatalf("Expected %d messages; got: %d", 10, len(msgs)) |
| 1472 |
} |
| 1473 |
}) |
| 1474 |
|
| 1475 |
t.Run("fetch subset of messages", func(t *testing.T) { |
| 1476 |
defer js.PurgeStream("TEST") |
| 1477 |
sub, err := js.PullSubscribe("foo", "") |
| 1478 |
if err != nil { |
| 1479 |
t.Fatalf("Unexpected error: %s", err) |
| 1480 |
} |
| 1481 |
for range 10 { |
| 1482 |
js.Publish("foo", []byte("msg")) |
| 1483 |
} |
| 1484 |
res, err := sub.FetchBatch(5) |
| 1485 |
if err != nil { |
| 1486 |
t.Fatalf("Unexpected error: %s", err) |
| 1487 |
} |
| 1488 |
msgs := make([]*nats.Msg, 0) |
| 1489 |
for msg := range res.Messages() { |
| 1490 |
msgs = append(msgs, msg) |
| 1491 |
} |
| 1492 |
if res.Error() != nil { |
| 1493 |
t.Fatalf("Unexpected error: %s", res.Error()) |
| 1494 |
} |
| 1495 |
if len(msgs) != 5 { |
| 1496 |
t.Fatalf("Expected %d messages; got: %d", 10, len(msgs)) |
| 1497 |
} |
| 1498 |
}) |
| 1499 |
|
| 1500 |
t.Run("context timeout, no error", func(t *testing.T) { |
| 1501 |
defer js.PurgeStream("TEST") |
| 1502 |
sub, err := js.PullSubscribe("foo", "") |
| 1503 |
if err != nil { |
| 1504 |
t.Fatalf("Unexpected error: %s", err) |
| 1505 |
} |
| 1506 |
for range 5 { |
| 1507 |
if _, err := js.Publish("foo", []byte("msg")); err != nil { |
| 1508 |
t.Fatalf("Unexpected error: %s", err) |
| 1509 |
} |
| 1510 |
} |
| 1511 |
// 500ms so the 5 in-flight messages have headroom while the |
| 1512 |
// deadline still fires before any nonexistent 6th message. |
| 1513 |
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) |
| 1514 |
defer cancel() |
| 1515 |
res, err := sub.FetchBatch(10, nats.Context(ctx)) |
| 1516 |
if err != nil { |
| 1517 |
t.Fatalf("Unexpected error: %s", err) |
| 1518 |
} |
| 1519 |
msgs := make([]*nats.Msg, 0) |
| 1520 |
for msg := range res.Messages() { |
| 1521 |
msgs = append(msgs, msg) |
| 1522 |
} |
| 1523 |
if res.Error() != nil { |
| 1524 |
t.Fatalf("Unexpected error: %s", res.Error()) |
| 1525 |
} |
| 1526 |
if len(msgs) != 5 { |
| 1527 |
t.Fatalf("Expected %d messages; got: %d", 5, len(msgs)) |
| 1528 |
} |
| 1529 |
}) |
| 1530 |
|
| 1531 |
t.Run("request expired", func(t *testing.T) { |
| 1532 |
defer js.PurgeStream("TEST") |
| 1533 |
sub, err := js.PullSubscribe("foo", "") |
| 1534 |
if err != nil { |
| 1535 |
t.Fatalf("Unexpected error: %s", err) |
| 1536 |
} |
| 1537 |
for range 5 { |
| 1538 |
if _, err := js.Publish("foo", []byte("msg")); err != nil { |
| 1539 |
t.Fatalf("Unexpected error: %s", err) |
| 1540 |
} |
| 1541 |
} |
| 1542 |
// The batch is under-filled (5 of 10 requested exist), so the |
| 1543 |
// request always runs the wait out; 500ms leaves room to deliver |
| 1544 |
// the 5 with the server a container hop away. |
| 1545 |
res, err := sub.FetchBatch(10, nats.MaxWait(500*time.Millisecond)) |
| 1546 |
if err != nil { |
| 1547 |
t.Fatalf("Unexpected error: %s", err) |
| 1548 |
} |
| 1549 |
msgs := make([]*nats.Msg, 0) |
| 1550 |
for msg := range res.Messages() { |
| 1551 |
msgs = append(msgs, msg) |
| 1552 |
} |
| 1553 |
if res.Error() != nil { |
| 1554 |
t.Fatalf("Unexpected error: %s", res.Error()) |
| 1555 |
} |
| 1556 |
if len(msgs) != 5 { |
| 1557 |
t.Fatalf("Expected %d messages; got: %d", 5, len(msgs)) |
| 1558 |
} |
| 1559 |
}) |
| 1560 |
|
| 1561 |
t.Run("cancel context during fetch", func(t *testing.T) { |
| 1562 |
defer js.PurgeStream("TEST") |
| 1563 |
sub, err := js.PullSubscribe("foo", "") |
| 1564 |
if err != nil { |
| 1565 |
t.Fatalf("Unexpected error: %s", err) |
| 1566 |
} |
| 1567 |
for range 5 { |
| 1568 |
if _, err := js.Publish("foo", []byte("msg")); err != nil { |
| 1569 |
t.Fatalf("Unexpected error: %s", err) |
| 1570 |
} |
| 1571 |
} |
| 1572 |
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) |
| 1573 |
res, err := sub.FetchBatch(10, nats.Context(ctx)) |
| 1574 |
if err != nil { |
| 1575 |
t.Fatalf("Unexpected error: %s", err) |
| 1576 |
} |
| 1577 |
go func() { |
| 1578 |
time.Sleep(200 * time.Millisecond) |
| 1579 |
cancel() |
| 1580 |
}() |
| 1581 |
msgs := make([]*nats.Msg, 0) |
| 1582 |
for msg := range res.Messages() { |
| 1583 |
msgs = append(msgs, msg) |
| 1584 |
} |
| 1585 |
if res.Error() == nil || !errors.Is(res.Error(), context.Canceled) { |
| 1586 |
t.Fatalf("Expected error: %s; got: %s", nats.ErrConsumerDeleted, res.Error()) |
| 1587 |
} |
| 1588 |
if len(msgs) != 5 { |
| 1589 |
t.Fatalf("Expected %d messages; got: %d", 5, len(msgs)) |
| 1590 |
} |
| 1591 |
}) |
| 1592 |
|
| 1593 |
t.Run("remove durable consumer during fetch", func(t *testing.T) { |
| 1594 |
defer js.PurgeStream("TEST") |
| 1595 |
sub, err := js.PullSubscribe("foo", "cons") |
| 1596 |
if err != nil { |
| 1597 |
t.Fatalf("Unexpected error: %s", err) |
| 1598 |
} |
| 1599 |
for range 5 { |
| 1600 |
if _, err := js.Publish("foo", []byte("msg")); err != nil { |
| 1601 |
t.Fatalf("Unexpected error: %s", err) |
| 1602 |
} |
| 1603 |
} |
| 1604 |
res, err := sub.FetchBatch(10) |
| 1605 |
if err != nil { |
| 1606 |
t.Fatalf("Unexpected error: %s", err) |
| 1607 |
} |
| 1608 |
go func() { |
| 1609 |
time.Sleep(10 * time.Millisecond) |
| 1610 |
js.DeleteConsumer("TEST", "cons") |
| 1611 |
}() |
| 1612 |
msgs := make([]*nats.Msg, 0) |
| 1613 |
for msg := range res.Messages() { |
| 1614 |
msgs = append(msgs, msg) |
| 1615 |
} |
| 1616 |
if res.Error() == nil || !errors.Is(res.Error(), nats.ErrConsumerDeleted) { |
| 1617 |
t.Fatalf("Expected error: %s; got: %s", nats.ErrConsumerDeleted, err) |
| 1618 |
} |
| 1619 |
if len(msgs) != 5 { |
| 1620 |
t.Fatalf("Expected %d messages; got: %d", 5, len(msgs)) |
| 1621 |
} |
| 1622 |
}) |
| 1623 |
|
| 1624 |
t.Run("validation errors", func(t *testing.T) { |
| 1625 |
defer js.PurgeStream("TEST") |
| 1626 |
sub, err := js.PullSubscribe("foo", "") |
| 1627 |
if err != nil { |
| 1628 |
t.Fatalf("Unexpected error: %s", err) |
| 1629 |
} |
| 1630 |
_, err = sub.FetchBatch(-1) |
| 1631 |
if !errors.Is(err, nats.ErrInvalidArg) { |
| 1632 |
t.Errorf("Expected error: %s; got: %s", nats.ErrInvalidArg, err) |
| 1633 |
} |
| 1634 |
|
| 1635 |
syncSub, err := js.SubscribeSync("foo") |
| 1636 |
if err != nil { |
| 1637 |
t.Fatalf("Unexpected error: %s", err) |
| 1638 |
} |
| 1639 |
_, err = syncSub.FetchBatch(10) |
| 1640 |
if !errors.Is(err, nats.ErrTypeSubscription) { |
| 1641 |
t.Errorf("Expected error: %s; got: %s", nats.ErrTypeSubscription, err) |
| 1642 |
} |
| 1643 |
|
| 1644 |
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) |
| 1645 |
defer cancel() |
| 1646 |
_, err = sub.FetchBatch(10, nats.Context(ctx), nats.MaxWait(2*time.Second)) |
| 1647 |
if !errors.Is(err, nats.ErrContextAndTimeout) { |
| 1648 |
t.Errorf("Expected error: %s; got: %s", nats.ErrContextAndTimeout, err) |
| 1649 |
} |
| 1650 |
|
| 1651 |
_, err = sub.FetchBatch(10, nats.Context(context.Background())) |
| 1652 |
if !errors.Is(err, nats.ErrNoDeadlineContext) { |
| 1653 |
t.Errorf("Expected error: %s; got: %s", nats.ErrNoDeadlineContext, err) |
| 1654 |
} |
| 1655 |
}) |
| 1656 |
|
| 1657 |
t.Run("close subscription", func(t *testing.T) { |
| 1658 |
defer js.PurgeStream("TEST") |
| 1659 |
sub, err := js.PullSubscribe("foo", "") |
| 1660 |
if err != nil { |
| 1661 |
t.Fatalf("Unexpected error: %s", err) |
| 1662 |
} |
| 1663 |
for range 100 { |
| 1664 |
if _, err := js.Publish("foo", []byte("msg")); err != nil { |
| 1665 |
t.Fatalf("Unexpected error: %s", err) |
| 1666 |
} |
| 1667 |
} |
| 1668 |
cinfo, err := sub.ConsumerInfo() |
| 1669 |
if err != nil { |
| 1670 |
t.Fatalf("Unexpected error: %s", err) |
| 1671 |
} |
| 1672 |
nextSubject := fmt.Sprintf("$JS.API.CONSUMER.MSG.NEXT.TEST.%s", cinfo.Name) |
| 1673 |
replySubject := strings.Replace(sub.Subject, "*", "abc", 1) |
| 1674 |
payload := `{"batch":10,"no_wait":true}` |
| 1675 |
if err := nc.PublishRequest(nextSubject, replySubject, []byte(payload)); err != nil { |
| 1676 |
t.Fatalf("Unexpected error: %s", err) |
| 1677 |
} |
| 1678 |
time.Sleep(100 * time.Millisecond) |
| 1679 |
|
| 1680 |
sub.Drain() |
| 1681 |
res, err := sub.FetchBatch(100) |
| 1682 |
if err != nil { |
| 1683 |
t.Fatalf("Unexpected error: %s", err) |
| 1684 |
} |
| 1685 |
msgs := make([]*nats.Msg, 0) |
| 1686 |
for msg := range res.Messages() { |
| 1687 |
msgs = append(msgs, msg) |
| 1688 |
msg.Ack() |
| 1689 |
} |
| 1690 |
if res.Error() != nil { |
| 1691 |
t.Fatalf("Unexpected error: %s", res.Error()) |
| 1692 |
} |
| 1693 |
if len(msgs) != 10 { |
| 1694 |
t.Fatalf("Expected %d messages; got: %d", 10, len(msgs)) |
| 1695 |
} |
| 1696 |
|
| 1697 |
_, err = sub.FetchBatch(10, nats.MaxWait(100*time.Millisecond)) |
| 1698 |
if !errors.Is(err, nats.ErrSubscriptionClosed) { |
| 1699 |
t.Fatalf("Expected error: %s; got: %s", nats.ErrSubscriptionClosed, err) |
| 1700 |
} |
| 1701 |
}) |
| 1702 |
}) |
| 1703 |
} |
| 1704 |
|
| 1705 |
func TestPullSubscribeConsumerDeleted(t *testing.T) { |
| 1706 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 1707 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 1708 |
if err != nil { |
| 1709 |
t.Fatalf("Unexpected error: %v", err) |
| 1710 |
} |
| 1711 |
|
| 1712 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 1713 |
Name: "TEST", |
| 1714 |
Subjects: []string{"foo"}, |
| 1715 |
}) |
| 1716 |
if err != nil { |
| 1717 |
t.Fatalf("Unexpected error: %v", err) |
| 1718 |
} |
| 1719 |
if _, err := js.Publish("foo", []byte("msg")); err != nil { |
| 1720 |
t.Fatal(err) |
| 1721 |
} |
| 1722 |
t.Run("delete consumer", func(t *testing.T) { |
| 1723 |
sub, err := js.PullSubscribe("foo", "cons") |
| 1724 |
if err != nil { |
| 1725 |
t.Fatal(err) |
| 1726 |
} |
| 1727 |
defer sub.Unsubscribe() |
| 1728 |
|
| 1729 |
if _, err = sub.Fetch(1, nats.MaxWait(10*time.Millisecond)); err != nil { |
| 1730 |
t.Fatalf("Expected error: %v; got: %v", nats.ErrTimeout, err) |
| 1731 |
} |
| 1732 |
time.AfterFunc(50*time.Millisecond, func() { js.DeleteConsumer("TEST", "cons") }) |
| 1733 |
if _, err = sub.Fetch(1, nats.MaxWait(100*time.Millisecond)); !errors.Is(err, nats.ErrConsumerDeleted) { |
| 1734 |
t.Fatalf("Expected error: %v; got: %v", nats.ErrConsumerDeleted, err) |
| 1735 |
} |
| 1736 |
}) |
| 1737 |
|
| 1738 |
t.Run("delete stream", func(t *testing.T) { |
| 1739 |
sub, err := js.PullSubscribe("foo", "cons") |
| 1740 |
if err != nil { |
| 1741 |
t.Fatal(err) |
| 1742 |
} |
| 1743 |
defer sub.Unsubscribe() |
| 1744 |
|
| 1745 |
if _, err = sub.Fetch(1, nats.MaxWait(10*time.Millisecond)); err != nil { |
| 1746 |
t.Fatalf("Expected error: %v; got: %v", nats.ErrTimeout, err) |
| 1747 |
} |
| 1748 |
time.AfterFunc(50*time.Millisecond, func() { js.DeleteStream("TEST") }) |
| 1749 |
if _, err = sub.Fetch(1, nats.MaxWait(100*time.Millisecond)); !errors.Is(err, nats.ErrConsumerDeleted) { |
| 1750 |
t.Fatalf("Expected error: %v; got: %v", nats.ErrConsumerDeleted, err) |
| 1751 |
} |
| 1752 |
}) |
| 1753 |
}) |
| 1754 |
} |
| 1755 |
|
| 1756 |
func TestJetStreamAckPending_Pull(t *testing.T) { |
| 1757 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 1758 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 1759 |
if err != nil { |
| 1760 |
t.Fatalf("Unexpected error: %v", err) |
| 1761 |
} |
| 1762 |
|
| 1763 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 1764 |
Name: "TEST", |
| 1765 |
Subjects: []string{"foo"}, |
| 1766 |
}) |
| 1767 |
if err != nil { |
| 1768 |
t.Fatalf("Unexpected error: %v", err) |
| 1769 |
} |
| 1770 |
|
| 1771 |
const totalMsgs = 4 |
| 1772 |
for i := range totalMsgs { |
| 1773 |
if _, err := js.Publish("foo", []byte(fmt.Sprintf("msg %d", i))); err != nil { |
| 1774 |
t.Fatal(err) |
| 1775 |
} |
| 1776 |
} |
| 1777 |
|
| 1778 |
ackPendingLimit := 3 |
| 1779 |
sub, err := js.PullSubscribe("foo", "dname-pull-ack-wait", nats.MaxAckPending(ackPendingLimit)) |
| 1780 |
if err != nil { |
| 1781 |
t.Fatal(err) |
| 1782 |
} |
| 1783 |
defer sub.Unsubscribe() |
| 1784 |
|
| 1785 |
var msgs []*nats.Msg |
| 1786 |
for range ackPendingLimit { |
| 1787 |
ms, err := sub.Fetch(1) |
| 1788 |
if err != nil { |
| 1789 |
t.Fatalf("Error on fetch: %v", err) |
| 1790 |
} |
| 1791 |
msgs = append(msgs, ms...) |
| 1792 |
} |
| 1793 |
|
| 1794 |
if _, err := sub.Fetch(1, nats.MaxWait(250*time.Millisecond)); err != nats.ErrTimeout { |
| 1795 |
t.Fatalf("Expected timeout, got: %v", err) |
| 1796 |
} |
| 1797 |
msgs[0].Ack() |
| 1798 |
if _, err := sub.Fetch(1); err != nil { |
| 1799 |
t.Fatalf("Unexpected error: %v", err) |
| 1800 |
} |
| 1801 |
}) |
| 1802 |
} |
| 1803 |
|
| 1804 |
func TestJetStreamAckPending_Push(t *testing.T) { |
| 1805 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 1806 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 1807 |
if err != nil { |
| 1808 |
t.Fatalf("Unexpected error: %v", err) |
| 1809 |
} |
| 1810 |
|
| 1811 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 1812 |
Name: "TEST", |
| 1813 |
Subjects: []string{"foo"}, |
| 1814 |
}) |
| 1815 |
if err != nil { |
| 1816 |
t.Fatalf("Unexpected error: %v", err) |
| 1817 |
} |
| 1818 |
|
| 1819 |
const totalMsgs = 3 |
| 1820 |
for i := range totalMsgs { |
| 1821 |
if _, err := js.Publish("foo", []byte(fmt.Sprintf("msg %d", i))); err != nil { |
| 1822 |
t.Fatal(err) |
| 1823 |
} |
| 1824 |
} |
| 1825 |
|
| 1826 |
sub, err := js.SubscribeSync("foo", |
| 1827 |
nats.Durable("dname-wait"), |
| 1828 |
nats.AckWait(100*time.Millisecond), |
| 1829 |
nats.MaxDeliver(5), |
| 1830 |
nats.MaxAckPending(3), |
| 1831 |
) |
| 1832 |
if err != nil { |
| 1833 |
t.Fatal(err) |
| 1834 |
} |
| 1835 |
defer sub.Unsubscribe() |
| 1836 |
|
| 1837 |
// 3 messages delivered 5 times. |
| 1838 |
expected := 15 |
| 1839 |
timeout := time.Now().Add(2 * time.Second) |
| 1840 |
pending := 0 |
| 1841 |
for time.Now().Before(timeout) { |
| 1842 |
if pending, _, _ = sub.Pending(); pending >= expected { |
| 1843 |
break |
| 1844 |
} |
| 1845 |
time.Sleep(10 * time.Millisecond) |
| 1846 |
} |
| 1847 |
if pending < expected { |
| 1848 |
t.Errorf("Expected %v, got %v", expected, pending) |
| 1849 |
} |
| 1850 |
|
| 1851 |
info, err := sub.ConsumerInfo() |
| 1852 |
if err != nil { |
| 1853 |
t.Fatal(err) |
| 1854 |
} |
| 1855 |
|
| 1856 |
got := info.NumRedelivered |
| 1857 |
expected = 3 |
| 1858 |
if got < expected { |
| 1859 |
t.Errorf("Expected %v, got: %v", expected, got) |
| 1860 |
} |
| 1861 |
|
| 1862 |
got = info.NumAckPending |
| 1863 |
expected = 3 |
| 1864 |
if got < expected { |
| 1865 |
t.Errorf("Expected %v, got: %v", expected, got) |
| 1866 |
} |
| 1867 |
|
| 1868 |
got = info.NumWaiting |
| 1869 |
expected = 0 |
| 1870 |
if got != expected { |
| 1871 |
t.Errorf("Expected %v, got: %v", expected, got) |
| 1872 |
} |
| 1873 |
|
| 1874 |
got = int(info.NumPending) |
| 1875 |
expected = 0 |
| 1876 |
if got != expected { |
| 1877 |
t.Errorf("Expected %v, got: %v", expected, got) |
| 1878 |
} |
| 1879 |
|
| 1880 |
got = info.Config.MaxAckPending |
| 1881 |
expected = 3 |
| 1882 |
if got != expected { |
| 1883 |
t.Errorf("Expected %v, got %v", expected, pending) |
| 1884 |
} |
| 1885 |
|
| 1886 |
got = info.Config.MaxDeliver |
| 1887 |
expected = 5 |
| 1888 |
if got != expected { |
| 1889 |
t.Errorf("Expected %v, got %v", expected, pending) |
| 1890 |
} |
| 1891 |
|
| 1892 |
acks := map[int]int{} |
| 1893 |
|
| 1894 |
ackPending := 3 |
| 1895 |
timeout = time.Now().Add(2 * time.Second) |
| 1896 |
for time.Now().Before(timeout) { |
| 1897 |
info, err := sub.ConsumerInfo() |
| 1898 |
if err != nil { |
| 1899 |
t.Fatal(err) |
| 1900 |
} |
| 1901 |
if got, want := info.NumAckPending, ackPending; got > 0 && got != want { |
| 1902 |
t.Fatalf("unexpected num ack pending: got=%d, want=%d", got, want) |
| 1903 |
} |
| 1904 |
|
| 1905 |
pending, _, _ = sub.Pending() |
| 1906 |
if pending == 0 { |
| 1907 |
break |
| 1908 |
} |
| 1909 |
|
| 1910 |
m, err := sub.NextMsg(100 * time.Millisecond) |
| 1911 |
if err != nil { |
| 1912 |
t.Fatalf("Error getting next message: %v", err) |
| 1913 |
} |
| 1914 |
|
| 1915 |
if err := m.AckSync(); err != nil { |
| 1916 |
t.Fatalf("Error on ack message: %v", err) |
| 1917 |
} |
| 1918 |
|
| 1919 |
meta, err := m.Metadata() |
| 1920 |
if err != nil { |
| 1921 |
t.Errorf("Unexpected error: %v", err) |
| 1922 |
} |
| 1923 |
acks[int(meta.Sequence.Stream)]++ |
| 1924 |
|
| 1925 |
if ackPending != 0 { |
| 1926 |
ackPending-- |
| 1927 |
} |
| 1928 |
if int(meta.NumPending) != ackPending { |
| 1929 |
t.Errorf("Expected %v, got %v", ackPending, meta.NumPending) |
| 1930 |
} |
| 1931 |
} |
| 1932 |
|
| 1933 |
got = len(acks) |
| 1934 |
expected = 3 |
| 1935 |
if got != expected { |
| 1936 |
t.Errorf("Expected %v, got %v", expected, got) |
| 1937 |
} |
| 1938 |
|
| 1939 |
expected = 5 |
| 1940 |
for _, got := range acks { |
| 1941 |
if got != expected { |
| 1942 |
t.Errorf("Expected %v, got %v", expected, got) |
| 1943 |
} |
| 1944 |
} |
| 1945 |
|
| 1946 |
_, err = sub.NextMsg(100 * time.Millisecond) |
| 1947 |
if err != nats.ErrTimeout { |
| 1948 |
t.Errorf("Expected timeout, got: %v", err) |
| 1949 |
} |
| 1950 |
}) |
| 1951 |
} |
| 1952 |
|
| 1953 |
func TestJetStream_Drain(t *testing.T) { |
| 1954 |
c := newTester(t) |
| 1955 |
inst := c.CreateServer(t, true) |
| 1956 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 1957 |
|
| 1958 |
ctx, done := context.WithTimeout(context.Background(), 10*time.Second) |
| 1959 |
|
| 1960 |
nc, err := nats.Connect(inst.Servers[0].URL, nats.MaxReconnects(-1), nats.ClosedHandler(func(_ *nats.Conn) { |
| 1961 |
done() |
| 1962 |
})) |
| 1963 |
if err != nil { |
| 1964 |
t.Fatalf("Unexpected error: %v", err) |
| 1965 |
} |
| 1966 |
defer nc.Close() |
| 1967 |
c.WaitForJetStream(t, nc) |
| 1968 |
|
| 1969 |
js, err := nc.JetStream(nats.MaxWait(250 * time.Millisecond)) |
| 1970 |
if err != nil { |
| 1971 |
t.Fatalf("Unexpected error: %v", err) |
| 1972 |
} |
| 1973 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 1974 |
Name: "TEST", |
| 1975 |
Subjects: []string{"drain"}, |
| 1976 |
}) |
| 1977 |
if err != nil { |
| 1978 |
t.Fatalf("Unexpected error: %v", err) |
| 1979 |
} |
| 1980 |
|
| 1981 |
total := 500 |
| 1982 |
for i := range total { |
| 1983 |
_, err := js.Publish("drain", []byte(fmt.Sprintf("i:%d", i))) |
| 1984 |
if err != nil { |
| 1985 |
t.Error(err) |
| 1986 |
} |
| 1987 |
} |
| 1988 |
|
| 1989 |
errCh := make(chan error, 2048) |
| 1990 |
createSub := func(name string) (*nats.Subscription, error) { |
| 1991 |
return js.Subscribe("drain", func(m *nats.Msg) { |
| 1992 |
err := m.AckSync() |
| 1993 |
if err != nil { |
| 1994 |
errCh <- err |
| 1995 |
} |
| 1996 |
}, nats.Durable(name), nats.ManualAck()) |
| 1997 |
} |
| 1998 |
|
| 1999 |
subA, err := createSub("A") |
| 2000 |
if err != nil { |
| 2001 |
t.Fatalf("Unexpected error: %v", err) |
| 2002 |
} |
| 2003 |
|
| 2004 |
subB, err := createSub("B") |
| 2005 |
if err != nil { |
| 2006 |
t.Fatalf("Unexpected error: %v", err) |
| 2007 |
} |
| 2008 |
|
| 2009 |
subC, err := createSub("C") |
| 2010 |
if err != nil { |
| 2011 |
t.Fatalf("Unexpected error: %v", err) |
| 2012 |
} |
| 2013 |
|
| 2014 |
subD, err := createSub("D") |
| 2015 |
if err != nil { |
| 2016 |
t.Fatalf("Unexpected error: %v", err) |
| 2017 |
} |
| 2018 |
|
| 2019 |
waitForDelivered := func(t *testing.T, sub *nats.Subscription) { |
| 2020 |
t.Helper() |
| 2021 |
timeout := time.Now().Add(2 * time.Second) |
| 2022 |
for time.Now().Before(timeout) { |
| 2023 |
if msgs, _ := sub.Delivered(); msgs != 0 { |
| 2024 |
return |
| 2025 |
} |
| 2026 |
time.Sleep(10 * time.Millisecond) |
| 2027 |
} |
| 2028 |
} |
| 2029 |
waitForDelivered(t, subA) |
| 2030 |
waitForDelivered(t, subB) |
| 2031 |
waitForDelivered(t, subC) |
| 2032 |
waitForDelivered(t, subD) |
| 2033 |
nc.Drain() |
| 2034 |
|
| 2035 |
select { |
| 2036 |
case err := <-errCh: |
| 2037 |
t.Fatalf("Error during drain: %+v", err) |
| 2038 |
case <-ctx.Done(): |
| 2039 |
// OK! |
| 2040 |
} |
| 2041 |
} |
| 2042 |
|
| 2043 |
func TestAckForNonJetStream(t *testing.T) { |
| 2044 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 2045 |
sub, _ := nc.SubscribeSync("foo") |
| 2046 |
nc.PublishRequest("foo", "_INBOX_", []byte("OK")) |
| 2047 |
m, err := sub.NextMsg(time.Second) |
| 2048 |
if err != nil { |
| 2049 |
t.Fatalf("Unexpected error: %v", err) |
| 2050 |
} |
| 2051 |
if err := m.Ack(); err != nil { |
| 2052 |
t.Fatalf("Expected no errors, got '%v'", err) |
| 2053 |
} |
| 2054 |
}) |
| 2055 |
} |
| 2056 |
|
| 2057 |
func TestJetStreamManagement(t *testing.T) { |
| 2058 |
c := newTester(t) |
| 2059 |
accounts := `accounts: { |
| 2060 |
A { |
| 2061 |
users: [{ user: "foo" }] |
| 2062 |
jetstream: { max_mem: 64MB, max_file: 64MB } |
| 2063 |
} |
| 2064 |
}` |
| 2065 |
inst := c.CreateServer(t, true, |
| 2066 |
testservice.WithAccounts(accounts), |
| 2067 |
testservice.WithAuthorization("# auth required, no no_auth_user"), |
| 2068 |
testservice.WithSystemAccount(noSystemAccountBody()), |
| 2069 |
) |
| 2070 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 2071 |
|
| 2072 |
nc := dialInstance(t, inst, nats.UserInfo("foo", "")) |
| 2073 |
c.WaitForJetStream(t, nc) |
| 2074 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 2075 |
if err != nil { |
| 2076 |
t.Fatalf("Unexpected error: %v", err) |
| 2077 |
} |
| 2078 |
|
| 2079 |
var si *nats.StreamInfo |
| 2080 |
|
| 2081 |
t.Run("create stream", func(t *testing.T) { |
| 2082 |
consLimits := nats.StreamConsumerLimits{ |
| 2083 |
MaxAckPending: 100, |
| 2084 |
InactiveThreshold: 10 * time.Second, |
| 2085 |
} |
| 2086 |
cfg := &nats.StreamConfig{ |
| 2087 |
Name: "foo", |
| 2088 |
Subjects: []string{"foo", "bar", "baz"}, |
| 2089 |
Compression: nats.S2Compression, |
| 2090 |
ConsumerLimits: nats.StreamConsumerLimits{ |
| 2091 |
MaxAckPending: 100, |
| 2092 |
InactiveThreshold: 10 * time.Second, |
| 2093 |
}, |
| 2094 |
FirstSeq: 22, |
| 2095 |
Metadata: map[string]string{ |
| 2096 |
"foo": "bar", |
| 2097 |
"baz": "quux", |
| 2098 |
}, |
| 2099 |
} |
| 2100 |
|
| 2101 |
si, err := js.AddStream(cfg) |
| 2102 |
if err != nil { |
| 2103 |
t.Fatalf("Unexpected error: %v", err) |
| 2104 |
} |
| 2105 |
if si == nil || si.Config.Name != "foo" { |
| 2106 |
t.Fatalf("StreamInfo is not correct %+v", si) |
| 2107 |
} |
| 2108 |
if v1, v2 := si.Config.Metadata["foo"], si.Config.Metadata["baz"]; v1 != "bar" || v2 != "quux" { |
| 2109 |
t.Fatalf("Metadata is not correct %+v", si.Config.Metadata) |
| 2110 |
} |
| 2111 |
if si.Config.Compression != nats.S2Compression { |
| 2112 |
t.Fatalf("Compression is not correct %+v", si.Config.Compression) |
| 2113 |
} |
| 2114 |
if si.Config.FirstSeq != 22 { |
| 2115 |
t.Fatalf("FirstSeq is not correct %+v", si.Config.FirstSeq) |
| 2116 |
} |
| 2117 |
if si.Config.ConsumerLimits != consLimits { |
| 2118 |
t.Fatalf("ConsumerLimits is not correct %+v", si.Config.ConsumerLimits) |
| 2119 |
} |
| 2120 |
}) |
| 2121 |
|
| 2122 |
t.Run("stream with given name already exists", func(t *testing.T) { |
| 2123 |
if _, err := js.AddStream(&nats.StreamConfig{Name: "foo", Description: "desc"}); !errors.Is(err, nats.ErrStreamNameAlreadyInUse) { |
| 2124 |
t.Fatalf("Expected error: %v; got: %v", nats.ErrStreamNameAlreadyInUse, err) |
| 2125 |
} |
| 2126 |
}) |
| 2127 |
|
| 2128 |
for range 25 { |
| 2129 |
js.Publish("foo", []byte("hi")) |
| 2130 |
} |
| 2131 |
|
| 2132 |
t.Run("stream not found", func(t *testing.T) { |
| 2133 |
si, err = js.StreamInfo("bar") |
| 2134 |
if !errors.Is(err, nats.ErrStreamNotFound) { |
| 2135 |
t.Fatalf("Expected error: %v, got: %v", nats.ErrStreamNotFound, err) |
| 2136 |
} |
| 2137 |
if si != nil { |
| 2138 |
t.Fatalf("StreamInfo should be nil %+v", si) |
| 2139 |
} |
| 2140 |
}) |
| 2141 |
|
| 2142 |
t.Run("stream info", func(t *testing.T) { |
| 2143 |
si, err = js.StreamInfo("foo") |
| 2144 |
if err != nil { |
| 2145 |
t.Fatalf("Unexpected error: %v", err) |
| 2146 |
} |
| 2147 |
if si == nil || si.Config.Name != "foo" { |
| 2148 |
t.Fatalf("StreamInfo is not correct %+v", si) |
| 2149 |
} |
| 2150 |
}) |
| 2151 |
|
| 2152 |
t.Run("create bad stream", func(t *testing.T) { |
| 2153 |
if _, err := js.AddStream(nil); err != nats.ErrStreamConfigRequired { |
| 2154 |
t.Fatalf("Expected %v, got %v", nats.ErrStreamConfigRequired, err) |
| 2155 |
} |
| 2156 |
if _, err := js.AddStream(&nats.StreamConfig{Name: ""}); err != nats.ErrStreamNameRequired { |
| 2157 |
t.Fatalf("Expected %v, got %v", nats.ErrStreamNameRequired, err) |
| 2158 |
} |
| 2159 |
if _, err := js.AddStream(&nats.StreamConfig{Name: "bad.stream.name"}); err != nats.ErrInvalidStreamName { |
| 2160 |
t.Fatalf("Expected %v, got %v", nats.ErrInvalidStreamName, err) |
| 2161 |
} |
| 2162 |
if _, err := js.AddStream(&nats.StreamConfig{Name: "bad stream name"}); err != nats.ErrInvalidStreamName { |
| 2163 |
t.Fatalf("Expected %v, got %v", nats.ErrInvalidStreamName, err) |
| 2164 |
} |
| 2165 |
}) |
| 2166 |
|
| 2167 |
t.Run("bad stream info", func(t *testing.T) { |
| 2168 |
if _, err := js.StreamInfo(""); err != nats.ErrStreamNameRequired { |
| 2169 |
t.Fatalf("Expected %v, got %v", nats.ErrStreamNameRequired, err) |
| 2170 |
} |
| 2171 |
if _, err := js.StreamInfo("bad.stream.name"); err != nats.ErrInvalidStreamName { |
| 2172 |
t.Fatalf("Expected %v, got %v", nats.ErrInvalidStreamName, err) |
| 2173 |
} |
| 2174 |
}) |
| 2175 |
|
| 2176 |
t.Run("stream update", func(t *testing.T) { |
| 2177 |
if _, err := js.UpdateStream(nil); err != nats.ErrStreamConfigRequired { |
| 2178 |
t.Fatalf("Expected %v, got %v", nats.ErrStreamConfigRequired, err) |
| 2179 |
} |
| 2180 |
if _, err := js.UpdateStream(&nats.StreamConfig{Name: ""}); err != nats.ErrStreamNameRequired { |
| 2181 |
t.Fatalf("Expected %v, got %v", nats.ErrStreamNameRequired, err) |
| 2182 |
} |
| 2183 |
if _, err := js.UpdateStream(&nats.StreamConfig{Name: "bad.stream.name"}); err != nats.ErrInvalidStreamName { |
| 2184 |
t.Fatalf("Expected %v, got %v", nats.ErrInvalidStreamName, err) |
| 2185 |
} |
| 2186 |
prevMaxMsgs := si.Config.MaxMsgs |
| 2187 |
si, err = js.UpdateStream(&nats.StreamConfig{Name: "foo", MaxMsgs: prevMaxMsgs + 100}) |
| 2188 |
if err != nil { |
| 2189 |
t.Fatalf("Unexpected error: %v", err) |
| 2190 |
} |
| 2191 |
if si == nil || si.Config.Name != "foo" || si.Config.MaxMsgs == prevMaxMsgs { |
| 2192 |
t.Fatalf("StreamInfo is not correct %+v", si) |
| 2193 |
} |
| 2194 |
}) |
| 2195 |
|
| 2196 |
t.Run("create consumer", func(t *testing.T) { |
| 2197 |
t.Run("with durable set", func(t *testing.T) { |
| 2198 |
sub, err := nc.SubscribeSync("$JS.API.CONSUMER.CREATE.foo.dlc") |
| 2199 |
if err != nil { |
| 2200 |
t.Fatalf("Unexpected error: %v", err) |
| 2201 |
} |
| 2202 |
defer sub.Unsubscribe() |
| 2203 |
ci, err := js.AddConsumer("foo", &nats.ConsumerConfig{ |
| 2204 |
Durable: "dlc", |
| 2205 |
AckPolicy: nats.AckExplicitPolicy, |
| 2206 |
Metadata: map[string]string{ |
| 2207 |
"foo": "bar", |
| 2208 |
"baz": "quux", |
| 2209 |
}, |
| 2210 |
}) |
| 2211 |
if err != nil { |
| 2212 |
t.Fatalf("Unexpected error: %v", err) |
| 2213 |
} |
| 2214 |
msg, err := sub.NextMsg(1 * time.Second) |
| 2215 |
if err != nil { |
| 2216 |
t.Fatalf("Unexpected error: %v", err) |
| 2217 |
} |
| 2218 |
if !strings.Contains(string(msg.Data), `"durable_name":"dlc"`) { |
| 2219 |
t.Fatalf("create consumer message is not correct: %q", string(msg.Data)) |
| 2220 |
} |
| 2221 |
if ci == nil || ci.Name != "dlc" || ci.Stream != "foo" { |
| 2222 |
t.Fatalf("ConsumerInfo is not correct %+v", ci) |
| 2223 |
} |
| 2224 |
if v1, v2 := ci.Config.Metadata["foo"], ci.Config.Metadata["baz"]; v1 != "bar" || v2 != "quux" { |
| 2225 |
t.Fatalf("Metadata is not correct %+v", ci.Config.Metadata) |
| 2226 |
} |
| 2227 |
}) |
| 2228 |
t.Run("with name set", func(t *testing.T) { |
| 2229 |
sub, err := nc.SubscribeSync("$JS.API.CONSUMER.CREATE.foo.dlc-1") |
| 2230 |
if err != nil { |
| 2231 |
t.Fatalf("Unexpected error: %v", err) |
| 2232 |
} |
| 2233 |
defer sub.Unsubscribe() |
| 2234 |
ci, err := js.AddConsumer("foo", &nats.ConsumerConfig{Durable: "dlc-1", AckPolicy: nats.AckExplicitPolicy}) |
| 2235 |
if err != nil { |
| 2236 |
t.Fatalf("Unexpected error: %v", err) |
| 2237 |
} |
| 2238 |
msg, err := sub.NextMsg(1 * time.Second) |
| 2239 |
if err != nil { |
| 2240 |
t.Fatalf("Unexpected error: %v", err) |
| 2241 |
} |
| 2242 |
if !strings.Contains(string(msg.Data), `"durable_name":"dlc-1"`) { |
| 2243 |
t.Fatalf("create consumer message is not correct: %q", string(msg.Data)) |
| 2244 |
} |
| 2245 |
if ci == nil || ci.Name != "dlc-1" || ci.Stream != "foo" { |
| 2246 |
t.Fatalf("ConsumerInfo is not correct %+v", ci) |
| 2247 |
} |
| 2248 |
}) |
| 2249 |
|
| 2250 |
t.Run("with same Durable and Name set", func(t *testing.T) { |
| 2251 |
sub, err := nc.SubscribeSync("$JS.API.CONSUMER.CREATE.foo.dlc-2") |
| 2252 |
if err != nil { |
| 2253 |
t.Fatalf("Unexpected error: %v", err) |
| 2254 |
} |
| 2255 |
defer sub.Unsubscribe() |
| 2256 |
ci, err := js.AddConsumer("foo", &nats.ConsumerConfig{Durable: "dlc-2", Name: "dlc-2", AckPolicy: nats.AckExplicitPolicy}) |
| 2257 |
if err != nil { |
| 2258 |
t.Fatalf("Unexpected error: %v", err) |
| 2259 |
} |
| 2260 |
msg, err := sub.NextMsg(1 * time.Second) |
| 2261 |
if err != nil { |
| 2262 |
t.Fatalf("Unexpected error: %v", err) |
| 2263 |
} |
| 2264 |
if !strings.Contains(string(msg.Data), `"durable_name":"dlc-2"`) { |
| 2265 |
t.Fatalf("create consumer message is not correct: %q", string(msg.Data)) |
| 2266 |
} |
| 2267 |
if ci == nil || ci.Name != "dlc-2" || ci.Stream != "foo" { |
| 2268 |
t.Fatalf("ConsumerInfo is not correct %+v", ci) |
| 2269 |
} |
| 2270 |
}) |
| 2271 |
|
| 2272 |
t.Run("with name and filter subject", func(t *testing.T) { |
| 2273 |
sub, err := nc.SubscribeSync("$JS.API.CONSUMER.CREATE.foo.dlc-3.foo") |
| 2274 |
if err != nil { |
| 2275 |
t.Fatalf("Unexpected error: %v", err) |
| 2276 |
} |
| 2277 |
defer sub.Unsubscribe() |
| 2278 |
ci, err := js.AddConsumer("foo", &nats.ConsumerConfig{ |
| 2279 |
Durable: "dlc-3", |
| 2280 |
AckPolicy: nats.AckExplicitPolicy, |
| 2281 |
FilterSubject: "foo", |
| 2282 |
}) |
| 2283 |
if err != nil { |
| 2284 |
t.Fatalf("Unexpected error: %v", err) |
| 2285 |
} |
| 2286 |
msg, err := sub.NextMsg(1 * time.Second) |
| 2287 |
if err != nil { |
| 2288 |
t.Fatalf("Unexpected error: %v", err) |
| 2289 |
} |
| 2290 |
if !strings.Contains(string(msg.Data), `"durable_name":"dlc-3"`) { |
| 2291 |
t.Fatalf("create consumer message is not correct: %q", string(msg.Data)) |
| 2292 |
} |
| 2293 |
if ci == nil || ci.Name != "dlc-3" || ci.Stream != "foo" || ci.Config.FilterSubject != "foo" { |
| 2294 |
t.Fatalf("ConsumerInfo is not correct %+v", ci) |
| 2295 |
} |
| 2296 |
}) |
| 2297 |
|
| 2298 |
t.Run("legacy ephemeral consumer without name", func(t *testing.T) { |
| 2299 |
sub, err := nc.SubscribeSync("$JS.API.CONSUMER.CREATE.foo") |
| 2300 |
if err != nil { |
| 2301 |
t.Fatalf("Unexpected error: %v", err) |
| 2302 |
} |
| 2303 |
defer sub.Unsubscribe() |
| 2304 |
ci, err := js.AddConsumer("foo", &nats.ConsumerConfig{AckPolicy: nats.AckExplicitPolicy}) |
| 2305 |
if err != nil { |
| 2306 |
t.Fatalf("Unexpected error: %v", err) |
| 2307 |
} |
| 2308 |
msg, err := sub.NextMsg(1 * time.Second) |
| 2309 |
if err != nil { |
| 2310 |
t.Fatalf("Unexpected error: %v", err) |
| 2311 |
} |
| 2312 |
if !strings.Contains(string(msg.Data), `"stream_name":"foo"`) { |
| 2313 |
t.Fatalf("create consumer message is not correct: %q", string(msg.Data)) |
| 2314 |
} |
| 2315 |
if ci == nil || ci.Config.Durable != "" || ci.Stream != "foo" { |
| 2316 |
t.Fatalf("ConsumerInfo is not correct %+v", ci) |
| 2317 |
} |
| 2318 |
}) |
| 2319 |
|
| 2320 |
t.Run("legacy durable with jetstream context option", func(t *testing.T) { |
| 2321 |
jsLegacy, err := nc.JetStream(nats.UseLegacyDurableConsumers()) |
| 2322 |
if err != nil { |
| 2323 |
t.Fatalf("Unexpected error: %v", err) |
| 2324 |
} |
| 2325 |
sub, err := nc.SubscribeSync("$JS.API.CONSUMER.DURABLE.CREATE.foo.dlc-4") |
| 2326 |
if err != nil { |
| 2327 |
t.Fatalf("Unexpected error: %v", err) |
| 2328 |
} |
| 2329 |
defer sub.Unsubscribe() |
| 2330 |
ci, err := jsLegacy.AddConsumer("foo", &nats.ConsumerConfig{Durable: "dlc-4", AckPolicy: nats.AckExplicitPolicy}) |
| 2331 |
if err != nil { |
| 2332 |
t.Fatalf("Unexpected error: %v", err) |
| 2333 |
} |
| 2334 |
msg, err := sub.NextMsg(1 * time.Second) |
| 2335 |
if err != nil { |
| 2336 |
t.Fatalf("Unexpected error: %v", err) |
| 2337 |
} |
| 2338 |
if !strings.Contains(string(msg.Data), `"durable_name":"dlc-4"`) { |
| 2339 |
t.Fatalf("create consumer message is not correct: %q", string(msg.Data)) |
| 2340 |
} |
| 2341 |
if ci == nil || ci.Config.Durable != "dlc-4" || ci.Stream != "foo" { |
| 2342 |
t.Fatalf("ConsumerInfo is not correct %+v", ci) |
| 2343 |
} |
| 2344 |
}) |
| 2345 |
|
| 2346 |
t.Run("durable consumer with multiple filter subjects", func(t *testing.T) { |
| 2347 |
sub, err := nc.SubscribeSync("$JS.API.CONSUMER.CREATE.foo.dlc-5") |
| 2348 |
if err != nil { |
| 2349 |
t.Fatalf("Unexpected error: %v", err) |
| 2350 |
} |
| 2351 |
defer sub.Unsubscribe() |
| 2352 |
ci, err := js.AddConsumer("foo", &nats.ConsumerConfig{ |
| 2353 |
Durable: "dlc-5", |
| 2354 |
AckPolicy: nats.AckExplicitPolicy, |
| 2355 |
FilterSubjects: []string{"foo", "bar"}, |
| 2356 |
}) |
| 2357 |
if err != nil { |
| 2358 |
t.Fatalf("Unexpected error: %v", err) |
| 2359 |
} |
| 2360 |
msg, err := sub.NextMsg(1 * time.Second) |
| 2361 |
if err != nil { |
| 2362 |
t.Fatalf("Unexpected error: %v", err) |
| 2363 |
} |
| 2364 |
if !strings.Contains(string(msg.Data), `"durable_name":"dlc-5"`) { |
| 2365 |
t.Fatalf("create consumer message is not correct: %q", string(msg.Data)) |
| 2366 |
} |
| 2367 |
if ci == nil || ci.Config.Durable != "dlc-5" || !reflect.DeepEqual(ci.Config.FilterSubjects, []string{"foo", "bar"}) { |
| 2368 |
t.Fatalf("ConsumerInfo is not correct %+v", ci) |
| 2369 |
} |
| 2370 |
}) |
| 2371 |
|
| 2372 |
t.Run("ephemeral consumer with multiple filter subjects", func(t *testing.T) { |
| 2373 |
sub, err := nc.SubscribeSync("$JS.API.CONSUMER.CREATE.foo") |
| 2374 |
if err != nil { |
| 2375 |
t.Fatalf("Unexpected error: %v", err) |
| 2376 |
} |
| 2377 |
defer sub.Unsubscribe() |
| 2378 |
ci, err := js.AddConsumer("foo", &nats.ConsumerConfig{ |
| 2379 |
AckPolicy: nats.AckExplicitPolicy, |
| 2380 |
FilterSubjects: []string{"foo", "bar"}, |
| 2381 |
}) |
| 2382 |
if err != nil { |
| 2383 |
t.Fatalf("Unexpected error: %v", err) |
| 2384 |
} |
| 2385 |
_, err = sub.NextMsg(1 * time.Second) |
| 2386 |
if err != nil { |
| 2387 |
t.Fatalf("Unexpected error: %v", err) |
| 2388 |
} |
| 2389 |
if ci == nil || !reflect.DeepEqual(ci.Config.FilterSubjects, []string{"foo", "bar"}) { |
| 2390 |
t.Fatalf("ConsumerInfo is not correct %+v", ci) |
| 2391 |
} |
| 2392 |
}) |
| 2393 |
|
| 2394 |
t.Run("multiple filter subjects errors", func(t *testing.T) { |
| 2395 |
_, err := js.AddConsumer("foo", &nats.ConsumerConfig{ |
| 2396 |
AckPolicy: nats.AckExplicitPolicy, |
| 2397 |
FilterSubjects: []string{"foo", "bar"}, |
| 2398 |
FilterSubject: "baz", |
| 2399 |
}) |
| 2400 |
if !errors.Is(err, nats.ErrDuplicateFilterSubjects) { |
| 2401 |
t.Fatalf("Expected: %v; got: %v", nats.ErrDuplicateFilterSubjects, err) |
| 2402 |
} |
| 2403 |
_, err = js.AddConsumer("foo", &nats.ConsumerConfig{ |
| 2404 |
AckPolicy: nats.AckExplicitPolicy, |
| 2405 |
FilterSubjects: []string{"foo.*", "foo.A"}, |
| 2406 |
}) |
| 2407 |
if !errors.Is(err, nats.ErrOverlappingFilterSubjects) { |
| 2408 |
t.Fatalf("Expected: %v; got: %v", nats.ErrOverlappingFilterSubjects, err) |
| 2409 |
} |
| 2410 |
_, err = js.AddConsumer("foo", &nats.ConsumerConfig{ |
| 2411 |
AckPolicy: nats.AckExplicitPolicy, |
| 2412 |
FilterSubjects: []string{"foo", ""}, |
| 2413 |
}) |
| 2414 |
if !errors.Is(err, nats.ErrEmptyFilter) { |
| 2415 |
t.Fatalf("Expected: %v; got: %v", nats.ErrEmptyFilter, err) |
| 2416 |
} |
| 2417 |
}) |
| 2418 |
|
| 2419 |
t.Run("with invalid filter subject", func(t *testing.T) { |
| 2420 |
if _, err = js.AddConsumer("foo", &nats.ConsumerConfig{Name: "tc", FilterSubject: ".foo"}); !errors.Is(err, nats.ErrInvalidFilterSubject) { |
| 2421 |
t.Fatalf("Expected: %v; got: %v", nats.ErrInvalidFilterSubject, err) |
| 2422 |
} |
| 2423 |
if _, err = js.AddConsumer("foo", &nats.ConsumerConfig{Name: "tc", FilterSubject: "foo."}); !errors.Is(err, nats.ErrInvalidFilterSubject) { |
| 2424 |
t.Fatalf("Expected: %v; got: %v", nats.ErrInvalidFilterSubject, err) |
| 2425 |
} |
| 2426 |
}) |
| 2427 |
|
| 2428 |
t.Run("with invalid consumer name", func(t *testing.T) { |
| 2429 |
if _, err = js.AddConsumer("foo", &nats.ConsumerConfig{Durable: "test.durable"}); err != nats.ErrInvalidConsumerName { |
| 2430 |
t.Fatalf("Expected: %v; got: %v", nats.ErrInvalidConsumerName, err) |
| 2431 |
} |
| 2432 |
if _, err = js.AddConsumer("foo", &nats.ConsumerConfig{Durable: "test durable"}); err != nats.ErrInvalidConsumerName { |
| 2433 |
t.Fatalf("Expected: %v; got: %v", nats.ErrInvalidConsumerName, err) |
| 2434 |
} |
| 2435 |
}) |
| 2436 |
|
| 2437 |
t.Run("consumer with given name already exists, configs do not match", func(t *testing.T) { |
| 2438 |
if _, err = js.AddConsumer("foo", &nats.ConsumerConfig{Durable: "dlc", AckPolicy: nats.AckAllPolicy}); !errors.Is(err, nats.ErrConsumerNameAlreadyInUse) { |
| 2439 |
t.Fatalf("Expected error: %v; got: %v", nats.ErrConsumerNameAlreadyInUse, err) |
| 2440 |
} |
| 2441 |
}) |
| 2442 |
|
| 2443 |
t.Run("consumer with given name already exists, configs are the same", func(t *testing.T) { |
| 2444 |
if _, err = js.AddConsumer("foo", &nats.ConsumerConfig{Durable: "dlc", AckPolicy: nats.AckExplicitPolicy}); err != nil { |
| 2445 |
t.Fatalf("Expected no error; got: %v", err) |
| 2446 |
} |
| 2447 |
}) |
| 2448 |
|
| 2449 |
t.Run("stream does not exist", func(t *testing.T) { |
| 2450 |
_, err = js.AddConsumer("missing", &nats.ConsumerConfig{Durable: "dlc", AckPolicy: nats.AckExplicitPolicy}) |
| 2451 |
if err != nats.ErrStreamNotFound { |
| 2452 |
t.Fatalf("Expected stream not found error, got: %v", err) |
| 2453 |
} |
| 2454 |
}) |
| 2455 |
|
| 2456 |
t.Run("params validation error", func(t *testing.T) { |
| 2457 |
_, err = js.AddConsumer("", &nats.ConsumerConfig{Durable: "dlc", AckPolicy: nats.AckExplicitPolicy}) |
| 2458 |
if err != nats.ErrStreamNameRequired { |
| 2459 |
t.Fatalf("Expected %v, got: %v", nats.ErrStreamNameRequired, err) |
| 2460 |
} |
| 2461 |
_, err = js.AddConsumer("bad.stream.name", &nats.ConsumerConfig{Durable: "dlc", AckPolicy: nats.AckExplicitPolicy}) |
| 2462 |
if err != nats.ErrInvalidStreamName { |
| 2463 |
t.Fatalf("Expected %v, got: %v", nats.ErrInvalidStreamName, err) |
| 2464 |
} |
| 2465 |
_, err = js.AddConsumer("foo", &nats.ConsumerConfig{Durable: "bad.consumer.name", AckPolicy: nats.AckExplicitPolicy}) |
| 2466 |
if err != nats.ErrInvalidConsumerName { |
| 2467 |
t.Fatalf("Expected %v, got: %v", nats.ErrInvalidConsumerName, err) |
| 2468 |
} |
| 2469 |
}) |
| 2470 |
}) |
| 2471 |
|
| 2472 |
t.Run("consumer info", func(t *testing.T) { |
| 2473 |
if _, err := js.ConsumerInfo("", "dlc"); err != nats.ErrStreamNameRequired { |
| 2474 |
t.Fatalf("Expected %v, got %v", nats.ErrStreamNameRequired, err) |
| 2475 |
} |
| 2476 |
if _, err := js.ConsumerInfo("bad.stream.name", "dlc"); err != nats.ErrInvalidStreamName { |
| 2477 |
t.Fatalf("Expected %v, got %v", nats.ErrInvalidStreamName, err) |
| 2478 |
} |
| 2479 |
if _, err := js.ConsumerInfo("foo", ""); err != nats.ErrConsumerNameRequired { |
| 2480 |
t.Fatalf("Expected %v, got %v", nats.ErrConsumerNameRequired, err) |
| 2481 |
} |
| 2482 |
if _, err := js.ConsumerInfo("foo", "bad.consumer.name"); err != nats.ErrInvalidConsumerName { |
| 2483 |
t.Fatalf("Expected %v, got %v", nats.ErrInvalidConsumerName, err) |
| 2484 |
} |
| 2485 |
ci, err := js.ConsumerInfo("foo", "dlc") |
| 2486 |
if err != nil { |
| 2487 |
t.Fatalf("Unexpected error: %v", err) |
| 2488 |
} |
| 2489 |
if ci == nil || ci.Config.Durable != "dlc" { |
| 2490 |
t.Fatalf("ConsumerInfo is not correct %+v", si) |
| 2491 |
} |
| 2492 |
}) |
| 2493 |
|
| 2494 |
t.Run("consumer not found", func(t *testing.T) { |
| 2495 |
ci, err := js.ConsumerInfo("foo", "cld") |
| 2496 |
if !errors.Is(err, nats.ErrConsumerNotFound) { |
| 2497 |
t.Fatalf("Expected error: %v, got: %v", nats.ErrConsumerNotFound, err) |
| 2498 |
} |
| 2499 |
if ci != nil { |
| 2500 |
t.Fatalf("ConsumerInfo should be nil %+v", ci) |
| 2501 |
} |
| 2502 |
}) |
| 2503 |
|
| 2504 |
t.Run("list streams", func(t *testing.T) { |
| 2505 |
var infos []*nats.StreamInfo |
| 2506 |
for info := range js.Streams() { |
| 2507 |
infos = append(infos, info) |
| 2508 |
} |
| 2509 |
if len(infos) != 1 || infos[0].Config.Name != "foo" { |
| 2510 |
t.Fatalf("StreamInfo is not correct %+v", infos) |
| 2511 |
} |
| 2512 |
}) |
| 2513 |
|
| 2514 |
t.Run("list consumers", func(t *testing.T) { |
| 2515 |
var infos []*nats.ConsumerInfo |
| 2516 |
for info := range js.Consumers("") { |
| 2517 |
infos = append(infos, info) |
| 2518 |
} |
| 2519 |
if len(infos) != 0 { |
| 2520 |
t.Fatalf("ConsumerInfo is not correct %+v", infos) |
| 2521 |
} |
| 2522 |
for info := range js.Consumers("bad.stream.name") { |
| 2523 |
infos = append(infos, info) |
| 2524 |
} |
| 2525 |
if len(infos) != 0 { |
| 2526 |
t.Fatalf("ConsumerInfo is not correct %+v", infos) |
| 2527 |
} |
| 2528 |
infos = infos[:0] |
| 2529 |
for info := range js.Consumers("foo") { |
| 2530 |
infos = append(infos, info) |
| 2531 |
} |
| 2532 |
if len(infos) != 8 || infos[0].Stream != "foo" { |
| 2533 |
t.Fatalf("ConsumerInfo is not correct %+v", infos) |
| 2534 |
} |
| 2535 |
}) |
| 2536 |
|
| 2537 |
t.Run("list consumer names", func(t *testing.T) { |
| 2538 |
var names []string |
| 2539 |
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) |
| 2540 |
defer cancel() |
| 2541 |
for name := range js.ConsumerNames("foo", nats.Context(ctx)) { |
| 2542 |
names = append(names, name) |
| 2543 |
} |
| 2544 |
if got, want := len(names), 8; got != want { |
| 2545 |
t.Fatalf("Unexpected names, got=%d, want=%d", got, want) |
| 2546 |
} |
| 2547 |
}) |
| 2548 |
|
| 2549 |
t.Run("delete consumers", func(t *testing.T) { |
| 2550 |
if err := js.DeleteConsumer("", "dlc"); err != nats.ErrStreamNameRequired { |
| 2551 |
t.Fatalf("Expected %v, got %v", nats.ErrStreamNameRequired, err) |
| 2552 |
} |
| 2553 |
if err := js.DeleteConsumer("bad.stream.name", "dlc"); err != nats.ErrInvalidStreamName { |
| 2554 |
t.Fatalf("Expected %v, got %v", nats.ErrInvalidStreamName, err) |
| 2555 |
} |
| 2556 |
if err := js.DeleteConsumer("foo", ""); err != nats.ErrConsumerNameRequired { |
| 2557 |
t.Fatalf("Expected %v, got %v", nats.ErrConsumerNameRequired, err) |
| 2558 |
} |
| 2559 |
if err := js.DeleteConsumer("foo", "bad.consumer.name"); err != nats.ErrInvalidConsumerName { |
| 2560 |
t.Fatalf("Expected %v, got %v", nats.ErrInvalidConsumerName, err) |
| 2561 |
} |
| 2562 |
if err := js.DeleteConsumer("foo", "dlc"); err != nil { |
| 2563 |
t.Fatalf("Unexpected error: %v", err) |
| 2564 |
} |
| 2565 |
}) |
| 2566 |
|
| 2567 |
t.Run("update consumer", func(t *testing.T) { |
| 2568 |
ci, err := js.AddConsumer("foo", &nats.ConsumerConfig{ |
| 2569 |
Durable: "update_push_consumer", |
| 2570 |
DeliverSubject: "bar", |
| 2571 |
AckPolicy: nats.AckExplicitPolicy, |
| 2572 |
}) |
| 2573 |
if err != nil { |
| 2574 |
t.Fatalf("Unexpected error: %v", err) |
| 2575 |
} |
| 2576 |
// Currently, server supports these fields: |
| 2577 |
// description, ack_wait, max_deliver, sample_freq, max_ack_pending, max_waiting and headers_only |
| 2578 |
expected := ci.Config |
| 2579 |
expected.Description = "my description" |
| 2580 |
expected.AckWait = 2 * time.Second |
| 2581 |
expected.MaxDeliver = 1 |
| 2582 |
expected.SampleFrequency = "30" |
| 2583 |
expected.MaxAckPending = 10 |
| 2584 |
expected.HeadersOnly = true |
| 2585 |
|
| 2586 |
_, err = js.UpdateConsumer("", &expected) |
| 2587 |
if err != nats.ErrStreamNameRequired { |
| 2588 |
t.Fatalf("Expected stream name required error, got %v", err) |
| 2589 |
} |
| 2590 |
_, err = js.UpdateConsumer("bad.stream.name", &expected) |
| 2591 |
if err != nats.ErrInvalidStreamName { |
| 2592 |
t.Fatalf("Expected stream name required error, got %v", err) |
| 2593 |
} |
| 2594 |
expected.Durable = "" |
| 2595 |
expected.Name = "" |
| 2596 |
_, err = js.UpdateConsumer("foo", &expected) |
| 2597 |
if err != nats.ErrConsumerNameRequired { |
| 2598 |
t.Fatalf("Expected consumer name required error, got %v", err) |
| 2599 |
} |
| 2600 |
expected.Durable = "bad.consumer.name" |
| 2601 |
_, err = js.UpdateConsumer("foo", &expected) |
| 2602 |
if err != nats.ErrInvalidConsumerName { |
| 2603 |
t.Fatalf("Expected invalid consumer name error, got %v", err) |
| 2604 |
} |
| 2605 |
expected.Durable = "update_push_consumer" |
| 2606 |
|
| 2607 |
_, err = js.UpdateConsumer("foo", nil) |
| 2608 |
if err != nats.ErrConsumerConfigRequired { |
| 2609 |
t.Fatalf("Expected consumer configuration required error, got %v", err) |
| 2610 |
} |
| 2611 |
|
| 2612 |
ci, err = js.UpdateConsumer("foo", &expected) |
| 2613 |
if err != nil { |
| 2614 |
t.Fatalf("Error on update: %v", err) |
| 2615 |
} |
| 2616 |
expected.Name = "update_push_consumer" |
| 2617 |
if !reflect.DeepEqual(ci.Config, expected) { |
| 2618 |
t.Fatalf("Expected config to be %+v, got %+v", expected, ci.Config) |
| 2619 |
} |
| 2620 |
|
| 2621 |
ci, err = js.AddConsumer("foo", &nats.ConsumerConfig{ |
| 2622 |
Durable: "update_pull_consumer", |
| 2623 |
AckPolicy: nats.AckExplicitPolicy, |
| 2624 |
MaxWaiting: 1, |
| 2625 |
}) |
| 2626 |
if err != nil { |
| 2627 |
t.Fatalf("Unexpected error: %v", err) |
| 2628 |
} |
| 2629 |
expected = ci.Config |
| 2630 |
expected.Description = "my description" |
| 2631 |
expected.AckWait = 2 * time.Second |
| 2632 |
expected.MaxDeliver = 1 |
| 2633 |
expected.SampleFrequency = "30" |
| 2634 |
expected.MaxAckPending = 10 |
| 2635 |
expected.HeadersOnly = true |
| 2636 |
expected.MaxRequestBatch = 10 |
| 2637 |
expected.MaxRequestExpires = 2 * time.Second |
| 2638 |
expected.MaxRequestMaxBytes = 1024 |
| 2639 |
|
| 2640 |
ci, err = js.UpdateConsumer("foo", &expected) |
| 2641 |
if err != nil { |
| 2642 |
t.Fatalf("Error on update: %v", err) |
| 2643 |
} |
| 2644 |
if !reflect.DeepEqual(ci.Config, expected) { |
| 2645 |
t.Fatalf("Expected config to be %+v, got %+v", expected, ci.Config) |
| 2646 |
} |
| 2647 |
}) |
| 2648 |
|
| 2649 |
t.Run("purge stream", func(t *testing.T) { |
| 2650 |
if err := js.PurgeStream(""); err != nats.ErrStreamNameRequired { |
| 2651 |
t.Fatalf("Expected %v, got %v", nats.ErrStreamNameRequired, err) |
| 2652 |
} |
| 2653 |
if err := js.PurgeStream("bad.stream.name"); err != nats.ErrInvalidStreamName { |
| 2654 |
t.Fatalf("Expected %v, got %v", nats.ErrInvalidStreamName, err) |
| 2655 |
} |
| 2656 |
if err := js.PurgeStream("foo"); err != nil { |
| 2657 |
t.Fatalf("Unexpected error: %v", err) |
| 2658 |
} |
| 2659 |
if si, err := js.StreamInfo("foo"); err != nil { |
| 2660 |
t.Fatalf("Unexpected error: %v", err) |
| 2661 |
} else if si.State.Msgs != 0 { |
| 2662 |
t.Fatalf("StreamInfo.Msgs is not correct") |
| 2663 |
} |
| 2664 |
}) |
| 2665 |
|
| 2666 |
t.Run("list stream names", func(t *testing.T) { |
| 2667 |
var names []string |
| 2668 |
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) |
| 2669 |
defer cancel() |
| 2670 |
for name := range js.StreamNames(nats.Context(ctx)) { |
| 2671 |
names = append(names, name) |
| 2672 |
} |
| 2673 |
if got, want := len(names), 1; got != want { |
| 2674 |
t.Fatalf("Unexpected names, got=%d, want=%d", got, want) |
| 2675 |
} |
| 2676 |
}) |
| 2677 |
|
| 2678 |
t.Run("delete stream", func(t *testing.T) { |
| 2679 |
if err := js.DeleteStream(""); err != nats.ErrStreamNameRequired { |
| 2680 |
t.Fatalf("Expected %v, got %v", nats.ErrStreamNameRequired, err) |
| 2681 |
} |
| 2682 |
if err := js.DeleteStream("bad.stream.name"); err != nats.ErrInvalidStreamName { |
| 2683 |
t.Fatalf("Expected %v, got %v", nats.ErrInvalidStreamName, err) |
| 2684 |
} |
| 2685 |
if err := js.DeleteStream("foo"); err != nil { |
| 2686 |
t.Fatalf("Unexpected error: %v", err) |
| 2687 |
} |
| 2688 |
if _, err := js.StreamInfo("foo"); err == nil { |
| 2689 |
t.Fatalf("Unexpected success") |
| 2690 |
} |
| 2691 |
}) |
| 2692 |
|
| 2693 |
t.Run("fetch account info", func(t *testing.T) { |
| 2694 |
info, err := js.AccountInfo() |
| 2695 |
if err != nil { |
| 2696 |
t.Fatal(err) |
| 2697 |
} |
| 2698 |
if info.Limits.MaxMemory != 67108864 { |
| 2699 |
t.Errorf("Expected to have memory limits, got: %v", info.Limits.MaxMemory) |
| 2700 |
} |
| 2701 |
if info.Limits.MaxStore != 67108864 { |
| 2702 |
t.Errorf("Expected to have disk limits, got: %v", info.Limits.MaxMemory) |
| 2703 |
} |
| 2704 |
if info.Limits.MaxStreams != -1 { |
| 2705 |
t.Errorf("Expected to not have stream limits, got: %v", info.Limits.MaxStreams) |
| 2706 |
} |
| 2707 |
if info.Limits.MaxConsumers != -1 { |
| 2708 |
t.Errorf("Expected to not have consumer limits, got: %v", info.Limits.MaxConsumers) |
| 2709 |
} |
| 2710 |
if info.API.Total == 0 { |
| 2711 |
t.Errorf("Expected some API calls, got: %v", info.API.Total) |
| 2712 |
} |
| 2713 |
if info.API.Errors == 0 { |
| 2714 |
t.Errorf("Expected some API error, got: %v", info.API.Errors) |
| 2715 |
} |
| 2716 |
}) |
| 2717 |
} |
| 2718 |
|
| 2719 |
func TestStreamConfigMatches(t *testing.T) { |
| 2720 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 2721 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 2722 |
if err != nil { |
| 2723 |
t.Fatalf("Unexpected error: %v", err) |
| 2724 |
} |
| 2725 |
|
| 2726 |
cfg := nats.StreamConfig{ |
| 2727 |
Name: "stream", |
| 2728 |
Description: "desc", |
| 2729 |
Subjects: []string{"foo.*"}, |
| 2730 |
Retention: nats.WorkQueuePolicy, |
| 2731 |
MaxConsumers: 10, |
| 2732 |
MaxMsgs: 100, |
| 2733 |
MaxBytes: 1000, |
| 2734 |
Discard: nats.DiscardNew, |
| 2735 |
DiscardNewPerSubject: true, |
| 2736 |
MaxAge: 100 * time.Second, |
| 2737 |
MaxMsgsPerSubject: 1000, |
| 2738 |
MaxMsgSize: 10000, |
| 2739 |
Storage: nats.MemoryStorage, |
| 2740 |
Replicas: 1, |
| 2741 |
NoAck: true, |
| 2742 |
Duplicates: 10 * time.Second, |
| 2743 |
Sealed: false, |
| 2744 |
DenyDelete: true, |
| 2745 |
DenyPurge: false, |
| 2746 |
AllowRollup: true, |
| 2747 |
Compression: nats.S2Compression, |
| 2748 |
FirstSeq: 5, |
| 2749 |
SubjectTransform: &nats.SubjectTransformConfig{Source: ">", Destination: "transformed.>"}, |
| 2750 |
RePublish: &nats.RePublish{ |
| 2751 |
Source: ">", |
| 2752 |
Destination: "RP.>", |
| 2753 |
HeadersOnly: true, |
| 2754 |
}, |
| 2755 |
AllowDirect: true, |
| 2756 |
ConsumerLimits: nats.StreamConsumerLimits{ |
| 2757 |
InactiveThreshold: 10 * time.Second, |
| 2758 |
MaxAckPending: 500, |
| 2759 |
}, |
| 2760 |
} |
| 2761 |
|
| 2762 |
s, err := js.AddStream(&cfg) |
| 2763 |
if err != nil { |
| 2764 |
t.Fatalf("Unexpected error: %v", err) |
| 2765 |
} |
| 2766 |
// server will set metadata values, so we need to clear them |
| 2767 |
s.Config.Metadata = nil |
| 2768 |
if !reflect.DeepEqual(s.Config, cfg) { |
| 2769 |
t.Fatalf("StreamConfig doesn't match: %#v", s.Config) |
| 2770 |
} |
| 2771 |
|
| 2772 |
cfgMirror := nats.StreamConfig{ |
| 2773 |
Name: "mirror", |
| 2774 |
MaxConsumers: 10, |
| 2775 |
MaxMsgs: 100, |
| 2776 |
MaxBytes: 1000, |
| 2777 |
MaxAge: 100 * time.Second, |
| 2778 |
MaxMsgsPerSubject: 1000, |
| 2779 |
MaxMsgSize: 10000, |
| 2780 |
Replicas: 1, |
| 2781 |
Duplicates: 10 * time.Second, |
| 2782 |
Mirror: &nats.StreamSource{ |
| 2783 |
Name: "stream", |
| 2784 |
OptStartSeq: 10, |
| 2785 |
SubjectTransforms: []nats.SubjectTransformConfig{ |
| 2786 |
{Source: ">", Destination: "transformed.>"}, |
| 2787 |
}, |
| 2788 |
}, |
| 2789 |
MirrorDirect: true, |
| 2790 |
SubjectTransform: &nats.SubjectTransformConfig{Source: ">", Destination: "transformed.>"}, |
| 2791 |
} |
| 2792 |
|
| 2793 |
s, err = js.AddStream(&cfgMirror) |
| 2794 |
if err != nil { |
| 2795 |
t.Fatalf("Unexpected error: %v", err) |
| 2796 |
} |
| 2797 |
s.Config.Metadata = nil |
| 2798 |
if !reflect.DeepEqual(s.Config, cfgMirror) { |
| 2799 |
t.Fatalf("StreamConfig doesn't match: %#v", s.Config) |
| 2800 |
} |
| 2801 |
|
| 2802 |
cfgSourcing := nats.StreamConfig{ |
| 2803 |
Name: "sourcing", |
| 2804 |
Subjects: []string{"BAR"}, |
| 2805 |
MaxConsumers: 10, |
| 2806 |
MaxMsgs: 100, |
| 2807 |
MaxBytes: 1000, |
| 2808 |
MaxAge: 100 * time.Second, |
| 2809 |
MaxMsgsPerSubject: 1000, |
| 2810 |
MaxMsgSize: 10000, |
| 2811 |
Replicas: 1, |
| 2812 |
Duplicates: 10 * time.Second, |
| 2813 |
Sources: []*nats.StreamSource{ |
| 2814 |
{ |
| 2815 |
Name: "stream", |
| 2816 |
OptStartSeq: 10, |
| 2817 |
SubjectTransforms: []nats.SubjectTransformConfig{ |
| 2818 |
{Source: ">", Destination: "transformed.>"}, |
| 2819 |
}, |
| 2820 |
}, |
| 2821 |
}, |
| 2822 |
SubjectTransform: &nats.SubjectTransformConfig{Source: ">", Destination: "transformed.>"}, |
| 2823 |
} |
| 2824 |
|
| 2825 |
s, err = js.AddStream(&cfgSourcing) |
| 2826 |
if err != nil { |
| 2827 |
t.Fatalf("Unexpected error: %v", err) |
| 2828 |
} |
| 2829 |
s.Config.Metadata = nil |
| 2830 |
if !reflect.DeepEqual(s.Config, cfgSourcing) { |
| 2831 |
t.Fatalf("StreamConfig doesn't match: %#v", s.Config) |
| 2832 |
} |
| 2833 |
}) |
| 2834 |
} |
| 2835 |
|
| 2836 |
func TestJetStreamClusterStreamLeaderChangeClientErr(t *testing.T) { |
| 2837 |
t.Skip("The 2.9 server changed behavior making this test fail now") |
| 2838 |
} |
| 2839 |
|
| 2840 |
func TestJetStreamConsumerConfigReplicasAndMemStorage(t *testing.T) { |
| 2841 |
c := newTester(t) |
| 2842 |
inst := c.CreateCluster(t, 3, true) |
| 2843 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 2844 |
|
| 2845 |
nc := dialInstance(t, inst) |
| 2846 |
c.WaitForJetStream(t, nc) |
| 2847 |
waitForJSCluster(t, nc) |
| 2848 |
|
| 2849 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 2850 |
if err != nil { |
| 2851 |
t.Fatalf("Unexpected error: %v", err) |
| 2852 |
} |
| 2853 |
|
| 2854 |
if _, err := js.AddStream(&nats.StreamConfig{ |
| 2855 |
Name: "CR", |
| 2856 |
Subjects: []string{"foo"}, |
| 2857 |
Replicas: 3, |
| 2858 |
}); err != nil { |
| 2859 |
t.Fatalf("Error adding stream: %v", err) |
| 2860 |
} |
| 2861 |
|
| 2862 |
// We can't really check if the consumer ends-up with memory storage or not. |
| 2863 |
// We are simply going to create a NATS subscription on the request subject |
| 2864 |
// and make sure that the request contains "mem_storage:true". |
| 2865 |
sub, err := nc.SubscribeSync("$JS.API.CONSUMER.CREATE.CR.dur") |
| 2866 |
if err != nil { |
| 2867 |
t.Fatalf("Error on subscribe: %v", err) |
| 2868 |
} |
| 2869 |
|
| 2870 |
ci, err := js.AddConsumer("CR", &nats.ConsumerConfig{ |
| 2871 |
Durable: "dur", |
| 2872 |
DeliverSubject: "bar", |
| 2873 |
Replicas: 1, |
| 2874 |
MemoryStorage: true, |
| 2875 |
}) |
| 2876 |
if err != nil { |
| 2877 |
t.Fatalf("Error adding consumer: %v", err) |
| 2878 |
} |
| 2879 |
if n := len(ci.Cluster.Replicas); n > 0 { |
| 2880 |
t.Fatalf("Expected replicas to be 1, got %+v", ci.Cluster) |
| 2881 |
} |
| 2882 |
msg, err := sub.NextMsg(time.Second) |
| 2883 |
if err != nil { |
| 2884 |
t.Fatalf("Error on next msg: %v", err) |
| 2885 |
} |
| 2886 |
if str := string(msg.Data); !strings.Contains(str, "mem_storage\":true") { |
| 2887 |
t.Fatalf("Does not look like the request asked for memory storage: %s", str) |
| 2888 |
} |
| 2889 |
} |
| 2890 |
|
| 2891 |
func TestJetStreamRePublish(t *testing.T) { |
| 2892 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 2893 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 2894 |
if err != nil { |
| 2895 |
t.Fatalf("Unexpected error: %v", err) |
| 2896 |
} |
| 2897 |
|
| 2898 |
if _, err := js.AddStream(&nats.StreamConfig{ |
| 2899 |
Name: "RP", |
| 2900 |
Storage: nats.MemoryStorage, |
| 2901 |
Subjects: []string{"foo", "bar", "baz"}, |
| 2902 |
RePublish: &nats.RePublish{ |
| 2903 |
Source: ">", |
| 2904 |
Destination: "RP.>", |
| 2905 |
}, |
| 2906 |
}); err != nil { |
| 2907 |
t.Fatalf("Error adding stream: %v", err) |
| 2908 |
} |
| 2909 |
|
| 2910 |
sub, err := nc.SubscribeSync("RP.>") |
| 2911 |
if err != nil { |
| 2912 |
t.Fatalf("Error on subscribe: %v", err) |
| 2913 |
} |
| 2914 |
|
| 2915 |
msg, toSend := []byte("OK TO REPUBLISH?"), 100 |
| 2916 |
for range toSend { |
| 2917 |
js.Publish("foo", msg) |
| 2918 |
js.Publish("bar", msg) |
| 2919 |
js.Publish("baz", msg) |
| 2920 |
} |
| 2921 |
|
| 2922 |
lseq := map[string]int{ |
| 2923 |
"foo": 0, |
| 2924 |
"bar": 0, |
| 2925 |
"baz": 0, |
| 2926 |
} |
| 2927 |
|
| 2928 |
for i := 1; i <= toSend; i++ { |
| 2929 |
m, err := sub.NextMsg(time.Second) |
| 2930 |
if err != nil { |
| 2931 |
t.Fatalf("Error on next msg: %v", err) |
| 2932 |
} |
| 2933 |
stream := m.Header.Get(nats.JSStream) |
| 2934 |
if stream != "RP" { |
| 2935 |
t.Fatalf("Unexpected header: %+v", m.Header) |
| 2936 |
} |
| 2937 |
seq, err := strconv.Atoi(m.Header.Get(nats.JSSequence)) |
| 2938 |
if err != nil { |
| 2939 |
t.Fatalf("Error decoding sequence for %s", m.Header.Get(nats.JSSequence)) |
| 2940 |
} |
| 2941 |
if seq != i { |
| 2942 |
t.Fatalf("Expected sequence to be %v, got %v", i, seq) |
| 2943 |
} |
| 2944 |
last, err := strconv.Atoi(m.Header.Get(nats.JSLastSequence)) |
| 2945 |
if err != nil { |
| 2946 |
t.Fatalf("Error decoding last sequence for %s", m.Header.Get(nats.JSLastSequence)) |
| 2947 |
} |
| 2948 |
if last != lseq[m.Subject] { |
| 2949 |
t.Fatalf("Expected last sequence to be %v, got %v", lseq[m.Subject], last) |
| 2950 |
} |
| 2951 |
lseq[m.Subject] = seq |
| 2952 |
} |
| 2953 |
}) |
| 2954 |
} |
| 2955 |
|
| 2956 |
func TestJetStreamDirectGetMsg(t *testing.T) { |
| 2957 |
// Using standalone server here, we are testing the client side API, not |
| 2958 |
// the server feature, which has tests checking it works in cluster mode. |
| 2959 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 2960 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 2961 |
if err != nil { |
| 2962 |
t.Fatalf("Unexpected error: %v", err) |
| 2963 |
} |
| 2964 |
|
| 2965 |
si, err := js.AddStream(&nats.StreamConfig{ |
| 2966 |
Name: "DGM", |
| 2967 |
Storage: nats.MemoryStorage, |
| 2968 |
Subjects: []string{"foo", "bar"}, |
| 2969 |
}) |
| 2970 |
if err != nil { |
| 2971 |
t.Fatalf("Error adding stream: %v", err) |
| 2972 |
} |
| 2973 |
|
| 2974 |
send := func(subj, body string) { |
| 2975 |
t.Helper() |
| 2976 |
if _, err := js.Publish(subj, []byte(body)); err != nil { |
| 2977 |
t.Fatalf("Error on publish: %v", err) |
| 2978 |
} |
| 2979 |
} |
| 2980 |
|
| 2981 |
send("foo", "a") |
| 2982 |
send("foo", "b") |
| 2983 |
send("foo", "c") |
| 2984 |
send("bar", "d") |
| 2985 |
send("foo", "e") |
| 2986 |
|
| 2987 |
// Without AllowDirect, we should get no responders |
| 2988 |
if _, err := js.GetMsg("DGM", 1, nats.DirectGet()); err != nats.ErrNoResponders { |
| 2989 |
t.Fatalf("Unexpected error: %v", err) |
| 2990 |
} |
| 2991 |
|
| 2992 |
si.Config.AllowDirect = true |
| 2993 |
si, err = js.UpdateStream(&si.Config) |
| 2994 |
if err != nil { |
| 2995 |
t.Fatalf("Error updating stream: %v", err) |
| 2996 |
} |
| 2997 |
if !si.Config.AllowDirect { |
| 2998 |
t.Fatalf("AllowDirect should be true: %+v", si) |
| 2999 |
} |
| 3000 |
|
| 3001 |
check := func(seq uint64, opt nats.JSOpt, useGetLast bool, expectedSubj string, expectedSeq uint64, expectedBody string) { |
| 3002 |
t.Helper() |
| 3003 |
|
| 3004 |
var msg *nats.RawStreamMsg |
| 3005 |
var err error |
| 3006 |
if useGetLast { |
| 3007 |
msg, err = js.GetLastMsg("DGM", expectedSubj, []nats.JSOpt{opt}...) |
| 3008 |
} else { |
| 3009 |
msg, err = js.GetMsg("DGM", seq, []nats.JSOpt{opt}...) |
| 3010 |
} |
| 3011 |
if err != nil { |
| 3012 |
t.Fatalf("Unable to get message: %v", err) |
| 3013 |
} |
| 3014 |
if msg.Subject != expectedSubj { |
| 3015 |
t.Fatalf("Expected subject %q, got %q", expectedSubj, msg.Subject) |
| 3016 |
} |
| 3017 |
if msg.Sequence != expectedSeq { |
| 3018 |
t.Fatalf("Expected sequence %v, got %v", expectedSeq, msg.Sequence) |
| 3019 |
} |
| 3020 |
if msg.Time.IsZero() { |
| 3021 |
t.Fatal("Expected timestamp, did not get one") |
| 3022 |
} |
| 3023 |
if b := string(msg.Data); b != expectedBody { |
| 3024 |
t.Fatalf("Expected body %q, got %q", expectedBody, b) |
| 3025 |
} |
| 3026 |
} |
| 3027 |
|
| 3028 |
check(0, nats.DirectGetNext("bar"), false, "bar", 4, "d") |
| 3029 |
check(0, nats.DirectGet(), true, "foo", 5, "e") |
| 3030 |
check(0, nats.DirectGetNext("foo"), false, "foo", 1, "a") |
| 3031 |
check(4, nats.DirectGetNext("foo"), false, "foo", 5, "e") |
| 3032 |
check(2, nats.DirectGetNext("foo"), false, "foo", 2, "b") |
| 3033 |
|
| 3034 |
msg := nats.NewMsg("foo") |
| 3035 |
msg.Header.Set("MyHeader", "MyValue") |
| 3036 |
if _, err := js.PublishMsg(msg); err != nil { |
| 3037 |
t.Fatalf("Error publishing message: %v", err) |
| 3038 |
} |
| 3039 |
r, err := js.GetMsg("DGM", 6, nats.DirectGet()) |
| 3040 |
if err != nil { |
| 3041 |
t.Fatalf("Error getting message: %v", err) |
| 3042 |
} |
| 3043 |
if v := r.Header.Get("MyHeader"); v != "MyValue" { |
| 3044 |
t.Fatalf("Expected header to be present, was not: %v", r.Header) |
| 3045 |
} |
| 3046 |
|
| 3047 |
if _, err := js.GetMsg("DGM", 100, nats.DirectGet()); err != nats.ErrMsgNotFound { |
| 3048 |
t.Fatalf("Expected not found error, got %v", err) |
| 3049 |
} |
| 3050 |
if _, err := js.GetMsg("DGM", 0, nats.DirectGet()); err == nil || !strings.Contains(err.Error(), "Empty Request") { |
| 3051 |
t.Fatalf("Unexpected error: %v", err) |
| 3052 |
} |
| 3053 |
|
| 3054 |
r, err = js.GetLastMsg("DGM", "bar", nats.DirectGet()) |
| 3055 |
if err != nil { |
| 3056 |
t.Fatalf("Error getting message: %v", err) |
| 3057 |
} |
| 3058 |
if r.Subject != "bar" { |
| 3059 |
t.Fatalf("expected subject to be 'bar', got: %v", r.Subject) |
| 3060 |
} |
| 3061 |
if string(r.Data) != "d" { |
| 3062 |
t.Fatalf("expected data to be 'd', got: %v", string(r.Data)) |
| 3063 |
} |
| 3064 |
}) |
| 3065 |
} |
| 3066 |
|
| 3067 |
func TestJetStreamConsumerReplicasOption(t *testing.T) { |
| 3068 |
c := newTester(t) |
| 3069 |
inst := c.CreateCluster(t, 3, true) |
| 3070 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 3071 |
|
| 3072 |
nc := dialInstance(t, inst) |
| 3073 |
c.WaitForJetStream(t, nc) |
| 3074 |
waitForJSCluster(t, nc) |
| 3075 |
|
| 3076 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 3077 |
if err != nil { |
| 3078 |
t.Fatalf("Unexpected error: %v", err) |
| 3079 |
} |
| 3080 |
|
| 3081 |
if _, err := js.AddStream(&nats.StreamConfig{ |
| 3082 |
Name: "ConsumerReplicasTest", |
| 3083 |
Subjects: []string{"foo"}, |
| 3084 |
Replicas: 3, |
| 3085 |
}); err != nil { |
| 3086 |
t.Fatalf("Error adding stream: %v", err) |
| 3087 |
} |
| 3088 |
|
| 3089 |
// Subscribe to the stream with a durable consumer "bar" and replica set to 1. |
| 3090 |
cb := func(msg *nats.Msg) {} |
| 3091 |
_, err = js.Subscribe("foo", cb, nats.Durable("bar"), nats.ConsumerReplicas(1)) |
| 3092 |
if err != nil { |
| 3093 |
t.Fatalf("Error on subscribe: %v", err) |
| 3094 |
} |
| 3095 |
|
| 3096 |
consInfo, err := js.ConsumerInfo("ConsumerReplicasTest", "bar") |
| 3097 |
if err != nil { |
| 3098 |
t.Fatalf("Error getting consumer info: %v", err) |
| 3099 |
} |
| 3100 |
|
| 3101 |
if consInfo.Config.Replicas != 1 { |
| 3102 |
t.Fatalf("Expected consumer replica to be %v, got %+v", 1, consInfo.Config.Replicas) |
| 3103 |
} |
| 3104 |
} |
| 3105 |
|
| 3106 |
func TestJetStreamMsgAckShouldErrForConsumerAckNone(t *testing.T) { |
| 3107 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 3108 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 3109 |
if err != nil { |
| 3110 |
t.Fatalf("Unexpected error: %v", err) |
| 3111 |
} |
| 3112 |
|
| 3113 |
if _, err := js.AddStream(&nats.StreamConfig{ |
| 3114 |
Name: "ACKNONE", |
| 3115 |
Storage: nats.MemoryStorage, |
| 3116 |
Subjects: []string{"foo"}, |
| 3117 |
}); err != nil { |
| 3118 |
t.Fatalf("Error adding stream: %v", err) |
| 3119 |
} |
| 3120 |
if _, err := js.Publish("foo", []byte("hello")); err != nil { |
| 3121 |
t.Fatalf("Error on publish: %v", err) |
| 3122 |
} |
| 3123 |
|
| 3124 |
sub, err := js.SubscribeSync("foo", nats.OrderedConsumer()) |
| 3125 |
if err != nil { |
| 3126 |
t.Fatalf("Error on subscribe: %v", err) |
| 3127 |
} |
| 3128 |
msg, err := sub.NextMsg(time.Second) |
| 3129 |
if err != nil { |
| 3130 |
t.Fatalf("Error getting message: %v", err) |
| 3131 |
} |
| 3132 |
if err := msg.Ack(); err != nats.ErrCantAckIfConsumerAckNone { |
| 3133 |
t.Fatalf("Expected error indicating that sub is AckNone, got %v", err) |
| 3134 |
} |
| 3135 |
}) |
| 3136 |
} |
| 3137 |
|
| 3138 |
func TestJetStreamOrderedConsumerRecreateAfterReconnect(t *testing.T) { |
| 3139 |
c := newTester(t) |
| 3140 |
inst := c.CreateServer(t, true) |
| 3141 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 3142 |
|
| 3143 |
// monitor for ErrConsumerNotActive error and suppress logging |
| 3144 |
hbMissed := make(chan struct{}, 10) |
| 3145 |
errHandler := func(c *nats.Conn, s *nats.Subscription, err error) { |
| 3146 |
if !errors.Is(err, nats.ErrConsumerNotActive) { |
| 3147 |
t.Errorf("Unexpected error: %v", err) |
| 3148 |
return |
| 3149 |
} |
| 3150 |
select { |
| 3151 |
case hbMissed <- struct{}{}: |
| 3152 |
default: |
| 3153 |
} |
| 3154 |
} |
| 3155 |
nc := dialInstance(t, inst, nats.ErrorHandler(errHandler)) |
| 3156 |
c.WaitForJetStream(t, nc) |
| 3157 |
|
| 3158 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 3159 |
if err != nil { |
| 3160 |
t.Fatalf("Unexpected error: %v", err) |
| 3161 |
} |
| 3162 |
|
| 3163 |
if _, err := js.AddStream(&nats.StreamConfig{Name: "foo", Subjects: []string{"FOO.*"}}); err != nil { |
| 3164 |
t.Fatalf("Unexpected error: %v", err) |
| 3165 |
} |
| 3166 |
sub, err := js.SubscribeSync("FOO.A", nats.OrderedConsumer(), nats.IdleHeartbeat(100*time.Millisecond)) |
| 3167 |
if err != nil { |
| 3168 |
t.Fatalf("Unexpected error: %v", err) |
| 3169 |
} |
| 3170 |
consInfo, err := sub.ConsumerInfo() |
| 3171 |
if err != nil { |
| 3172 |
t.Fatalf("Unexpected error: %v", err) |
| 3173 |
} |
| 3174 |
consName := consInfo.Name |
| 3175 |
// validate that the generated name of the consumer is 8 |
| 3176 |
// characters long (shorter than standard nuid) |
| 3177 |
if len(consName) != 8 { |
| 3178 |
t.Fatalf("Unexpected consumer name: %q", consName) |
| 3179 |
} |
| 3180 |
if _, err := js.Publish("FOO.A", []byte("msg 1")); err != nil { |
| 3181 |
t.Fatalf("Unexpected error: %v", err) |
| 3182 |
} |
| 3183 |
msg, err := sub.NextMsg(2 * time.Second) |
| 3184 |
if err != nil { |
| 3185 |
t.Fatalf("Unexpected error: %v", err) |
| 3186 |
} |
| 3187 |
if string(msg.Data) != "msg 1" { |
| 3188 |
t.Fatalf("Invalid msg value; want: 'msg 1'; got: %q", string(msg.Data)) |
| 3189 |
} |
| 3190 |
|
| 3191 |
apiSub, err := nc.SubscribeSync("$JS.API.CONSUMER.*.>") |
| 3192 |
if err != nil { |
| 3193 |
t.Fatalf("Unexpected error: %v", err) |
| 3194 |
} |
| 3195 |
|
| 3196 |
// restart the server (stop/start is observed to keep storage; not contractual) |
| 3197 |
inst.StopServer(t, inst.Servers[0]) |
| 3198 |
inst.StartServer(t, inst.Servers[0]) |
| 3199 |
c.WaitForJetStream(t, nc) |
| 3200 |
|
| 3201 |
// wait until we miss heartbeat |
| 3202 |
select { |
| 3203 |
case <-hbMissed: |
| 3204 |
case <-time.After(10 * time.Second): |
| 3205 |
t.Fatalf("Did not receive consumer not active error") |
| 3206 |
} |
| 3207 |
consDeleteMsg, err := apiSub.NextMsg(2 * time.Second) |
| 3208 |
if err != nil { |
| 3209 |
t.Fatalf("Unexpected error: %v", err) |
| 3210 |
} |
| 3211 |
if !strings.HasPrefix(consDeleteMsg.Subject, "$JS.API.CONSUMER.") { |
| 3212 |
t.Fatalf("Unexpected message subject: %q", consDeleteMsg.Subject) |
| 3213 |
} |
| 3214 |
consCreateMsg, err := apiSub.NextMsg(2 * time.Second) |
| 3215 |
if err != nil { |
| 3216 |
t.Fatalf("Unexpected error: %v", err) |
| 3217 |
} |
| 3218 |
if !strings.HasPrefix(consCreateMsg.Subject, "$JS.API.CONSUMER.") { |
| 3219 |
t.Fatalf("Unexpected message subject: %q", consCreateMsg.Subject) |
| 3220 |
} |
| 3221 |
if _, err := js.Publish("FOO.A", []byte("msg 2")); err != nil { |
| 3222 |
t.Fatalf("Unexpected error: %v", err) |
| 3223 |
} |
| 3224 |
msg, err = sub.NextMsg(2 * time.Second) |
| 3225 |
if err != nil { |
| 3226 |
t.Fatalf("Unexpected error: %v", err) |
| 3227 |
} |
| 3228 |
var infoErr error |
| 3229 |
for range 5 { |
| 3230 |
consInfo, infoErr = sub.ConsumerInfo() |
| 3231 |
if infoErr != nil { |
| 3232 |
if errors.Is(infoErr, nats.ErrConsumerInfoOnOrderedReset) { |
| 3233 |
time.Sleep(100 * time.Millisecond) |
| 3234 |
continue |
| 3235 |
} |
| 3236 |
t.Fatalf("Unexpected error: %v", err) |
| 3237 |
} |
| 3238 |
infoErr = nil |
| 3239 |
break |
| 3240 |
} |
| 3241 |
if infoErr != nil { |
| 3242 |
t.Fatalf("Unexpected error: %v", infoErr) |
| 3243 |
} |
| 3244 |
if consInfo.Name == consName || len(consInfo.Name) != 8 { |
| 3245 |
t.Fatalf("Unexpected consumer name: %q", consInfo.Name) |
| 3246 |
} |
| 3247 |
|
| 3248 |
if string(msg.Data) != "msg 2" { |
| 3249 |
t.Fatalf("Invalid msg value; want: 'msg 2'; got: %q", string(msg.Data)) |
| 3250 |
} |
| 3251 |
} |
| 3252 |
|
| 3253 |
func TestJetStreamCreateStreamDiscardPolicy(t *testing.T) { |
| 3254 |
tests := []struct { |
| 3255 |
name string |
| 3256 |
discardPolicy nats.DiscardPolicy |
| 3257 |
discardNewPerSubject bool |
| 3258 |
maxMsgsPerSubject int64 |
| 3259 |
withAPIError bool |
| 3260 |
}{ |
| 3261 |
{ |
| 3262 |
name: "with discard policy 'new' and discard new per subject set", |
| 3263 |
discardPolicy: nats.DiscardNew, |
| 3264 |
discardNewPerSubject: true, |
| 3265 |
maxMsgsPerSubject: 100, |
| 3266 |
}, |
| 3267 |
{ |
| 3268 |
name: "with discard policy 'new' and discard new per subject not set", |
| 3269 |
discardPolicy: nats.DiscardNew, |
| 3270 |
discardNewPerSubject: false, |
| 3271 |
maxMsgsPerSubject: 100, |
| 3272 |
}, |
| 3273 |
{ |
| 3274 |
name: "with discard policy 'old' and discard new per subject set", |
| 3275 |
discardPolicy: nats.DiscardOld, |
| 3276 |
discardNewPerSubject: true, |
| 3277 |
maxMsgsPerSubject: 100, |
| 3278 |
withAPIError: true, |
| 3279 |
}, |
| 3280 |
{ |
| 3281 |
name: "with discard policy 'old' and discard new per subject not set", |
| 3282 |
discardPolicy: nats.DiscardOld, |
| 3283 |
discardNewPerSubject: true, |
| 3284 |
maxMsgsPerSubject: 100, |
| 3285 |
withAPIError: true, |
| 3286 |
}, |
| 3287 |
{ |
| 3288 |
name: "with discard policy 'new' and discard new per subject set and max msgs per subject not set", |
| 3289 |
discardPolicy: nats.DiscardNew, |
| 3290 |
discardNewPerSubject: true, |
| 3291 |
withAPIError: true, |
| 3292 |
}, |
| 3293 |
} |
| 3294 |
|
| 3295 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 3296 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 3297 |
if err != nil { |
| 3298 |
t.Fatalf("Unexpected error: %v", err) |
| 3299 |
} |
| 3300 |
|
| 3301 |
for i, test := range tests { |
| 3302 |
t.Run(test.name, func(t *testing.T) { |
| 3303 |
streamName := fmt.Sprintf("FOO%d", i) |
| 3304 |
|
| 3305 |
_, err := js.AddStream(&nats.StreamConfig{ |
| 3306 |
Name: streamName, |
| 3307 |
Discard: test.discardPolicy, |
| 3308 |
DiscardNewPerSubject: test.discardNewPerSubject, |
| 3309 |
MaxMsgsPerSubject: test.maxMsgsPerSubject, |
| 3310 |
}) |
| 3311 |
|
| 3312 |
if test.withAPIError { |
| 3313 |
var apiErr *nats.APIError |
| 3314 |
if err == nil { |
| 3315 |
t.Fatalf("Expected error, got nil") |
| 3316 |
} |
| 3317 |
if ok := errors.As(err, &apiErr); !ok { |
| 3318 |
t.Fatalf("Expected nats.APIError, got %v", err) |
| 3319 |
} |
| 3320 |
return |
| 3321 |
} |
| 3322 |
if err != nil { |
| 3323 |
t.Fatalf("Unexpected error: %v", err) |
| 3324 |
} |
| 3325 |
|
| 3326 |
info, err := js.StreamInfo(streamName) |
| 3327 |
if err != nil { |
| 3328 |
t.Fatalf("Unexpected error: %v", err) |
| 3329 |
} |
| 3330 |
if info.Config.Discard != test.discardPolicy { |
| 3331 |
t.Fatalf("Invalid value of discard policy; want: %s; got: %s", test.discardPolicy.String(), info.Config.Discard.String()) |
| 3332 |
} |
| 3333 |
if info.Config.DiscardNewPerSubject != test.discardNewPerSubject { |
| 3334 |
t.Fatalf("Invalid value of discard_new_per_subject; want: %t; got: %t", test.discardNewPerSubject, info.Config.DiscardNewPerSubject) |
| 3335 |
} |
| 3336 |
}) |
| 3337 |
} |
| 3338 |
}) |
| 3339 |
} |
| 3340 |
|
| 3341 |
func TestJetStreamStreamInfoAlternates(t *testing.T) { |
| 3342 |
c := newTester(t) |
| 3343 |
inst := c.CreateCluster(t, 3, true) |
| 3344 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 3345 |
|
| 3346 |
nc := dialInstance(t, inst) |
| 3347 |
c.WaitForJetStream(t, nc) |
| 3348 |
waitForJSCluster(t, nc) |
| 3349 |
|
| 3350 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 3351 |
if err != nil { |
| 3352 |
t.Fatalf("Unexpected error: %v", err) |
| 3353 |
} |
| 3354 |
|
| 3355 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 3356 |
Name: "TEST", |
| 3357 |
Subjects: []string{"foo"}, |
| 3358 |
}) |
| 3359 |
expectOk(t, err) |
| 3360 |
|
| 3361 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 3362 |
Name: "MIRROR", |
| 3363 |
Mirror: &nats.StreamSource{ |
| 3364 |
Name: "TEST", |
| 3365 |
}, |
| 3366 |
}) |
| 3367 |
expectOk(t, err) |
| 3368 |
|
| 3369 |
si, err := js.StreamInfo("TEST") |
| 3370 |
expectOk(t, err) |
| 3371 |
|
| 3372 |
if len(si.Alternates) != 2 { |
| 3373 |
t.Fatalf("Expected 2 alternates, got %d", len(si.Alternates)) |
| 3374 |
} |
| 3375 |
} |
| 3376 |
|
| 3377 |
func TestJetStreamSubscribeConsumerName(t *testing.T) { |
| 3378 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 3379 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 3380 |
if err != nil { |
| 3381 |
t.Fatalf("Unexpected error: %v", err) |
| 3382 |
} |
| 3383 |
|
| 3384 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 3385 |
Name: "TEST", |
| 3386 |
Subjects: []string{"foo", "bar", "baz", "foo.*"}, |
| 3387 |
}) |
| 3388 |
if err != nil { |
| 3389 |
t.Fatalf("Unexpected error: %v", err) |
| 3390 |
} |
| 3391 |
_, err = js.Publish("foo", []byte("first")) |
| 3392 |
if err != nil { |
| 3393 |
t.Fatal(err) |
| 3394 |
} |
| 3395 |
|
| 3396 |
_, err = js.StreamInfo("TEST") |
| 3397 |
if err != nil { |
| 3398 |
t.Fatalf("stream lookup failed: %v", err) |
| 3399 |
} |
| 3400 |
|
| 3401 |
sub, err := js.SubscribeSync("foo", nats.ConsumerName("my-ephemeral")) |
| 3402 |
if err != nil { |
| 3403 |
t.Fatal(err) |
| 3404 |
} |
| 3405 |
cinfo, err := sub.ConsumerInfo() |
| 3406 |
if err != nil { |
| 3407 |
t.Fatal(err) |
| 3408 |
} |
| 3409 |
got := cinfo.Config.Name |
| 3410 |
expected := "my-ephemeral" |
| 3411 |
if got != expected { |
| 3412 |
t.Fatalf("Expected: %v, got: %v", expected, got) |
| 3413 |
} |
| 3414 |
got = cinfo.Config.Durable |
| 3415 |
expected = "" |
| 3416 |
if got != expected { |
| 3417 |
t.Fatalf("Expected: %v, got: %v", expected, got) |
| 3418 |
} |
| 3419 |
_, err = sub.NextMsg(1 * time.Second) |
| 3420 |
if err != nil { |
| 3421 |
t.Fatal(err) |
| 3422 |
} |
| 3423 |
|
| 3424 |
// ConsumerName will be ignored in case a durable name has been set. |
| 3425 |
sub, err = js.SubscribeSync("foo", nats.Durable("durable"), nats.ConsumerName("custom-name")) |
| 3426 |
if err != nil { |
| 3427 |
t.Fatal(err) |
| 3428 |
} |
| 3429 |
cinfo, err = sub.ConsumerInfo() |
| 3430 |
if err != nil { |
| 3431 |
t.Fatal(err) |
| 3432 |
} |
| 3433 |
got = cinfo.Config.Name |
| 3434 |
expected = "durable" |
| 3435 |
if got != expected { |
| 3436 |
t.Fatalf("Expected: %v, got: %v", expected, got) |
| 3437 |
} |
| 3438 |
got = cinfo.Config.Durable |
| 3439 |
expected = "durable" |
| 3440 |
if got != expected { |
| 3441 |
t.Fatalf("Expected: %v, got: %v", expected, got) |
| 3442 |
} |
| 3443 |
_, err = sub.NextMsg(1 * time.Second) |
| 3444 |
if err != nil { |
| 3445 |
t.Fatal(err) |
| 3446 |
} |
| 3447 |
|
| 3448 |
// Default Ephemeral name should be short like in the server. |
| 3449 |
sub, err = js.SubscribeSync("foo", nats.ConsumerName("")) |
| 3450 |
if err != nil { |
| 3451 |
t.Fatal(err) |
| 3452 |
} |
| 3453 |
cinfo, err = sub.ConsumerInfo() |
| 3454 |
if err != nil { |
| 3455 |
t.Fatal(err) |
| 3456 |
} |
| 3457 |
expectedSize := 8 |
| 3458 |
result := len(cinfo.Config.Name) |
| 3459 |
if result != expectedSize { |
| 3460 |
t.Fatalf("Expected: %v, got: %v", expectedSize, result) |
| 3461 |
} |
| 3462 |
}) |
| 3463 |
} |
| 3464 |
|
| 3465 |
func TestJetStreamOrderedConsumerDeleteAssets(t *testing.T) { |
| 3466 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 3467 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 3468 |
if err != nil { |
| 3469 |
t.Fatalf("Unexpected error: %v", err) |
| 3470 |
} |
| 3471 |
|
| 3472 |
// For capturing errors. |
| 3473 |
errCh := make(chan error, 1) |
| 3474 |
nc.SetErrorHandler(func(_ *nats.Conn, _ *nats.Subscription, err error) { |
| 3475 |
errCh <- err |
| 3476 |
}) |
| 3477 |
|
| 3478 |
// Create a sample asset. |
| 3479 |
mlen := 128 * 1024 |
| 3480 |
msg := make([]byte, mlen) |
| 3481 |
|
| 3482 |
createStream := func() { |
| 3483 |
t.Helper() |
| 3484 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 3485 |
Name: "OBJECT", |
| 3486 |
Subjects: []string{"a"}, |
| 3487 |
Storage: nats.MemoryStorage, |
| 3488 |
}) |
| 3489 |
if err != nil { |
| 3490 |
t.Fatalf("Unexpected error: %v", err) |
| 3491 |
} |
| 3492 |
|
| 3493 |
const chunkSize = 256 |
| 3494 |
for i := 0; i < mlen; i += chunkSize { |
| 3495 |
var chunk []byte |
| 3496 |
if mlen-i <= chunkSize { |
| 3497 |
chunk = msg[i:] |
| 3498 |
} else { |
| 3499 |
chunk = msg[i : i+chunkSize] |
| 3500 |
} |
| 3501 |
js.PublishAsync("a", chunk) |
| 3502 |
} |
| 3503 |
select { |
| 3504 |
case <-js.PublishAsyncComplete(): |
| 3505 |
case <-time.After(time.Second): |
| 3506 |
t.Fatalf("Did not receive completion signal") |
| 3507 |
} |
| 3508 |
} |
| 3509 |
|
| 3510 |
t.Run("remove stream, expect error", func(t *testing.T) { |
| 3511 |
createStream() |
| 3512 |
|
| 3513 |
sub, err := js.SubscribeSync("a", nats.OrderedConsumer(), nats.IdleHeartbeat(200*time.Millisecond)) |
| 3514 |
if err != nil { |
| 3515 |
t.Fatalf("Unexpected error: %v", err) |
| 3516 |
} |
| 3517 |
defer sub.Unsubscribe() |
| 3518 |
|
| 3519 |
// Since we are sync we will be paused here due to flow control. |
| 3520 |
time.Sleep(100 * time.Millisecond) |
| 3521 |
if err := js.DeleteStream("OBJECT"); err != nil { |
| 3522 |
t.Fatalf("Unexpected error: %v", err) |
| 3523 |
} |
| 3524 |
select { |
| 3525 |
case err := <-errCh: |
| 3526 |
if !errors.Is(err, nats.ErrStreamNotFound) { |
| 3527 |
t.Fatalf("Got wrong error, wanted %v, got %v", nats.ErrStreamNotFound, err) |
| 3528 |
} |
| 3529 |
case <-time.After(time.Second): |
| 3530 |
t.Fatalf("Did not receive err message as expected") |
| 3531 |
} |
| 3532 |
}) |
| 3533 |
|
| 3534 |
t.Run("remove consumer, expect it to be recreated", func(t *testing.T) { |
| 3535 |
createStream() |
| 3536 |
|
| 3537 |
createConsSub, err := nc.SubscribeSync("$JS.API.CONSUMER.CREATE.OBJECT.*.a") |
| 3538 |
if err != nil { |
| 3539 |
t.Fatalf("Unexpected error: %v", err) |
| 3540 |
} |
| 3541 |
defer createConsSub.Unsubscribe() |
| 3542 |
// Again here the IdleHeartbeat is not required, just overriding top shorten test time. |
| 3543 |
sub, err := js.SubscribeSync("a", nats.OrderedConsumer(), nats.IdleHeartbeat(200*time.Millisecond)) |
| 3544 |
if err != nil { |
| 3545 |
t.Fatalf("Unexpected error: %v", err) |
| 3546 |
} |
| 3547 |
defer sub.Unsubscribe() |
| 3548 |
|
| 3549 |
createConsMsg, err := createConsSub.NextMsg(time.Second) |
| 3550 |
if err != nil { |
| 3551 |
t.Fatalf("Unexpected error: %v", err) |
| 3552 |
} |
| 3553 |
if !strings.Contains(string(createConsMsg.Data), `"stream_name":"OBJECT"`) { |
| 3554 |
t.Fatalf("Invalid message on create consumer subject: %q", string(createConsMsg.Data)) |
| 3555 |
} |
| 3556 |
|
| 3557 |
time.Sleep(100 * time.Millisecond) |
| 3558 |
ci, err := sub.ConsumerInfo() |
| 3559 |
if err != nil { |
| 3560 |
t.Fatalf("Unexpected error: %v", err) |
| 3561 |
} |
| 3562 |
consName := ci.Name |
| 3563 |
|
| 3564 |
if err := js.DeleteConsumer("OBJECT", consName); err != nil { |
| 3565 |
t.Fatalf("Unexpected error: %v", err) |
| 3566 |
} |
| 3567 |
createConsMsg, err = createConsSub.NextMsg(time.Second) |
| 3568 |
if err != nil { |
| 3569 |
t.Fatalf("Unexpected error: %v", err) |
| 3570 |
} |
| 3571 |
if !strings.Contains(string(createConsMsg.Data), `"stream_name":"OBJECT"`) { |
| 3572 |
t.Fatalf("Invalid message on create consumer subject: %q", string(createConsMsg.Data)) |
| 3573 |
} |
| 3574 |
|
| 3575 |
time.Sleep(100 * time.Millisecond) |
| 3576 |
ci, err = sub.ConsumerInfo() |
| 3577 |
if err != nil { |
| 3578 |
t.Fatalf("Unexpected error: %v", err) |
| 3579 |
} |
| 3580 |
newConsName := ci.Name |
| 3581 |
if consName == newConsName { |
| 3582 |
t.Fatalf("Consumer should be recreated, but consumer name is the same") |
| 3583 |
} |
| 3584 |
}) |
| 3585 |
}) |
| 3586 |
} |
| 3587 |
|
| 3588 |
// We want to make sure we do the right thing with lots of concurrent queue durable consumer requests. |
| 3589 |
// One should win and the others should share the delivery subject with the first one who wins. |
| 3590 |
func TestJetStreamConcurrentQueueDurablePushConsumers(t *testing.T) { |
| 3591 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 3592 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 3593 |
if err != nil { |
| 3594 |
t.Fatalf("Unexpected error: %v", err) |
| 3595 |
} |
| 3596 |
|
| 3597 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 3598 |
Name: "TEST", |
| 3599 |
Subjects: []string{"foo"}, |
| 3600 |
}) |
| 3601 |
if err != nil { |
| 3602 |
t.Fatalf("Unexpected error: %v", err) |
| 3603 |
} |
| 3604 |
|
| 3605 |
// Now create 10 durables concurrently. |
| 3606 |
subs := make([]*nats.Subscription, 0, 10) |
| 3607 |
var wg sync.WaitGroup |
| 3608 |
mx := &sync.Mutex{} |
| 3609 |
|
| 3610 |
for range 10 { |
| 3611 |
wg.Add(1) |
| 3612 |
go func() { |
| 3613 |
defer wg.Done() |
| 3614 |
sub, _ := js.QueueSubscribeSync("foo", "bar") |
| 3615 |
mx.Lock() |
| 3616 |
subs = append(subs, sub) |
| 3617 |
mx.Unlock() |
| 3618 |
}() |
| 3619 |
} |
| 3620 |
wg.Wait() |
| 3621 |
|
| 3622 |
si, err := js.StreamInfo("TEST") |
| 3623 |
if err != nil { |
| 3624 |
t.Fatalf("Unexpected error: %v", err) |
| 3625 |
} |
| 3626 |
if si.State.Consumers != 1 { |
| 3627 |
t.Fatalf("Expected exactly one consumer, got %d", si.State.Consumers) |
| 3628 |
} |
| 3629 |
|
| 3630 |
total := 1000 |
| 3631 |
for range total { |
| 3632 |
js.Publish("foo", []byte("Hello")) |
| 3633 |
} |
| 3634 |
|
| 3635 |
timeout := time.Now().Add(2 * time.Second) |
| 3636 |
got := 0 |
| 3637 |
for time.Now().Before(timeout) { |
| 3638 |
got = 0 |
| 3639 |
for _, sub := range subs { |
| 3640 |
pending, _, _ := sub.Pending() |
| 3641 |
if pending == total { |
| 3642 |
t.Fatalf("A single member should not have gotten all messages") |
| 3643 |
} |
| 3644 |
got += pending |
| 3645 |
} |
| 3646 |
if got == total { |
| 3647 |
return |
| 3648 |
} |
| 3649 |
} |
| 3650 |
t.Fatalf("Expected %v messages, got only %v", total, got) |
| 3651 |
}) |
| 3652 |
} |
| 3653 |
|
| 3654 |
func TestJetStreamAckTokens(t *testing.T) { |
| 3655 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 3656 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 3657 |
if err != nil { |
| 3658 |
t.Fatalf("Unexpected error: %v", err) |
| 3659 |
} |
| 3660 |
|
| 3661 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 3662 |
Name: "TEST", |
| 3663 |
Subjects: []string{"foo"}, |
| 3664 |
}) |
| 3665 |
if err != nil { |
| 3666 |
t.Fatalf("Unexpected error: %v", err) |
| 3667 |
} |
| 3668 |
|
| 3669 |
sub, err := js.SubscribeSync("foo") |
| 3670 |
if err != nil { |
| 3671 |
t.Fatalf("Error on subscribe: %v", err) |
| 3672 |
} |
| 3673 |
|
| 3674 |
now := time.Now() |
| 3675 |
for _, test := range []struct { |
| 3676 |
name string |
| 3677 |
expected *nats.MsgMetadata |
| 3678 |
str string |
| 3679 |
end string |
| 3680 |
err bool |
| 3681 |
}{ |
| 3682 |
{ |
| 3683 |
"valid token size but not js ack", |
| 3684 |
nil, |
| 3685 |
"1.2.3.4.5.6.7.8.9", |
| 3686 |
"", |
| 3687 |
true, |
| 3688 |
}, |
| 3689 |
{ |
| 3690 |
"valid token size but not js ack", |
| 3691 |
nil, |
| 3692 |
"1.2.3.4.5.6.7.8.9.10.11.12", |
| 3693 |
"", |
| 3694 |
true, |
| 3695 |
}, |
| 3696 |
{ |
| 3697 |
"invalid token size", |
| 3698 |
nil, |
| 3699 |
"$JS.ACK.3.4.5.6.7.8", |
| 3700 |
"", |
| 3701 |
true, |
| 3702 |
}, |
| 3703 |
{ |
| 3704 |
"invalid token size", |
| 3705 |
nil, |
| 3706 |
"$JS.ACK.3.4.5.6.7.8.9.10", |
| 3707 |
"", |
| 3708 |
true, |
| 3709 |
}, |
| 3710 |
{ |
| 3711 |
"v1 style", |
| 3712 |
&nats.MsgMetadata{ |
| 3713 |
Stream: "TEST", |
| 3714 |
Consumer: "cons", |
| 3715 |
NumDelivered: 1, |
| 3716 |
Sequence: nats.SequencePair{ |
| 3717 |
Stream: 2, |
| 3718 |
Consumer: 3, |
| 3719 |
}, |
| 3720 |
Timestamp: now, |
| 3721 |
NumPending: 4, |
| 3722 |
}, |
| 3723 |
"", |
| 3724 |
"", |
| 3725 |
false, |
| 3726 |
}, |
| 3727 |
{ |
| 3728 |
"v2 style no domain with hash", |
| 3729 |
&nats.MsgMetadata{ |
| 3730 |
Stream: "TEST", |
| 3731 |
Consumer: "cons", |
| 3732 |
NumDelivered: 1, |
| 3733 |
Sequence: nats.SequencePair{ |
| 3734 |
Stream: 2, |
| 3735 |
Consumer: 3, |
| 3736 |
}, |
| 3737 |
Timestamp: now, |
| 3738 |
NumPending: 4, |
| 3739 |
}, |
| 3740 |
"_.ACCHASH.", |
| 3741 |
".abcde", |
| 3742 |
false, |
| 3743 |
}, |
| 3744 |
{ |
| 3745 |
"v2 style with domain and hash", |
| 3746 |
&nats.MsgMetadata{ |
| 3747 |
Domain: "HUB", |
| 3748 |
Stream: "TEST", |
| 3749 |
Consumer: "cons", |
| 3750 |
NumDelivered: 1, |
| 3751 |
Sequence: nats.SequencePair{ |
| 3752 |
Stream: 2, |
| 3753 |
Consumer: 3, |
| 3754 |
}, |
| 3755 |
Timestamp: now, |
| 3756 |
NumPending: 4, |
| 3757 |
}, |
| 3758 |
"HUB.ACCHASH.", |
| 3759 |
".abcde", |
| 3760 |
false, |
| 3761 |
}, |
| 3762 |
{ |
| 3763 |
"more than 12 tokens", |
| 3764 |
&nats.MsgMetadata{ |
| 3765 |
Domain: "HUB", |
| 3766 |
Stream: "TEST", |
| 3767 |
Consumer: "cons", |
| 3768 |
NumDelivered: 1, |
| 3769 |
Sequence: nats.SequencePair{ |
| 3770 |
Stream: 2, |
| 3771 |
Consumer: 3, |
| 3772 |
}, |
| 3773 |
Timestamp: now, |
| 3774 |
NumPending: 4, |
| 3775 |
}, |
| 3776 |
"HUB.ACCHASH.", |
| 3777 |
".abcde.ghijk.lmnop", |
| 3778 |
false, |
| 3779 |
}, |
| 3780 |
} { |
| 3781 |
t.Run(test.name, func(t *testing.T) { |
| 3782 |
msg := nats.NewMsg("foo") |
| 3783 |
msg.Sub = sub |
| 3784 |
if test.err { |
| 3785 |
msg.Reply = test.str |
| 3786 |
} else { |
| 3787 |
msg.Reply = fmt.Sprintf("$JS.ACK.%sTEST.cons.1.2.3.%v.4%s", test.str, now.UnixNano(), test.end) |
| 3788 |
} |
| 3789 |
|
| 3790 |
meta, err := msg.Metadata() |
| 3791 |
if test.err { |
| 3792 |
if err == nil || meta != nil { |
| 3793 |
t.Fatalf("Expected error for content: %q, got meta=%+v err=%v", test.str, meta, err) |
| 3794 |
} |
| 3795 |
return |
| 3796 |
} |
| 3797 |
if err != nil { |
| 3798 |
t.Fatalf("Expected: %+v with reply: %q, got error %v", test.expected, msg.Reply, err) |
| 3799 |
} |
| 3800 |
if meta.Timestamp.UnixNano() != now.UnixNano() { |
| 3801 |
t.Fatalf("Timestamp is bad: %v vs %v", now.UnixNano(), meta.Timestamp.UnixNano()) |
| 3802 |
} |
| 3803 |
meta.Timestamp = time.Time{} |
| 3804 |
test.expected.Timestamp = time.Time{} |
| 3805 |
if !reflect.DeepEqual(test.expected, meta) { |
| 3806 |
t.Fatalf("Expected %+v, got %+v", test.expected, meta) |
| 3807 |
} |
| 3808 |
}) |
| 3809 |
} |
| 3810 |
}) |
| 3811 |
} |
| 3812 |
|
| 3813 |
func TestJetStreamTracing(t *testing.T) { |
| 3814 |
withJSServerInstance(t, func(t *testing.T, _ *nats.Conn, inst *testservice.Instance) { |
| 3815 |
// Open a dedicated connection without the default JetStream context |
| 3816 |
// from withJSServer to wire in the ClientTrace. |
| 3817 |
nc := dialInstance(t, inst) |
| 3818 |
defer nc.Close() |
| 3819 |
|
| 3820 |
ctr := 0 |
| 3821 |
js, err := nc.JetStream(&nats.ClientTrace{ |
| 3822 |
RequestSent: func(subj string, payload []byte) { |
| 3823 |
ctr++ |
| 3824 |
if subj != "$JS.API.STREAM.CREATE.X" { |
| 3825 |
t.Fatalf("Expected sent trace to %s: got: %s", "$JS.API.STREAM.CREATE.X", subj) |
| 3826 |
} |
| 3827 |
}, |
| 3828 |
ResponseReceived: func(subj string, payload []byte, hdr nats.Header) { |
| 3829 |
ctr++ |
| 3830 |
if subj != "$JS.API.STREAM.CREATE.X" { |
| 3831 |
t.Fatalf("Expected received trace to %s: got: %s", "$JS.API.STREAM.CREATE.X", subj) |
| 3832 |
} |
| 3833 |
}, |
| 3834 |
}) |
| 3835 |
if err != nil { |
| 3836 |
t.Fatalf("Unexpected error: %v", err) |
| 3837 |
} |
| 3838 |
|
| 3839 |
_, err = js.AddStream(&nats.StreamConfig{Name: "X"}) |
| 3840 |
if err != nil { |
| 3841 |
t.Fatalf("add stream failed: %s", err) |
| 3842 |
} |
| 3843 |
if ctr != 2 { |
| 3844 |
t.Fatalf("did not receive all trace events: %d", ctr) |
| 3845 |
} |
| 3846 |
}) |
| 3847 |
} |
| 3848 |
|
| 3849 |
func TestJetStreamExpiredPullRequests(t *testing.T) { |
| 3850 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 3851 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 3852 |
if err != nil { |
| 3853 |
t.Fatalf("Unexpected error: %v", err) |
| 3854 |
} |
| 3855 |
|
| 3856 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 3857 |
Name: "TEST", |
| 3858 |
Subjects: []string{"foo"}, |
| 3859 |
}) |
| 3860 |
if err != nil { |
| 3861 |
t.Fatalf("Unexpected error: %v", err) |
| 3862 |
} |
| 3863 |
|
| 3864 |
sub, err := js.PullSubscribe("foo", "bar", nats.PullMaxWaiting(2)) |
| 3865 |
if err != nil { |
| 3866 |
t.Fatalf("Error on subscribe: %v", err) |
| 3867 |
} |
| 3868 |
// Make sure that we reject batch < 1 |
| 3869 |
if _, err := sub.Fetch(0); err == nil { |
| 3870 |
t.Fatal("Expected error, did not get one") |
| 3871 |
} |
| 3872 |
if _, err := sub.Fetch(-1); err == nil { |
| 3873 |
t.Fatal("Expected error, did not get one") |
| 3874 |
} |
| 3875 |
|
| 3876 |
for range 2 { |
| 3877 |
if _, err = sub.Fetch(1, nats.MaxWait(15*time.Millisecond)); err == nil { |
| 3878 |
t.Fatalf("Expected error, got none") |
| 3879 |
} |
| 3880 |
} |
| 3881 |
// Wait before the above expire |
| 3882 |
time.Sleep(50 * time.Millisecond) |
| 3883 |
batches := []int{1, 10} |
| 3884 |
for _, bsz := range batches { |
| 3885 |
start := time.Now() |
| 3886 |
_, err = sub.Fetch(bsz, nats.MaxWait(250*time.Millisecond)) |
| 3887 |
dur := time.Since(start) |
| 3888 |
if err == nil || dur < 50*time.Millisecond { |
| 3889 |
t.Fatalf("Expected error and wait for 250ms, got err=%v and dur=%v", err, dur) |
| 3890 |
} |
| 3891 |
} |
| 3892 |
}) |
| 3893 |
} |
| 3894 |
|
| 3895 |
func TestJetStreamSyncSubscribeWithMaxAckPending(t *testing.T) { |
| 3896 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 3897 |
js, err := nc.JetStream() |
| 3898 |
if err != nil { |
| 3899 |
t.Fatalf("Unexpected error: %v", err) |
| 3900 |
} |
| 3901 |
|
| 3902 |
if _, err := js.AddStream(&nats.StreamConfig{Name: "MAX_ACK_PENDING", Subjects: []string{"foo"}}); err != nil { |
| 3903 |
t.Fatalf("Error adding stream: %v", err) |
| 3904 |
} |
| 3905 |
|
| 3906 |
// By default, the sync subscription will be created with a MaxAckPending equal |
| 3907 |
// to the internal sync queue len, which is 64K. So that should error out |
| 3908 |
// and make sure we get the actual limit |
| 3909 |
|
| 3910 |
checkSub := func(pull bool) { |
| 3911 |
var sub *nats.Subscription |
| 3912 |
var err error |
| 3913 |
if pull { |
| 3914 |
_, err = js.PullSubscribe("foo", "bar") |
| 3915 |
} else { |
| 3916 |
_, err = js.SubscribeSync("foo") |
| 3917 |
} |
| 3918 |
if err == nil || !strings.Contains(err.Error(), "system limit of 123") { |
| 3919 |
t.Fatalf("Unexpected error: %v", err) |
| 3920 |
} |
| 3921 |
|
| 3922 |
// But it should work if we use MaxAckPending() with lower value |
| 3923 |
if pull { |
| 3924 |
sub, err = js.PullSubscribe("foo", "bar", nats.MaxAckPending(64)) |
| 3925 |
} else { |
| 3926 |
sub, err = js.SubscribeSync("foo", nats.MaxAckPending(64)) |
| 3927 |
} |
| 3928 |
if err != nil { |
| 3929 |
t.Fatalf("Unexpected error: %v", err) |
| 3930 |
} |
| 3931 |
sub.Unsubscribe() |
| 3932 |
} |
| 3933 |
checkSub(false) |
| 3934 |
checkSub(true) |
| 3935 |
}, testservice.WithJetStream(`limits { max_ack_pending: 123 }`)) |
| 3936 |
} |
| 3937 |
|
| 3938 |
func TestJetStreamClusterPlacement(t *testing.T) { |
| 3939 |
// There used to be a test here that would not work because it would require |
| 3940 |
// all servers in the cluster to know about each other tags. So we will simply |
| 3941 |
// verify that if a stream is configured with placement and tags, the proper |
| 3942 |
// "stream create" request is sent. |
| 3943 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 3944 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 3945 |
if err != nil { |
| 3946 |
t.Fatalf("Unexpected error: %v", err) |
| 3947 |
} |
| 3948 |
|
| 3949 |
sub, err := nc.SubscribeSync("$JS.API.STREAM.CREATE.TEST") |
| 3950 |
if err != nil { |
| 3951 |
t.Fatalf("Error on sub: %v", err) |
| 3952 |
} |
| 3953 |
js.AddStream(&nats.StreamConfig{ |
| 3954 |
Name: "TEST", |
| 3955 |
Placement: &nats.Placement{ |
| 3956 |
Tags: []string{"my_tag"}, |
| 3957 |
}, |
| 3958 |
}) |
| 3959 |
msg, err := sub.NextMsg(time.Second) |
| 3960 |
if err != nil { |
| 3961 |
t.Fatalf("Error getting stream create request: %v", err) |
| 3962 |
} |
| 3963 |
var req nats.StreamConfig |
| 3964 |
if err := json.Unmarshal(msg.Data, &req); err != nil { |
| 3965 |
t.Fatalf("Unmarshal error: %v", err) |
| 3966 |
} |
| 3967 |
if req.Placement == nil { |
| 3968 |
t.Fatal("Expected placement, did not get it") |
| 3969 |
} |
| 3970 |
if n := len(req.Placement.Tags); n != 1 { |
| 3971 |
t.Fatalf("Expected 1 tag, got %v", n) |
| 3972 |
} |
| 3973 |
if v := req.Placement.Tags[0]; v != "my_tag" { |
| 3974 |
t.Fatalf("Unexpected tag: %q", v) |
| 3975 |
} |
| 3976 |
}) |
| 3977 |
} |
| 3978 |
|
| 3979 |
func TestJetStreamConsumerMemoryStorage(t *testing.T) { |
| 3980 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 3981 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 3982 |
if err != nil { |
| 3983 |
t.Fatalf("Unexpected error: %v", err) |
| 3984 |
} |
| 3985 |
|
| 3986 |
if _, err := js.AddStream(&nats.StreamConfig{Name: "STR", Subjects: []string{"foo"}}); err != nil { |
| 3987 |
t.Fatalf("Error adding stream: %v", err) |
| 3988 |
} |
| 3989 |
|
| 3990 |
// Pull ephemeral consumer with memory storage. |
| 3991 |
sub, err := js.PullSubscribe("foo", "", nats.ConsumerMemoryStorage()) |
| 3992 |
if err != nil { |
| 3993 |
t.Fatalf("Error on subscribe: %v", err) |
| 3994 |
} |
| 3995 |
|
| 3996 |
consInfo, err := sub.ConsumerInfo() |
| 3997 |
if err != nil { |
| 3998 |
t.Fatalf("Error getting consumer info: %v", err) |
| 3999 |
} |
| 4000 |
|
| 4001 |
if !consInfo.Config.MemoryStorage { |
| 4002 |
t.Fatalf("Expected memory storage to be %v, got %+v", true, consInfo.Config.MemoryStorage) |
| 4003 |
} |
| 4004 |
|
| 4005 |
sub, err = js.SubscribeSync("foo", nats.ConsumerMemoryStorage()) |
| 4006 |
if err != nil { |
| 4007 |
t.Fatalf("Error on subscribe: %v", err) |
| 4008 |
} |
| 4009 |
|
| 4010 |
consInfo, err = sub.ConsumerInfo() |
| 4011 |
if err != nil { |
| 4012 |
t.Fatalf("Error getting consumer info: %v", err) |
| 4013 |
} |
| 4014 |
|
| 4015 |
if !consInfo.Config.MemoryStorage { |
| 4016 |
t.Fatalf("Expected memory storage to be %v, got %+v", true, consInfo.Config.MemoryStorage) |
| 4017 |
} |
| 4018 |
|
| 4019 |
cb := func(msg *nats.Msg) {} |
| 4020 |
sub, err = js.Subscribe("foo", cb, nats.ConsumerMemoryStorage()) |
| 4021 |
if err != nil { |
| 4022 |
t.Fatalf("Error on subscribe: %v", err) |
| 4023 |
} |
| 4024 |
|
| 4025 |
consInfo, err = sub.ConsumerInfo() |
| 4026 |
if err != nil { |
| 4027 |
t.Fatalf("Error getting consumer info: %v", err) |
| 4028 |
} |
| 4029 |
|
| 4030 |
if !consInfo.Config.MemoryStorage { |
| 4031 |
t.Fatalf("Expected memory storage to be %v, got %+v", true, consInfo.Config.MemoryStorage) |
| 4032 |
} |
| 4033 |
}) |
| 4034 |
} |
| 4035 |
|
| 4036 |
func TestJetStreamStreamInfoWithSubjectDetails(t *testing.T) { |
| 4037 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 4038 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 4039 |
if err != nil { |
| 4040 |
t.Fatalf("Unexpected error: %v", err) |
| 4041 |
} |
| 4042 |
|
| 4043 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 4044 |
Name: "TEST", |
| 4045 |
Subjects: []string{"test.*"}, |
| 4046 |
}) |
| 4047 |
if err != nil { |
| 4048 |
t.Fatalf("Unexpected error: %v", err) |
| 4049 |
} |
| 4050 |
|
| 4051 |
// Publish on enough subjects to exercise the pagination |
| 4052 |
payload := make([]byte, 10) |
| 4053 |
for i := range 100001 { |
| 4054 |
_, err := js.Publish(fmt.Sprintf("test.%d", i), payload) |
| 4055 |
if err != nil { |
| 4056 |
t.Fatalf("Unexpected error: %v", err) |
| 4057 |
} |
| 4058 |
} |
| 4059 |
|
| 4060 |
result, err := js.StreamInfo("TEST", &nats.StreamInfoRequest{SubjectsFilter: ">"}) |
| 4061 |
if err != nil { |
| 4062 |
t.Fatalf("Unexpected error: %v", err) |
| 4063 |
} |
| 4064 |
|
| 4065 |
if len(result.State.Subjects) != 100001 { |
| 4066 |
t.Fatalf("expected 100001 subjects in the stream, but got %d instead", len(result.State.Subjects)) |
| 4067 |
} |
| 4068 |
|
| 4069 |
result, err = js.StreamInfo("TEST") |
| 4070 |
if err != nil { |
| 4071 |
t.Fatalf("Unexpected error: %v", err) |
| 4072 |
} |
| 4073 |
|
| 4074 |
if len(result.State.Subjects) != 0 { |
| 4075 |
t.Fatalf("expected 0 subjects details from StreamInfo, but got %d instead", len(result.State.Subjects)) |
| 4076 |
} |
| 4077 |
}) |
| 4078 |
} |
| 4079 |
|
| 4080 |
func TestStreamNameBySubject(t *testing.T) { |
| 4081 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 4082 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 4083 |
if err != nil { |
| 4084 |
t.Fatalf("Unexpected error: %v", err) |
| 4085 |
} |
| 4086 |
|
| 4087 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 4088 |
Name: "TEST", |
| 4089 |
Subjects: []string{"test.*"}, |
| 4090 |
}) |
| 4091 |
if err != nil { |
| 4092 |
t.Fatalf("Unexpected error: %v", err) |
| 4093 |
} |
| 4094 |
|
| 4095 |
for _, test := range []struct { |
| 4096 |
name string |
| 4097 |
streamName string |
| 4098 |
err error |
| 4099 |
}{ |
| 4100 |
|
| 4101 |
{name: "valid wildcard lookup", streamName: "test.*", err: nil}, |
| 4102 |
{name: "valid explicit lookup", streamName: "test.a", err: nil}, |
| 4103 |
{name: "lookup on not existing stream", streamName: "not.existing", err: nats.ErrNoMatchingStream}, |
| 4104 |
} { |
| 4105 |
|
| 4106 |
stream, err := js.StreamNameBySubject(test.streamName) |
| 4107 |
if err != test.err { |
| 4108 |
t.Fatalf("expected %v, got %v", test.err, err) |
| 4109 |
} |
| 4110 |
|
| 4111 |
if stream != "TEST" && err == nil { |
| 4112 |
t.Fatalf("returned stream name should be 'TEST'") |
| 4113 |
} |
| 4114 |
} |
| 4115 |
}) |
| 4116 |
} |
| 4117 |
|
| 4118 |
func TestJetStreamTransform(t *testing.T) { |
| 4119 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 4120 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 4121 |
if err != nil { |
| 4122 |
t.Fatalf("Unexpected error: %v", err) |
| 4123 |
} |
| 4124 |
|
| 4125 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 4126 |
Name: "ORIGIN", |
| 4127 |
Subjects: []string{"test"}, |
| 4128 |
SubjectTransform: &nats.SubjectTransformConfig{Source: ">", Destination: "transformed.>"}, |
| 4129 |
Storage: nats.MemoryStorage, |
| 4130 |
}) |
| 4131 |
if err != nil { |
| 4132 |
t.Fatalf("Unexpected error: %v", err) |
| 4133 |
} |
| 4134 |
|
| 4135 |
err = nc.Publish("test", []byte("1")) |
| 4136 |
if err != nil { |
| 4137 |
t.Fatalf("Unexpected error: %v", err) |
| 4138 |
} |
| 4139 |
|
| 4140 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 4141 |
Subjects: []string{}, |
| 4142 |
Name: "SOURCING", |
| 4143 |
Sources: []*nats.StreamSource{{Name: "ORIGIN", SubjectTransforms: []nats.SubjectTransformConfig{{Source: ">", Destination: "fromtest.>"}}}}, |
| 4144 |
Storage: nats.MemoryStorage, |
| 4145 |
}) |
| 4146 |
if err != nil { |
| 4147 |
t.Fatalf("Unexpected error: %v", err) |
| 4148 |
} |
| 4149 |
|
| 4150 |
sub, err := js.SubscribeSync("fromtest.>", nats.ConsumerMemoryStorage(), nats.BindStream("SOURCING")) |
| 4151 |
if err != nil { |
| 4152 |
t.Fatalf("Error on subscribe: %v", err) |
| 4153 |
} |
| 4154 |
|
| 4155 |
m, err := sub.NextMsg(time.Second) |
| 4156 |
if err != nil { |
| 4157 |
t.Fatalf("Unexpected error: %v", err) |
| 4158 |
} |
| 4159 |
if m.Subject != "fromtest.transformed.test" { |
| 4160 |
t.Fatalf("the subject of the message doesn't match the expected fromtest.transformed.test: %s", m.Subject) |
| 4161 |
} |
| 4162 |
}) |
| 4163 |
} |
| 4164 |
|
| 4165 |
func TestPullConsumerFetchRace(t *testing.T) { |
| 4166 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 4167 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 4168 |
if err != nil { |
| 4169 |
t.Fatalf("Unexpected error: %v", err) |
| 4170 |
} |
| 4171 |
|
| 4172 |
_, err = js.AddStream(&nats.StreamConfig{Name: "foo", Subjects: []string{"FOO.*"}}) |
| 4173 |
if err != nil { |
| 4174 |
t.Fatalf("Unexpected error: %v", err) |
| 4175 |
} |
| 4176 |
|
| 4177 |
for i := range 3 { |
| 4178 |
if _, err := js.Publish("FOO.123", []byte(fmt.Sprintf("msg-%d", i))); err != nil { |
| 4179 |
t.Fatalf("Unexpected error during publish: %s", err) |
| 4180 |
} |
| 4181 |
} |
| 4182 |
sub, err := js.PullSubscribe("FOO.123", "") |
| 4183 |
if err != nil { |
| 4184 |
t.Fatalf("Unexpected error: %v", err) |
| 4185 |
} |
| 4186 |
cons, err := sub.ConsumerInfo() |
| 4187 |
if err != nil { |
| 4188 |
t.Fatalf("Unexpected error: %v", err) |
| 4189 |
} |
| 4190 |
msgs, err := sub.FetchBatch(5) |
| 4191 |
if err != nil { |
| 4192 |
t.Fatalf("Unexpected error: %v", err) |
| 4193 |
} |
| 4194 |
errCh := make(chan error) |
| 4195 |
go func() { |
| 4196 |
for { |
| 4197 |
err := msgs.Error() |
| 4198 |
if err != nil { |
| 4199 |
errCh <- err |
| 4200 |
return |
| 4201 |
} |
| 4202 |
} |
| 4203 |
}() |
| 4204 |
deleteErrCh := make(chan error, 1) |
| 4205 |
go func() { |
| 4206 |
time.Sleep(100 * time.Millisecond) |
| 4207 |
if err := js.DeleteConsumer("foo", cons.Name); err != nil { |
| 4208 |
deleteErrCh <- err |
| 4209 |
} |
| 4210 |
close(deleteErrCh) |
| 4211 |
}() |
| 4212 |
|
| 4213 |
var i int |
| 4214 |
for msg := range msgs.Messages() { |
| 4215 |
if string(msg.Data) != fmt.Sprintf("msg-%d", i) { |
| 4216 |
t.Fatalf("Invalid msg on index %d; expected: %s; got: %s", i, fmt.Sprintf("msg-%d", i), string(msg.Data)) |
| 4217 |
} |
| 4218 |
i++ |
| 4219 |
} |
| 4220 |
if i != 3 { |
| 4221 |
t.Fatalf("Invalid number of messages received; want: %d; got: %d", 5, i) |
| 4222 |
} |
| 4223 |
select { |
| 4224 |
case err := <-errCh: |
| 4225 |
if !errors.Is(err, nats.ErrConsumerDeleted) { |
| 4226 |
t.Fatalf("Expected error: %v; got: %v", nats.ErrConsumerDeleted, err) |
| 4227 |
} |
| 4228 |
case <-time.After(1 * time.Second): |
| 4229 |
t.Fatalf("Expected error: %v; got: %v", nats.ErrConsumerDeleted, nil) |
| 4230 |
} |
| 4231 |
|
| 4232 |
// wait until the consumer is deleted, otherwise we may close the connection |
| 4233 |
// before the consumer delete response is received |
| 4234 |
select { |
| 4235 |
case ert, ok := <-deleteErrCh: |
| 4236 |
if !ok { |
| 4237 |
break |
| 4238 |
} |
| 4239 |
t.Fatalf("Error deleting consumer: %s", ert) |
| 4240 |
case <-time.After(1 * time.Second): |
| 4241 |
t.Fatalf("Expected done to be closed") |
| 4242 |
} |
| 4243 |
}) |
| 4244 |
} |
| 4245 |
|
| 4246 |
func TestJetStreamSubscribeConsumerCreateTimeout(t *testing.T) { |
| 4247 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 4248 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 4249 |
if err != nil { |
| 4250 |
t.Fatalf("Unexpected error: %v", err) |
| 4251 |
} |
| 4252 |
|
| 4253 |
_, err = js.AddStream(&nats.StreamConfig{Name: "foo", Subjects: []string{"FOO.*"}}) |
| 4254 |
if err != nil { |
| 4255 |
t.Fatalf("Unexpected error: %v", err) |
| 4256 |
} |
| 4257 |
ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond) |
| 4258 |
defer cancel() |
| 4259 |
_, err = js.SubscribeSync("", nats.BindStream("foo"), nats.Context(ctx)) |
| 4260 |
if !errors.Is(err, context.DeadlineExceeded) { |
| 4261 |
t.Fatalf("Expected error") |
| 4262 |
} |
| 4263 |
}) |
| 4264 |
} |
| 4265 |
|
| 4266 |
func TestJetStreamPullSubscribeFetchErrOnReconnect(t *testing.T) { |
| 4267 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 4268 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 4269 |
if err != nil { |
| 4270 |
t.Fatalf("Unexpected error: %v", err) |
| 4271 |
} |
| 4272 |
_, err = js.AddStream(&nats.StreamConfig{Name: "foo", Subjects: []string{"FOO.*"}}) |
| 4273 |
if err != nil { |
| 4274 |
t.Fatalf("Unexpected error: %v", err) |
| 4275 |
} |
| 4276 |
sub, err := js.PullSubscribe("FOO.123", "bar") |
| 4277 |
if err != nil { |
| 4278 |
t.Fatalf("Unexpected error: %v", err) |
| 4279 |
} |
| 4280 |
defer sub.Unsubscribe() |
| 4281 |
errs := make(chan error, 1) |
| 4282 |
go func() { |
| 4283 |
time.Sleep(100 * time.Millisecond) |
| 4284 |
errs <- nc.ForceReconnect() |
| 4285 |
}() |
| 4286 |
_, err = sub.Fetch(1, nats.MaxWait(time.Second)) |
| 4287 |
if !errors.Is(err, nats.ErrFetchDisconnected) { |
| 4288 |
t.Fatalf("Expected error: %v; got: %v", nats.ErrFetchDisconnected, err) |
| 4289 |
} |
| 4290 |
if err := <-errs; err != nil { |
| 4291 |
t.Fatalf("Error on reconnect: %v", err) |
| 4292 |
} |
| 4293 |
}) |
| 4294 |
} |
| 4295 |
|
| 4296 |
func TestJetStreamPullSubscribeFetchBatchErrOnReconnect(t *testing.T) { |
| 4297 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 4298 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 4299 |
if err != nil { |
| 4300 |
t.Fatalf("Unexpected error: %v", err) |
| 4301 |
} |
| 4302 |
_, err = js.AddStream(&nats.StreamConfig{Name: "foo", Subjects: []string{"FOO.*"}}) |
| 4303 |
if err != nil { |
| 4304 |
t.Fatalf("Unexpected error: %v", err) |
| 4305 |
} |
| 4306 |
sub, err := js.PullSubscribe("FOO.123", "bar") |
| 4307 |
if err != nil { |
| 4308 |
t.Fatalf("Unexpected error: %v", err) |
| 4309 |
} |
| 4310 |
defer sub.Unsubscribe() |
| 4311 |
errs := make(chan error, 1) |
| 4312 |
go func() { |
| 4313 |
time.Sleep(100 * time.Millisecond) |
| 4314 |
errs <- nc.ForceReconnect() |
| 4315 |
}() |
| 4316 |
msgs, err := sub.FetchBatch(1, nats.MaxWait(time.Second), nats.PullHeartbeat(100*time.Millisecond)) |
| 4317 |
if err != nil { |
| 4318 |
t.Fatalf("Unexpected error: %v", err) |
| 4319 |
} |
| 4320 |
for range msgs.Messages() { |
| 4321 |
t.Fatalf("Expected no messages, got one") |
| 4322 |
} |
| 4323 |
if !errors.Is(msgs.Error(), nats.ErrFetchDisconnected) { |
| 4324 |
t.Fatalf("Expected error: %v; got: %v", nats.ErrFetchDisconnected, msgs.Error()) |
| 4325 |
} |
| 4326 |
if err := <-errs; err != nil { |
| 4327 |
t.Fatalf("Error on reconnect: %v", err) |
| 4328 |
} |
| 4329 |
}) |
| 4330 |
} |
| 4331 |
|
| 4332 |
func TestJetStreamSubscribeShortTimeoutWithContext(t *testing.T) { |
| 4333 |
withJSServerInstance(t, func(t *testing.T, nc *nats.Conn, _ *testservice.Instance) { |
| 4334 |
// create separate JetStream context to create a stream |
| 4335 |
js, err := nc.JetStream() |
| 4336 |
if err != nil { |
| 4337 |
t.Fatalf("Unexpected error: %v", err) |
| 4338 |
} |
| 4339 |
|
| 4340 |
_, err = js.AddStream(&nats.StreamConfig{Name: "foo", Subjects: []string{"FOO.*"}}) |
| 4341 |
if err != nil { |
| 4342 |
t.Fatalf("Unexpected error: %v", err) |
| 4343 |
} |
| 4344 |
|
| 4345 |
// subscribe with MaxWait set to nanosecond |
| 4346 |
// this should fail with context.DeadlineExceeded |
| 4347 |
jsShort, err := nc.JetStream(nats.MaxWait(time.Nanosecond)) |
| 4348 |
if err != nil { |
| 4349 |
t.Fatalf("Unexpected error: %v", err) |
| 4350 |
} |
| 4351 |
|
| 4352 |
// no context - creating consumer should fail with js.MaxWait set to nanosecond |
| 4353 |
_, err = jsShort.SubscribeSync("FOO.123", nats.BindStream("foo")) |
| 4354 |
if !errors.Is(err, context.DeadlineExceeded) { |
| 4355 |
t.Fatalf("Expected error: %v; got: %v", context.DeadlineExceeded, err) |
| 4356 |
} |
| 4357 |
|
| 4358 |
// use context.Background() - creating consumer should fail with js.MaxWait set to nanosecond |
| 4359 |
_, err = jsShort.SubscribeSync("FOO.123", nats.BindStream("foo"), nats.Context(context.Background())) |
| 4360 |
if !errors.Is(err, context.DeadlineExceeded) { |
| 4361 |
t.Fatalf("Expected error: %v; got: %v", context.DeadlineExceeded, err) |
| 4362 |
} |
| 4363 |
}) |
| 4364 |
} |
| 4365 |
|
| 4366 |
func TestStreamLister(t *testing.T) { |
| 4367 |
tests := []struct { |
| 4368 |
name string |
| 4369 |
streamsNum int |
| 4370 |
}{ |
| 4371 |
{ |
| 4372 |
name: "single page", |
| 4373 |
streamsNum: 5, |
| 4374 |
}, |
| 4375 |
{ |
| 4376 |
name: "multi page", |
| 4377 |
streamsNum: 1025, |
| 4378 |
}, |
| 4379 |
} |
| 4380 |
|
| 4381 |
for _, test := range tests { |
| 4382 |
t.Run(test.name, func(t *testing.T) { |
| 4383 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 4384 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 4385 |
if err != nil { |
| 4386 |
t.Fatalf("Unexpected error: %v", err) |
| 4387 |
} |
| 4388 |
for i := range test.streamsNum { |
| 4389 |
if _, err := js.AddStream(&nats.StreamConfig{Name: fmt.Sprintf("stream_%d", i)}); err != nil { |
| 4390 |
t.Fatalf("Unexpected error: %v", err) |
| 4391 |
} |
| 4392 |
} |
| 4393 |
names := make([]string, 0) |
| 4394 |
for name := range js.StreamNames() { |
| 4395 |
names = append(names, name) |
| 4396 |
} |
| 4397 |
if len(names) != test.streamsNum { |
| 4398 |
t.Fatalf("Invalid number of stream names; want: %d; got: %d", test.streamsNum, len(names)) |
| 4399 |
} |
| 4400 |
infos := make([]*nats.StreamInfo, 0) |
| 4401 |
for info := range js.Streams() { |
| 4402 |
infos = append(infos, info) |
| 4403 |
} |
| 4404 |
if len(infos) != test.streamsNum { |
| 4405 |
t.Fatalf("Invalid number of streams; want: %d; got: %d", test.streamsNum, len(infos)) |
| 4406 |
} |
| 4407 |
// test the deprecated StreamsInfo() |
| 4408 |
infos = make([]*nats.StreamInfo, 0) |
| 4409 |
for info := range js.StreamsInfo() { |
| 4410 |
infos = append(infos, info) |
| 4411 |
} |
| 4412 |
if len(infos) != test.streamsNum { |
| 4413 |
t.Fatalf("Invalid number of streams; want: %d; got: %d", test.streamsNum, len(infos)) |
| 4414 |
} |
| 4415 |
}) |
| 4416 |
}) |
| 4417 |
} |
| 4418 |
} |
| 4419 |
|
| 4420 |
func TestStreamLister_FilterSubject(t *testing.T) { |
| 4421 |
streams := map[string][]string{ |
| 4422 |
"s1": {"foo"}, |
| 4423 |
"s2": {"bar"}, |
| 4424 |
"s3": {"foo.*", "bar.*"}, |
| 4425 |
"s4": {"foo-1.A"}, |
| 4426 |
"s5": {"foo.A.bar.B"}, |
| 4427 |
"s6": {"foo.C.bar.D.E"}, |
| 4428 |
} |
| 4429 |
tests := []struct { |
| 4430 |
filter string |
| 4431 |
expected []string |
| 4432 |
}{ |
| 4433 |
{ |
| 4434 |
filter: "foo", |
| 4435 |
expected: []string{"s1"}, |
| 4436 |
}, |
| 4437 |
{ |
| 4438 |
filter: "bar", |
| 4439 |
expected: []string{"s2"}, |
| 4440 |
}, |
| 4441 |
{ |
| 4442 |
filter: "*", |
| 4443 |
expected: []string{"s1", "s2"}, |
| 4444 |
}, |
| 4445 |
{ |
| 4446 |
filter: ">", |
| 4447 |
expected: []string{"s1", "s2", "s3", "s4", "s5", "s6"}, |
| 4448 |
}, |
| 4449 |
{ |
| 4450 |
filter: "*.A", |
| 4451 |
expected: []string{"s3", "s4"}, |
| 4452 |
}, |
| 4453 |
} |
| 4454 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 4455 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 4456 |
if err != nil { |
| 4457 |
t.Fatalf("Unexpected error: %v", err) |
| 4458 |
} |
| 4459 |
for name, subjects := range streams { |
| 4460 |
if _, err := js.AddStream(&nats.StreamConfig{Name: name, Subjects: subjects}); err != nil { |
| 4461 |
t.Fatalf("Unexpected error: %v", err) |
| 4462 |
} |
| 4463 |
} |
| 4464 |
|
| 4465 |
for _, test := range tests { |
| 4466 |
t.Run(test.filter, func(t *testing.T) { |
| 4467 |
names := make([]string, 0) |
| 4468 |
|
| 4469 |
// list stream names |
| 4470 |
for name := range js.StreamNames(nats.StreamListFilter(test.filter)) { |
| 4471 |
names = append(names, name) |
| 4472 |
} |
| 4473 |
if !reflect.DeepEqual(names, test.expected) { |
| 4474 |
t.Fatalf("Invalid result; want: %v; got: %v", test.expected, names) |
| 4475 |
} |
| 4476 |
|
| 4477 |
// list streams |
| 4478 |
names = make([]string, 0) |
| 4479 |
for info := range js.Streams(nats.StreamListFilter(test.filter)) { |
| 4480 |
names = append(names, info.Config.Name) |
| 4481 |
} |
| 4482 |
if !reflect.DeepEqual(names, test.expected) { |
| 4483 |
t.Fatalf("Invalid result; want: %v; got: %v", test.expected, names) |
| 4484 |
} |
| 4485 |
}) |
| 4486 |
} |
| 4487 |
}) |
| 4488 |
} |
| 4489 |
|
| 4490 |
func TestConsumersLister(t *testing.T) { |
| 4491 |
tests := []struct { |
| 4492 |
name string |
| 4493 |
consumersNum int |
| 4494 |
}{ |
| 4495 |
{ |
| 4496 |
name: "single page", |
| 4497 |
consumersNum: 5, |
| 4498 |
}, |
| 4499 |
{ |
| 4500 |
name: "multi page", |
| 4501 |
consumersNum: 1025, |
| 4502 |
}, |
| 4503 |
} |
| 4504 |
|
| 4505 |
for _, test := range tests { |
| 4506 |
t.Run(test.name, func(t *testing.T) { |
| 4507 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 4508 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 4509 |
if err != nil { |
| 4510 |
t.Fatalf("Unexpected error: %v", err) |
| 4511 |
} |
| 4512 |
js.AddStream(&nats.StreamConfig{Name: "foo", MaxConsumers: 2000}) |
| 4513 |
for i := range test.consumersNum { |
| 4514 |
if _, err := js.AddConsumer("foo", &nats.ConsumerConfig{Durable: fmt.Sprintf("cons_%d", i), AckPolicy: nats.AckExplicitPolicy}); err != nil { |
| 4515 |
t.Fatalf("Unexpected error: %v", err) |
| 4516 |
} |
| 4517 |
} |
| 4518 |
names := make([]string, 0) |
| 4519 |
for name := range js.ConsumerNames("foo") { |
| 4520 |
names = append(names, name) |
| 4521 |
} |
| 4522 |
if len(names) != test.consumersNum { |
| 4523 |
t.Fatalf("Invalid number of consumer names; want: %d; got: %d", test.consumersNum, len(names)) |
| 4524 |
} |
| 4525 |
infos := make([]*nats.ConsumerInfo, 0) |
| 4526 |
for info := range js.Consumers("foo") { |
| 4527 |
infos = append(infos, info) |
| 4528 |
} |
| 4529 |
if len(infos) != test.consumersNum { |
| 4530 |
t.Fatalf("Invalid number of consumers; want: %d; got: %d", test.consumersNum, len(infos)) |
| 4531 |
} |
| 4532 |
|
| 4533 |
// test the deprecated ConsumersInfo() |
| 4534 |
infos = make([]*nats.ConsumerInfo, 0) |
| 4535 |
for info := range js.ConsumersInfo("foo") { |
| 4536 |
infos = append(infos, info) |
| 4537 |
} |
| 4538 |
if len(infos) != test.consumersNum { |
| 4539 |
t.Fatalf("Invalid number of consumers; want: %d; got: %d", test.consumersNum, len(infos)) |
| 4540 |
} |
| 4541 |
}) |
| 4542 |
}) |
| 4543 |
} |
| 4544 |
} |
| 4545 |
|
| 4546 |
func TestAccountInfo(t *testing.T) { |
| 4547 |
type tc struct { |
| 4548 |
name string |
| 4549 |
setup func(t *testing.T) *nats.Conn |
| 4550 |
expected *nats.AccountInfo |
| 4551 |
withError error |
| 4552 |
} |
| 4553 |
tests := []tc{ |
| 4554 |
{ |
| 4555 |
name: "server with default values", |
| 4556 |
setup: func(t *testing.T) *nats.Conn { |
| 4557 |
c := newTester(t) |
| 4558 |
accounts := `accounts: { |
| 4559 |
A { |
| 4560 |
users: [{ user: "foo" }] |
| 4561 |
jetstream: enabled |
| 4562 |
} |
| 4563 |
}` |
| 4564 |
inst := c.CreateServer(t, true, |
| 4565 |
testservice.WithAccounts(accounts), |
| 4566 |
testservice.WithAuthorization("no_auth_user: foo"), |
| 4567 |
testservice.WithSystemAccount(noSystemAccountBody()), |
| 4568 |
) |
| 4569 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 4570 |
nc := dialInstance(t, inst, nats.UserInfo("foo", "")) |
| 4571 |
// Note: do NOT call WaitForJetStream here — it hits $JS.API.INFO |
| 4572 |
// and would inflate the account's API.Total counter from 0 to 1, |
| 4573 |
// breaking the equality assertion against the expected zero baseline. |
| 4574 |
return nc |
| 4575 |
}, |
| 4576 |
expected: &nats.AccountInfo{ |
| 4577 |
Tier: nats.Tier{ |
| 4578 |
Memory: 0, |
| 4579 |
Store: 0, |
| 4580 |
Streams: 0, |
| 4581 |
Consumers: 0, |
| 4582 |
ReservedMemory: 0, |
| 4583 |
ReservedStore: 0, |
| 4584 |
Limits: nats.AccountLimits{ |
| 4585 |
MaxMemory: -1, |
| 4586 |
MaxStore: -1, |
| 4587 |
MaxStreams: -1, |
| 4588 |
MaxConsumers: -1, |
| 4589 |
MaxAckPending: -1, |
| 4590 |
MemoryMaxStreamBytes: -1, |
| 4591 |
StoreMaxStreamBytes: -1, |
| 4592 |
MaxBytesRequired: false, |
| 4593 |
}, |
| 4594 |
}, |
| 4595 |
API: nats.APIStats{ |
| 4596 |
Total: 0, |
| 4597 |
Errors: 0, |
| 4598 |
// Level is populated from the server response below; not pinned |
| 4599 |
// because it bumps with each server release. |
| 4600 |
Inflight: 0, |
| 4601 |
}, |
| 4602 |
}, |
| 4603 |
}, |
| 4604 |
{ |
| 4605 |
name: "server with limits set", |
| 4606 |
setup: func(t *testing.T) *nats.Conn { |
| 4607 |
c := newTester(t) |
| 4608 |
accounts := `accounts: { |
| 4609 |
A { |
| 4610 |
users: [{ user: "foo" }] |
| 4611 |
jetstream: { |
| 4612 |
max_mem: 64MB, |
| 4613 |
max_file: 32MB, |
| 4614 |
max_streams: 10, |
| 4615 |
max_consumers: 20, |
| 4616 |
max_ack_pending: 100, |
| 4617 |
memory_max_stream_bytes: 2048, |
| 4618 |
store_max_stream_bytes: 4096, |
| 4619 |
max_stream_bytes: true |
| 4620 |
} |
| 4621 |
} |
| 4622 |
}` |
| 4623 |
inst := c.CreateServer(t, true, |
| 4624 |
testservice.WithJetStream(`domain: "test-domain"`), |
| 4625 |
testservice.WithAccounts(accounts), |
| 4626 |
testservice.WithAuthorization("no_auth_user: foo"), |
| 4627 |
testservice.WithSystemAccount(noSystemAccountBody()), |
| 4628 |
) |
| 4629 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 4630 |
nc := dialInstance(t, inst, nats.UserInfo("foo", "")) |
| 4631 |
// Note: do NOT call WaitForJetStream here — it hits $JS.API.INFO |
| 4632 |
// and would inflate the account's API.Total counter from 0 to 1, |
| 4633 |
// breaking the equality assertion against the expected zero baseline. |
| 4634 |
return nc |
| 4635 |
}, |
| 4636 |
expected: &nats.AccountInfo{ |
| 4637 |
Tier: nats.Tier{ |
| 4638 |
Memory: 0, |
| 4639 |
Store: 0, |
| 4640 |
Streams: 0, |
| 4641 |
Consumers: 0, |
| 4642 |
ReservedMemory: 0, |
| 4643 |
ReservedStore: 0, |
| 4644 |
Limits: nats.AccountLimits{ |
| 4645 |
MaxMemory: 67108864, |
| 4646 |
MaxStore: 33554432, |
| 4647 |
MaxStreams: 10, |
| 4648 |
MaxConsumers: 20, |
| 4649 |
MaxAckPending: 100, |
| 4650 |
MemoryMaxStreamBytes: 2048, |
| 4651 |
StoreMaxStreamBytes: 4096, |
| 4652 |
MaxBytesRequired: true, |
| 4653 |
}, |
| 4654 |
}, |
| 4655 |
Domain: "test-domain", |
| 4656 |
API: nats.APIStats{ |
| 4657 |
Total: 0, |
| 4658 |
Errors: 0, |
| 4659 |
Inflight: 0, |
| 4660 |
}, |
| 4661 |
}, |
| 4662 |
}, |
| 4663 |
{ |
| 4664 |
name: "jetstream not enabled", |
| 4665 |
setup: func(t *testing.T) *nats.Conn { |
| 4666 |
c := newTester(t) |
| 4667 |
inst := c.CreateServer(t, false, |
| 4668 |
testservice.WithAccounts(`accounts: { |
| 4669 |
A { |
| 4670 |
users: [{ user: "foo" }] |
| 4671 |
} |
| 4672 |
}`), |
| 4673 |
testservice.WithAuthorization("no_auth_user: foo"), |
| 4674 |
testservice.WithSystemAccount(noSystemAccountBody()), |
| 4675 |
) |
| 4676 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 4677 |
return dialInstance(t, inst, nats.UserInfo("foo", "")) |
| 4678 |
}, |
| 4679 |
withError: nats.ErrJetStreamNotEnabled, |
| 4680 |
}, |
| 4681 |
{ |
| 4682 |
name: "jetstream not enabled for account", |
| 4683 |
setup: func(t *testing.T) *nats.Conn { |
| 4684 |
c := newTester(t) |
| 4685 |
accounts := `accounts: { |
| 4686 |
A: { |
| 4687 |
users: [ {user: foo} ] |
| 4688 |
}, |
| 4689 |
}` |
| 4690 |
inst := c.CreateServer(t, true, |
| 4691 |
testservice.WithAccounts(accounts), |
| 4692 |
testservice.WithAuthorization("no_auth_user: foo"), |
| 4693 |
testservice.WithSystemAccount(noSystemAccountBody()), |
| 4694 |
) |
| 4695 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 4696 |
nc := dialInstance(t, inst, nats.UserInfo("foo", "")) |
| 4697 |
// Wait for JS to be ready system-wide via an admin user is not possible here without $SYS; |
| 4698 |
// instead just give the server time to start. A small sleep is okay because subsequent |
| 4699 |
// js.AccountInfo() call has its own MaxWait. |
| 4700 |
return nc |
| 4701 |
}, |
| 4702 |
withError: nats.ErrJetStreamNotEnabledForAccount, |
| 4703 |
}, |
| 4704 |
} |
| 4705 |
|
| 4706 |
for _, test := range tests { |
| 4707 |
t.Run(test.name, func(t *testing.T) { |
| 4708 |
nc := test.setup(t) |
| 4709 |
defer nc.Close() |
| 4710 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 4711 |
if err != nil { |
| 4712 |
t.Fatalf("Unexpected error: %v", err) |
| 4713 |
} |
| 4714 |
info, err := js.AccountInfo() |
| 4715 |
if test.withError != nil { |
| 4716 |
if err == nil || !errors.Is(err, test.withError) { |
| 4717 |
t.Fatalf("Expected error: '%s'; got '%s'", test.withError, err) |
| 4718 |
} |
| 4719 |
return |
| 4720 |
} |
| 4721 |
if err != nil { |
| 4722 |
t.Fatalf("Unexpected error: %v", err) |
| 4723 |
} |
| 4724 |
|
| 4725 |
// API.Level is server-version-dependent; take it from the |
| 4726 |
// actual response so the assertion below stays portable. |
| 4727 |
test.expected.API.Level = info.API.Level |
| 4728 |
|
| 4729 |
if !reflect.DeepEqual(test.expected, info) { |
| 4730 |
t.Fatalf("Account info does not match; expected: %+v; got: %+v", test.expected, info) |
| 4731 |
} |
| 4732 |
_, err = js.AddStream(&nats.StreamConfig{Name: "FOO", MaxBytes: 1024}) |
| 4733 |
if err != nil { |
| 4734 |
t.Fatalf("Unexpected error: %v", err) |
| 4735 |
} |
| 4736 |
_, err = js.AddConsumer("FOO", &nats.ConsumerConfig{AckPolicy: nats.AckExplicitPolicy}) |
| 4737 |
if err != nil { |
| 4738 |
t.Fatalf("Unexpected error: %v", err) |
| 4739 |
} |
| 4740 |
|
| 4741 |
// a total of 3 API calls is expected - get account info, create stream, create consumer |
| 4742 |
test.expected.API.Total = 3 |
| 4743 |
test.expected.Streams = 1 |
| 4744 |
test.expected.Consumers = 1 |
| 4745 |
|
| 4746 |
info, err = js.AccountInfo() |
| 4747 |
if err != nil { |
| 4748 |
t.Fatalf("Unexpected error: %v", err) |
| 4749 |
} |
| 4750 |
// ignore reserved store in comparison since this is dynamically |
| 4751 |
// assigned by the server |
| 4752 |
info.ReservedStore = test.expected.ReservedStore |
| 4753 |
|
| 4754 |
if !reflect.DeepEqual(test.expected, info) { |
| 4755 |
t.Fatalf("Account info does not match; expected: %+v; got: %+v", test.expected, info) |
| 4756 |
} |
| 4757 |
}) |
| 4758 |
} |
| 4759 |
} |
| 4760 |
|
| 4761 |
func TestPurgeStream(t *testing.T) { |
| 4762 |
testData := []nats.Msg{ |
| 4763 |
{ |
| 4764 |
Subject: "foo.A", |
| 4765 |
Data: []byte("first on A"), |
| 4766 |
}, |
| 4767 |
{ |
| 4768 |
Subject: "foo.C", |
| 4769 |
Data: []byte("first on C"), |
| 4770 |
}, |
| 4771 |
{ |
| 4772 |
Subject: "foo.B", |
| 4773 |
Data: []byte("first on B"), |
| 4774 |
}, |
| 4775 |
{ |
| 4776 |
Subject: "foo.C", |
| 4777 |
Data: []byte("second on C"), |
| 4778 |
}, |
| 4779 |
} |
| 4780 |
|
| 4781 |
tests := []struct { |
| 4782 |
name string |
| 4783 |
stream string |
| 4784 |
req *nats.StreamPurgeRequest |
| 4785 |
withError error |
| 4786 |
expected []nats.Msg |
| 4787 |
}{ |
| 4788 |
{ |
| 4789 |
name: "purge all messages", |
| 4790 |
stream: "foo", |
| 4791 |
expected: []nats.Msg{}, |
| 4792 |
}, |
| 4793 |
{ |
| 4794 |
name: "with filter subject", |
| 4795 |
stream: "foo", |
| 4796 |
req: &nats.StreamPurgeRequest{ |
| 4797 |
Subject: "foo.C", |
| 4798 |
}, |
| 4799 |
expected: []nats.Msg{ |
| 4800 |
{ |
| 4801 |
Subject: "foo.A", |
| 4802 |
Data: []byte("first on A"), |
| 4803 |
}, |
| 4804 |
{ |
| 4805 |
Subject: "foo.B", |
| 4806 |
Data: []byte("first on B"), |
| 4807 |
}, |
| 4808 |
}, |
| 4809 |
}, |
| 4810 |
{ |
| 4811 |
name: "with sequence", |
| 4812 |
stream: "foo", |
| 4813 |
req: &nats.StreamPurgeRequest{ |
| 4814 |
Sequence: 3, |
| 4815 |
}, |
| 4816 |
expected: []nats.Msg{ |
| 4817 |
{ |
| 4818 |
Subject: "foo.B", |
| 4819 |
Data: []byte("first on B"), |
| 4820 |
}, |
| 4821 |
{ |
| 4822 |
Subject: "foo.C", |
| 4823 |
Data: []byte("second on C"), |
| 4824 |
}, |
| 4825 |
}, |
| 4826 |
}, |
| 4827 |
{ |
| 4828 |
name: "with keep", |
| 4829 |
stream: "foo", |
| 4830 |
req: &nats.StreamPurgeRequest{ |
| 4831 |
Keep: 1, |
| 4832 |
}, |
| 4833 |
expected: []nats.Msg{ |
| 4834 |
{ |
| 4835 |
Subject: "foo.C", |
| 4836 |
Data: []byte("second on C"), |
| 4837 |
}, |
| 4838 |
}, |
| 4839 |
}, |
| 4840 |
{ |
| 4841 |
name: "with filter and sequence", |
| 4842 |
stream: "foo", |
| 4843 |
req: &nats.StreamPurgeRequest{ |
| 4844 |
Subject: "foo.C", |
| 4845 |
Sequence: 3, |
| 4846 |
}, |
| 4847 |
expected: []nats.Msg{ |
| 4848 |
{ |
| 4849 |
Subject: "foo.A", |
| 4850 |
Data: []byte("first on A"), |
| 4851 |
}, |
| 4852 |
{ |
| 4853 |
Subject: "foo.B", |
| 4854 |
Data: []byte("first on B"), |
| 4855 |
}, |
| 4856 |
{ |
| 4857 |
Subject: "foo.C", |
| 4858 |
Data: []byte("second on C"), |
| 4859 |
}, |
| 4860 |
}, |
| 4861 |
}, |
| 4862 |
{ |
| 4863 |
name: "with filter and keep", |
| 4864 |
stream: "foo", |
| 4865 |
req: &nats.StreamPurgeRequest{ |
| 4866 |
Subject: "foo.C", |
| 4867 |
Keep: 1, |
| 4868 |
}, |
| 4869 |
expected: []nats.Msg{ |
| 4870 |
{ |
| 4871 |
Subject: "foo.A", |
| 4872 |
Data: []byte("first on A"), |
| 4873 |
}, |
| 4874 |
{ |
| 4875 |
Subject: "foo.B", |
| 4876 |
Data: []byte("first on B"), |
| 4877 |
}, |
| 4878 |
{ |
| 4879 |
Subject: "foo.C", |
| 4880 |
Data: []byte("second on C"), |
| 4881 |
}, |
| 4882 |
}, |
| 4883 |
}, |
| 4884 |
{ |
| 4885 |
name: "empty stream name", |
| 4886 |
stream: "", |
| 4887 |
req: &nats.StreamPurgeRequest{ |
| 4888 |
Subject: "foo.C", |
| 4889 |
Keep: 1, |
| 4890 |
}, |
| 4891 |
withError: nats.ErrStreamNameRequired, |
| 4892 |
}, |
| 4893 |
{ |
| 4894 |
name: "invalid stream name", |
| 4895 |
stream: "bad.stream.name", |
| 4896 |
req: &nats.StreamPurgeRequest{ |
| 4897 |
Subject: "foo.C", |
| 4898 |
Keep: 1, |
| 4899 |
}, |
| 4900 |
withError: nats.ErrInvalidStreamName, |
| 4901 |
}, |
| 4902 |
{ |
| 4903 |
name: "invalid request - both sequence and keep provided", |
| 4904 |
stream: "foo", |
| 4905 |
req: &nats.StreamPurgeRequest{ |
| 4906 |
Sequence: 3, |
| 4907 |
Keep: 1, |
| 4908 |
}, |
| 4909 |
withError: nats.ErrBadRequest, |
| 4910 |
}, |
| 4911 |
} |
| 4912 |
for _, test := range tests { |
| 4913 |
t.Run(test.name, func(t *testing.T) { |
| 4914 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 4915 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 4916 |
if err != nil { |
| 4917 |
t.Fatalf("Unexpected error: %v", err) |
| 4918 |
} |
| 4919 |
|
| 4920 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 4921 |
Name: "foo", |
| 4922 |
Subjects: []string{"foo.A", "foo.B", "foo.C"}, |
| 4923 |
}) |
| 4924 |
if err != nil { |
| 4925 |
t.Fatalf("Unexpected error: %v", err) |
| 4926 |
} |
| 4927 |
|
| 4928 |
for _, msg := range testData { |
| 4929 |
if _, err := js.PublishMsg(&msg); err != nil { |
| 4930 |
t.Fatalf("Unexpected error during publish: %v", err) |
| 4931 |
} |
| 4932 |
} |
| 4933 |
|
| 4934 |
err = js.PurgeStream(test.stream, test.req) |
| 4935 |
if test.withError != nil { |
| 4936 |
if err == nil { |
| 4937 |
t.Fatal("Expected error, got nil") |
| 4938 |
} |
| 4939 |
if !errors.Is(err, test.withError) { |
| 4940 |
t.Fatalf("Expected error: '%s'; got '%s'", test.withError, err) |
| 4941 |
} |
| 4942 |
return |
| 4943 |
} |
| 4944 |
|
| 4945 |
streamInfo, err := js.StreamInfo("foo", test.req) |
| 4946 |
if err != nil { |
| 4947 |
t.Fatalf("Unexpected error: %v", err) |
| 4948 |
} |
| 4949 |
if streamInfo.State.Msgs != uint64(len(test.expected)) { |
| 4950 |
t.Fatalf("Unexpected message count: expected %d; got: %d", len(test.expected), streamInfo.State.Msgs) |
| 4951 |
} |
| 4952 |
sub, err := js.SubscribeSync("foo.*", nats.BindStream("foo")) |
| 4953 |
if err != nil { |
| 4954 |
t.Fatalf("Unexpected error: %s", err) |
| 4955 |
} |
| 4956 |
for i := range int(streamInfo.State.Msgs) { |
| 4957 |
msg, err := sub.NextMsg(1 * time.Second) |
| 4958 |
if err != nil { |
| 4959 |
t.Fatalf("Unexpected error: %s", err) |
| 4960 |
} |
| 4961 |
if msg.Subject != test.expected[i].Subject { |
| 4962 |
t.Fatalf("Unexpected message; subject is different than expected: want %s; got: %s", test.expected[i].Subject, msg.Subject) |
| 4963 |
} |
| 4964 |
if string(msg.Data) != string(test.expected[i].Data) { |
| 4965 |
t.Fatalf("Unexpected message; data is different than expected: want %s; got: %s", test.expected[i].Data, msg.Data) |
| 4966 |
} |
| 4967 |
} |
| 4968 |
}) |
| 4969 |
}) |
| 4970 |
} |
| 4971 |
} |
| 4972 |
|
| 4973 |
func TestStreamInfoSubjectInfo(t *testing.T) { |
| 4974 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 4975 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 4976 |
if err != nil { |
| 4977 |
t.Fatalf("Unexpected error: %v", err) |
| 4978 |
} |
| 4979 |
|
| 4980 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 4981 |
Name: "foo", |
| 4982 |
Subjects: []string{"foo.*"}, |
| 4983 |
}) |
| 4984 |
if err != nil { |
| 4985 |
t.Fatalf("Unexpected error: %v", err) |
| 4986 |
} |
| 4987 |
|
| 4988 |
if _, err := js.Publish("foo.A", []byte("")); err != nil { |
| 4989 |
t.Fatalf("Unexpected error during publish: %v", err) |
| 4990 |
} |
| 4991 |
if _, err := js.Publish("foo.B", []byte("")); err != nil { |
| 4992 |
t.Fatalf("Unexpected error during publish: %v", err) |
| 4993 |
} |
| 4994 |
|
| 4995 |
si, err := js.StreamInfo("foo", &nats.StreamInfoRequest{ |
| 4996 |
SubjectsFilter: "foo.A", |
| 4997 |
}) |
| 4998 |
if err != nil { |
| 4999 |
t.Fatalf("Unexpected error: %v", err) |
| 5000 |
} |
| 5001 |
if si.State.NumSubjects != 2 { |
| 5002 |
t.Fatal("Expected NumSubjects to be 1") |
| 5003 |
} |
| 5004 |
if len(si.State.Subjects) != 1 { |
| 5005 |
t.Fatal("Expected Subjects len to be 1") |
| 5006 |
} |
| 5007 |
if si.State.Subjects["foo.A"] != 1 { |
| 5008 |
t.Fatal("Expected Subjects to have an entry for foo.A with a count of 1") |
| 5009 |
} |
| 5010 |
}) |
| 5011 |
} |
| 5012 |
|
| 5013 |
func TestStreamInfoDeletedDetails(t *testing.T) { |
| 5014 |
testData := []string{"one", "two", "three", "four"} |
| 5015 |
|
| 5016 |
tests := []struct { |
| 5017 |
name string |
| 5018 |
stream string |
| 5019 |
req *nats.StreamInfoRequest |
| 5020 |
withError error |
| 5021 |
expectedDeletedDetails []uint64 |
| 5022 |
}{ |
| 5023 |
{ |
| 5024 |
name: "empty request body", |
| 5025 |
stream: "foo", |
| 5026 |
}, |
| 5027 |
{ |
| 5028 |
name: "with deleted details", |
| 5029 |
stream: "foo", |
| 5030 |
req: &nats.StreamInfoRequest{ |
| 5031 |
DeletedDetails: true, |
| 5032 |
}, |
| 5033 |
expectedDeletedDetails: []uint64{2, 4}, |
| 5034 |
}, |
| 5035 |
{ |
| 5036 |
name: "with deleted details set to false", |
| 5037 |
stream: "foo", |
| 5038 |
req: &nats.StreamInfoRequest{ |
| 5039 |
DeletedDetails: false, |
| 5040 |
}, |
| 5041 |
}, |
| 5042 |
{ |
| 5043 |
name: "empty stream name", |
| 5044 |
stream: "", |
| 5045 |
withError: nats.ErrStreamNameRequired, |
| 5046 |
}, |
| 5047 |
{ |
| 5048 |
name: "invalid stream name", |
| 5049 |
stream: "bad.stream.name", |
| 5050 |
withError: nats.ErrInvalidStreamName, |
| 5051 |
}, |
| 5052 |
{ |
| 5053 |
name: "stream not found", |
| 5054 |
stream: "bar", |
| 5055 |
withError: nats.ErrStreamNotFound, |
| 5056 |
}, |
| 5057 |
} |
| 5058 |
for _, test := range tests { |
| 5059 |
t.Run(test.name, func(t *testing.T) { |
| 5060 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 5061 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 5062 |
if err != nil { |
| 5063 |
t.Fatalf("Unexpected error: %v", err) |
| 5064 |
} |
| 5065 |
|
| 5066 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 5067 |
Name: "foo", |
| 5068 |
Subjects: []string{"foo.A"}, |
| 5069 |
}) |
| 5070 |
if err != nil { |
| 5071 |
t.Fatalf("Unexpected error: %v", err) |
| 5072 |
} |
| 5073 |
|
| 5074 |
for _, msg := range testData { |
| 5075 |
if _, err := js.Publish("foo.A", []byte(msg)); err != nil { |
| 5076 |
t.Fatalf("Unexpected error during publish: %v", err) |
| 5077 |
} |
| 5078 |
} |
| 5079 |
if err := js.DeleteMsg("foo", 2); err != nil { |
| 5080 |
t.Fatalf("Unexpected error while deleting message from stream: %v", err) |
| 5081 |
} |
| 5082 |
if err := js.DeleteMsg("foo", 4); err != nil { |
| 5083 |
t.Fatalf("Unexpected error while deleting message from stream: %v", err) |
| 5084 |
} |
| 5085 |
|
| 5086 |
var streamInfo *nats.StreamInfo |
| 5087 |
if test.req != nil { |
| 5088 |
streamInfo, err = js.StreamInfo(test.stream, test.req) |
| 5089 |
} else { |
| 5090 |
streamInfo, err = js.StreamInfo(test.stream) |
| 5091 |
} |
| 5092 |
if test.withError != nil { |
| 5093 |
if err == nil { |
| 5094 |
t.Fatal("Expected error, got nil") |
| 5095 |
} |
| 5096 |
if !errors.Is(err, test.withError) { |
| 5097 |
t.Fatalf("Expected error: '%s'; got '%s'", test.withError, err) |
| 5098 |
} |
| 5099 |
return |
| 5100 |
} |
| 5101 |
if err != nil { |
| 5102 |
t.Fatalf("Unexpected error: %v", err) |
| 5103 |
} |
| 5104 |
if streamInfo.Config.Name != "foo" { |
| 5105 |
t.Fatalf("Invalid stream name in StreamInfo response: want: 'foo'; got: '%s'", streamInfo.Config.Name) |
| 5106 |
} |
| 5107 |
if streamInfo.State.NumDeleted != 2 { |
| 5108 |
t.Fatalf("Invalid value for num_deleted in state: want: 2; got: %d", streamInfo.State.NumDeleted) |
| 5109 |
} |
| 5110 |
if !reflect.DeepEqual(test.expectedDeletedDetails, streamInfo.State.Deleted) { |
| 5111 |
t.Fatalf("Invalid value for deleted msgs in state: want: %v; got: %v", test.expectedDeletedDetails, streamInfo.State.Deleted) |
| 5112 |
} |
| 5113 |
}) |
| 5114 |
}) |
| 5115 |
} |
| 5116 |
} |
| 5117 |
|
| 5118 |
// testJetStreamManagement_GetMsgBody is the shared body used by both the |
| 5119 |
// 1-node and 3-node subtests of TestJetStreamManagement_GetMsg. |
| 5120 |
func testJetStreamManagement_GetMsgBody(t *testing.T, nc *nats.Conn) { |
| 5121 |
t.Helper() |
| 5122 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 5123 |
if err != nil { |
| 5124 |
t.Fatalf("Unexpected error: %v", err) |
| 5125 |
} |
| 5126 |
|
| 5127 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 5128 |
Name: "foo", |
| 5129 |
Subjects: []string{"foo.A", "foo.B", "foo.C"}, |
| 5130 |
}) |
| 5131 |
if err != nil { |
| 5132 |
t.Fatalf("Unexpected error: %v", err) |
| 5133 |
} |
| 5134 |
|
| 5135 |
for i := range 5 { |
| 5136 |
msg := nats.NewMsg("foo.A") |
| 5137 |
data := fmt.Sprintf("A:%d", i) |
| 5138 |
msg.Data = []byte(data) |
| 5139 |
msg.Header = nats.Header{ |
| 5140 |
"X-NATS-Key": []string{"123"}, |
| 5141 |
} |
| 5142 |
msg.Header.Add("X-Nats-Test-Data", data) |
| 5143 |
js.PublishMsg(msg) |
| 5144 |
js.Publish("foo.B", []byte(fmt.Sprintf("B:%d", i))) |
| 5145 |
js.Publish("foo.C", []byte(fmt.Sprintf("C:%d", i))) |
| 5146 |
} |
| 5147 |
|
| 5148 |
var originalSeq uint64 |
| 5149 |
t.Run("get message", func(t *testing.T) { |
| 5150 |
expected := 5 |
| 5151 |
msgs := make([]*nats.Msg, 0) |
| 5152 |
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 5153 |
defer cancel() |
| 5154 |
|
| 5155 |
sub, err := js.Subscribe("foo.C", func(msg *nats.Msg) { |
| 5156 |
msgs = append(msgs, msg) |
| 5157 |
if len(msgs) == expected { |
| 5158 |
cancel() |
| 5159 |
} |
| 5160 |
}) |
| 5161 |
if err != nil { |
| 5162 |
t.Fatal(err) |
| 5163 |
} |
| 5164 |
<-ctx.Done() |
| 5165 |
sub.Unsubscribe() |
| 5166 |
|
| 5167 |
got := len(msgs) |
| 5168 |
if got != expected { |
| 5169 |
t.Fatalf("Expected: %d, got: %d", expected, got) |
| 5170 |
} |
| 5171 |
|
| 5172 |
msg := msgs[3] |
| 5173 |
meta, err := msg.Metadata() |
| 5174 |
if err != nil { |
| 5175 |
t.Fatal(err) |
| 5176 |
} |
| 5177 |
originalSeq = meta.Sequence.Stream |
| 5178 |
|
| 5179 |
// Get the same message using JSM. |
| 5180 |
fetchedMsg, err := js.GetMsg("foo", originalSeq) |
| 5181 |
if err != nil { |
| 5182 |
t.Fatal(err) |
| 5183 |
} |
| 5184 |
|
| 5185 |
expectedData := "C:3" |
| 5186 |
if string(fetchedMsg.Data) != expectedData { |
| 5187 |
t.Errorf("Expected: %v, got: %v", expectedData, string(fetchedMsg.Data)) |
| 5188 |
} |
| 5189 |
}) |
| 5190 |
|
| 5191 |
t.Run("get deleted message", func(t *testing.T) { |
| 5192 |
err := js.DeleteMsg("foo", originalSeq) |
| 5193 |
if err != nil { |
| 5194 |
t.Fatal(err) |
| 5195 |
} |
| 5196 |
|
| 5197 |
si, err := js.StreamInfo("foo") |
| 5198 |
if err != nil { |
| 5199 |
t.Fatal(err) |
| 5200 |
} |
| 5201 |
expected := 14 |
| 5202 |
if int(si.State.Msgs) != expected { |
| 5203 |
t.Errorf("Expected %d msgs, got: %d", expected, si.State.Msgs) |
| 5204 |
} |
| 5205 |
|
| 5206 |
// There should be only 4 messages since one deleted. |
| 5207 |
expected = 4 |
| 5208 |
msgs := make([]*nats.Msg, 0) |
| 5209 |
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 5210 |
defer cancel() |
| 5211 |
|
| 5212 |
sub, err := js.Subscribe("foo.C", func(msg *nats.Msg) { |
| 5213 |
msgs = append(msgs, msg) |
| 5214 |
|
| 5215 |
if len(msgs) == expected { |
| 5216 |
cancel() |
| 5217 |
} |
| 5218 |
}) |
| 5219 |
if err != nil { |
| 5220 |
t.Fatal(err) |
| 5221 |
} |
| 5222 |
<-ctx.Done() |
| 5223 |
sub.Unsubscribe() |
| 5224 |
|
| 5225 |
msg := msgs[3] |
| 5226 |
meta, err := msg.Metadata() |
| 5227 |
if err != nil { |
| 5228 |
t.Fatal(err) |
| 5229 |
} |
| 5230 |
newSeq := meta.Sequence.Stream |
| 5231 |
|
| 5232 |
// First message removed |
| 5233 |
if newSeq <= originalSeq { |
| 5234 |
t.Errorf("Expected %d to be higher sequence than %d", |
| 5235 |
newSeq, originalSeq) |
| 5236 |
} |
| 5237 |
|
| 5238 |
// Try to fetch the same message which should be gone. |
| 5239 |
_, err = js.GetMsg("foo", originalSeq) |
| 5240 |
if err == nil || err != nats.ErrMsgNotFound { |
| 5241 |
t.Errorf("Expected no message found error, got: %v", err) |
| 5242 |
} |
| 5243 |
}) |
| 5244 |
|
| 5245 |
t.Run("get message with headers", func(t *testing.T) { |
| 5246 |
streamMsg, err := js.GetMsg("foo", 4) |
| 5247 |
if err != nil { |
| 5248 |
t.Fatal(err) |
| 5249 |
} |
| 5250 |
if streamMsg.Sequence != 4 { |
| 5251 |
t.Errorf("Expected %v, got: %v", 4, streamMsg.Sequence) |
| 5252 |
} |
| 5253 |
expectedMap := map[string][]string{ |
| 5254 |
"X-Nats-Test-Data": {"A:1"}, |
| 5255 |
"X-NATS-Key": {"123"}, |
| 5256 |
} |
| 5257 |
if !reflect.DeepEqual(streamMsg.Header, nats.Header(expectedMap)) { |
| 5258 |
t.Errorf("Expected %v, got: %v", expectedMap, streamMsg.Header) |
| 5259 |
} |
| 5260 |
|
| 5261 |
sub, err := js.SubscribeSync("foo.A", nats.StartSequence(4)) |
| 5262 |
if err != nil { |
| 5263 |
t.Fatal(err) |
| 5264 |
} |
| 5265 |
msg, err := sub.NextMsg(2 * time.Second) |
| 5266 |
if err != nil { |
| 5267 |
t.Fatal(err) |
| 5268 |
} |
| 5269 |
if !reflect.DeepEqual(msg.Header, nats.Header(expectedMap)) { |
| 5270 |
t.Errorf("Expected %v, got: %v", expectedMap, msg.Header) |
| 5271 |
} |
| 5272 |
}) |
| 5273 |
} |
| 5274 |
|
| 5275 |
func TestJetStreamManagement_GetMsg(t *testing.T) { |
| 5276 |
t.Run("1-node", func(t *testing.T) { |
| 5277 |
withJSServer(t, testJetStreamManagement_GetMsgBody) |
| 5278 |
}) |
| 5279 |
t.Run("3-node", func(t *testing.T) { |
| 5280 |
c := newTester(t) |
| 5281 |
inst := c.CreateCluster(t, 3, true) |
| 5282 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 5283 |
nc := dialInstance(t, inst) |
| 5284 |
c.WaitForJetStream(t, nc) |
| 5285 |
waitForJSCluster(t, nc) |
| 5286 |
testJetStreamManagement_GetMsgBody(t, nc) |
| 5287 |
}) |
| 5288 |
} |
| 5289 |
|
| 5290 |
func TestJetStreamManagement_DeleteMsg(t *testing.T) { |
| 5291 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 5292 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 5293 |
if err != nil { |
| 5294 |
t.Fatalf("Unexpected error: %v", err) |
| 5295 |
} |
| 5296 |
|
| 5297 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 5298 |
Name: "foo", |
| 5299 |
Subjects: []string{"foo.A", "foo.B", "foo.C"}, |
| 5300 |
}) |
| 5301 |
if err != nil { |
| 5302 |
t.Fatalf("Unexpected error: %v", err) |
| 5303 |
} |
| 5304 |
|
| 5305 |
for range 5 { |
| 5306 |
js.Publish("foo.A", []byte("A")) |
| 5307 |
js.Publish("foo.B", []byte("B")) |
| 5308 |
js.Publish("foo.C", []byte("C")) |
| 5309 |
} |
| 5310 |
|
| 5311 |
si, err := js.StreamInfo("foo") |
| 5312 |
if err != nil { |
| 5313 |
t.Fatal(err) |
| 5314 |
} |
| 5315 |
var total uint64 = 15 |
| 5316 |
if si.State.Msgs != total { |
| 5317 |
t.Errorf("Expected %d msgs, got: %d", total, si.State.Msgs) |
| 5318 |
} |
| 5319 |
|
| 5320 |
expected := 5 |
| 5321 |
msgs := make([]*nats.Msg, 0) |
| 5322 |
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 5323 |
defer cancel() |
| 5324 |
|
| 5325 |
sub, err := js.Subscribe("foo.C", func(msg *nats.Msg) { |
| 5326 |
msgs = append(msgs, msg) |
| 5327 |
if len(msgs) == expected { |
| 5328 |
cancel() |
| 5329 |
} |
| 5330 |
}) |
| 5331 |
if err != nil { |
| 5332 |
t.Fatal(err) |
| 5333 |
} |
| 5334 |
<-ctx.Done() |
| 5335 |
sub.Unsubscribe() |
| 5336 |
|
| 5337 |
got := len(msgs) |
| 5338 |
if got != expected { |
| 5339 |
t.Fatalf("Expected %d, got %d", expected, got) |
| 5340 |
} |
| 5341 |
|
| 5342 |
msg := msgs[0] |
| 5343 |
meta, err := msg.Metadata() |
| 5344 |
if err != nil { |
| 5345 |
t.Fatal(err) |
| 5346 |
} |
| 5347 |
originalSeq := meta.Sequence.Stream |
| 5348 |
|
| 5349 |
// create a subscription on delete message API subject to verify the content of delete operation |
| 5350 |
apiSub, err := nc.SubscribeSync("$JS.API.STREAM.MSG.DELETE.foo") |
| 5351 |
if err != nil { |
| 5352 |
t.Fatalf("Error on subscribe: %v", err) |
| 5353 |
} |
| 5354 |
err = js.DeleteMsg("foo", originalSeq) |
| 5355 |
if err != nil { |
| 5356 |
t.Fatal(err) |
| 5357 |
} |
| 5358 |
msg, err = apiSub.NextMsg(1 * time.Second) |
| 5359 |
if err != nil { |
| 5360 |
t.Fatalf("Unexpected error: %v", err) |
| 5361 |
} |
| 5362 |
if str := string(msg.Data); !strings.Contains(str, "no_erase\":true") { |
| 5363 |
t.Fatalf("Request should not have no_erase field set: %s", str) |
| 5364 |
} |
| 5365 |
|
| 5366 |
si, err = js.StreamInfo("foo") |
| 5367 |
if err != nil { |
| 5368 |
t.Fatal(err) |
| 5369 |
} |
| 5370 |
total = 14 |
| 5371 |
if si.State.Msgs != total { |
| 5372 |
t.Errorf("Expected %d msgs, got: %d", total, si.State.Msgs) |
| 5373 |
} |
| 5374 |
|
| 5375 |
// There should be only 4 messages since one deleted. |
| 5376 |
expected = 4 |
| 5377 |
msgs = make([]*nats.Msg, 0) |
| 5378 |
ctx, cancel = context.WithTimeout(context.Background(), 2*time.Second) |
| 5379 |
defer cancel() |
| 5380 |
|
| 5381 |
sub, err = js.Subscribe("foo.C", func(msg *nats.Msg) { |
| 5382 |
msgs = append(msgs, msg) |
| 5383 |
|
| 5384 |
if len(msgs) == expected { |
| 5385 |
cancel() |
| 5386 |
} |
| 5387 |
}) |
| 5388 |
if err != nil { |
| 5389 |
t.Fatal(err) |
| 5390 |
} |
| 5391 |
<-ctx.Done() |
| 5392 |
sub.Unsubscribe() |
| 5393 |
|
| 5394 |
msg = msgs[0] |
| 5395 |
meta, err = msg.Metadata() |
| 5396 |
if err != nil { |
| 5397 |
t.Fatal(err) |
| 5398 |
} |
| 5399 |
newSeq := meta.Sequence.Stream |
| 5400 |
|
| 5401 |
// First message removed |
| 5402 |
if newSeq <= originalSeq { |
| 5403 |
t.Errorf("Expected %d to be higher sequence than %d", newSeq, originalSeq) |
| 5404 |
} |
| 5405 |
}) |
| 5406 |
} |
| 5407 |
|
| 5408 |
func TestJetStreamManagement_SecureDeleteMsg(t *testing.T) { |
| 5409 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 5410 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 5411 |
if err != nil { |
| 5412 |
t.Fatalf("Unexpected error: %v", err) |
| 5413 |
} |
| 5414 |
|
| 5415 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 5416 |
Name: "foo", |
| 5417 |
Subjects: []string{"foo.A", "foo.B", "foo.C"}, |
| 5418 |
}) |
| 5419 |
if err != nil { |
| 5420 |
t.Fatalf("Unexpected error: %v", err) |
| 5421 |
} |
| 5422 |
|
| 5423 |
for range 5 { |
| 5424 |
js.Publish("foo.A", []byte("A")) |
| 5425 |
js.Publish("foo.B", []byte("B")) |
| 5426 |
js.Publish("foo.C", []byte("C")) |
| 5427 |
} |
| 5428 |
|
| 5429 |
si, err := js.StreamInfo("foo") |
| 5430 |
if err != nil { |
| 5431 |
t.Fatal(err) |
| 5432 |
} |
| 5433 |
var total uint64 = 15 |
| 5434 |
if si.State.Msgs != total { |
| 5435 |
t.Errorf("Expected %d msgs, got: %d", total, si.State.Msgs) |
| 5436 |
} |
| 5437 |
|
| 5438 |
expected := 5 |
| 5439 |
msgs := make([]*nats.Msg, 0) |
| 5440 |
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 5441 |
defer cancel() |
| 5442 |
|
| 5443 |
sub, err := js.Subscribe("foo.C", func(msg *nats.Msg) { |
| 5444 |
msgs = append(msgs, msg) |
| 5445 |
if len(msgs) == expected { |
| 5446 |
cancel() |
| 5447 |
} |
| 5448 |
}) |
| 5449 |
if err != nil { |
| 5450 |
t.Fatal(err) |
| 5451 |
} |
| 5452 |
<-ctx.Done() |
| 5453 |
sub.Unsubscribe() |
| 5454 |
|
| 5455 |
got := len(msgs) |
| 5456 |
if got != expected { |
| 5457 |
t.Fatalf("Expected %d, got %d", expected, got) |
| 5458 |
} |
| 5459 |
|
| 5460 |
msg := msgs[0] |
| 5461 |
meta, err := msg.Metadata() |
| 5462 |
if err != nil { |
| 5463 |
t.Fatal(err) |
| 5464 |
} |
| 5465 |
originalSeq := meta.Sequence.Stream |
| 5466 |
|
| 5467 |
// create a subscription on delete message API subject to verify the content of delete operation |
| 5468 |
apiSub, err := nc.SubscribeSync("$JS.API.STREAM.MSG.DELETE.foo") |
| 5469 |
if err != nil { |
| 5470 |
t.Fatalf("Error on subscribe: %v", err) |
| 5471 |
} |
| 5472 |
err = js.SecureDeleteMsg("foo", originalSeq) |
| 5473 |
if err != nil { |
| 5474 |
t.Fatal(err) |
| 5475 |
} |
| 5476 |
msg, err = apiSub.NextMsg(1 * time.Second) |
| 5477 |
if err != nil { |
| 5478 |
t.Fatalf("Unexpected error: %v", err) |
| 5479 |
} |
| 5480 |
if str := string(msg.Data); strings.Contains(str, "no_erase\":true") { |
| 5481 |
t.Fatalf("Request should not have no_erase field set: %s", str) |
| 5482 |
} |
| 5483 |
|
| 5484 |
si, err = js.StreamInfo("foo") |
| 5485 |
if err != nil { |
| 5486 |
t.Fatal(err) |
| 5487 |
} |
| 5488 |
total = 14 |
| 5489 |
if si.State.Msgs != total { |
| 5490 |
t.Errorf("Expected %d msgs, got: %d", total, si.State.Msgs) |
| 5491 |
} |
| 5492 |
|
| 5493 |
// There should be only 4 messages since one deleted. |
| 5494 |
expected = 4 |
| 5495 |
msgs = make([]*nats.Msg, 0) |
| 5496 |
ctx, cancel = context.WithTimeout(context.Background(), 2*time.Second) |
| 5497 |
defer cancel() |
| 5498 |
|
| 5499 |
sub, err = js.Subscribe("foo.C", func(msg *nats.Msg) { |
| 5500 |
msgs = append(msgs, msg) |
| 5501 |
|
| 5502 |
if len(msgs) == expected { |
| 5503 |
cancel() |
| 5504 |
} |
| 5505 |
}) |
| 5506 |
if err != nil { |
| 5507 |
t.Fatal(err) |
| 5508 |
} |
| 5509 |
<-ctx.Done() |
| 5510 |
sub.Unsubscribe() |
| 5511 |
|
| 5512 |
msg = msgs[0] |
| 5513 |
meta, err = msg.Metadata() |
| 5514 |
if err != nil { |
| 5515 |
t.Fatal(err) |
| 5516 |
} |
| 5517 |
newSeq := meta.Sequence.Stream |
| 5518 |
|
| 5519 |
// First message removed |
| 5520 |
if newSeq <= originalSeq { |
| 5521 |
t.Errorf("Expected %d to be higher sequence than %d", newSeq, originalSeq) |
| 5522 |
} |
| 5523 |
}) |
| 5524 |
} |
| 5525 |
|
| 5526 |
func TestJetStreamImport(t *testing.T) { |
| 5527 |
c := newTester(t) |
| 5528 |
accounts := `accounts: { |
| 5529 |
JS: { |
| 5530 |
jetstream: enabled |
| 5531 |
users: [ {user: dlc, password: foo} ] |
| 5532 |
exports [ { service: "$JS.API.>" }, { service: "foo" }] |
| 5533 |
}, |
| 5534 |
U: { |
| 5535 |
users: [ {user: rip, password: bar} ] |
| 5536 |
imports [ |
| 5537 |
{ service: { subject: "$JS.API.>", account: JS } , to: "dlc.>" } |
| 5538 |
{ service: { subject: "foo", account: JS } } |
| 5539 |
] |
| 5540 |
}, |
| 5541 |
}` |
| 5542 |
inst := c.CreateServer(t, true, |
| 5543 |
testservice.WithAccounts(accounts), |
| 5544 |
testservice.WithAuthorization("no_auth_user: rip"), |
| 5545 |
testservice.WithSystemAccount(noSystemAccountBody()), |
| 5546 |
) |
| 5547 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 5548 |
|
| 5549 |
// Create a stream using JSM. |
| 5550 |
ncm := dialInstance(t, inst, nats.UserInfo("dlc", "foo")) |
| 5551 |
c.WaitForJetStream(t, ncm) |
| 5552 |
defer ncm.Close() |
| 5553 |
jsm, err := ncm.JetStream(nats.MaxWait(10 * time.Second)) |
| 5554 |
if err != nil { |
| 5555 |
t.Fatalf("Unexpected error: %v", err) |
| 5556 |
} |
| 5557 |
|
| 5558 |
_, err = jsm.AddStream(&nats.StreamConfig{ |
| 5559 |
Name: "TEST", |
| 5560 |
Subjects: []string{"foo", "bar"}, |
| 5561 |
}) |
| 5562 |
if err != nil { |
| 5563 |
t.Fatalf("stream create failed: %v", err) |
| 5564 |
} |
| 5565 |
|
| 5566 |
// Client with the imports. |
| 5567 |
nc := dialInstance(t, inst, nats.UserInfo("rip", "bar")) |
| 5568 |
defer nc.Close() |
| 5569 |
|
| 5570 |
// Since we import with a prefix from above we can use that when creating our JS context. |
| 5571 |
js, err := nc.JetStream(nats.APIPrefix("dlc")) |
| 5572 |
if err != nil { |
| 5573 |
t.Fatalf("Unexpected error: %v", err) |
| 5574 |
} |
| 5575 |
|
| 5576 |
msg := []byte("Hello JS Import!") |
| 5577 |
|
| 5578 |
if _, err = js.Publish("foo", msg); err != nil { |
| 5579 |
t.Fatalf("Unexpected publish error: %v", err) |
| 5580 |
} |
| 5581 |
} |
| 5582 |
|
| 5583 |
func TestJetStreamImportDirectOnly(t *testing.T) { |
| 5584 |
c := newTester(t) |
| 5585 |
accounts := `accounts: { |
| 5586 |
JS: { |
| 5587 |
jetstream: enabled |
| 5588 |
users: [ {user: dlc, password: foo} ] |
| 5589 |
exports [ |
| 5590 |
# For now have to expose the API to enable JS context across account. |
| 5591 |
{ service: "$JS.API.INFO" } |
| 5592 |
# For the stream publish. |
| 5593 |
{ service: "ORDERS" } |
| 5594 |
# For the pull based consumer. Response type needed for batchsize > 1 |
| 5595 |
{ service: "$JS.API.CONSUMER.INFO.ORDERS.d1", response: stream } |
| 5596 |
{ service: "$JS.API.CONSUMER.MSG.NEXT.ORDERS.d1", response: stream } |
| 5597 |
# For the push based consumer delivery and ack. |
| 5598 |
{ stream: "p.d" } |
| 5599 |
{ stream: "p.d3" } |
| 5600 |
# For the acks. Service in case we want an ack to our ack. |
| 5601 |
{ service: "$JS.ACK.ORDERS.*.>" } |
| 5602 |
|
| 5603 |
# Allow lookup of stream to be able to bind from another account. |
| 5604 |
{ service: "$JS.API.CONSUMER.INFO.ORDERS.d4", response: stream } |
| 5605 |
{ stream: "p.d4" } |
| 5606 |
] |
| 5607 |
}, |
| 5608 |
U: { |
| 5609 |
users: [ { user: rip, password: bar } ] |
| 5610 |
imports [ |
| 5611 |
{ service: { subject: "$JS.API.INFO", account: JS } } |
| 5612 |
{ service: { subject: "ORDERS", account: JS } , to: "orders" } |
| 5613 |
# { service: { subject: "$JS.API.CONSUMER.INFO.ORDERS.d1", account: JS } } |
| 5614 |
{ service: { subject: "$JS.API.CONSUMER.INFO.ORDERS.d4", account: JS } } |
| 5615 |
{ service: { subject: "$JS.API.CONSUMER.MSG.NEXT.ORDERS.d1", account: JS } } |
| 5616 |
{ stream: { subject: "p.d", account: JS } } |
| 5617 |
{ stream: { subject: "p.d3", account: JS } } |
| 5618 |
{ stream: { subject: "p.d4", account: JS } } |
| 5619 |
{ service: { subject: "$JS.ACK.ORDERS.*.>", account: JS } } |
| 5620 |
] |
| 5621 |
}, |
| 5622 |
V: { |
| 5623 |
users: [ { |
| 5624 |
user: v, |
| 5625 |
password: quux, |
| 5626 |
permissions: { publish: {deny: ["$JS.API.CONSUMER.INFO.ORDERS.d1"]} } |
| 5627 |
} ] |
| 5628 |
imports [ |
| 5629 |
{ service: { subject: "$JS.API.INFO", account: JS } } |
| 5630 |
{ service: { subject: "ORDERS", account: JS } , to: "orders" } |
| 5631 |
{ service: { subject: "$JS.API.CONSUMER.INFO.ORDERS.d1", account: JS } } |
| 5632 |
{ service: { subject: "$JS.API.CONSUMER.INFO.ORDERS.d4", account: JS } } |
| 5633 |
{ service: { subject: "$JS.API.CONSUMER.MSG.NEXT.ORDERS.d1", account: JS } } |
| 5634 |
{ stream: { subject: "p.d", account: JS } } |
| 5635 |
{ stream: { subject: "p.d3", account: JS } } |
| 5636 |
{ stream: { subject: "p.d4", account: JS } } |
| 5637 |
{ service: { subject: "$JS.ACK.ORDERS.*.>", account: JS } } |
| 5638 |
] |
| 5639 |
}, |
| 5640 |
}` |
| 5641 |
inst := c.CreateServer(t, true, |
| 5642 |
testservice.WithAccounts(accounts), |
| 5643 |
testservice.WithAuthorization("no_auth_user: rip"), |
| 5644 |
testservice.WithSystemAccount(noSystemAccountBody()), |
| 5645 |
) |
| 5646 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 5647 |
|
| 5648 |
// Create a stream using JSM. |
| 5649 |
ncm := dialInstance(t, inst, nats.UserInfo("dlc", "foo")) |
| 5650 |
c.WaitForJetStream(t, ncm) |
| 5651 |
defer ncm.Close() |
| 5652 |
jsm, err := ncm.JetStream(nats.MaxWait(10 * time.Second)) |
| 5653 |
if err != nil { |
| 5654 |
t.Fatalf("Unexpected error: %v", err) |
| 5655 |
} |
| 5656 |
|
| 5657 |
// Create a stream using the server directly. |
| 5658 |
_, err = jsm.AddStream(&nats.StreamConfig{Name: "ORDERS"}) |
| 5659 |
if err != nil { |
| 5660 |
t.Fatalf("stream create failed: %v", err) |
| 5661 |
} |
| 5662 |
|
| 5663 |
// Create a pull based consumer. |
| 5664 |
_, err = jsm.AddConsumer("ORDERS", &nats.ConsumerConfig{Durable: "d1", AckPolicy: nats.AckExplicitPolicy}) |
| 5665 |
if err != nil { |
| 5666 |
t.Fatalf("pull consumer create failed: %v", err) |
| 5667 |
} |
| 5668 |
|
| 5669 |
// Create a push based consumers. |
| 5670 |
_, err = jsm.AddConsumer("ORDERS", &nats.ConsumerConfig{ |
| 5671 |
Durable: "d2", |
| 5672 |
AckPolicy: nats.AckExplicitPolicy, |
| 5673 |
DeliverSubject: "p.d", |
| 5674 |
}) |
| 5675 |
if err != nil { |
| 5676 |
t.Fatalf("push consumer create failed: %v", err) |
| 5677 |
} |
| 5678 |
|
| 5679 |
_, err = jsm.AddConsumer("ORDERS", &nats.ConsumerConfig{ |
| 5680 |
Durable: "d3", |
| 5681 |
AckPolicy: nats.AckExplicitPolicy, |
| 5682 |
DeliverSubject: "p.d3", |
| 5683 |
}) |
| 5684 |
if err != nil { |
| 5685 |
t.Fatalf("push consumer create failed: %v", err) |
| 5686 |
} |
| 5687 |
|
| 5688 |
_, err = jsm.AddConsumer("ORDERS", &nats.ConsumerConfig{ |
| 5689 |
Durable: "d4", |
| 5690 |
AckPolicy: nats.AckExplicitPolicy, |
| 5691 |
DeliverSubject: "p.d4", |
| 5692 |
}) |
| 5693 |
if err != nil { |
| 5694 |
t.Fatalf("push consumer create failed: %v", err) |
| 5695 |
} |
| 5696 |
|
| 5697 |
nc := dialInstance(t, inst, nats.UserInfo("rip", "bar")) |
| 5698 |
defer nc.Close() |
| 5699 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 5700 |
if err != nil { |
| 5701 |
t.Fatalf("Unexpected error: %v", err) |
| 5702 |
} |
| 5703 |
|
| 5704 |
// Now make sure we can send to the stream from another account. |
| 5705 |
toSend := 100 |
| 5706 |
for i := range toSend { |
| 5707 |
if _, err := js.Publish("orders", []byte(fmt.Sprintf("ORDER-%d", i+1))); err != nil { |
| 5708 |
t.Fatalf("Unexpected error publishing message %d: %v", i+1, err) |
| 5709 |
} |
| 5710 |
} |
| 5711 |
|
| 5712 |
var sub *nats.Subscription |
| 5713 |
|
| 5714 |
waitForPending := func(t *testing.T, n int) { |
| 5715 |
t.Helper() |
| 5716 |
timeout := time.Now().Add(2 * time.Second) |
| 5717 |
for time.Now().Before(timeout) { |
| 5718 |
if msgs, _, _ := sub.Pending(); msgs == n { |
| 5719 |
return |
| 5720 |
} |
| 5721 |
time.Sleep(10 * time.Millisecond) |
| 5722 |
} |
| 5723 |
msgs, _, _ := sub.Pending() |
| 5724 |
t.Fatalf("Expected to receive %d messages, but got %d", n, msgs) |
| 5725 |
} |
| 5726 |
|
| 5727 |
// Do push based consumer using a regular NATS subscription on the import subject. |
| 5728 |
sub, err = nc.SubscribeSync("p.d3") |
| 5729 |
if err != nil { |
| 5730 |
t.Fatalf("Unexpected error: %v", err) |
| 5731 |
} |
| 5732 |
waitForPending(t, toSend) |
| 5733 |
|
| 5734 |
// Can also ack from the regular NATS subscription via the imported subject. |
| 5735 |
for range toSend { |
| 5736 |
m, err := sub.NextMsg(100 * time.Millisecond) |
| 5737 |
if err != nil { |
| 5738 |
t.Fatalf("Unexpected error: %v", err) |
| 5739 |
} |
| 5740 |
|
| 5741 |
// Test that can expect an ack of the ack. |
| 5742 |
err = m.AckSync() |
| 5743 |
if err != nil { |
| 5744 |
t.Errorf("Unexpected error: %v", err) |
| 5745 |
} |
| 5746 |
} |
| 5747 |
|
| 5748 |
// Can attach to the consumer from another JS account if there is a durable name. |
| 5749 |
sub, err = js.SubscribeSync("ORDERS", nats.Durable("d4"), nats.BindStream("ORDERS")) |
| 5750 |
if err != nil { |
| 5751 |
t.Fatalf("Unexpected error: %v", err) |
| 5752 |
} |
| 5753 |
waitForPending(t, toSend) |
| 5754 |
|
| 5755 |
// Even if there are no permissions or import to check that a consumer exists, |
| 5756 |
// it is still possible to bind subscription to it. |
| 5757 |
sub, err = js.PullSubscribe("ORDERS", "d1", nats.Bind("ORDERS", "d1")) |
| 5758 |
if err != nil { |
| 5759 |
t.Fatal(err) |
| 5760 |
} |
| 5761 |
expected := 10 |
| 5762 |
msgs, err := sub.Fetch(expected) |
| 5763 |
if err != nil { |
| 5764 |
t.Fatal(err) |
| 5765 |
} |
| 5766 |
got := len(msgs) |
| 5767 |
if got != expected { |
| 5768 |
t.Fatalf("Expected %d, got %d", expected, got) |
| 5769 |
} |
| 5770 |
|
| 5771 |
// Account without permissions to lookup should be able to bind as well. |
| 5772 |
eh := func(_ *nats.Conn, _ *nats.Subscription, err error) {} |
| 5773 |
ncV := dialInstance(t, inst, nats.UserInfo("v", "quux"), nats.ErrorHandler(eh)) |
| 5774 |
defer ncV.Close() |
| 5775 |
|
| 5776 |
// Since we know that the lookup will fail, we use a smaller timeout than the 5s default. |
| 5777 |
jsV, err := ncV.JetStream(nats.MaxWait(500 * time.Millisecond)) |
| 5778 |
if err != nil { |
| 5779 |
t.Fatal(err) |
| 5780 |
} |
| 5781 |
sub, err = jsV.PullSubscribe("ORDERS", "d1", nats.Bind("ORDERS", "d1")) |
| 5782 |
if err != nil { |
| 5783 |
t.Fatal(err) |
| 5784 |
} |
| 5785 |
expected = 10 |
| 5786 |
msgs, err = sub.Fetch(expected) |
| 5787 |
if err != nil { |
| 5788 |
t.Fatal(err) |
| 5789 |
} |
| 5790 |
got = len(msgs) |
| 5791 |
if got != expected { |
| 5792 |
t.Fatalf("Expected %d, got %d", expected, got) |
| 5793 |
} |
| 5794 |
} |
| 5795 |
|
| 5796 |
func TestJetStreamCrossAccountMirrorsAndSources(t *testing.T) { |
| 5797 |
c := newTester(t) |
| 5798 |
accounts := `accounts { |
| 5799 |
JS { |
| 5800 |
jetstream: enabled |
| 5801 |
users = [ { user: "rip", pass: "pass" } ] |
| 5802 |
exports [ |
| 5803 |
{ service: "$JS.API.CONSUMER.>" } # To create internal consumers to mirror/source. |
| 5804 |
{ stream: "RI.DELIVER.SYNC.>" } # For the mirror/source consumers sending to IA via delivery subject. |
| 5805 |
] |
| 5806 |
} |
| 5807 |
IA { |
| 5808 |
jetstream: enabled |
| 5809 |
users = [ { user: "dlc", pass: "pass" } ] |
| 5810 |
imports [ |
| 5811 |
{ service: { account: JS, subject: "$JS.API.CONSUMER.>"}, to: "RI.JS.API.CONSUMER.>" } |
| 5812 |
{ stream: { account: JS, subject: "RI.DELIVER.SYNC.>"} } |
| 5813 |
] |
| 5814 |
} |
| 5815 |
$SYS { users = [ { user: "admin", pass: "s3cr3t!" } ] } |
| 5816 |
}` |
| 5817 |
inst := c.CreateServer(t, true, |
| 5818 |
testservice.WithAccounts(accounts), |
| 5819 |
testservice.WithAuthorization("no_auth_user: rip"), |
| 5820 |
testservice.WithSystemAccount(`system_account: "$SYS"`), |
| 5821 |
) |
| 5822 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 5823 |
|
| 5824 |
nc1 := dialInstance(t, inst, nats.UserInfo("rip", "pass")) |
| 5825 |
c.WaitForJetStream(t, nc1) |
| 5826 |
defer nc1.Close() |
| 5827 |
js1, err := nc1.JetStream(nats.MaxWait(10 * time.Second)) |
| 5828 |
if err != nil { |
| 5829 |
t.Fatalf("Unexpected error: %v", err) |
| 5830 |
} |
| 5831 |
|
| 5832 |
_, err = js1.AddStream(&nats.StreamConfig{ |
| 5833 |
Name: "TEST", |
| 5834 |
Replicas: 1, |
| 5835 |
}) |
| 5836 |
if err != nil { |
| 5837 |
t.Fatal(err) |
| 5838 |
} |
| 5839 |
|
| 5840 |
const ( |
| 5841 |
toSend = 100 |
| 5842 |
publishSubj = "TEST" |
| 5843 |
sourceName = "MY_SOURCE_TEST" |
| 5844 |
mirrorName = "MY_MIRROR_TEST" |
| 5845 |
) |
| 5846 |
for i := range toSend { |
| 5847 |
data := []byte(fmt.Sprintf("OK %d", i)) |
| 5848 |
if _, err := js1.Publish(publishSubj, data); err != nil { |
| 5849 |
t.Fatalf("Unexpected publish error: %v", err) |
| 5850 |
} |
| 5851 |
} |
| 5852 |
|
| 5853 |
nc2 := dialInstance(t, inst, nats.UserInfo("dlc", "pass")) |
| 5854 |
defer nc2.Close() |
| 5855 |
js2, err := nc2.JetStream(nats.MaxWait(10 * time.Second)) |
| 5856 |
if err != nil { |
| 5857 |
t.Fatalf("Unexpected error: %v", err) |
| 5858 |
} |
| 5859 |
|
| 5860 |
checkMsgCount := func(t *testing.T, stream string) { |
| 5861 |
t.Helper() |
| 5862 |
checkFor(t, 20*time.Second, 100*time.Millisecond, func() error { |
| 5863 |
si, err := js2.StreamInfo(stream) |
| 5864 |
if err != nil { |
| 5865 |
return err |
| 5866 |
} |
| 5867 |
if si.State.Msgs != uint64(toSend) { |
| 5868 |
return fmt.Errorf("Expected %d msgs, got state: %+v", toSend, si.State) |
| 5869 |
} |
| 5870 |
return nil |
| 5871 |
}) |
| 5872 |
} |
| 5873 |
checkConsume := func(t *testing.T, js nats.JetStream, subject, stream string, want int) { |
| 5874 |
t.Helper() |
| 5875 |
sub, err := js.SubscribeSync(subject, nats.BindStream(stream)) |
| 5876 |
if err != nil { |
| 5877 |
t.Fatal(err) |
| 5878 |
} |
| 5879 |
defer sub.Unsubscribe() |
| 5880 |
|
| 5881 |
checkFor(t, 4*time.Second, 20*time.Millisecond, func() error { |
| 5882 |
if nmsgs, _, err := sub.Pending(); err != nil || nmsgs != want { |
| 5883 |
return fmt.Errorf("Did not receive correct number of messages: %d vs %d", nmsgs, want) |
| 5884 |
} |
| 5885 |
return nil |
| 5886 |
}) |
| 5887 |
|
| 5888 |
for range want { |
| 5889 |
msg, err := sub.NextMsg(time.Second) |
| 5890 |
if err != nil { |
| 5891 |
t.Fatal(err) |
| 5892 |
} |
| 5893 |
meta, err := msg.Metadata() |
| 5894 |
if err != nil { |
| 5895 |
t.Fatal(err) |
| 5896 |
} |
| 5897 |
if got, want := meta.Stream, stream; got != want { |
| 5898 |
t.Fatalf("unexpected stream name, got=%q, want=%q", got, want) |
| 5899 |
} |
| 5900 |
} |
| 5901 |
} |
| 5902 |
|
| 5903 |
_, err = js2.AddStream(&nats.StreamConfig{ |
| 5904 |
Name: mirrorName, |
| 5905 |
Storage: nats.FileStorage, |
| 5906 |
Mirror: &nats.StreamSource{ |
| 5907 |
Name: publishSubj, |
| 5908 |
External: &nats.ExternalStream{ |
| 5909 |
APIPrefix: "RI.JS.API", |
| 5910 |
DeliverPrefix: "RI.DELIVER.SYNC.MIRRORS", |
| 5911 |
}, |
| 5912 |
}, |
| 5913 |
}) |
| 5914 |
if err != nil { |
| 5915 |
t.Fatal(err) |
| 5916 |
} |
| 5917 |
checkMsgCount(t, mirrorName) |
| 5918 |
checkConsume(t, js2, publishSubj, mirrorName, toSend) |
| 5919 |
|
| 5920 |
_, err = js2.AddStream(&nats.StreamConfig{ |
| 5921 |
Name: sourceName, |
| 5922 |
Storage: nats.FileStorage, |
| 5923 |
Sources: []*nats.StreamSource{ |
| 5924 |
{ |
| 5925 |
Name: publishSubj, |
| 5926 |
External: &nats.ExternalStream{ |
| 5927 |
APIPrefix: "RI.JS.API", |
| 5928 |
DeliverPrefix: "RI.DELIVER.SYNC.SOURCES", |
| 5929 |
}, |
| 5930 |
}, |
| 5931 |
}, |
| 5932 |
}) |
| 5933 |
if err != nil { |
| 5934 |
t.Fatal(err) |
| 5935 |
} |
| 5936 |
checkMsgCount(t, sourceName) |
| 5937 |
checkConsume(t, js2, publishSubj, sourceName, toSend) |
| 5938 |
} |
| 5939 |
|
| 5940 |
func TestJetStreamAutoMaxAckPending(t *testing.T) { |
| 5941 |
c := newTester(t) |
| 5942 |
inst := c.CreateServer(t, true) |
| 5943 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 5944 |
nc := dialInstance(t, inst, nats.SyncQueueLen(500)) |
| 5945 |
c.WaitForJetStream(t, nc) |
| 5946 |
defer nc.Close() |
| 5947 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 5948 |
if err != nil { |
| 5949 |
t.Fatalf("Unexpected error: %v", err) |
| 5950 |
} |
| 5951 |
|
| 5952 |
_, err = js.AddStream(&nats.StreamConfig{Name: "foo"}) |
| 5953 |
if err != nil { |
| 5954 |
t.Fatalf("Unexpected error: %v", err) |
| 5955 |
} |
| 5956 |
|
| 5957 |
toSend := 10_000 |
| 5958 |
|
| 5959 |
msg := []byte("Hello") |
| 5960 |
for range toSend { |
| 5961 |
// Use plain NATS here for speed. |
| 5962 |
nc.Publish("foo", msg) |
| 5963 |
} |
| 5964 |
nc.Flush() |
| 5965 |
|
| 5966 |
// Create a consumer. |
| 5967 |
sub, err := js.SubscribeSync("foo") |
| 5968 |
if err != nil { |
| 5969 |
t.Fatalf("Unexpected error: %v", err) |
| 5970 |
} |
| 5971 |
defer sub.Unsubscribe() |
| 5972 |
expectedMaxAck, _, _ := sub.PendingLimits() |
| 5973 |
|
| 5974 |
ci, err := sub.ConsumerInfo() |
| 5975 |
if err != nil { |
| 5976 |
t.Fatalf("Unexpected error: %v", err) |
| 5977 |
} |
| 5978 |
if ci.Config.MaxAckPending != expectedMaxAck { |
| 5979 |
t.Fatalf("Expected MaxAckPending to be set to %d, got %d", expectedMaxAck, ci.Config.MaxAckPending) |
| 5980 |
} |
| 5981 |
|
| 5982 |
waitForPending := func(n int) { |
| 5983 |
timeout := time.Now().Add(2 * time.Second) |
| 5984 |
for time.Now().Before(timeout) { |
| 5985 |
if msgs, _, _ := sub.Pending(); msgs == n { |
| 5986 |
return |
| 5987 |
} |
| 5988 |
time.Sleep(10 * time.Millisecond) |
| 5989 |
} |
| 5990 |
msgs, _, _ := sub.Pending() |
| 5991 |
t.Fatalf("Expected to receive %d messages, but got %d", n, msgs) |
| 5992 |
} |
| 5993 |
|
| 5994 |
waitForPending(expectedMaxAck) |
| 5995 |
// We do it twice to make sure it does not go over. |
| 5996 |
waitForPending(expectedMaxAck) |
| 5997 |
|
| 5998 |
// Now make sure we can consume them all with no slow consumers etc. |
| 5999 |
for i := range toSend { |
| 6000 |
m, err := sub.NextMsg(time.Second) |
| 6001 |
if err != nil { |
| 6002 |
t.Fatalf("Unexpected error receiving %d: %v", i+1, err) |
| 6003 |
} |
| 6004 |
m.Ack() |
| 6005 |
} |
| 6006 |
} |
| 6007 |
|
| 6008 |
func TestJetStreamInterfaces(t *testing.T) { |
| 6009 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 6010 |
var jsm nats.JetStreamManager |
| 6011 |
var jsctx nats.JetStreamContext |
| 6012 |
|
| 6013 |
// JetStream that can publish/subscribe but cannot manage streams. |
| 6014 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 6015 |
if err != nil { |
| 6016 |
t.Fatalf("Unexpected error: %v", err) |
| 6017 |
} |
| 6018 |
|
| 6019 |
js.Publish("foo", []byte("hello")) |
| 6020 |
|
| 6021 |
// JetStream context that can manage streams/consumers but cannot produce messages. |
| 6022 |
jsm, err = nc.JetStream() |
| 6023 |
if err != nil { |
| 6024 |
t.Fatalf("Unexpected error: %v", err) |
| 6025 |
} |
| 6026 |
jsm.AddStream(&nats.StreamConfig{Name: "FOO"}) |
| 6027 |
|
| 6028 |
// JetStream context that can both manage streams/consumers |
| 6029 |
// as well as publish/subscribe. |
| 6030 |
jsctx, err = nc.JetStream() |
| 6031 |
if err != nil { |
| 6032 |
t.Fatalf("Unexpected error: %v", err) |
| 6033 |
} |
| 6034 |
jsctx.AddStream(&nats.StreamConfig{Name: "BAR"}) |
| 6035 |
jsctx.Publish("bar", []byte("hello world")) |
| 6036 |
|
| 6037 |
publishMsg := func(js nats.JetStream, payload []byte) { |
| 6038 |
js.Publish("foo", payload) |
| 6039 |
} |
| 6040 |
publishMsg(js, []byte("hello world")) |
| 6041 |
}) |
| 6042 |
} |
| 6043 |
|
| 6044 |
func TestJetStreamSubscribe_DeliverPolicy(t *testing.T) { |
| 6045 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 6046 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 6047 |
if err != nil { |
| 6048 |
t.Fatalf("Unexpected error: %v", err) |
| 6049 |
} |
| 6050 |
|
| 6051 |
// Create the stream using our client API. |
| 6052 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 6053 |
Name: "TEST", |
| 6054 |
Subjects: []string{"foo", "bar"}, |
| 6055 |
}) |
| 6056 |
if err != nil { |
| 6057 |
t.Fatalf("Unexpected error: %v", err) |
| 6058 |
} |
| 6059 |
|
| 6060 |
var publishTime time.Time |
| 6061 |
|
| 6062 |
for i := range 10 { |
| 6063 |
payload := fmt.Sprintf("i:%d", i) |
| 6064 |
if i == 5 { |
| 6065 |
// Derive publishTime from the SERVER's own clock by reading |
| 6066 |
// the stream's LastTime (server-side timestamp of msg 4), |
| 6067 |
// then add 1ns. Using host's time.Now() here is unreliable |
| 6068 |
// when the testservice server runs in a docker container |
| 6069 |
// whose clock can drift slightly relative to the host. |
| 6070 |
si, err := js.StreamInfo("TEST") |
| 6071 |
if err != nil { |
| 6072 |
t.Fatalf("StreamInfo: %v", err) |
| 6073 |
} |
| 6074 |
publishTime = si.State.LastTime.Add(time.Nanosecond) |
| 6075 |
} |
| 6076 |
js.Publish("foo", []byte(payload)) |
| 6077 |
time.Sleep(15 * time.Millisecond) |
| 6078 |
} |
| 6079 |
|
| 6080 |
for _, test := range []struct { |
| 6081 |
name string |
| 6082 |
subopt nats.SubOpt |
| 6083 |
expected int |
| 6084 |
}{ |
| 6085 |
{ |
| 6086 |
"deliver.all", nats.DeliverAll(), 10, |
| 6087 |
}, |
| 6088 |
{ |
| 6089 |
"deliver.last", nats.DeliverLast(), 1, |
| 6090 |
}, |
| 6091 |
{ |
| 6092 |
"deliver.new", nats.DeliverNew(), 0, |
| 6093 |
}, |
| 6094 |
{ |
| 6095 |
"deliver.starttime", nats.StartTime(publishTime), 5, |
| 6096 |
}, |
| 6097 |
{ |
| 6098 |
"deliver.startseq", nats.StartSequence(6), 5, |
| 6099 |
}, |
| 6100 |
} { |
| 6101 |
test := test |
| 6102 |
t.Run(test.name, func(t *testing.T) { |
| 6103 |
timeout := 2 * time.Second |
| 6104 |
if test.expected == 0 { |
| 6105 |
timeout = 250 * time.Millisecond |
| 6106 |
} |
| 6107 |
ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 6108 |
defer cancel() |
| 6109 |
|
| 6110 |
got := 0 |
| 6111 |
sub, err := js.Subscribe("foo", func(m *nats.Msg) { |
| 6112 |
got++ |
| 6113 |
if got == test.expected { |
| 6114 |
cancel() |
| 6115 |
} |
| 6116 |
}, test.subopt) |
| 6117 |
|
| 6118 |
if err != nil { |
| 6119 |
t.Fatalf("Unexpected error: %v", err) |
| 6120 |
} |
| 6121 |
|
| 6122 |
<-ctx.Done() |
| 6123 |
sub.Drain() |
| 6124 |
|
| 6125 |
if got != test.expected { |
| 6126 |
t.Fatalf("Expected %d, got %d", test.expected, got) |
| 6127 |
} |
| 6128 |
}) |
| 6129 |
} |
| 6130 |
|
| 6131 |
js.Publish("bar", []byte("bar msg 1")) |
| 6132 |
js.Publish("bar", []byte("bar msg 2")) |
| 6133 |
|
| 6134 |
sub, err := js.SubscribeSync("bar", nats.BindStream("TEST"), nats.DeliverLastPerSubject()) |
| 6135 |
if err != nil { |
| 6136 |
t.Fatalf("Error on subscribe: %v", err) |
| 6137 |
} |
| 6138 |
msg, err := sub.NextMsg(time.Second) |
| 6139 |
if err != nil { |
| 6140 |
t.Fatalf("Error on next msg: %v", err) |
| 6141 |
} |
| 6142 |
if string(msg.Data) != "bar msg 2" { |
| 6143 |
t.Fatalf("Unexpected last message: %q", msg.Data) |
| 6144 |
} |
| 6145 |
}) |
| 6146 |
} |
| 6147 |
|
| 6148 |
func TestJetStreamSubscribe_AckPolicy(t *testing.T) { |
| 6149 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 6150 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 6151 |
if err != nil { |
| 6152 |
t.Fatalf("Unexpected error: %v", err) |
| 6153 |
} |
| 6154 |
|
| 6155 |
// Create the stream using our client API. |
| 6156 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 6157 |
Name: "TEST", |
| 6158 |
Subjects: []string{"foo", "bar"}, |
| 6159 |
}) |
| 6160 |
if err != nil { |
| 6161 |
t.Fatalf("Unexpected error: %v", err) |
| 6162 |
} |
| 6163 |
|
| 6164 |
for i := range 10 { |
| 6165 |
payload := fmt.Sprintf("i:%d", i) |
| 6166 |
js.Publish("foo", []byte(payload)) |
| 6167 |
} |
| 6168 |
|
| 6169 |
for _, test := range []struct { |
| 6170 |
name string |
| 6171 |
subopt nats.SubOpt |
| 6172 |
expected nats.AckPolicy |
| 6173 |
}{ |
| 6174 |
{ |
| 6175 |
"ack-none", nats.AckNone(), nats.AckNonePolicy, |
| 6176 |
}, |
| 6177 |
{ |
| 6178 |
"ack-all", nats.AckAll(), nats.AckAllPolicy, |
| 6179 |
}, |
| 6180 |
{ |
| 6181 |
"ack-explicit", nats.AckExplicit(), nats.AckExplicitPolicy, |
| 6182 |
}, |
| 6183 |
} { |
| 6184 |
test := test |
| 6185 |
t.Run(test.name, func(t *testing.T) { |
| 6186 |
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 6187 |
defer cancel() |
| 6188 |
|
| 6189 |
got := 0 |
| 6190 |
totalMsgs := 10 |
| 6191 |
sub, err := js.Subscribe("foo", func(m *nats.Msg) { |
| 6192 |
got++ |
| 6193 |
if got == totalMsgs { |
| 6194 |
cancel() |
| 6195 |
} |
| 6196 |
}, test.subopt, nats.Durable(test.name)) |
| 6197 |
|
| 6198 |
if err != nil { |
| 6199 |
t.Fatalf("Unexpected error: %v", err) |
| 6200 |
} |
| 6201 |
|
| 6202 |
<-ctx.Done() |
| 6203 |
if got != totalMsgs { |
| 6204 |
t.Fatalf("Expected %d, got %d", totalMsgs, got) |
| 6205 |
} |
| 6206 |
|
| 6207 |
// check if consumer is configured properly |
| 6208 |
ci, err := js.ConsumerInfo("TEST", test.name) |
| 6209 |
if err != nil { |
| 6210 |
t.Fatalf("Unexpected error: %v", err) |
| 6211 |
} |
| 6212 |
if ci.Config.AckPolicy != test.expected { |
| 6213 |
t.Fatalf("Expected %v, got %v", test.expected, ci.Config.AckPolicy) |
| 6214 |
} |
| 6215 |
|
| 6216 |
// drain the subscription. This will remove the consumer |
| 6217 |
sub.Drain() |
| 6218 |
}) |
| 6219 |
} |
| 6220 |
|
| 6221 |
checkAcks := func(t *testing.T, sub *nats.Subscription) { |
| 6222 |
// Normal Ack |
| 6223 |
msg, err := sub.NextMsg(2 * time.Second) |
| 6224 |
if err != nil { |
| 6225 |
t.Fatalf("Unexpected error: %v", err) |
| 6226 |
} |
| 6227 |
|
| 6228 |
meta, err := msg.Metadata() |
| 6229 |
if err != nil { |
| 6230 |
t.Errorf("Unexpected error: %v", err) |
| 6231 |
} |
| 6232 |
if meta.Sequence.Consumer != 1 || meta.Sequence.Stream != 1 || meta.NumDelivered != 1 { |
| 6233 |
t.Errorf("Unexpected metadata: %v", meta) |
| 6234 |
} |
| 6235 |
|
| 6236 |
got := string(msg.Data) |
| 6237 |
expected := "i:0" |
| 6238 |
if got != expected { |
| 6239 |
t.Errorf("Expected %v, got %v", expected, got) |
| 6240 |
} |
| 6241 |
err = msg.Ack() |
| 6242 |
if err != nil { |
| 6243 |
t.Errorf("Unexpected error: %v", err) |
| 6244 |
} |
| 6245 |
|
| 6246 |
// AckSync |
| 6247 |
msg, err = sub.NextMsg(2 * time.Second) |
| 6248 |
if err != nil { |
| 6249 |
t.Errorf("Unexpected error: %v", err) |
| 6250 |
} |
| 6251 |
got = string(msg.Data) |
| 6252 |
expected = "i:1" |
| 6253 |
if got != expected { |
| 6254 |
t.Errorf("Expected %v, got %v", expected, got) |
| 6255 |
} |
| 6256 |
|
| 6257 |
// Give an already canceled context. |
| 6258 |
ctx, cancel := context.WithCancel(context.Background()) |
| 6259 |
cancel() |
| 6260 |
|
| 6261 |
err = msg.AckSync(nats.Context(ctx)) |
| 6262 |
if err != context.Canceled { |
| 6263 |
t.Errorf("Unexpected error: %v", err) |
| 6264 |
} |
| 6265 |
|
| 6266 |
// Context that not yet canceled. |
| 6267 |
ctx, cancel = context.WithTimeout(context.Background(), 2*time.Second) |
| 6268 |
defer cancel() |
| 6269 |
|
| 6270 |
// Prevent double context and ack wait options. |
| 6271 |
err = msg.AckSync(nats.Context(ctx), nats.AckWait(1*time.Second)) |
| 6272 |
if err != nats.ErrContextAndTimeout { |
| 6273 |
t.Errorf("Unexpected error: %v", err) |
| 6274 |
} |
| 6275 |
|
| 6276 |
err = msg.AckSync(nats.Context(ctx)) |
| 6277 |
if err != nil { |
| 6278 |
t.Errorf("Unexpected error: %v", err) |
| 6279 |
} |
| 6280 |
|
| 6281 |
err = msg.AckSync(nats.AckWait(2 * time.Second)) |
| 6282 |
if err != nats.ErrMsgAlreadyAckd { |
| 6283 |
t.Errorf("Unexpected error: %v", err) |
| 6284 |
} |
| 6285 |
|
| 6286 |
// AckSync default |
| 6287 |
msg, err = sub.NextMsg(2 * time.Second) |
| 6288 |
if err != nil { |
| 6289 |
t.Errorf("Unexpected error: %v", err) |
| 6290 |
} |
| 6291 |
got = string(msg.Data) |
| 6292 |
expected = "i:2" |
| 6293 |
if got != expected { |
| 6294 |
t.Errorf("Expected %v, got %v", expected, got) |
| 6295 |
} |
| 6296 |
err = msg.AckSync() |
| 6297 |
if err != nil { |
| 6298 |
t.Errorf("Unexpected error: %v", err) |
| 6299 |
} |
| 6300 |
|
| 6301 |
// Nak |
| 6302 |
msg, err = sub.NextMsg(2 * time.Second) |
| 6303 |
if err != nil { |
| 6304 |
t.Errorf("Unexpected error: %v", err) |
| 6305 |
} |
| 6306 |
got = string(msg.Data) |
| 6307 |
expected = "i:3" |
| 6308 |
if got != expected { |
| 6309 |
t.Errorf("Expected %v, got %v", expected, got) |
| 6310 |
} |
| 6311 |
|
| 6312 |
// Prevent double context and ack wait options. |
| 6313 |
err = msg.Nak(nats.Context(ctx), nats.AckWait(1*time.Second)) |
| 6314 |
if err != nats.ErrContextAndTimeout { |
| 6315 |
t.Errorf("Unexpected error: %v", err) |
| 6316 |
} |
| 6317 |
|
| 6318 |
// Skip the message. |
| 6319 |
err = msg.Nak() |
| 6320 |
if err != nil { |
| 6321 |
t.Errorf("Unexpected error: %v", err) |
| 6322 |
} |
| 6323 |
|
| 6324 |
msg, err = sub.NextMsg(2 * time.Second) |
| 6325 |
if err != nil { |
| 6326 |
t.Errorf("Unexpected error: %v", err) |
| 6327 |
} |
| 6328 |
got = string(msg.Data) |
| 6329 |
expected = "i:4" |
| 6330 |
if got != expected { |
| 6331 |
t.Errorf("Expected %v, got %v", expected, got) |
| 6332 |
} |
| 6333 |
err = msg.Nak(nats.AckWait(2 * time.Second)) |
| 6334 |
if err != nil { |
| 6335 |
t.Errorf("Unexpected error: %v", err) |
| 6336 |
} |
| 6337 |
|
| 6338 |
msg, err = sub.NextMsg(2 * time.Second) |
| 6339 |
if err != nil { |
| 6340 |
t.Errorf("Unexpected error: %v", err) |
| 6341 |
} |
| 6342 |
got = string(msg.Data) |
| 6343 |
expected = "i:5" |
| 6344 |
if got != expected { |
| 6345 |
t.Errorf("Expected %v, got %v", expected, got) |
| 6346 |
} |
| 6347 |
|
| 6348 |
// Prevent double context and ack wait options. |
| 6349 |
err = msg.Term(nats.Context(ctx), nats.AckWait(1*time.Second)) |
| 6350 |
if err != nats.ErrContextAndTimeout { |
| 6351 |
t.Errorf("Unexpected error: %v", err) |
| 6352 |
} |
| 6353 |
|
| 6354 |
err = msg.Term() |
| 6355 |
if err != nil { |
| 6356 |
t.Errorf("Unexpected error: %v", err) |
| 6357 |
} |
| 6358 |
|
| 6359 |
ctx, done := context.WithTimeout(context.Background(), 2*time.Second) |
| 6360 |
defer done() |
| 6361 |
|
| 6362 |
// Convert context into nats option. |
| 6363 |
nctx := nats.Context(ctx) |
| 6364 |
msg, err = sub.NextMsgWithContext(nctx) |
| 6365 |
if err != nil { |
| 6366 |
t.Errorf("Unexpected error: %v", err) |
| 6367 |
} |
| 6368 |
got = string(msg.Data) |
| 6369 |
expected = "i:6" |
| 6370 |
if got != expected { |
| 6371 |
t.Errorf("Expected %v, got %v", expected, got) |
| 6372 |
} |
| 6373 |
err = msg.Term(nctx) |
| 6374 |
if err != nil { |
| 6375 |
t.Errorf("Unexpected error: %v", err) |
| 6376 |
} |
| 6377 |
|
| 6378 |
msg, err = sub.NextMsgWithContext(nctx) |
| 6379 |
if err != nil { |
| 6380 |
t.Errorf("Unexpected error: %v", err) |
| 6381 |
} |
| 6382 |
got = string(msg.Data) |
| 6383 |
expected = "i:7" |
| 6384 |
if got != expected { |
| 6385 |
t.Errorf("Expected %v, got %v", expected, got) |
| 6386 |
} |
| 6387 |
|
| 6388 |
// Prevent double context and ack wait options. |
| 6389 |
err = msg.InProgress(nats.Context(ctx), nats.AckWait(1*time.Second)) |
| 6390 |
if err != nats.ErrContextAndTimeout { |
| 6391 |
t.Errorf("Unexpected error: %v", err) |
| 6392 |
} |
| 6393 |
|
| 6394 |
err = msg.InProgress(nctx) |
| 6395 |
if err != nil { |
| 6396 |
t.Errorf("Unexpected error: %v", err) |
| 6397 |
} |
| 6398 |
err = msg.InProgress(nctx) |
| 6399 |
if err != nil { |
| 6400 |
t.Errorf("Unexpected error: %v", err) |
| 6401 |
} |
| 6402 |
err = msg.Ack(nctx) |
| 6403 |
if err != nil { |
| 6404 |
t.Errorf("Unexpected error: %v", err) |
| 6405 |
} |
| 6406 |
} |
| 6407 |
|
| 6408 |
t.Run("js sub ack", func(t *testing.T) { |
| 6409 |
sub, err := js.SubscribeSync("foo", nats.Durable("wq2")) |
| 6410 |
if err != nil { |
| 6411 |
t.Fatalf("Unexpected error: %v", err) |
| 6412 |
} |
| 6413 |
|
| 6414 |
checkAcks(t, sub) |
| 6415 |
}) |
| 6416 |
|
| 6417 |
t.Run("non js sub ack", func(t *testing.T) { |
| 6418 |
inbox := nats.NewInbox() |
| 6419 |
_, err = js.AddConsumer("TEST", &nats.ConsumerConfig{ |
| 6420 |
Durable: "wq", |
| 6421 |
AckPolicy: nats.AckExplicitPolicy, |
| 6422 |
DeliverPolicy: nats.DeliverAllPolicy, |
| 6423 |
DeliverSubject: inbox, |
| 6424 |
}) |
| 6425 |
if err != nil { |
| 6426 |
t.Fatalf("Unexpected error: %v", err) |
| 6427 |
} |
| 6428 |
|
| 6429 |
sub, err := nc.SubscribeSync(inbox) |
| 6430 |
if err != nil { |
| 6431 |
t.Fatalf("Unexpected error: %v", err) |
| 6432 |
} |
| 6433 |
|
| 6434 |
checkAcks(t, sub) |
| 6435 |
}) |
| 6436 |
|
| 6437 |
t.Run("Nak with delay", func(t *testing.T) { |
| 6438 |
js.Publish("bar", []byte("msg")) |
| 6439 |
sub, err := js.SubscribeSync("bar", nats.Durable("nak_dur")) |
| 6440 |
if err != nil { |
| 6441 |
t.Fatalf("Error on subscribe: %v", err) |
| 6442 |
} |
| 6443 |
msg, err := sub.NextMsg(time.Second) |
| 6444 |
if err != nil { |
| 6445 |
t.Fatalf("Error on NextMsg: %v", err) |
| 6446 |
} |
| 6447 |
if err := msg.NakWithDelay(500 * time.Millisecond); err != nil { |
| 6448 |
t.Fatalf("Error on Nak: %v", err) |
| 6449 |
} |
| 6450 |
// We should not get redelivery before 500ms+ |
| 6451 |
if _, err = sub.NextMsg(250 * time.Millisecond); err != nats.ErrTimeout { |
| 6452 |
t.Fatalf("Expected timeout, got %v", err) |
| 6453 |
} |
| 6454 |
msg, err = sub.NextMsg(time.Second) |
| 6455 |
if err != nil { |
| 6456 |
t.Fatalf("Error on NextMsg: %v", err) |
| 6457 |
} |
| 6458 |
if err := msg.NakWithDelay(0); err != nil { |
| 6459 |
t.Fatalf("Error on Nak: %v", err) |
| 6460 |
} |
| 6461 |
msg, err = sub.NextMsg(250 * time.Millisecond) |
| 6462 |
if err != nil { |
| 6463 |
t.Fatalf("Expected timeout, got %v", err) |
| 6464 |
} |
| 6465 |
msg.Ack() |
| 6466 |
}) |
| 6467 |
|
| 6468 |
t.Run("BackOff redeliveries", func(t *testing.T) { |
| 6469 |
inbox := nats.NewInbox() |
| 6470 |
sub, err := nc.SubscribeSync(inbox) |
| 6471 |
if err != nil { |
| 6472 |
t.Fatalf("Error on subscribe: %v", err) |
| 6473 |
} |
| 6474 |
defer sub.Unsubscribe() |
| 6475 |
cc := nats.ConsumerConfig{ |
| 6476 |
Durable: "backoff", |
| 6477 |
AckPolicy: nats.AckExplicitPolicy, |
| 6478 |
DeliverPolicy: nats.DeliverAllPolicy, |
| 6479 |
FilterSubject: "bar", |
| 6480 |
DeliverSubject: inbox, |
| 6481 |
BackOff: []time.Duration{50 * time.Millisecond, 250 * time.Millisecond}, |
| 6482 |
} |
| 6483 |
// First, try with a MaxDeliver that is < len(BackOff), which the |
| 6484 |
// server should reject. |
| 6485 |
cc.MaxDeliver = 1 |
| 6486 |
_, err = js.AddConsumer("TEST", &cc) |
| 6487 |
if err == nil || !strings.Contains(err.Error(), "max deliver is required to be > length of backoff values") { |
| 6488 |
t.Fatalf("Expected backoff/max deliver error, got %v", err) |
| 6489 |
} |
| 6490 |
// Now put a valid value |
| 6491 |
cc.MaxDeliver = 4 |
| 6492 |
ci, err := js.AddConsumer("TEST", &cc) |
| 6493 |
if err != nil { |
| 6494 |
t.Fatalf("Error on add consumer: %v", err) |
| 6495 |
} |
| 6496 |
if !reflect.DeepEqual(ci.Config.BackOff, cc.BackOff) { |
| 6497 |
t.Fatalf("Expected backoff to be %v, got %v", cc.BackOff, ci.Config.BackOff) |
| 6498 |
} |
| 6499 |
// Consume the first delivery |
| 6500 |
_, err = sub.NextMsg(time.Second) |
| 6501 |
if err != nil { |
| 6502 |
t.Fatalf("Error on nextMsg: %v", err) |
| 6503 |
} |
| 6504 |
// We should get a redelivery at around 50ms |
| 6505 |
start := time.Now() |
| 6506 |
_, err = sub.NextMsg(time.Second) |
| 6507 |
if err != nil { |
| 6508 |
t.Fatalf("Error on nextMsg: %v", err) |
| 6509 |
} |
| 6510 |
if dur := time.Since(start); dur < 25*time.Millisecond || dur > 100*time.Millisecond { |
| 6511 |
t.Fatalf("Expected to be redelivered at around 50ms, took %v", dur) |
| 6512 |
} |
| 6513 |
// Now it should be every 250ms or so |
| 6514 |
for i := range 2 { |
| 6515 |
start = time.Now() |
| 6516 |
_, err = sub.NextMsg(time.Second) |
| 6517 |
if err != nil { |
| 6518 |
t.Fatalf("Error on nextMsg for iter=%v: %v", i+1, err) |
| 6519 |
} |
| 6520 |
if dur := time.Since(start); dur < 200*time.Millisecond || dur > 300*time.Millisecond { |
| 6521 |
t.Fatalf("Expected to be redelivered at around 250ms, took %v", dur) |
| 6522 |
} |
| 6523 |
} |
| 6524 |
// At this point, we should have go reach MaxDeliver |
| 6525 |
_, err = sub.NextMsg(300 * time.Millisecond) |
| 6526 |
if err != nats.ErrTimeout { |
| 6527 |
t.Fatalf("Expected timeout, got %v", err) |
| 6528 |
} |
| 6529 |
}) |
| 6530 |
}) |
| 6531 |
} |
| 6532 |
|
| 6533 |
func TestJetStreamPullSubscribe_AckPending(t *testing.T) { |
| 6534 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 6535 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 6536 |
if err != nil { |
| 6537 |
t.Fatalf("Unexpected error: %v", err) |
| 6538 |
} |
| 6539 |
|
| 6540 |
// Create the stream using our client API. |
| 6541 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 6542 |
Name: "TEST", |
| 6543 |
Subjects: []string{"foo"}, |
| 6544 |
}) |
| 6545 |
if err != nil { |
| 6546 |
t.Fatalf("Unexpected error: %v", err) |
| 6547 |
} |
| 6548 |
|
| 6549 |
const totalMsgs = 10 |
| 6550 |
for i := range totalMsgs { |
| 6551 |
payload := fmt.Sprintf("i:%d", i) |
| 6552 |
js.Publish("foo", []byte(payload)) |
| 6553 |
} |
| 6554 |
|
| 6555 |
sub, err := js.PullSubscribe("foo", "wq", |
| 6556 |
nats.AckWait(200*time.Millisecond), |
| 6557 |
nats.MaxAckPending(5), |
| 6558 |
) |
| 6559 |
if err != nil { |
| 6560 |
t.Fatalf("Unexpected error: %v", err) |
| 6561 |
} |
| 6562 |
|
| 6563 |
nextMsg := func() *nats.Msg { |
| 6564 |
t.Helper() |
| 6565 |
msgs, err := sub.Fetch(1) |
| 6566 |
if err != nil { |
| 6567 |
t.Fatal(err) |
| 6568 |
} |
| 6569 |
return msgs[0] |
| 6570 |
} |
| 6571 |
|
| 6572 |
getPending := func() (int, int) { |
| 6573 |
t.Helper() |
| 6574 |
info, err := sub.ConsumerInfo() |
| 6575 |
if err != nil { |
| 6576 |
t.Fatal(err) |
| 6577 |
} |
| 6578 |
return info.NumAckPending, int(info.NumPending) |
| 6579 |
} |
| 6580 |
|
| 6581 |
getMetadata := func(msg *nats.Msg) *nats.MsgMetadata { |
| 6582 |
t.Helper() |
| 6583 |
meta, err := msg.Metadata() |
| 6584 |
if err != nil { |
| 6585 |
t.Fatalf("Unexpected error: %v", err) |
| 6586 |
} |
| 6587 |
return meta |
| 6588 |
} |
| 6589 |
|
| 6590 |
expectedPending := func(inflight int, pending int) { |
| 6591 |
t.Helper() |
| 6592 |
checkFor(t, 2*time.Second, 15*time.Millisecond, func() error { |
| 6593 |
i, p := getPending() |
| 6594 |
if i != inflight || p != pending { |
| 6595 |
return fmt.Errorf("Unexpected inflight/pending msgs: %v/%v", i, p) |
| 6596 |
} |
| 6597 |
return nil |
| 6598 |
}) |
| 6599 |
} |
| 6600 |
|
| 6601 |
checkFor(t, 2*time.Second, 15*time.Millisecond, func() error { |
| 6602 |
inflight, pending := getPending() |
| 6603 |
if inflight != 0 || pending != totalMsgs { |
| 6604 |
return fmt.Errorf("Unexpected inflight/pending msgs: %v/%v", inflight, pending) |
| 6605 |
} |
| 6606 |
return nil |
| 6607 |
}) |
| 6608 |
|
| 6609 |
// Normal Ack should decrease pending |
| 6610 |
msg := nextMsg() |
| 6611 |
err = msg.Ack() |
| 6612 |
if err != nil { |
| 6613 |
t.Fatal(err) |
| 6614 |
} |
| 6615 |
|
| 6616 |
expectedPending(0, 9) |
| 6617 |
meta := getMetadata(msg) |
| 6618 |
|
| 6619 |
if meta.Sequence.Consumer != 1 || meta.Sequence.Stream != 1 || meta.NumDelivered != 1 { |
| 6620 |
t.Errorf("Unexpected metadata: %+v", meta) |
| 6621 |
} |
| 6622 |
|
| 6623 |
// AckSync |
| 6624 |
msg = nextMsg() |
| 6625 |
err = msg.AckSync() |
| 6626 |
if err != nil { |
| 6627 |
t.Fatal(err) |
| 6628 |
} |
| 6629 |
expectedPending(0, 8) |
| 6630 |
meta = getMetadata(msg) |
| 6631 |
if meta.Sequence.Consumer != 2 || meta.Sequence.Stream != 2 || meta.NumDelivered != 1 { |
| 6632 |
t.Errorf("Unexpected metadata: %+v", meta) |
| 6633 |
} |
| 6634 |
|
| 6635 |
// Nak the message so that it is redelivered. |
| 6636 |
msg = nextMsg() |
| 6637 |
err = msg.Nak() |
| 6638 |
if err != nil { |
| 6639 |
t.Fatal(err) |
| 6640 |
} |
| 6641 |
expectedPending(1, 7) |
| 6642 |
meta = getMetadata(msg) |
| 6643 |
if meta.Sequence.Consumer != 3 || meta.Sequence.Stream != 3 || meta.NumDelivered != 1 { |
| 6644 |
t.Errorf("Unexpected metadata: %+v", meta) |
| 6645 |
} |
| 6646 |
prevSeq := meta.Sequence.Stream |
| 6647 |
prevPayload := string(msg.Data) |
| 6648 |
|
| 6649 |
// Nak same sequence again, sequence number should not change. |
| 6650 |
msg = nextMsg() |
| 6651 |
err = msg.Nak() |
| 6652 |
if err != nil { |
| 6653 |
t.Fatal(err) |
| 6654 |
} |
| 6655 |
expectedPending(1, 7) |
| 6656 |
meta = getMetadata(msg) |
| 6657 |
if meta.Sequence.Stream != prevSeq { |
| 6658 |
t.Errorf("Expected to get message at seq=%v, got seq=%v", prevSeq, meta.Sequence.Stream) |
| 6659 |
} |
| 6660 |
if string(msg.Data) != prevPayload { |
| 6661 |
t.Errorf("Expected: %q, got: %q", string(prevPayload), string(msg.Data)) |
| 6662 |
} |
| 6663 |
if meta.Sequence.Consumer != 4 || meta.NumDelivered != 2 { |
| 6664 |
t.Errorf("Unexpected metadata: %+v", meta) |
| 6665 |
} |
| 6666 |
|
| 6667 |
// Terminate message so it is no longer pending. |
| 6668 |
msg = nextMsg() |
| 6669 |
err = msg.Term() |
| 6670 |
if err != nil { |
| 6671 |
t.Fatal(err) |
| 6672 |
} |
| 6673 |
expectedPending(0, 7) |
| 6674 |
meta = getMetadata(msg) |
| 6675 |
if meta.Sequence.Stream != prevSeq { |
| 6676 |
t.Errorf("Expected to get message at seq=%v, got seq=%v", prevSeq, meta.Sequence.Stream) |
| 6677 |
} |
| 6678 |
if string(msg.Data) != prevPayload { |
| 6679 |
t.Errorf("Expected: %q, got: %q", string(prevPayload), string(msg.Data)) |
| 6680 |
} |
| 6681 |
if meta.Sequence.Consumer != 5 || meta.Sequence.Stream != 3 || meta.NumDelivered != 3 { |
| 6682 |
t.Errorf("Unexpected metadata: %+v", meta) |
| 6683 |
} |
| 6684 |
|
| 6685 |
// Get next message and ack in progress a few times |
| 6686 |
msg = nextMsg() |
| 6687 |
expected := "i:3" |
| 6688 |
if string(msg.Data) != expected { |
| 6689 |
t.Errorf("Expected: %q, got: %q", string(msg.Data), expected) |
| 6690 |
} |
| 6691 |
err = msg.InProgress() |
| 6692 |
if err != nil { |
| 6693 |
t.Fatal(err) |
| 6694 |
} |
| 6695 |
err = msg.InProgress() |
| 6696 |
if err != nil { |
| 6697 |
t.Fatal(err) |
| 6698 |
} |
| 6699 |
expectedPending(1, 6) |
| 6700 |
meta = getMetadata(msg) |
| 6701 |
if meta.Sequence.Consumer != 6 || meta.Sequence.Stream != 4 || meta.NumDelivered != 1 { |
| 6702 |
t.Errorf("Unexpected metadata: %+v", meta) |
| 6703 |
} |
| 6704 |
|
| 6705 |
// Now ack the message to mark it as done. |
| 6706 |
err = msg.AckSync() |
| 6707 |
if err != nil { |
| 6708 |
t.Fatal(err) |
| 6709 |
} |
| 6710 |
expectedPending(0, 6) |
| 6711 |
|
| 6712 |
// Fetch next message, but do not ack and wait for redelivery. |
| 6713 |
msg = nextMsg() |
| 6714 |
expectedPending(1, 5) |
| 6715 |
meta = getMetadata(msg) |
| 6716 |
if meta.Sequence.Consumer != 7 || meta.Sequence.Stream != 5 || meta.NumDelivered != 1 { |
| 6717 |
t.Errorf("Unexpected metadata: %+v", meta) |
| 6718 |
} |
| 6719 |
prevSeq = meta.Sequence.Stream |
| 6720 |
time.Sleep(500 * time.Millisecond) |
| 6721 |
expectedPending(1, 5) |
| 6722 |
|
| 6723 |
// Next message should be a redelivery. |
| 6724 |
msg = nextMsg() |
| 6725 |
expectedPending(1, 5) |
| 6726 |
meta = getMetadata(msg) |
| 6727 |
if meta.Sequence.Consumer != 8 || meta.Sequence.Stream != prevSeq || meta.NumDelivered != 2 { |
| 6728 |
t.Errorf("Unexpected metadata: %+v", meta) |
| 6729 |
} |
| 6730 |
err = msg.AckSync() |
| 6731 |
if err != nil { |
| 6732 |
t.Fatal(err) |
| 6733 |
} |
| 6734 |
|
| 6735 |
// Get rest of messages. |
| 6736 |
count := 5 |
| 6737 |
for count > 0 { |
| 6738 |
msgs, err := sub.Fetch(count) |
| 6739 |
if err != nil { |
| 6740 |
t.Fatal(err) |
| 6741 |
} |
| 6742 |
for _, msg := range msgs { |
| 6743 |
count-- |
| 6744 |
getMetadata(msg) |
| 6745 |
msg.Ack() |
| 6746 |
} |
| 6747 |
} |
| 6748 |
expectedPending(0, 0) |
| 6749 |
}) |
| 6750 |
} |
| 6751 |
|
| 6752 |
func TestJetStreamSubscribe_AckDup(t *testing.T) { |
| 6753 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 6754 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 6755 |
if err != nil { |
| 6756 |
t.Fatalf("Unexpected error: %v", err) |
| 6757 |
} |
| 6758 |
|
| 6759 |
// Create the stream using our client API. |
| 6760 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 6761 |
Name: "TEST", |
| 6762 |
Subjects: []string{"foo"}, |
| 6763 |
}) |
| 6764 |
if err != nil { |
| 6765 |
t.Fatalf("Unexpected error: %v", err) |
| 6766 |
} |
| 6767 |
|
| 6768 |
js.Publish("foo", []byte("hello")) |
| 6769 |
|
| 6770 |
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) |
| 6771 |
defer cancel() |
| 6772 |
|
| 6773 |
pings := make(chan struct{}, 6) |
| 6774 |
nc.Subscribe("$JS.ACK.TEST.>", func(msg *nats.Msg) { |
| 6775 |
pings <- struct{}{} |
| 6776 |
}) |
| 6777 |
nc.Flush() |
| 6778 |
|
| 6779 |
ch := make(chan error, 6) |
| 6780 |
_, err = js.Subscribe("foo", func(m *nats.Msg) { |
| 6781 |
// Only first ack will be sent, auto ack that will occur after |
| 6782 |
// this won't be sent either. |
| 6783 |
ch <- m.Ack() |
| 6784 |
|
| 6785 |
// Any following acks should fail. |
| 6786 |
ch <- m.Ack() |
| 6787 |
ch <- m.Nak() |
| 6788 |
ch <- m.AckSync() |
| 6789 |
ch <- m.Term() |
| 6790 |
ch <- m.InProgress() |
| 6791 |
}) |
| 6792 |
if err != nil { |
| 6793 |
t.Fatalf("Unexpected error: %v", err) |
| 6794 |
} |
| 6795 |
<-ctx.Done() |
| 6796 |
ackErr1 := <-ch |
| 6797 |
if ackErr1 != nil { |
| 6798 |
t.Errorf("Unexpected error: %v", ackErr1) |
| 6799 |
} |
| 6800 |
|
| 6801 |
for range 5 { |
| 6802 |
e := <-ch |
| 6803 |
if e != nats.ErrMsgAlreadyAckd { |
| 6804 |
t.Errorf("Expected error: %v", e) |
| 6805 |
} |
| 6806 |
} |
| 6807 |
if len(pings) != 1 { |
| 6808 |
t.Logf("Expected to receive a single ack, got: %v", len(pings)) |
| 6809 |
} |
| 6810 |
}) |
| 6811 |
} |
| 6812 |
|
| 6813 |
func TestJetStreamSubscribe_AutoAck(t *testing.T) { |
| 6814 |
tests := []struct { |
| 6815 |
name string |
| 6816 |
opt nats.SubOpt |
| 6817 |
expectedAck bool |
| 6818 |
}{ |
| 6819 |
{ |
| 6820 |
name: "with ack explicit", |
| 6821 |
opt: nats.AckExplicit(), |
| 6822 |
expectedAck: true, |
| 6823 |
}, |
| 6824 |
{ |
| 6825 |
name: "with ack all", |
| 6826 |
opt: nats.AckAll(), |
| 6827 |
expectedAck: true, |
| 6828 |
}, |
| 6829 |
{ |
| 6830 |
name: "with ack none", |
| 6831 |
opt: nats.AckNone(), |
| 6832 |
expectedAck: false, |
| 6833 |
}, |
| 6834 |
} |
| 6835 |
for _, test := range tests { |
| 6836 |
t.Run(test.name, func(t *testing.T) { |
| 6837 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 6838 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 6839 |
if err != nil { |
| 6840 |
t.Fatalf("Unexpected error: %v", err) |
| 6841 |
} |
| 6842 |
|
| 6843 |
// Create the stream using our client API. |
| 6844 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 6845 |
Name: "TEST", |
| 6846 |
Subjects: []string{"foo"}, |
| 6847 |
}) |
| 6848 |
if err != nil { |
| 6849 |
t.Fatalf("Unexpected error: %v", err) |
| 6850 |
} |
| 6851 |
|
| 6852 |
js.Publish("foo", []byte("hello")) |
| 6853 |
|
| 6854 |
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) |
| 6855 |
defer cancel() |
| 6856 |
|
| 6857 |
acks := make(chan struct{}, 2) |
| 6858 |
nc.Subscribe("$JS.ACK.TEST.>", func(msg *nats.Msg) { |
| 6859 |
acks <- struct{}{} |
| 6860 |
}) |
| 6861 |
nc.Flush() |
| 6862 |
|
| 6863 |
_, err = js.Subscribe("foo", func(m *nats.Msg) { |
| 6864 |
}, test.opt) |
| 6865 |
if err != nil { |
| 6866 |
t.Fatalf("Unexpected error: %v", err) |
| 6867 |
} |
| 6868 |
<-ctx.Done() |
| 6869 |
|
| 6870 |
if test.expectedAck { |
| 6871 |
if len(acks) != 1 { |
| 6872 |
t.Fatalf("Expected to receive a single ack, got: %v", len(acks)) |
| 6873 |
} |
| 6874 |
return |
| 6875 |
} |
| 6876 |
if len(acks) != 0 { |
| 6877 |
t.Fatalf("Expected no acks, got: %v", len(acks)) |
| 6878 |
} |
| 6879 |
}) |
| 6880 |
}) |
| 6881 |
} |
| 6882 |
} |
| 6883 |
|
| 6884 |
func TestJetStreamSubscribe_AckDupInProgress(t *testing.T) { |
| 6885 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 6886 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 6887 |
if err != nil { |
| 6888 |
t.Fatalf("Unexpected error: %v", err) |
| 6889 |
} |
| 6890 |
|
| 6891 |
// Create the stream using our client API. |
| 6892 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 6893 |
Name: "TEST", |
| 6894 |
Subjects: []string{"foo"}, |
| 6895 |
}) |
| 6896 |
if err != nil { |
| 6897 |
t.Fatalf("Unexpected error: %v", err) |
| 6898 |
} |
| 6899 |
|
| 6900 |
js.Publish("foo", []byte("hello")) |
| 6901 |
|
| 6902 |
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) |
| 6903 |
defer cancel() |
| 6904 |
|
| 6905 |
pings := make(chan struct{}, 3) |
| 6906 |
nc.Subscribe("$JS.ACK.TEST.>", func(msg *nats.Msg) { |
| 6907 |
pings <- struct{}{} |
| 6908 |
}) |
| 6909 |
nc.Flush() |
| 6910 |
|
| 6911 |
ch := make(chan error, 3) |
| 6912 |
_, err = js.Subscribe("foo", func(m *nats.Msg) { |
| 6913 |
// InProgress ACK can be sent any number of times. |
| 6914 |
ch <- m.InProgress() |
| 6915 |
ch <- m.InProgress() |
| 6916 |
ch <- m.Ack() |
| 6917 |
}, nats.Durable("WQ"), nats.ManualAck()) |
| 6918 |
if err != nil { |
| 6919 |
t.Fatalf("Unexpected error: %v", err) |
| 6920 |
} |
| 6921 |
<-ctx.Done() |
| 6922 |
ackErr1 := <-ch |
| 6923 |
ackErr2 := <-ch |
| 6924 |
ackErr3 := <-ch |
| 6925 |
if ackErr1 != nil { |
| 6926 |
t.Errorf("Unexpected error: %v", ackErr1) |
| 6927 |
} |
| 6928 |
if ackErr2 != nil { |
| 6929 |
t.Errorf("Unexpected error: %v", ackErr2) |
| 6930 |
} |
| 6931 |
if ackErr3 != nil { |
| 6932 |
t.Errorf("Unexpected error: %v", ackErr3) |
| 6933 |
} |
| 6934 |
if len(pings) != 3 { |
| 6935 |
t.Logf("Expected to receive multiple acks, got: %v", len(pings)) |
| 6936 |
} |
| 6937 |
}) |
| 6938 |
} |
| 6939 |
|
| 6940 |
func TestJetStream_Unsubscribe(t *testing.T) { |
| 6941 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 6942 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 6943 |
if err != nil { |
| 6944 |
t.Fatalf("Unexpected error: %v", err) |
| 6945 |
} |
| 6946 |
|
| 6947 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 6948 |
Name: "foo", |
| 6949 |
Subjects: []string{"foo.A", "foo.B", "foo.C"}, |
| 6950 |
}) |
| 6951 |
if err != nil { |
| 6952 |
t.Fatalf("Unexpected error: %v", err) |
| 6953 |
} |
| 6954 |
|
| 6955 |
fetchConsumers := func(t *testing.T, expected int) { |
| 6956 |
t.Helper() |
| 6957 |
checkFor(t, time.Second, 15*time.Millisecond, func() error { |
| 6958 |
var infos []*nats.ConsumerInfo |
| 6959 |
for info := range js.Consumers("foo") { |
| 6960 |
infos = append(infos, info) |
| 6961 |
} |
| 6962 |
if len(infos) != expected { |
| 6963 |
return fmt.Errorf("Expected %d consumers, got: %d", expected, len(infos)) |
| 6964 |
} |
| 6965 |
return nil |
| 6966 |
}) |
| 6967 |
} |
| 6968 |
|
| 6969 |
deleteAllConsumers := func(t *testing.T) { |
| 6970 |
t.Helper() |
| 6971 |
for cn := range js.ConsumerNames("foo") { |
| 6972 |
js.DeleteConsumer("foo", cn) |
| 6973 |
} |
| 6974 |
} |
| 6975 |
|
| 6976 |
js.Publish("foo.A", []byte("A")) |
| 6977 |
js.Publish("foo.B", []byte("B")) |
| 6978 |
js.Publish("foo.C", []byte("C")) |
| 6979 |
|
| 6980 |
t.Run("consumers deleted on unsubscribe", func(t *testing.T) { |
| 6981 |
sub, err := js.SubscribeSync("foo.A") |
| 6982 |
if err != nil { |
| 6983 |
t.Fatal(err) |
| 6984 |
} |
| 6985 |
if err := sub.Unsubscribe(); err != nil { |
| 6986 |
t.Errorf("Unexpected error: %v", err) |
| 6987 |
} |
| 6988 |
|
| 6989 |
sub, err = js.SubscribeSync("foo.B", nats.Durable("B")) |
| 6990 |
if err != nil { |
| 6991 |
t.Fatal(err) |
| 6992 |
} |
| 6993 |
if err := sub.Unsubscribe(); err != nil { |
| 6994 |
t.Errorf("Unexpected error: %v", err) |
| 6995 |
} |
| 6996 |
|
| 6997 |
sub, err = js.Subscribe("foo.C", func(_ *nats.Msg) {}) |
| 6998 |
if err != nil { |
| 6999 |
t.Fatal(err) |
| 7000 |
} |
| 7001 |
if err := sub.Unsubscribe(); err != nil { |
| 7002 |
t.Errorf("Unexpected error: %v", err) |
| 7003 |
} |
| 7004 |
|
| 7005 |
sub, err = js.Subscribe("foo.C", func(_ *nats.Msg) {}, nats.Durable("C")) |
| 7006 |
if err != nil { |
| 7007 |
t.Fatal(err) |
| 7008 |
} |
| 7009 |
if err := sub.Unsubscribe(); err != nil { |
| 7010 |
t.Errorf("Unexpected error: %v", err) |
| 7011 |
} |
| 7012 |
fetchConsumers(t, 0) |
| 7013 |
}) |
| 7014 |
|
| 7015 |
t.Run("not deleted on unsubscribe if consumer created externally", func(t *testing.T) { |
| 7016 |
// Created by JetStreamManagement |
| 7017 |
if _, err = js.AddConsumer("foo", &nats.ConsumerConfig{ |
| 7018 |
Durable: "wq", |
| 7019 |
AckPolicy: nats.AckExplicitPolicy, |
| 7020 |
// Need to specify filter subject here otherwise |
| 7021 |
// would get messages from foo.A as well. |
| 7022 |
FilterSubject: "foo.C", |
| 7023 |
}); err != nil { |
| 7024 |
t.Fatalf("Unexpected error: %v", err) |
| 7025 |
} |
| 7026 |
subC, err := js.PullSubscribe("foo.C", "wq") |
| 7027 |
if err != nil { |
| 7028 |
t.Fatalf("Unexpected error: %v", err) |
| 7029 |
} |
| 7030 |
fetchConsumers(t, 1) |
| 7031 |
|
| 7032 |
msgs, err := subC.Fetch(1, nats.MaxWait(2*time.Second)) |
| 7033 |
if err != nil { |
| 7034 |
t.Errorf("Unexpected error getting message: %v", err) |
| 7035 |
} |
| 7036 |
msg := msgs[0] |
| 7037 |
got := string(msg.Data) |
| 7038 |
expected := "C" |
| 7039 |
if got != expected { |
| 7040 |
t.Errorf("Expected %v, got %v", expected, got) |
| 7041 |
} |
| 7042 |
subC.Unsubscribe() |
| 7043 |
fetchConsumers(t, 1) |
| 7044 |
deleteAllConsumers(t) |
| 7045 |
}) |
| 7046 |
|
| 7047 |
t.Run("consumers deleted on drain", func(t *testing.T) { |
| 7048 |
subA, err := js.Subscribe("foo.A", func(_ *nats.Msg) {}) |
| 7049 |
if err != nil { |
| 7050 |
t.Fatal(err) |
| 7051 |
} |
| 7052 |
fetchConsumers(t, 1) |
| 7053 |
err = subA.Drain() |
| 7054 |
if err != nil { |
| 7055 |
t.Errorf("Unexpected error: %v", err) |
| 7056 |
} |
| 7057 |
fetchConsumers(t, 0) |
| 7058 |
deleteAllConsumers(t) |
| 7059 |
}) |
| 7060 |
|
| 7061 |
t.Run("durable consumers deleted on drain", func(t *testing.T) { |
| 7062 |
subB, err := js.Subscribe("foo.B", func(_ *nats.Msg) {}, nats.Durable("B")) |
| 7063 |
if err != nil { |
| 7064 |
t.Fatal(err) |
| 7065 |
} |
| 7066 |
fetchConsumers(t, 1) |
| 7067 |
err = subB.Drain() |
| 7068 |
if err != nil { |
| 7069 |
t.Errorf("Unexpected error: %v", err) |
| 7070 |
} |
| 7071 |
fetchConsumers(t, 0) |
| 7072 |
}) |
| 7073 |
}) |
| 7074 |
} |
| 7075 |
|
| 7076 |
func TestJetStream_UnsubscribeCloseDrain(t *testing.T) { |
| 7077 |
withJSServerInstance(t, func(t *testing.T, mc *nats.Conn, inst *testservice.Instance) { |
| 7078 |
serverURL := inst.Servers[0].URL |
| 7079 |
jsm, err := mc.JetStream(nats.MaxWait(10 * time.Second)) |
| 7080 |
if err != nil { |
| 7081 |
t.Fatalf("Unexpected error: %v", err) |
| 7082 |
} |
| 7083 |
|
| 7084 |
_, err = jsm.AddStream(&nats.StreamConfig{ |
| 7085 |
Name: "foo", |
| 7086 |
Subjects: []string{"foo.A", "foo.B", "foo.C"}, |
| 7087 |
}) |
| 7088 |
if err != nil { |
| 7089 |
t.Fatalf("Unexpected error: %v", err) |
| 7090 |
} |
| 7091 |
|
| 7092 |
fetchConsumers := func(t *testing.T, expected int) []*nats.ConsumerInfo { |
| 7093 |
t.Helper() |
| 7094 |
var infos []*nats.ConsumerInfo |
| 7095 |
for info := range jsm.Consumers("foo") { |
| 7096 |
infos = append(infos, info) |
| 7097 |
} |
| 7098 |
if len(infos) != expected { |
| 7099 |
t.Fatalf("Expected %d consumers, got: %d", expected, len(infos)) |
| 7100 |
} |
| 7101 |
|
| 7102 |
return infos |
| 7103 |
} |
| 7104 |
|
| 7105 |
t.Run("conn drain deletes ephemeral consumers", func(t *testing.T) { |
| 7106 |
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 7107 |
nc, err := nats.Connect(serverURL, nats.ClosedHandler(func(_ *nats.Conn) { |
| 7108 |
cancel() |
| 7109 |
})) |
| 7110 |
if err != nil { |
| 7111 |
t.Fatalf("Unexpected error: %v", err) |
| 7112 |
} |
| 7113 |
|
| 7114 |
js, err := nc.JetStream() |
| 7115 |
if err != nil { |
| 7116 |
t.Fatalf("Unexpected error: %v", err) |
| 7117 |
} |
| 7118 |
_, err = js.SubscribeSync("foo.C") |
| 7119 |
if err != nil { |
| 7120 |
t.Fatal(err) |
| 7121 |
} |
| 7122 |
|
| 7123 |
// sub.Drain() or nc.Drain() delete JS consumer, same than Unsubscribe() |
| 7124 |
nc.Drain() |
| 7125 |
<-ctx.Done() |
| 7126 |
fetchConsumers(t, 0) |
| 7127 |
}) |
| 7128 |
|
| 7129 |
jsm.Publish("foo.A", []byte("A.1")) |
| 7130 |
jsm.Publish("foo.B", []byte("B.1")) |
| 7131 |
jsm.Publish("foo.C", []byte("C.1")) |
| 7132 |
|
| 7133 |
t.Run("conn close does not delete any consumer", func(t *testing.T) { |
| 7134 |
nc, err := nats.Connect(serverURL) |
| 7135 |
if err != nil { |
| 7136 |
t.Fatalf("Unexpected error: %v", err) |
| 7137 |
} |
| 7138 |
defer nc.Close() |
| 7139 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 7140 |
if err != nil { |
| 7141 |
t.Fatalf("Unexpected error: %v", err) |
| 7142 |
} |
| 7143 |
|
| 7144 |
if _, err := js.SubscribeSync("foo.A"); err != nil { |
| 7145 |
t.Fatalf("Unexpected error: %v", err) |
| 7146 |
} |
| 7147 |
subB, err := js.SubscribeSync("foo.B", nats.Durable("B")) |
| 7148 |
if err != nil { |
| 7149 |
t.Fatalf("Unexpected error: %v", err) |
| 7150 |
} |
| 7151 |
resp, err := subB.NextMsg(2 * time.Second) |
| 7152 |
if err != nil { |
| 7153 |
t.Fatalf("Unexpected error: %v", err) |
| 7154 |
} |
| 7155 |
|
| 7156 |
got := string(resp.Data) |
| 7157 |
expected := "B.1" |
| 7158 |
if got != expected { |
| 7159 |
t.Errorf("Expected %v, got: %v", expected, got) |
| 7160 |
} |
| 7161 |
fetchConsumers(t, 2) |
| 7162 |
|
| 7163 |
// There will be still all consumers since nc.Close |
| 7164 |
// does not delete ephemeral consumers. |
| 7165 |
nc.Close() |
| 7166 |
fetchConsumers(t, 2) |
| 7167 |
}) |
| 7168 |
|
| 7169 |
jsm.Publish("foo.A", []byte("A.2")) |
| 7170 |
jsm.Publish("foo.B", []byte("B.2")) |
| 7171 |
jsm.Publish("foo.C", []byte("C.2")) |
| 7172 |
|
| 7173 |
t.Run("reattached durables consumers cannot be deleted with unsubscribe", func(t *testing.T) { |
| 7174 |
nc, err := nats.Connect(serverURL) |
| 7175 |
if err != nil { |
| 7176 |
t.Fatalf("Unexpected error: %v", err) |
| 7177 |
} |
| 7178 |
defer nc.Close() |
| 7179 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 7180 |
if err != nil { |
| 7181 |
t.Fatalf("Unexpected error: %v", err) |
| 7182 |
} |
| 7183 |
|
| 7184 |
fetchConsumers(t, 2) |
| 7185 |
|
| 7186 |
// The durable interest remains so have to attach now, |
| 7187 |
// otherwise would get a stream already used error. |
| 7188 |
subB, err := js.SubscribeSync("foo.B", nats.Durable("B")) |
| 7189 |
if err != nil { |
| 7190 |
t.Fatal(err) |
| 7191 |
} |
| 7192 |
|
| 7193 |
// No new consumers created since reattached to the same one. |
| 7194 |
fetchConsumers(t, 2) |
| 7195 |
|
| 7196 |
resp, err := subB.NextMsg(2 * time.Second) |
| 7197 |
if err != nil { |
| 7198 |
t.Fatalf("Unexpected error: %v", err) |
| 7199 |
} |
| 7200 |
|
| 7201 |
got := string(resp.Data) |
| 7202 |
expected := "B.2" |
| 7203 |
if got != expected { |
| 7204 |
t.Errorf("Expected %v, got: %v", expected, got) |
| 7205 |
} |
| 7206 |
|
| 7207 |
jsm.Publish("foo.B", []byte("B.3")) |
| 7208 |
|
| 7209 |
// Sub can still receive the same message. |
| 7210 |
resp, err = subB.NextMsg(1 * time.Second) |
| 7211 |
if err != nil { |
| 7212 |
t.Fatalf("Unexpected error: %v", err) |
| 7213 |
} |
| 7214 |
|
| 7215 |
got = string(resp.Data) |
| 7216 |
expected = "B.3" |
| 7217 |
if got != expected { |
| 7218 |
t.Errorf("Expected %v, got: %v", expected, got) |
| 7219 |
} |
| 7220 |
// Delete durable consumer. |
| 7221 |
err = subB.Unsubscribe() |
| 7222 |
if err != nil { |
| 7223 |
t.Errorf("Unexpected error: %v", err) |
| 7224 |
} |
| 7225 |
|
| 7226 |
// Since library did not create, the JS consumers remain. |
| 7227 |
fetchConsumers(t, 2) |
| 7228 |
}) |
| 7229 |
}) |
| 7230 |
} |
| 7231 |
|
| 7232 |
func TestJetStream_UnsubscribeDeleteNoPermissions(t *testing.T) { |
| 7233 |
c := newTester(t) |
| 7234 |
accounts := `accounts: { |
| 7235 |
JS: { # User should not be able to delete consumer. |
| 7236 |
jetstream: enabled |
| 7237 |
users: [ {user: guest, password: "", permissions: { |
| 7238 |
publish: { deny: "$JS.API.CONSUMER.DELETE.>" } |
| 7239 |
}}] |
| 7240 |
} |
| 7241 |
}` |
| 7242 |
inst := c.CreateServer(t, true, |
| 7243 |
testservice.WithAccounts(accounts), |
| 7244 |
testservice.WithAuthorization("no_auth_user: guest"), |
| 7245 |
testservice.WithSystemAccount(noSystemAccountBody()), |
| 7246 |
) |
| 7247 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 7248 |
|
| 7249 |
errCh := make(chan error, 2) |
| 7250 |
nc := dialInstance(t, inst, nats.ErrorHandler(func(_ *nats.Conn, _ *nats.Subscription, err error) { |
| 7251 |
errCh <- err |
| 7252 |
})) |
| 7253 |
c.WaitForJetStream(t, nc) |
| 7254 |
defer nc.Close() |
| 7255 |
|
| 7256 |
js, err := nc.JetStream(nats.MaxWait(time.Second)) |
| 7257 |
if err != nil { |
| 7258 |
t.Fatal(err) |
| 7259 |
} |
| 7260 |
|
| 7261 |
js.AddStream(&nats.StreamConfig{ |
| 7262 |
Name: "foo", |
| 7263 |
}) |
| 7264 |
js.Publish("foo", []byte("test")) |
| 7265 |
|
| 7266 |
sub, err := js.SubscribeSync("foo") |
| 7267 |
if err != nil { |
| 7268 |
t.Fatal(err) |
| 7269 |
} |
| 7270 |
|
| 7271 |
_, err = sub.NextMsg(2 * time.Second) |
| 7272 |
if err != nil { |
| 7273 |
t.Fatal(err) |
| 7274 |
} |
| 7275 |
|
| 7276 |
// Should fail due to lack of permissions. |
| 7277 |
err = sub.Unsubscribe() |
| 7278 |
if err == nil { |
| 7279 |
t.Errorf("Unexpected success attempting to delete consumer without permissions") |
| 7280 |
} |
| 7281 |
|
| 7282 |
select { |
| 7283 |
case <-time.After(2 * time.Second): |
| 7284 |
t.Error("Timeout waiting for permissions error") |
| 7285 |
case err = <-errCh: |
| 7286 |
if !strings.Contains(err.Error(), `Permissions Violation for Publish to "$JS.API.CONSUMER.DELETE`) { |
| 7287 |
t.Error("Expected permissions violation error") |
| 7288 |
} |
| 7289 |
} |
| 7290 |
} |
| 7291 |
|
| 7292 |
func TestJetStreamSubscribe_ReplayPolicy(t *testing.T) { |
| 7293 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 7294 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 7295 |
if err != nil { |
| 7296 |
t.Fatalf("Unexpected error: %v", err) |
| 7297 |
} |
| 7298 |
|
| 7299 |
// Create the stream using our client API. |
| 7300 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 7301 |
Name: "TEST", |
| 7302 |
Subjects: []string{"foo"}, |
| 7303 |
}) |
| 7304 |
if err != nil { |
| 7305 |
t.Fatalf("Unexpected error: %v", err) |
| 7306 |
} |
| 7307 |
|
| 7308 |
i := 0 |
| 7309 |
totalMsgs := 10 |
| 7310 |
for range time.NewTicker(100 * time.Millisecond).C { |
| 7311 |
payload := fmt.Sprintf("i:%d", i) |
| 7312 |
js.Publish("foo", []byte(payload)) |
| 7313 |
i++ |
| 7314 |
|
| 7315 |
if i == totalMsgs { |
| 7316 |
break |
| 7317 |
} |
| 7318 |
} |
| 7319 |
|
| 7320 |
// By default it is ReplayInstant playback policy. |
| 7321 |
isub, err := js.SubscribeSync("foo") |
| 7322 |
if err != nil { |
| 7323 |
t.Fatalf("Unexpected error: %v", err) |
| 7324 |
} |
| 7325 |
ci, err := isub.ConsumerInfo() |
| 7326 |
if err != nil { |
| 7327 |
t.Fatalf("Unexpected error: %v", err) |
| 7328 |
} |
| 7329 |
if ci.Config.ReplayPolicy != nats.ReplayInstantPolicy { |
| 7330 |
t.Fatalf("Expected original replay policy, got: %v", ci.Config.ReplayPolicy) |
| 7331 |
} |
| 7332 |
|
| 7333 |
// Change into original playback. |
| 7334 |
sub, err := js.SubscribeSync("foo", nats.ReplayOriginal()) |
| 7335 |
if err != nil { |
| 7336 |
t.Fatalf("Unexpected error: %v", err) |
| 7337 |
} |
| 7338 |
ci, err = sub.ConsumerInfo() |
| 7339 |
if err != nil { |
| 7340 |
t.Fatalf("Unexpected error: %v", err) |
| 7341 |
} |
| 7342 |
if ci.Config.ReplayPolicy != nats.ReplayOriginalPolicy { |
| 7343 |
t.Fatalf("Expected original replay policy, got: %v", ci.Config.ReplayPolicy) |
| 7344 |
} |
| 7345 |
|
| 7346 |
// There should already be a message delivered. |
| 7347 |
_, err = sub.NextMsg(10 * time.Millisecond) |
| 7348 |
if err != nil { |
| 7349 |
t.Fatalf("Unexpected error: %v", err) |
| 7350 |
} |
| 7351 |
|
| 7352 |
// We should timeout faster since too soon for the original playback. |
| 7353 |
_, err = sub.NextMsg(10 * time.Millisecond) |
| 7354 |
if err != nats.ErrTimeout { |
| 7355 |
t.Fatalf("Expected timeout error replaying the stream, got: %v", err) |
| 7356 |
} |
| 7357 |
|
| 7358 |
// Enough time to get the next message according to the original playback. |
| 7359 |
_, err = sub.NextMsg(110 * time.Millisecond) |
| 7360 |
if err != nil { |
| 7361 |
t.Fatalf("Unexpected error: %v", err) |
| 7362 |
} |
| 7363 |
}) |
| 7364 |
} |
| 7365 |
|
| 7366 |
func TestJetStreamSubscribe_RateLimit(t *testing.T) { |
| 7367 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 7368 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 7369 |
if err != nil { |
| 7370 |
t.Fatalf("Unexpected error: %v", err) |
| 7371 |
} |
| 7372 |
|
| 7373 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 7374 |
Name: "TEST", |
| 7375 |
Subjects: []string{"foo"}, |
| 7376 |
}) |
| 7377 |
if err != nil { |
| 7378 |
t.Fatalf("Unexpected error: %v", err) |
| 7379 |
} |
| 7380 |
|
| 7381 |
totalMsgs := 2048 |
| 7382 |
for range totalMsgs { |
| 7383 |
payload := strings.Repeat("A", 1024) |
| 7384 |
js.Publish("foo", []byte(payload)) |
| 7385 |
} |
| 7386 |
|
| 7387 |
// By default there is no RateLimit |
| 7388 |
isub, err := js.SubscribeSync("foo") |
| 7389 |
if err != nil { |
| 7390 |
t.Fatalf("Unexpected error: %v", err) |
| 7391 |
} |
| 7392 |
ci, err := isub.ConsumerInfo() |
| 7393 |
if err != nil { |
| 7394 |
t.Fatalf("Unexpected error: %v", err) |
| 7395 |
} |
| 7396 |
if ci.Config.RateLimit != 0 { |
| 7397 |
t.Fatalf("Expected no rate limit, got: %v", ci.Config.RateLimit) |
| 7398 |
} |
| 7399 |
|
| 7400 |
recvd := make(chan *nats.Msg, totalMsgs) |
| 7401 |
duration := 2 * time.Second |
| 7402 |
ctx, cancel := context.WithTimeout(context.Background(), duration) |
| 7403 |
defer cancel() |
| 7404 |
|
| 7405 |
var rl uint64 = 1024 |
| 7406 |
sub, err := js.Subscribe("foo", func(m *nats.Msg) { |
| 7407 |
recvd <- m |
| 7408 |
|
| 7409 |
if len(recvd) == totalMsgs { |
| 7410 |
cancel() |
| 7411 |
} |
| 7412 |
}, nats.RateLimit(rl)) |
| 7413 |
if err != nil { |
| 7414 |
t.Fatalf("Unexpected error: %v", err) |
| 7415 |
} |
| 7416 |
ci, err = sub.ConsumerInfo() |
| 7417 |
if err != nil { |
| 7418 |
t.Fatalf("Unexpected error: %v", err) |
| 7419 |
} |
| 7420 |
if ci.Config.RateLimit != rl { |
| 7421 |
t.Fatalf("Expected %v, got: %v", rl, ci.Config.RateLimit) |
| 7422 |
} |
| 7423 |
<-ctx.Done() |
| 7424 |
|
| 7425 |
if len(recvd) >= int(rl) { |
| 7426 |
t.Errorf("Expected applied rate limit to push consumer, got %v msgs in %v", recvd, duration) |
| 7427 |
} |
| 7428 |
}) |
| 7429 |
} |
| 7430 |
|
| 7431 |
func TestJetStreamSubscribe_FilterSubjects(t *testing.T) { |
| 7432 |
tests := []struct { |
| 7433 |
name string |
| 7434 |
durable string |
| 7435 |
}{ |
| 7436 |
{ |
| 7437 |
name: "ephemeral consumer", |
| 7438 |
}, |
| 7439 |
{ |
| 7440 |
name: "durable consumer", |
| 7441 |
durable: "cons", |
| 7442 |
}, |
| 7443 |
} |
| 7444 |
|
| 7445 |
for _, test := range tests { |
| 7446 |
t.Run(test.name, func(t *testing.T) { |
| 7447 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 7448 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 7449 |
if err != nil { |
| 7450 |
t.Fatalf("Unexpected error: %v", err) |
| 7451 |
} |
| 7452 |
|
| 7453 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 7454 |
Name: "TEST", |
| 7455 |
Subjects: []string{"foo", "bar", "baz"}, |
| 7456 |
}) |
| 7457 |
if err != nil { |
| 7458 |
t.Fatalf("Unexpected error: %v", err) |
| 7459 |
} |
| 7460 |
for range 5 { |
| 7461 |
js.Publish("foo", []byte("msg")) |
| 7462 |
} |
| 7463 |
for range 5 { |
| 7464 |
js.Publish("bar", []byte("msg")) |
| 7465 |
} |
| 7466 |
for range 5 { |
| 7467 |
js.Publish("baz", []byte("msg")) |
| 7468 |
} |
| 7469 |
|
| 7470 |
opts := []nats.SubOpt{nats.BindStream("TEST"), nats.ConsumerFilterSubjects("foo", "baz")} |
| 7471 |
if test.durable != "" { |
| 7472 |
opts = append(opts, nats.Durable(test.durable)) |
| 7473 |
} |
| 7474 |
sub, err := js.SubscribeSync("", opts...) |
| 7475 |
if err != nil { |
| 7476 |
t.Fatalf("Unexpected error: %s", err) |
| 7477 |
} |
| 7478 |
|
| 7479 |
for range 10 { |
| 7480 |
msg, err := sub.NextMsg(500 * time.Millisecond) |
| 7481 |
if err != nil { |
| 7482 |
t.Fatalf("Unexpected error: %s", err) |
| 7483 |
} |
| 7484 |
if msg.Subject != "foo" && msg.Subject != "baz" { |
| 7485 |
t.Fatalf("Unexpected message subject: %s", msg.Subject) |
| 7486 |
} |
| 7487 |
} |
| 7488 |
}) |
| 7489 |
}) |
| 7490 |
} |
| 7491 |
} |
| 7492 |
|
| 7493 |
func TestJetStreamSubscribe_ConfigCantChange(t *testing.T) { |
| 7494 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 7495 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 7496 |
if err != nil { |
| 7497 |
t.Fatalf("Unexpected error: %v", err) |
| 7498 |
} |
| 7499 |
|
| 7500 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 7501 |
Name: "TEST", |
| 7502 |
Subjects: []string{"foo"}, |
| 7503 |
}) |
| 7504 |
if err != nil { |
| 7505 |
t.Fatalf("Unexpected error: %v", err) |
| 7506 |
} |
| 7507 |
|
| 7508 |
for _, test := range []struct { |
| 7509 |
name string |
| 7510 |
first nats.SubOpt |
| 7511 |
second nats.SubOpt |
| 7512 |
}{ |
| 7513 |
{"description", nats.Description("a"), nats.Description("b")}, |
| 7514 |
{"deliver policy", nats.DeliverAll(), nats.DeliverLast()}, |
| 7515 |
{"optional start sequence", nats.StartSequence(1), nats.StartSequence(10)}, |
| 7516 |
{"optional start time", nats.StartTime(time.Now()), nats.StartTime(time.Now().Add(-2 * time.Hour))}, |
| 7517 |
{"ack wait", nats.AckWait(10 * time.Second), nats.AckWait(15 * time.Second)}, |
| 7518 |
{"max deliver", nats.MaxDeliver(3), nats.MaxDeliver(5)}, |
| 7519 |
{"replay policy", nats.ReplayOriginal(), nats.ReplayInstant()}, |
| 7520 |
{"max waiting", nats.PullMaxWaiting(10), nats.PullMaxWaiting(20)}, |
| 7521 |
{"max ack pending", nats.MaxAckPending(10), nats.MaxAckPending(20)}, |
| 7522 |
} { |
| 7523 |
t.Run(test.name, func(t *testing.T) { |
| 7524 |
durName := nuid.Next() |
| 7525 |
sub, err := js.PullSubscribe("foo", durName, test.first) |
| 7526 |
if err != nil { |
| 7527 |
t.Fatalf("Error on subscribe: %v", err) |
| 7528 |
} |
| 7529 |
// Once it is created, options can't be changed. |
| 7530 |
_, err = js.PullSubscribe("foo", durName, test.second) |
| 7531 |
if err == nil || !strings.Contains(err.Error(), test.name) { |
| 7532 |
t.Fatalf("Unexpected error: %v", err) |
| 7533 |
} |
| 7534 |
sub.Unsubscribe() |
| 7535 |
}) |
| 7536 |
} |
| 7537 |
|
| 7538 |
for _, test := range []struct { |
| 7539 |
name string |
| 7540 |
cc *nats.ConsumerConfig |
| 7541 |
opt nats.SubOpt |
| 7542 |
}{ |
| 7543 |
{"ack policy", &nats.ConsumerConfig{AckPolicy: nats.AckAllPolicy}, nats.AckNone()}, |
| 7544 |
{"rate limit", &nats.ConsumerConfig{RateLimit: 10}, nats.RateLimit(100)}, |
| 7545 |
{"flow control", &nats.ConsumerConfig{FlowControl: false}, nats.EnableFlowControl()}, |
| 7546 |
{"heartbeat", &nats.ConsumerConfig{Heartbeat: 10 * time.Second}, nats.IdleHeartbeat(20 * time.Second)}, |
| 7547 |
} { |
| 7548 |
t.Run(test.name, func(t *testing.T) { |
| 7549 |
durName := nuid.Next() |
| 7550 |
|
| 7551 |
cc := test.cc |
| 7552 |
cc.Durable = durName |
| 7553 |
cc.DeliverSubject = nuid.Next() |
| 7554 |
if _, err := js.AddConsumer("TEST", cc); err != nil { |
| 7555 |
t.Fatalf("Error creating consumer: %v", err) |
| 7556 |
} |
| 7557 |
|
| 7558 |
sub, err := js.SubscribeSync("foo", nats.Durable(durName), test.opt) |
| 7559 |
if err == nil || !strings.Contains(err.Error(), test.name) { |
| 7560 |
t.Fatalf("Unexpected error: %v", err) |
| 7561 |
} |
| 7562 |
sub.Unsubscribe() |
| 7563 |
}) |
| 7564 |
} |
| 7565 |
|
| 7566 |
// Verify that we don't fail if user did not set it. |
| 7567 |
for _, test := range []struct { |
| 7568 |
name string |
| 7569 |
opt nats.SubOpt |
| 7570 |
}{ |
| 7571 |
{"description", nats.Description("a")}, |
| 7572 |
{"deliver policy", nats.DeliverAll()}, |
| 7573 |
{"optional start sequence", nats.StartSequence(10)}, |
| 7574 |
{"optional start time", nats.StartTime(time.Now())}, |
| 7575 |
{"ack wait", nats.AckWait(10 * time.Second)}, |
| 7576 |
{"max deliver", nats.MaxDeliver(3)}, |
| 7577 |
{"replay policy", nats.ReplayOriginal()}, |
| 7578 |
{"max waiting", nats.PullMaxWaiting(10)}, |
| 7579 |
{"max ack pending", nats.MaxAckPending(10)}, |
| 7580 |
} { |
| 7581 |
t.Run(test.name+" not set", func(t *testing.T) { |
| 7582 |
durName := nuid.Next() |
| 7583 |
sub, err := js.PullSubscribe("foo", durName, test.opt) |
| 7584 |
if err != nil { |
| 7585 |
t.Fatalf("Error on subscribe: %v", err) |
| 7586 |
} |
| 7587 |
// If not explicitly asked by the user, we are ok |
| 7588 |
_, err = js.PullSubscribe("foo", durName) |
| 7589 |
if err != nil { |
| 7590 |
t.Fatalf("Unexpected error: %v", err) |
| 7591 |
} |
| 7592 |
sub.Unsubscribe() |
| 7593 |
}) |
| 7594 |
} |
| 7595 |
|
| 7596 |
for _, test := range []struct { |
| 7597 |
name string |
| 7598 |
opt nats.SubOpt |
| 7599 |
}{ |
| 7600 |
{"default deliver policy", nats.DeliverAll()}, |
| 7601 |
{"default ack wait", nats.AckWait(30 * time.Second)}, |
| 7602 |
{"default replay policy", nats.ReplayInstant()}, |
| 7603 |
{"default max waiting", nats.PullMaxWaiting(512)}, |
| 7604 |
{"default ack pending", nats.MaxAckPending(65536)}, |
| 7605 |
} { |
| 7606 |
t.Run(test.name, func(t *testing.T) { |
| 7607 |
durName := nuid.Next() |
| 7608 |
sub, err := js.PullSubscribe("foo", durName) |
| 7609 |
if err != nil { |
| 7610 |
t.Fatalf("Error on subscribe: %v", err) |
| 7611 |
} |
| 7612 |
// If the option is the same as the server default, it is not an error either. |
| 7613 |
_, err = js.PullSubscribe("foo", durName, test.opt) |
| 7614 |
if err != nil { |
| 7615 |
t.Fatalf("Unexpected error: %v", err) |
| 7616 |
} |
| 7617 |
sub.Unsubscribe() |
| 7618 |
}) |
| 7619 |
} |
| 7620 |
|
| 7621 |
for _, test := range []struct { |
| 7622 |
name string |
| 7623 |
opt nats.SubOpt |
| 7624 |
}{ |
| 7625 |
{"policy", nats.DeliverNew()}, |
| 7626 |
{"ack wait", nats.AckWait(31 * time.Second)}, |
| 7627 |
{"replay policy", nats.ReplayOriginal()}, |
| 7628 |
{"max waiting", nats.PullMaxWaiting(513)}, |
| 7629 |
{"ack pending", nats.MaxAckPending(2)}, |
| 7630 |
} { |
| 7631 |
t.Run(test.name+" changed from default", func(t *testing.T) { |
| 7632 |
durName := nuid.Next() |
| 7633 |
sub, err := js.PullSubscribe("foo", durName) |
| 7634 |
if err != nil { |
| 7635 |
t.Fatalf("Error on subscribe: %v", err) |
| 7636 |
} |
| 7637 |
// First time it was created with defaults and the |
| 7638 |
// second time a change is attempted, so it is an error. |
| 7639 |
_, err = js.PullSubscribe("foo", durName, test.opt) |
| 7640 |
if err == nil || !strings.Contains(err.Error(), test.name) { |
| 7641 |
t.Fatalf("Unexpected error: %v", err) |
| 7642 |
} |
| 7643 |
sub.Unsubscribe() |
| 7644 |
}) |
| 7645 |
} |
| 7646 |
|
| 7647 |
if _, err := js.AddConsumer("TEST", &nats.ConsumerConfig{ |
| 7648 |
Durable: "BindDurable", |
| 7649 |
DeliverSubject: "bar", |
| 7650 |
}); err != nil { |
| 7651 |
t.Fatalf("Failed to create consumer: %v", err) |
| 7652 |
} |
| 7653 |
if _, err := js.SubscribeSync("foo", nats.Bind("TEST", "BindDurable")); err != nil { |
| 7654 |
t.Fatalf("Error on subscribe: %v", err) |
| 7655 |
} |
| 7656 |
}) |
| 7657 |
} |
| 7658 |
|
| 7659 |
func TestJetStreamStreamMirror(t *testing.T) { |
| 7660 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 7661 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 7662 |
if err != nil { |
| 7663 |
t.Fatalf("Unexpected error: %v", err) |
| 7664 |
} |
| 7665 |
|
| 7666 |
// No placement tags: the cluster helper only assigns NODE_x tags when |
| 7667 |
// size > 1, so a single server has none to place on. |
| 7668 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 7669 |
Name: "origin", |
| 7670 |
Storage: nats.MemoryStorage, |
| 7671 |
Replicas: 1, |
| 7672 |
}) |
| 7673 |
if err != nil { |
| 7674 |
t.Fatalf("Unexpected error creating stream: %v", err) |
| 7675 |
} |
| 7676 |
|
| 7677 |
totalMsgs := 10 |
| 7678 |
for i := range totalMsgs { |
| 7679 |
payload := fmt.Sprintf("i:%d", i) |
| 7680 |
js.Publish("origin", []byte(payload)) |
| 7681 |
} |
| 7682 |
|
| 7683 |
t.Run("create mirrors", func(t *testing.T) { |
| 7684 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 7685 |
Name: "m1", |
| 7686 |
Mirror: &nats.StreamSource{Name: "origin"}, |
| 7687 |
Storage: nats.FileStorage, |
| 7688 |
Replicas: 1, |
| 7689 |
}) |
| 7690 |
if err != nil { |
| 7691 |
t.Fatalf("Unexpected error creating stream: %v", err) |
| 7692 |
} |
| 7693 |
|
| 7694 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 7695 |
Name: "m2", |
| 7696 |
Mirror: &nats.StreamSource{Name: "origin"}, |
| 7697 |
Storage: nats.MemoryStorage, |
| 7698 |
Replicas: 1, |
| 7699 |
}) |
| 7700 |
if err != nil { |
| 7701 |
t.Fatalf("Unexpected error creating stream: %v", err) |
| 7702 |
} |
| 7703 |
msgs := make([]*nats.RawStreamMsg, 0) |
| 7704 |
|
| 7705 |
startSequence := 1 |
| 7706 |
|
| 7707 |
GetNextMsg: |
| 7708 |
for i := startSequence; i < totalMsgs+1; i++ { |
| 7709 |
var ( |
| 7710 |
err error |
| 7711 |
seq = uint64(i) |
| 7712 |
msgA *nats.RawStreamMsg |
| 7713 |
msgB *nats.RawStreamMsg |
| 7714 |
sourceMsg *nats.RawStreamMsg |
| 7715 |
timeout = time.Now().Add(2 * time.Second) |
| 7716 |
) |
| 7717 |
|
| 7718 |
for time.Now().Before(timeout) { |
| 7719 |
sourceMsg, err = js.GetMsg("origin", seq) |
| 7720 |
if err != nil { |
| 7721 |
time.Sleep(100 * time.Millisecond) |
| 7722 |
continue |
| 7723 |
} |
| 7724 |
msgA, err = js.GetMsg("m1", seq) |
| 7725 |
if err != nil { |
| 7726 |
time.Sleep(100 * time.Millisecond) |
| 7727 |
continue |
| 7728 |
} |
| 7729 |
if !reflect.DeepEqual(sourceMsg, msgA) { |
| 7730 |
t.Errorf("Expected %+v, got: %+v", sourceMsg, msgA) |
| 7731 |
} |
| 7732 |
|
| 7733 |
msgB, err = js.GetMsg("m2", seq) |
| 7734 |
if err != nil { |
| 7735 |
time.Sleep(100 * time.Millisecond) |
| 7736 |
continue |
| 7737 |
} |
| 7738 |
if !reflect.DeepEqual(sourceMsg, msgB) { |
| 7739 |
t.Errorf("Expected %+v, got: %+v", sourceMsg, msgB) |
| 7740 |
} |
| 7741 |
|
| 7742 |
msgs = append(msgs, msgA) |
| 7743 |
continue GetNextMsg |
| 7744 |
} |
| 7745 |
if err != nil { |
| 7746 |
t.Fatalf("Unexpected error: %v", err) |
| 7747 |
} |
| 7748 |
} |
| 7749 |
|
| 7750 |
got := len(msgs) |
| 7751 |
if got < totalMsgs { |
| 7752 |
t.Errorf("Expected %v, got: %v", totalMsgs, got) |
| 7753 |
} |
| 7754 |
|
| 7755 |
t.Run("consume from mirror", func(t *testing.T) { |
| 7756 |
sub, err := js.SubscribeSync("origin", nats.BindStream("m1")) |
| 7757 |
if err != nil { |
| 7758 |
t.Fatal(err) |
| 7759 |
} |
| 7760 |
|
| 7761 |
mmsgs := make([]*nats.Msg, 0) |
| 7762 |
for range totalMsgs { |
| 7763 |
msg, err := sub.NextMsg(2 * time.Second) |
| 7764 |
if err != nil { |
| 7765 |
t.Error(err) |
| 7766 |
} |
| 7767 |
meta, err := msg.Metadata() |
| 7768 |
if err != nil { |
| 7769 |
t.Error(err) |
| 7770 |
} |
| 7771 |
if meta.Stream != "m1" { |
| 7772 |
t.Errorf("Expected m1, got: %v", meta.Stream) |
| 7773 |
} |
| 7774 |
mmsgs = append(mmsgs, msg) |
| 7775 |
} |
| 7776 |
if len(mmsgs) != totalMsgs { |
| 7777 |
t.Errorf("Expected to consume %v msgs, got: %v", totalMsgs, len(mmsgs)) |
| 7778 |
} |
| 7779 |
}) |
| 7780 |
}) |
| 7781 |
|
| 7782 |
t.Run("consume from original source", func(t *testing.T) { |
| 7783 |
sub, err := js.SubscribeSync("origin") |
| 7784 |
defer sub.Unsubscribe() |
| 7785 |
if err != nil { |
| 7786 |
t.Fatal(err) |
| 7787 |
} |
| 7788 |
msg, err := sub.NextMsg(2 * time.Second) |
| 7789 |
if err != nil { |
| 7790 |
t.Error(err) |
| 7791 |
} |
| 7792 |
meta, err := msg.Metadata() |
| 7793 |
if err != nil { |
| 7794 |
t.Error(err) |
| 7795 |
} |
| 7796 |
if meta.Stream != "origin" { |
| 7797 |
t.Errorf("Expected m1, got: %v", meta.Stream) |
| 7798 |
} |
| 7799 |
}) |
| 7800 |
|
| 7801 |
t.Run("bind to non existing stream fails", func(t *testing.T) { |
| 7802 |
_, err := js.SubscribeSync("origin", nats.BindStream("foo")) |
| 7803 |
if err == nil { |
| 7804 |
t.Fatal("Unexpected success") |
| 7805 |
} |
| 7806 |
if !errors.Is(err, nats.ErrStreamNotFound) { |
| 7807 |
t.Fatalf("Expected error: %v; got: %v", nats.ErrStreamNotFound, err) |
| 7808 |
} |
| 7809 |
}) |
| 7810 |
|
| 7811 |
t.Run("bind to origin stream", func(t *testing.T) { |
| 7812 |
sub, err := js.SubscribeSync("origin", nats.BindStream("origin")) |
| 7813 |
if err != nil { |
| 7814 |
t.Fatal(err) |
| 7815 |
} |
| 7816 |
msg, err := sub.NextMsg(2 * time.Second) |
| 7817 |
if err != nil { |
| 7818 |
t.Error(err) |
| 7819 |
} |
| 7820 |
meta, err := msg.Metadata() |
| 7821 |
if err != nil { |
| 7822 |
t.Error(err) |
| 7823 |
} |
| 7824 |
if meta.Stream != "origin" { |
| 7825 |
t.Errorf("Expected m1, got: %v", meta.Stream) |
| 7826 |
} |
| 7827 |
}) |
| 7828 |
|
| 7829 |
t.Run("get mirror info", func(t *testing.T) { |
| 7830 |
m1, err := js.StreamInfo("m1") |
| 7831 |
if err != nil { |
| 7832 |
t.Fatalf("Unexpected error: %v", err) |
| 7833 |
} |
| 7834 |
got := m1.Mirror.Name |
| 7835 |
expected := "origin" |
| 7836 |
if got != expected { |
| 7837 |
t.Errorf("Expected %v, got: %v", expected, got) |
| 7838 |
} |
| 7839 |
|
| 7840 |
m2, err := js.StreamInfo("m2") |
| 7841 |
if err != nil { |
| 7842 |
t.Fatalf("Unexpected error: %v", err) |
| 7843 |
} |
| 7844 |
got = m2.Mirror.Name |
| 7845 |
expected = "origin" |
| 7846 |
if got != expected { |
| 7847 |
t.Errorf("Expected %v, got: %v", expected, got) |
| 7848 |
} |
| 7849 |
}) |
| 7850 |
|
| 7851 |
t.Run("create stream from sources", func(t *testing.T) { |
| 7852 |
sources := make([]*nats.StreamSource, 0) |
| 7853 |
sources = append(sources, &nats.StreamSource{Name: "m1"}) |
| 7854 |
sources = append(sources, &nats.StreamSource{Name: "m2"}) |
| 7855 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 7856 |
Name: "s1", |
| 7857 |
Sources: sources, |
| 7858 |
Storage: nats.FileStorage, |
| 7859 |
Replicas: 1, |
| 7860 |
}) |
| 7861 |
if err != nil { |
| 7862 |
t.Fatalf("Unexpected error creating stream: %v", err) |
| 7863 |
} |
| 7864 |
|
| 7865 |
msgs := make([]*nats.RawStreamMsg, 0) |
| 7866 |
|
| 7867 |
startSequence := 1 |
| 7868 |
expectedTotal := totalMsgs * 2 |
| 7869 |
|
| 7870 |
GetNextMsg: |
| 7871 |
for i := startSequence; i < expectedTotal+1; i++ { |
| 7872 |
var ( |
| 7873 |
err error |
| 7874 |
seq = uint64(i) |
| 7875 |
msg *nats.RawStreamMsg |
| 7876 |
timeout = time.Now().Add(5 * time.Second) |
| 7877 |
) |
| 7878 |
|
| 7879 |
Retry: |
| 7880 |
for time.Now().Before(timeout) { |
| 7881 |
msg, err = js.GetMsg("s1", seq) |
| 7882 |
if err != nil { |
| 7883 |
time.Sleep(100 * time.Millisecond) |
| 7884 |
continue Retry |
| 7885 |
} |
| 7886 |
msgs = append(msgs, msg) |
| 7887 |
continue GetNextMsg |
| 7888 |
} |
| 7889 |
if err != nil { |
| 7890 |
t.Fatalf("Unexpected error fetching seq=%v: %v", seq, err) |
| 7891 |
} |
| 7892 |
} |
| 7893 |
|
| 7894 |
got := len(msgs) |
| 7895 |
if got < expectedTotal { |
| 7896 |
t.Errorf("Expected %v, got: %v", expectedTotal, got) |
| 7897 |
} |
| 7898 |
|
| 7899 |
si, err := js.StreamInfo("s1") |
| 7900 |
if err != nil { |
| 7901 |
t.Fatalf("Unexpected error: %v", err) |
| 7902 |
} |
| 7903 |
got = int(si.State.Msgs) |
| 7904 |
if got != expectedTotal { |
| 7905 |
t.Errorf("Expected %v, got: %v", expectedTotal, got) |
| 7906 |
} |
| 7907 |
|
| 7908 |
got = len(si.Sources) |
| 7909 |
expected := 2 |
| 7910 |
if got != expected { |
| 7911 |
t.Errorf("Expected %v, got: %v", expected, got) |
| 7912 |
} |
| 7913 |
|
| 7914 |
t.Run("consume from sourced stream", func(t *testing.T) { |
| 7915 |
sub, err := js.SubscribeSync("origin", nats.BindStream("s1")) |
| 7916 |
if err != nil { |
| 7917 |
t.Error(err) |
| 7918 |
} |
| 7919 |
_, err = sub.NextMsg(2 * time.Second) |
| 7920 |
if err != nil { |
| 7921 |
t.Error(err) |
| 7922 |
} |
| 7923 |
}) |
| 7924 |
}) |
| 7925 |
|
| 7926 |
t.Run("update stream with sources", func(t *testing.T) { |
| 7927 |
si, err := js.StreamInfo("s1") |
| 7928 |
if err != nil { |
| 7929 |
t.Fatalf("Unexpected error creating stream: %v", err) |
| 7930 |
} |
| 7931 |
got := len(si.Config.Sources) |
| 7932 |
expected := 2 |
| 7933 |
if got != expected { |
| 7934 |
t.Errorf("Expected %v, got: %v", expected, got) |
| 7935 |
} |
| 7936 |
|
| 7937 |
got = len(si.Sources) |
| 7938 |
if got != expected { |
| 7939 |
t.Errorf("Expected %v, got: %v", expected, got) |
| 7940 |
} |
| 7941 |
|
| 7942 |
config := si.Config |
| 7943 |
config.MaxMsgs = 128 |
| 7944 |
updated, err := js.UpdateStream(&config) |
| 7945 |
if err != nil { |
| 7946 |
t.Fatalf("Unexpected error creating stream: %v", err) |
| 7947 |
} |
| 7948 |
|
| 7949 |
got = len(updated.Config.Sources) |
| 7950 |
if got != expected { |
| 7951 |
t.Errorf("Expected %v, got: %v", expected, got) |
| 7952 |
} |
| 7953 |
|
| 7954 |
got = int(updated.Config.MaxMsgs) |
| 7955 |
expected = int(config.MaxMsgs) |
| 7956 |
if got != expected { |
| 7957 |
t.Errorf("Expected %v, got: %v", expected, got) |
| 7958 |
} |
| 7959 |
}) |
| 7960 |
|
| 7961 |
t.Run("bind to stream with subject not in stream", func(t *testing.T) { |
| 7962 |
sub, err := js.SubscribeSync("nothing", nats.BindStream("origin")) |
| 7963 |
if err != nil { |
| 7964 |
t.Fatal(err) |
| 7965 |
} |
| 7966 |
_, err = sub.NextMsg(1 * time.Second) |
| 7967 |
if !errors.Is(err, nats.ErrTimeout) { |
| 7968 |
t.Fatal("Expected timeout error") |
| 7969 |
} |
| 7970 |
|
| 7971 |
info, err := sub.ConsumerInfo() |
| 7972 |
if err != nil { |
| 7973 |
t.Fatal(err) |
| 7974 |
} |
| 7975 |
got := info.Stream |
| 7976 |
expected := "origin" |
| 7977 |
if got != expected { |
| 7978 |
t.Fatalf("Expected %v, got %v", expected, got) |
| 7979 |
} |
| 7980 |
|
| 7981 |
got = info.Config.FilterSubject |
| 7982 |
expected = "nothing" |
| 7983 |
if got != expected { |
| 7984 |
t.Fatalf("Expected %v, got %v", expected, got) |
| 7985 |
} |
| 7986 |
|
| 7987 |
t.Run("can consume after stream update", func(t *testing.T) { |
| 7988 |
_, err = js.UpdateStream(&nats.StreamConfig{ |
| 7989 |
Name: "origin", |
| 7990 |
Storage: nats.MemoryStorage, |
| 7991 |
Replicas: 1, |
| 7992 |
Subjects: []string{"origin", "nothing"}, |
| 7993 |
}) |
| 7994 |
js.Publish("nothing", []byte("hello world")) |
| 7995 |
|
| 7996 |
msg, err := sub.NextMsg(1 * time.Second) |
| 7997 |
if err != nil { |
| 7998 |
t.Error(err) |
| 7999 |
} |
| 8000 |
got = msg.Subject |
| 8001 |
expected = "nothing" |
| 8002 |
if got != expected { |
| 8003 |
t.Fatalf("Expected %v, got %v", expected, got) |
| 8004 |
} |
| 8005 |
}) |
| 8006 |
}) |
| 8007 |
|
| 8008 |
t.Run("create sourced stream with a cycle", func(t *testing.T) { |
| 8009 |
sources := make([]*nats.StreamSource, 0) |
| 8010 |
sources = append(sources, &nats.StreamSource{Name: "origin"}) |
| 8011 |
sources = append(sources, &nats.StreamSource{Name: "m1"}) |
| 8012 |
streamName := "s2" |
| 8013 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 8014 |
Name: streamName, |
| 8015 |
Sources: sources, |
| 8016 |
Storage: nats.FileStorage, |
| 8017 |
Replicas: 1, |
| 8018 |
}) |
| 8019 |
var aerr *nats.APIError |
| 8020 |
if ok := errors.As(err, &aerr); !ok || aerr.ErrorCode != nats.JSStreamInvalidConfig { |
| 8021 |
t.Fatalf("Expected nats.APIError, got %v", err) |
| 8022 |
} |
| 8023 |
}) |
| 8024 |
}) |
| 8025 |
} |
| 8026 |
|
| 8027 |
func TestJetStream_ClusterMultipleSubscribe(t *testing.T) { |
| 8028 |
nodes := []int{1, 3} |
| 8029 |
replicas := []int{1} |
| 8030 |
|
| 8031 |
for _, n := range nodes { |
| 8032 |
for _, r := range replicas { |
| 8033 |
if r > 1 && n == 1 { |
| 8034 |
continue |
| 8035 |
} |
| 8036 |
|
| 8037 |
t.Run(fmt.Sprintf("qsub n=%d r=%d", n, r), func(t *testing.T) { |
| 8038 |
name := fmt.Sprintf("MSUB%d%d", n, r) |
| 8039 |
stream := &nats.StreamConfig{ |
| 8040 |
Name: name, |
| 8041 |
Replicas: r, |
| 8042 |
} |
| 8043 |
withClusterAndStream(t, n, stream, testJetStreamClusterMultipleQueueSubscribeTS) |
| 8044 |
}) |
| 8045 |
|
| 8046 |
t.Run(fmt.Sprintf("psub n=%d r=%d", n, r), func(t *testing.T) { |
| 8047 |
name := fmt.Sprintf("PSUBN%d%d", n, r) |
| 8048 |
stream := &nats.StreamConfig{ |
| 8049 |
Name: name, |
| 8050 |
Replicas: n, |
| 8051 |
} |
| 8052 |
withClusterAndStream(t, n, stream, testJetStreamClusterMultiplePullSubscribeTS) |
| 8053 |
}) |
| 8054 |
|
| 8055 |
t.Run(fmt.Sprintf("psub n=%d r=%d multi fetch", n, r), func(t *testing.T) { |
| 8056 |
name := fmt.Sprintf("PFSUBN%d%d", n, r) |
| 8057 |
stream := &nats.StreamConfig{ |
| 8058 |
Name: name, |
| 8059 |
Replicas: n, |
| 8060 |
} |
| 8061 |
withClusterAndStream(t, n, stream, testJetStreamClusterMultipleFetchPullSubscribeTS) |
| 8062 |
}) |
| 8063 |
} |
| 8064 |
} |
| 8065 |
} |
| 8066 |
|
| 8067 |
// withClusterAndStream spins up a single JS server when size == 1 or a JS |
| 8068 |
// cluster when size > 1, then creates the supplied stream and invokes fn. |
| 8069 |
// Sized for the testservice-based TestJetStream_ClusterMultipleSubscribe. |
| 8070 |
func withClusterAndStream(t *testing.T, size int, stream *nats.StreamConfig, fn func(t *testing.T, subject string, nc *nats.Conn)) { |
| 8071 |
t.Helper() |
| 8072 |
c := newTester(t) |
| 8073 |
var inst *testservice.Instance |
| 8074 |
if size == 1 { |
| 8075 |
inst = c.CreateServer(t, true) |
| 8076 |
} else { |
| 8077 |
inst = c.CreateCluster(t, size, true) |
| 8078 |
} |
| 8079 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 8080 |
|
| 8081 |
nc := dialInstance(t, inst) |
| 8082 |
c.WaitForJetStream(t, nc) |
| 8083 |
|
| 8084 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 8085 |
if err != nil { |
| 8086 |
t.Fatalf("Unexpected error: %v", err) |
| 8087 |
} |
| 8088 |
|
| 8089 |
// Retry stream creation while cluster settles (mirrors withJSClusterAndStream). |
| 8090 |
deadline := time.Now().Add(10 * time.Second) |
| 8091 |
for { |
| 8092 |
_, err = js.AddStream(stream) |
| 8093 |
if err == nil { |
| 8094 |
break |
| 8095 |
} |
| 8096 |
if time.Now().After(deadline) { |
| 8097 |
t.Fatalf("Unexpected error creating stream: %v", err) |
| 8098 |
} |
| 8099 |
time.Sleep(500 * time.Millisecond) |
| 8100 |
} |
| 8101 |
|
| 8102 |
fn(t, stream.Name, nc) |
| 8103 |
} |
| 8104 |
|
| 8105 |
func testJetStreamClusterMultipleQueueSubscribeTS(t *testing.T, subject string, nc *nats.Conn) { |
| 8106 |
var wg sync.WaitGroup |
| 8107 |
ctx, done := context.WithTimeout(context.Background(), 2*time.Second) |
| 8108 |
defer done() |
| 8109 |
|
| 8110 |
js, err := nc.JetStream() |
| 8111 |
if err != nil { |
| 8112 |
t.Fatal(err) |
| 8113 |
} |
| 8114 |
|
| 8115 |
size := 5 |
| 8116 |
subs := make([]*nats.Subscription, size) |
| 8117 |
errCh := make(chan error, size) |
| 8118 |
|
| 8119 |
sub, err := js.QueueSubscribeSync(subject, "wq", nats.Durable("shared")) |
| 8120 |
if err != nil { |
| 8121 |
t.Fatalf("Unexpected error: %v", err) |
| 8122 |
} |
| 8123 |
subs[0] = sub |
| 8124 |
|
| 8125 |
for i := 1; i < size; i++ { |
| 8126 |
wg.Add(1) |
| 8127 |
go func(n int) { |
| 8128 |
defer wg.Done() |
| 8129 |
var sub *nats.Subscription |
| 8130 |
var err error |
| 8131 |
for range 5 { |
| 8132 |
sub, err = js.QueueSubscribeSync(subject, "wq", nats.Durable("shared")) |
| 8133 |
if err != nil { |
| 8134 |
time.Sleep(1 * time.Second) |
| 8135 |
continue |
| 8136 |
} |
| 8137 |
break |
| 8138 |
} |
| 8139 |
if err != nil { |
| 8140 |
errCh <- err |
| 8141 |
} else { |
| 8142 |
subs[n] = sub |
| 8143 |
} |
| 8144 |
}(i) |
| 8145 |
} |
| 8146 |
|
| 8147 |
go func() { |
| 8148 |
wg.Wait() |
| 8149 |
done() |
| 8150 |
}() |
| 8151 |
|
| 8152 |
wg.Wait() |
| 8153 |
for range size * 2 { |
| 8154 |
js.Publish(subject, []byte("test")) |
| 8155 |
} |
| 8156 |
|
| 8157 |
delivered := 0 |
| 8158 |
for _, sub := range subs { |
| 8159 |
if sub == nil { |
| 8160 |
continue |
| 8161 |
} |
| 8162 |
if nmsgs, _, _ := sub.Pending(); err != nil || nmsgs > 0 { |
| 8163 |
delivered++ |
| 8164 |
} |
| 8165 |
} |
| 8166 |
if delivered < 2 { |
| 8167 |
t.Fatalf("Expected more than one subscriber to receive a message, got: %v", delivered) |
| 8168 |
} |
| 8169 |
|
| 8170 |
select { |
| 8171 |
case <-ctx.Done(): |
| 8172 |
case err := <-errCh: |
| 8173 |
if err != nil { |
| 8174 |
t.Fatalf("Unexpected error with multiple queue subscribers: %v", err) |
| 8175 |
} |
| 8176 |
} |
| 8177 |
} |
| 8178 |
|
| 8179 |
func testJetStreamClusterMultiplePullSubscribeTS(t *testing.T, subject string, nc *nats.Conn) { |
| 8180 |
js, err := nc.JetStream() |
| 8181 |
if err != nil { |
| 8182 |
t.Fatal(err) |
| 8183 |
} |
| 8184 |
|
| 8185 |
var wg sync.WaitGroup |
| 8186 |
ctx, done := context.WithTimeout(context.Background(), 2*time.Second) |
| 8187 |
defer done() |
| 8188 |
|
| 8189 |
size := 5 |
| 8190 |
subs := make([]*nats.Subscription, size) |
| 8191 |
errCh := make(chan error, size) |
| 8192 |
for i := range size { |
| 8193 |
wg.Add(1) |
| 8194 |
go func(n int) { |
| 8195 |
defer wg.Done() |
| 8196 |
var sub *nats.Subscription |
| 8197 |
var err error |
| 8198 |
for range 5 { |
| 8199 |
sub, err = js.PullSubscribe(subject, "shared") |
| 8200 |
if err != nil { |
| 8201 |
time.Sleep(1 * time.Second) |
| 8202 |
continue |
| 8203 |
} |
| 8204 |
break |
| 8205 |
} |
| 8206 |
if err != nil { |
| 8207 |
errCh <- err |
| 8208 |
} else { |
| 8209 |
subs[n] = sub |
| 8210 |
} |
| 8211 |
}(i) |
| 8212 |
} |
| 8213 |
|
| 8214 |
go func() { |
| 8215 |
wg.Wait() |
| 8216 |
done() |
| 8217 |
}() |
| 8218 |
|
| 8219 |
wg.Wait() |
| 8220 |
for range size * 2 { |
| 8221 |
js.Publish(subject, []byte("test")) |
| 8222 |
} |
| 8223 |
|
| 8224 |
delivered := 0 |
| 8225 |
for i, sub := range subs { |
| 8226 |
if sub == nil { |
| 8227 |
continue |
| 8228 |
} |
| 8229 |
for range 4 { |
| 8230 |
_, err := sub.Fetch(1, nats.MaxWait(250*time.Millisecond)) |
| 8231 |
if err != nil { |
| 8232 |
t.Logf("%v WARN: Timeout waiting for next message: %v", i, err) |
| 8233 |
continue |
| 8234 |
} |
| 8235 |
delivered++ |
| 8236 |
break |
| 8237 |
} |
| 8238 |
} |
| 8239 |
|
| 8240 |
if delivered < 2 { |
| 8241 |
t.Fatalf("Expected more than one subscriber to receive a message, got: %v", delivered) |
| 8242 |
} |
| 8243 |
|
| 8244 |
select { |
| 8245 |
case <-ctx.Done(): |
| 8246 |
case err := <-errCh: |
| 8247 |
if err != nil { |
| 8248 |
t.Fatalf("Unexpected error with multiple pull subscribers: %v", err) |
| 8249 |
} |
| 8250 |
} |
| 8251 |
} |
| 8252 |
|
| 8253 |
func testJetStreamClusterMultipleFetchPullSubscribeTS(t *testing.T, subject string, nc *nats.Conn) { |
| 8254 |
js, err := nc.JetStream() |
| 8255 |
if err != nil { |
| 8256 |
t.Fatal(err) |
| 8257 |
} |
| 8258 |
|
| 8259 |
var wg sync.WaitGroup |
| 8260 |
ctx, done := context.WithTimeout(context.Background(), 5*time.Second) |
| 8261 |
defer done() |
| 8262 |
|
| 8263 |
nsubs := 4 |
| 8264 |
subs := make([]*nats.Subscription, nsubs) |
| 8265 |
errCh := make(chan error, nsubs) |
| 8266 |
var queues sync.Map |
| 8267 |
for i := range nsubs { |
| 8268 |
wg.Add(1) |
| 8269 |
go func(n int) { |
| 8270 |
defer wg.Done() |
| 8271 |
sub, err := js.PullSubscribe(subject, "shared") |
| 8272 |
if err != nil { |
| 8273 |
errCh <- err |
| 8274 |
} else { |
| 8275 |
subs[n] = sub |
| 8276 |
queues.Store(sub.Subject, make([]*nats.Msg, 0)) |
| 8277 |
} |
| 8278 |
}(i) |
| 8279 |
} |
| 8280 |
|
| 8281 |
wg.Wait() |
| 8282 |
var ( |
| 8283 |
total uint64 = 100 |
| 8284 |
delivered uint64 |
| 8285 |
batchSize = 2 |
| 8286 |
) |
| 8287 |
go func() { |
| 8288 |
for i := range int(total) { |
| 8289 |
js.Publish(subject, []byte(fmt.Sprintf("n:%v", i))) |
| 8290 |
time.Sleep(1 * time.Millisecond) |
| 8291 |
} |
| 8292 |
}() |
| 8293 |
|
| 8294 |
ctx2, done2 := context.WithTimeout(ctx, 3*time.Second) |
| 8295 |
defer done2() |
| 8296 |
|
| 8297 |
for _, psub := range subs { |
| 8298 |
if psub == nil { |
| 8299 |
continue |
| 8300 |
} |
| 8301 |
sub := psub |
| 8302 |
subject := sub.Subject |
| 8303 |
v, _ := queues.Load(sub.Subject) |
| 8304 |
queue := v.([]*nats.Msg) |
| 8305 |
go func() { |
| 8306 |
for { |
| 8307 |
select { |
| 8308 |
case <-ctx2.Done(): |
| 8309 |
return |
| 8310 |
default: |
| 8311 |
} |
| 8312 |
|
| 8313 |
if current := atomic.LoadUint64(&delivered); current >= total { |
| 8314 |
done2() |
| 8315 |
return |
| 8316 |
} |
| 8317 |
|
| 8318 |
for range 4 { |
| 8319 |
recvd, err := sub.Fetch(batchSize, nats.MaxWait(1*time.Second)) |
| 8320 |
if err != nil { |
| 8321 |
if err == nats.ErrConnectionClosed { |
| 8322 |
return |
| 8323 |
} |
| 8324 |
current := atomic.LoadUint64(&delivered) |
| 8325 |
if current >= total { |
| 8326 |
done2() |
| 8327 |
return |
| 8328 |
} else { |
| 8329 |
t.Logf("WARN: Timeout waiting for next message: %v", err) |
| 8330 |
} |
| 8331 |
continue |
| 8332 |
} |
| 8333 |
for _, msg := range recvd { |
| 8334 |
queue = append(queue, msg) |
| 8335 |
queues.Store(subject, queue) |
| 8336 |
} |
| 8337 |
atomic.AddUint64(&delivered, uint64(len(recvd))) |
| 8338 |
break |
| 8339 |
} |
| 8340 |
} |
| 8341 |
}() |
| 8342 |
} |
| 8343 |
|
| 8344 |
<-ctx2.Done() |
| 8345 |
|
| 8346 |
if delivered < total { |
| 8347 |
t.Fatalf("Expected %v, got: %v", total, delivered) |
| 8348 |
} |
| 8349 |
|
| 8350 |
select { |
| 8351 |
case <-ctx.Done(): |
| 8352 |
case err := <-errCh: |
| 8353 |
if err != nil { |
| 8354 |
t.Fatalf("Unexpected error with multiple pull subscribers: %v", err) |
| 8355 |
} |
| 8356 |
} |
| 8357 |
|
| 8358 |
var ( |
| 8359 |
gotNoMessages bool |
| 8360 |
count = 0 |
| 8361 |
) |
| 8362 |
queues.Range(func(k, v any) bool { |
| 8363 |
msgs := v.([]*nats.Msg) |
| 8364 |
count += len(msgs) |
| 8365 |
|
| 8366 |
if len(msgs) == 0 { |
| 8367 |
gotNoMessages = true |
| 8368 |
return false |
| 8369 |
} |
| 8370 |
return true |
| 8371 |
}) |
| 8372 |
|
| 8373 |
if gotNoMessages { |
| 8374 |
t.Error("Expected all pull subscribers to receive some messages") |
| 8375 |
} |
| 8376 |
} |
| 8377 |
|
| 8378 |
func TestJetStream_ClusterReconnect(t *testing.T) { |
| 8379 |
t.Skip("This test need to be revisited") |
| 8380 |
} |
| 8381 |
|
| 8382 |
func TestJetStreamPullSubscribeOptions(t *testing.T) { |
| 8383 |
c := newTester(t) |
| 8384 |
inst := c.CreateCluster(t, 3, true) |
| 8385 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 8386 |
|
| 8387 |
nc := dialInstance(t, inst) |
| 8388 |
c.WaitForJetStream(t, nc) |
| 8389 |
waitForJSCluster(t, nc) |
| 8390 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 8391 |
if err != nil { |
| 8392 |
t.Fatalf("Unexpected error: %v", err) |
| 8393 |
} |
| 8394 |
|
| 8395 |
subject := "WQ" |
| 8396 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 8397 |
Name: subject, |
| 8398 |
Replicas: 1, |
| 8399 |
}) |
| 8400 |
if err != nil { |
| 8401 |
t.Fatal(err) |
| 8402 |
} |
| 8403 |
|
| 8404 |
sendMsgs := func(t *testing.T, totalMsgs int) { |
| 8405 |
t.Helper() |
| 8406 |
for i := range totalMsgs { |
| 8407 |
payload := fmt.Sprintf("i:%d", i) |
| 8408 |
if _, err := js.Publish(subject, []byte(payload)); err != nil { |
| 8409 |
t.Errorf("Unexpected error: %v", err) |
| 8410 |
} |
| 8411 |
} |
| 8412 |
} |
| 8413 |
|
| 8414 |
t.Run("max request batch", func(t *testing.T) { |
| 8415 |
defer js.PurgeStream(subject) |
| 8416 |
|
| 8417 |
sub, err := js.PullSubscribe(subject, "max-request-batch", nats.MaxRequestBatch(2)) |
| 8418 |
if err != nil { |
| 8419 |
t.Fatal(err) |
| 8420 |
} |
| 8421 |
defer sub.Unsubscribe() |
| 8422 |
if _, err := sub.Fetch(10); err == nil || !strings.Contains(err.Error(), "MaxRequestBatch of 2") { |
| 8423 |
t.Fatalf("Expected error about max request batch size, got %v", err) |
| 8424 |
} |
| 8425 |
}) |
| 8426 |
|
| 8427 |
t.Run("max request max bytes", func(t *testing.T) { |
| 8428 |
defer js.PurgeStream(subject) |
| 8429 |
|
| 8430 |
sub, err := js.PullSubscribe(subject, "max-request-max-bytes", nats.MaxRequestMaxBytes(100)) |
| 8431 |
if err != nil { |
| 8432 |
t.Fatal(err) |
| 8433 |
} |
| 8434 |
defer sub.Unsubscribe() |
| 8435 |
|
| 8436 |
if _, err := sub.Fetch(10, nats.PullMaxBytes(200)); err == nil || !strings.Contains(err.Error(), "MaxRequestMaxBytes of 100") { |
| 8437 |
t.Fatalf("Expected error about max request max bytes, got %v", err) |
| 8438 |
} |
| 8439 |
}) |
| 8440 |
|
| 8441 |
t.Run("max request expires", func(t *testing.T) { |
| 8442 |
defer js.PurgeStream(subject) |
| 8443 |
|
| 8444 |
sub, err := js.PullSubscribe(subject, "max-request-expires", nats.MaxRequestExpires(50*time.Millisecond)) |
| 8445 |
if err != nil { |
| 8446 |
t.Fatal(err) |
| 8447 |
} |
| 8448 |
defer sub.Unsubscribe() |
| 8449 |
if _, err := sub.Fetch(10); err == nil || !strings.Contains(err.Error(), "MaxRequestExpires of 50ms") { |
| 8450 |
t.Fatalf("Expected error about max request expiration, got %v", err) |
| 8451 |
} |
| 8452 |
}) |
| 8453 |
|
| 8454 |
t.Run("batch size", func(t *testing.T) { |
| 8455 |
defer js.PurgeStream(subject) |
| 8456 |
|
| 8457 |
expected := 10 |
| 8458 |
sendMsgs(t, expected) |
| 8459 |
sub, err := js.PullSubscribe(subject, "batch-size") |
| 8460 |
if err != nil { |
| 8461 |
t.Fatal(err) |
| 8462 |
} |
| 8463 |
defer sub.Unsubscribe() |
| 8464 |
|
| 8465 |
msgs, err := sub.Fetch(expected, nats.MaxWait(2*time.Second)) |
| 8466 |
if err != nil { |
| 8467 |
t.Fatal(err) |
| 8468 |
} |
| 8469 |
|
| 8470 |
for _, msg := range msgs { |
| 8471 |
msg.AckSync() |
| 8472 |
} |
| 8473 |
|
| 8474 |
got := len(msgs) |
| 8475 |
if got != expected { |
| 8476 |
t.Fatalf("Got %v messages, expected at least: %v", got, expected) |
| 8477 |
} |
| 8478 |
|
| 8479 |
_, err = sub.Fetch(1, nats.MaxWait(250*time.Millisecond)) |
| 8480 |
if err != nats.ErrTimeout { |
| 8481 |
t.Errorf("Expected timeout fetching next message, got: %v", err) |
| 8482 |
} |
| 8483 |
|
| 8484 |
expected = 5 |
| 8485 |
sendMsgs(t, expected) |
| 8486 |
msgs, err = sub.Fetch(expected, nats.MaxWait(1*time.Second)) |
| 8487 |
if err != nil { |
| 8488 |
t.Fatalf("Unexpected error: %v", err) |
| 8489 |
} |
| 8490 |
got = len(msgs) |
| 8491 |
if got != expected { |
| 8492 |
t.Fatalf("Got %v messages, expected at least: %v", got, expected) |
| 8493 |
} |
| 8494 |
|
| 8495 |
for _, msg := range msgs { |
| 8496 |
msg.Ack() |
| 8497 |
} |
| 8498 |
}) |
| 8499 |
|
| 8500 |
t.Run("sub drain is no op", func(t *testing.T) { |
| 8501 |
defer js.PurgeStream(subject) |
| 8502 |
|
| 8503 |
expected := 10 |
| 8504 |
sendMsgs(t, expected) |
| 8505 |
sub, err := js.PullSubscribe(subject, "batch-ctx") |
| 8506 |
if err != nil { |
| 8507 |
t.Fatal(err) |
| 8508 |
} |
| 8509 |
defer sub.Unsubscribe() |
| 8510 |
|
| 8511 |
msgs, err := sub.Fetch(expected, nats.MaxWait(2*time.Second)) |
| 8512 |
if err != nil { |
| 8513 |
t.Fatalf("Unexpected error: %v", err) |
| 8514 |
} |
| 8515 |
got := len(msgs) |
| 8516 |
if got != expected { |
| 8517 |
t.Fatalf("Got %v messages, expected at least: %v", got, expected) |
| 8518 |
} |
| 8519 |
if err := sub.Drain(); err != nil { |
| 8520 |
t.Errorf("Unexpected error: %v", err) |
| 8521 |
} |
| 8522 |
}) |
| 8523 |
|
| 8524 |
t.Run("fetch after unsubscribe", func(t *testing.T) { |
| 8525 |
defer js.PurgeStream(subject) |
| 8526 |
|
| 8527 |
expected := 10 |
| 8528 |
sendMsgs(t, expected) |
| 8529 |
sub, err := js.PullSubscribe(subject, "fetch-unsub") |
| 8530 |
if err != nil { |
| 8531 |
t.Fatal(err) |
| 8532 |
} |
| 8533 |
|
| 8534 |
if err := sub.Unsubscribe(); err != nil { |
| 8535 |
t.Fatal(err) |
| 8536 |
} |
| 8537 |
|
| 8538 |
_, err = sub.Fetch(1, nats.MaxWait(500*time.Millisecond)) |
| 8539 |
if err == nil { |
| 8540 |
t.Fatal("Unexpected success") |
| 8541 |
} |
| 8542 |
if !errors.Is(err, nats.ErrBadSubscription) { |
| 8543 |
t.Fatalf("Unexpected error: %v", err) |
| 8544 |
} |
| 8545 |
}) |
| 8546 |
|
| 8547 |
t.Run("max waiting exceeded", func(t *testing.T) { |
| 8548 |
defer js.PurgeStream(subject) |
| 8549 |
|
| 8550 |
_, err := js.AddConsumer(subject, &nats.ConsumerConfig{ |
| 8551 |
Durable: "max-waiting", |
| 8552 |
MaxWaiting: 2, |
| 8553 |
AckPolicy: nats.AckExplicitPolicy, |
| 8554 |
}) |
| 8555 |
if err != nil { |
| 8556 |
t.Fatal(err) |
| 8557 |
} |
| 8558 |
|
| 8559 |
var wg sync.WaitGroup |
| 8560 |
wg.Add(2) |
| 8561 |
for range 2 { |
| 8562 |
go func() { |
| 8563 |
defer wg.Done() |
| 8564 |
|
| 8565 |
sub, err := js.PullSubscribe(subject, "max-waiting") |
| 8566 |
if err != nil { |
| 8567 |
return |
| 8568 |
} |
| 8569 |
sub.Fetch(1, nats.MaxWait(time.Second)) |
| 8570 |
}() |
| 8571 |
} |
| 8572 |
|
| 8573 |
checkFor(t, time.Second, 15*time.Millisecond, func() error { |
| 8574 |
ci, err := js.ConsumerInfo(subject, "max-waiting") |
| 8575 |
if err != nil { |
| 8576 |
return err |
| 8577 |
} |
| 8578 |
if n := ci.NumWaiting; n != 2 { |
| 8579 |
return fmt.Errorf("NumWaiting should be 2, was %v", n) |
| 8580 |
} |
| 8581 |
return nil |
| 8582 |
}) |
| 8583 |
|
| 8584 |
sub, err := js.PullSubscribe(subject, "max-waiting") |
| 8585 |
if err != nil { |
| 8586 |
t.Fatal(err) |
| 8587 |
} |
| 8588 |
_, err = sub.Fetch(1, nats.MaxWait(time.Second)) |
| 8589 |
if err == nil || !strings.Contains(err.Error(), "MaxWaiting") { |
| 8590 |
t.Fatalf("Unexpected error: %v", err) |
| 8591 |
} |
| 8592 |
wg.Wait() |
| 8593 |
}) |
| 8594 |
|
| 8595 |
t.Run("no wait", func(t *testing.T) { |
| 8596 |
defer js.PurgeStream(subject) |
| 8597 |
|
| 8598 |
expected := 10 |
| 8599 |
sendMsgs(t, expected) |
| 8600 |
sub, err := js.PullSubscribe(subject, "no-wait") |
| 8601 |
if err != nil { |
| 8602 |
t.Fatal(err) |
| 8603 |
} |
| 8604 |
defer sub.Unsubscribe() |
| 8605 |
|
| 8606 |
ctx, done := context.WithTimeout(context.Background(), 5*time.Second) |
| 8607 |
defer done() |
| 8608 |
recvd := make([]*nats.Msg, 0) |
| 8609 |
|
| 8610 |
Loop: |
| 8611 |
for range time.NewTicker(100 * time.Millisecond).C { |
| 8612 |
select { |
| 8613 |
case <-ctx.Done(): |
| 8614 |
break Loop |
| 8615 |
default: |
| 8616 |
} |
| 8617 |
|
| 8618 |
msgs, err := sub.Fetch(1, nats.MaxWait(250*time.Millisecond)) |
| 8619 |
if err != nil { |
| 8620 |
t.Fatalf("Unexpected error: %v", err) |
| 8621 |
} |
| 8622 |
recvd = append(recvd, msgs[0]) |
| 8623 |
|
| 8624 |
for _, msg := range msgs { |
| 8625 |
if err := msg.AckSync(); err != nil { |
| 8626 |
t.Error(err) |
| 8627 |
} |
| 8628 |
} |
| 8629 |
|
| 8630 |
if len(recvd) == expected { |
| 8631 |
done() |
| 8632 |
break |
| 8633 |
} |
| 8634 |
} |
| 8635 |
|
| 8636 |
got := len(recvd) |
| 8637 |
if got != expected { |
| 8638 |
t.Fatalf("Got %v messages, expected at least: %v", got, expected) |
| 8639 |
} |
| 8640 |
|
| 8641 |
msgs, err := sub.Fetch(expected, nats.MaxWait(2*time.Second)) |
| 8642 |
if err == nil { |
| 8643 |
t.Fatal("Unexpected success", len(msgs)) |
| 8644 |
} |
| 8645 |
if err != nats.ErrTimeout { |
| 8646 |
t.Fatalf("Expected timeout error, got: %v", err) |
| 8647 |
} |
| 8648 |
}) |
| 8649 |
} |
| 8650 |
|
| 8651 |
func TestJetStreamPublishAsync(t *testing.T) { |
| 8652 |
withJSServerInstance(t, func(t *testing.T, nc *nats.Conn, _ *testservice.Instance) { |
| 8653 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 8654 |
if err != nil { |
| 8655 |
t.Fatalf("Unexpected error: %v", err) |
| 8656 |
} |
| 8657 |
|
| 8658 |
paf, err := js.PublishAsync("foo", []byte("Hello JS")) |
| 8659 |
if err != nil { |
| 8660 |
t.Fatalf("Unexpected error: %v", err) |
| 8661 |
} |
| 8662 |
|
| 8663 |
select { |
| 8664 |
case <-paf.Ok(): |
| 8665 |
t.Fatalf("Did not expect to get PubAck") |
| 8666 |
case err := <-paf.Err(): |
| 8667 |
if err != nats.ErrNoResponders { |
| 8668 |
t.Fatalf("Expected a ErrNoResponders error, got %v", err) |
| 8669 |
} |
| 8670 |
m := paf.Msg() |
| 8671 |
if m == nil { |
| 8672 |
t.Fatalf("Expected to be able to retrieve the message") |
| 8673 |
} |
| 8674 |
if m.Subject != "foo" || string(m.Data) != "Hello JS" { |
| 8675 |
t.Fatalf("Wrong message: %+v", m) |
| 8676 |
} |
| 8677 |
case <-time.After(time.Second): |
| 8678 |
t.Fatalf("Did not receive an error in time") |
| 8679 |
} |
| 8680 |
|
| 8681 |
if _, err := js.AddStream(&nats.StreamConfig{Name: "TEST"}); err != nil { |
| 8682 |
t.Fatalf("Unexpected error: %v", err) |
| 8683 |
} |
| 8684 |
|
| 8685 |
paf, err = js.PublishAsync("TEST", []byte("Hello JS ASYNC PUB")) |
| 8686 |
if err != nil { |
| 8687 |
t.Fatalf("Unexpected error: %v", err) |
| 8688 |
} |
| 8689 |
|
| 8690 |
select { |
| 8691 |
case pa := <-paf.Ok(): |
| 8692 |
if pa.Stream != "TEST" || pa.Sequence != 1 { |
| 8693 |
t.Fatalf("Bad PubAck: %+v", pa) |
| 8694 |
} |
| 8695 |
case err := <-paf.Err(): |
| 8696 |
t.Fatalf("Did not expect to get an error: %v", err) |
| 8697 |
case <-time.After(time.Second): |
| 8698 |
t.Fatalf("Did not receive a PubAck in time") |
| 8699 |
} |
| 8700 |
|
| 8701 |
errCh := make(chan error, 1) |
| 8702 |
|
| 8703 |
errHandler := func(js nats.JetStream, originalMsg *nats.Msg, err error) { |
| 8704 |
if originalMsg == nil { |
| 8705 |
t.Fatalf("Expected non-nil original message") |
| 8706 |
} |
| 8707 |
errCh <- err |
| 8708 |
} |
| 8709 |
|
| 8710 |
js, err = nc.JetStream(nats.PublishAsyncErrHandler(errHandler)) |
| 8711 |
if err != nil { |
| 8712 |
t.Fatalf("Unexpected error: %v", err) |
| 8713 |
} |
| 8714 |
|
| 8715 |
if _, err = js.PublishAsync("bar", []byte("Hello JS ASYNC PUB")); err != nil { |
| 8716 |
t.Fatalf("Unexpected error: %v", err) |
| 8717 |
} |
| 8718 |
|
| 8719 |
select { |
| 8720 |
case err := <-errCh: |
| 8721 |
if err != nats.ErrNoResponders { |
| 8722 |
t.Fatalf("Expected a ErrNoResponders error, got %v", err) |
| 8723 |
} |
| 8724 |
case <-time.After(time.Second): |
| 8725 |
t.Fatalf("Did not receive an async err in time") |
| 8726 |
} |
| 8727 |
|
| 8728 |
js, err = nc.JetStream(nats.PublishAsyncMaxPending(10)) |
| 8729 |
if err != nil { |
| 8730 |
t.Fatalf("Unexpected error: %v", err) |
| 8731 |
} |
| 8732 |
|
| 8733 |
for range 100 { |
| 8734 |
if _, err = js.PublishAsync("bar", []byte("Hello JS ASYNC PUB")); err != nil { |
| 8735 |
t.Fatalf("Unexpected error: %v", err) |
| 8736 |
} |
| 8737 |
if np := js.PublishAsyncPending(); np > 10 { |
| 8738 |
t.Fatalf("Expected num pending to not exceed 10, got %d", np) |
| 8739 |
} |
| 8740 |
} |
| 8741 |
|
| 8742 |
js, err = nc.JetStream(nats.PublishAsyncMaxPending(10)) |
| 8743 |
if err != nil { |
| 8744 |
t.Fatalf("Unexpected error: %v", err) |
| 8745 |
} |
| 8746 |
|
| 8747 |
for range 500 { |
| 8748 |
if _, err = js.PublishAsync("bar", []byte("Hello JS ASYNC PUB")); err != nil { |
| 8749 |
t.Fatalf("Unexpected error: %v", err) |
| 8750 |
} |
| 8751 |
} |
| 8752 |
|
| 8753 |
select { |
| 8754 |
case <-js.PublishAsyncComplete(): |
| 8755 |
case <-time.After(5 * time.Second): |
| 8756 |
t.Fatalf("Did not receive completion signal") |
| 8757 |
} |
| 8758 |
|
| 8759 |
_, err = js.PublishAsync("foo", []byte("Bad"), nats.StallWait(0)) |
| 8760 |
expectedErr := "nats: stall wait should be more than 0" |
| 8761 |
if err == nil || err.Error() != expectedErr { |
| 8762 |
t.Errorf("Expected %v, got: %v", expectedErr, err) |
| 8763 |
} |
| 8764 |
|
| 8765 |
_, err = js.Publish("foo", []byte("Also bad"), nats.StallWait(200*time.Millisecond)) |
| 8766 |
expectedErr = "nats: stall wait cannot be set to sync publish" |
| 8767 |
if err == nil || err.Error() != expectedErr { |
| 8768 |
t.Errorf("Expected %v, got: %v", expectedErr, err) |
| 8769 |
} |
| 8770 |
}) |
| 8771 |
|
| 8772 |
// CustomInboxPrefix variant in its own scope so the connection is opened |
| 8773 |
// with the custom prefix from the start. |
| 8774 |
withJSServerInstance(t, func(t *testing.T, _ *nats.Conn, inst *testservice.Instance) { |
| 8775 |
nc := dialInstance(t, inst, nats.CustomInboxPrefix("_BOX")) |
| 8776 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 8777 |
if err != nil { |
| 8778 |
t.Fatalf("Unexpected error: %v", err) |
| 8779 |
} |
| 8780 |
|
| 8781 |
paf, err := js.PublishAsync("foo", []byte("Hello JS with Custom Inbox")) |
| 8782 |
if err != nil { |
| 8783 |
t.Fatalf("Unexpected error: %v", err) |
| 8784 |
} |
| 8785 |
|
| 8786 |
select { |
| 8787 |
case <-paf.Ok(): |
| 8788 |
t.Fatalf("Did not expect to get PubAck") |
| 8789 |
case err := <-paf.Err(): |
| 8790 |
if err != nats.ErrNoResponders { |
| 8791 |
t.Fatalf("Expected a ErrNoResponders error, got %v", err) |
| 8792 |
} |
| 8793 |
m := paf.Msg() |
| 8794 |
if m == nil { |
| 8795 |
t.Fatalf("Expected to be able to retrieve the message") |
| 8796 |
} |
| 8797 |
if m.Subject != "foo" || string(m.Data) != "Hello JS with Custom Inbox" { |
| 8798 |
t.Fatalf("Wrong message: %+v", m) |
| 8799 |
} |
| 8800 |
case <-time.After(time.Second): |
| 8801 |
t.Fatalf("Did not receive an error in time") |
| 8802 |
} |
| 8803 |
}) |
| 8804 |
} |
| 8805 |
|
| 8806 |
func TestPublishAsyncResetPendingOnReconnect(t *testing.T) { |
| 8807 |
withJSServerInstance(t, func(t *testing.T, nc *nats.Conn, inst *testservice.Instance) { |
| 8808 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 8809 |
if err != nil { |
| 8810 |
t.Fatalf("Unexpected error: %v", err) |
| 8811 |
} |
| 8812 |
|
| 8813 |
if _, err := js.AddStream(&nats.StreamConfig{Name: "TEST", Subjects: []string{"FOO"}}); err != nil { |
| 8814 |
t.Fatalf("Unexpected error: %v", err) |
| 8815 |
} |
| 8816 |
|
| 8817 |
errs := make(chan error, 1) |
| 8818 |
doneCh := make(chan struct{}, 1) |
| 8819 |
acks := make(chan nats.PubAckFuture, 100) |
| 8820 |
go func() { |
| 8821 |
for range 100 { |
| 8822 |
if ack, err := js.PublishAsync("FOO", []byte("hello")); err != nil { |
| 8823 |
errs <- err |
| 8824 |
return |
| 8825 |
} else { |
| 8826 |
acks <- ack |
| 8827 |
} |
| 8828 |
} |
| 8829 |
close(acks) |
| 8830 |
doneCh <- struct{}{} |
| 8831 |
}() |
| 8832 |
select { |
| 8833 |
case <-doneCh: |
| 8834 |
case err := <-errs: |
| 8835 |
t.Fatalf("Unexpected error during publish: %v", err) |
| 8836 |
case <-time.After(5 * time.Second): |
| 8837 |
t.Fatalf("Did not receive completion signal") |
| 8838 |
} |
| 8839 |
inst.StopServer(t, inst.Servers[0]) |
| 8840 |
time.Sleep(100 * time.Millisecond) |
| 8841 |
if pending := js.PublishAsyncPending(); pending != 0 { |
| 8842 |
t.Fatalf("Expected no pending messages after server shutdown; got: %d", pending) |
| 8843 |
} |
| 8844 |
inst.StartServer(t, inst.Servers[0]) |
| 8845 |
|
| 8846 |
for ack := range acks { |
| 8847 |
select { |
| 8848 |
case <-ack.Ok(): |
| 8849 |
case err := <-ack.Err(): |
| 8850 |
if !errors.Is(err, nats.ErrDisconnected) && !errors.Is(err, nats.ErrNoResponders) { |
| 8851 |
t.Fatalf("Expected error: %v or %v; got: %v", nats.ErrDisconnected, nats.ErrNoResponders, err) |
| 8852 |
} |
| 8853 |
case <-time.After(5 * time.Second): |
| 8854 |
t.Fatalf("Did not receive completion signal") |
| 8855 |
} |
| 8856 |
} |
| 8857 |
}) |
| 8858 |
} |
| 8859 |
|
| 8860 |
func TestPublishAsyncAckTimeout(t *testing.T) { |
| 8861 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 8862 |
errs := make(chan error, 1) |
| 8863 |
js, err := nc.JetStream( |
| 8864 |
nats.PublishAsyncTimeout(50*time.Millisecond), |
| 8865 |
nats.PublishAsyncErrHandler(func(js nats.JetStream, m *nats.Msg, e error) { |
| 8866 |
errs <- e |
| 8867 |
}), |
| 8868 |
) |
| 8869 |
if err != nil { |
| 8870 |
t.Fatalf("Unexpected error: %v", err) |
| 8871 |
} |
| 8872 |
|
| 8873 |
_, err = js.AddStream(&nats.StreamConfig{Name: "foo", Subjects: []string{"FOO.*"}, NoAck: true}) |
| 8874 |
if err != nil { |
| 8875 |
t.Fatalf("Unexpected error: %v", err) |
| 8876 |
} |
| 8877 |
|
| 8878 |
ack, err := js.PublishAsync("FOO.A", []byte("hello")) |
| 8879 |
if err != nil { |
| 8880 |
t.Fatalf("Unexpected error: %v", err) |
| 8881 |
} |
| 8882 |
|
| 8883 |
select { |
| 8884 |
case <-ack.Ok(): |
| 8885 |
t.Fatalf("Expected timeout") |
| 8886 |
case err := <-ack.Err(): |
| 8887 |
if !errors.Is(err, nats.ErrAsyncPublishTimeout) { |
| 8888 |
t.Fatalf("Expected error: %v; got: %v", nats.ErrAsyncPublishTimeout, err) |
| 8889 |
} |
| 8890 |
case <-time.After(time.Second): |
| 8891 |
t.Fatalf("Did not receive ack timeout") |
| 8892 |
} |
| 8893 |
|
| 8894 |
select { |
| 8895 |
case err := <-errs: |
| 8896 |
if !errors.Is(err, nats.ErrAsyncPublishTimeout) { |
| 8897 |
t.Fatalf("Expected error: %v; got: %v", nats.ErrAsyncPublishTimeout, err) |
| 8898 |
} |
| 8899 |
case <-time.After(time.Second): |
| 8900 |
t.Fatalf("Did not receive error from error handler") |
| 8901 |
} |
| 8902 |
|
| 8903 |
if js.PublishAsyncPending() != 0 { |
| 8904 |
t.Fatalf("Expected no pending messages") |
| 8905 |
} |
| 8906 |
|
| 8907 |
select { |
| 8908 |
case <-js.PublishAsyncComplete(): |
| 8909 |
case <-time.After(100 * time.Millisecond): |
| 8910 |
t.Fatalf("Did not receive completion signal") |
| 8911 |
} |
| 8912 |
}) |
| 8913 |
} |
| 8914 |
|
| 8915 |
func TestPublishAsyncClearStall(t *testing.T) { |
| 8916 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 8917 |
js, err := nc.JetStream( |
| 8918 |
nats.PublishAsyncTimeout(500*time.Millisecond), |
| 8919 |
nats.PublishAsyncMaxPending(100)) |
| 8920 |
if err != nil { |
| 8921 |
t.Fatalf("Unexpected error: %v", err) |
| 8922 |
} |
| 8923 |
|
| 8924 |
_, err = js.AddStream(&nats.StreamConfig{Name: "foo", Subjects: []string{"FOO.*"}, NoAck: true}) |
| 8925 |
if err != nil { |
| 8926 |
t.Fatalf("Unexpected error: %v", err) |
| 8927 |
} |
| 8928 |
|
| 8929 |
for range 100 { |
| 8930 |
_, err := js.PublishAsync("FOO.A", []byte("hello"), nats.StallWait(1*time.Nanosecond)) |
| 8931 |
if err != nil { |
| 8932 |
t.Fatalf("Unexpected error: %v", err) |
| 8933 |
} |
| 8934 |
} |
| 8935 |
_, err = js.PublishAsync("FOO.A", []byte("hello"), nats.StallWait(50*time.Millisecond)) |
| 8936 |
if !errors.Is(err, nats.ErrTooManyStalledMsgs) { |
| 8937 |
t.Fatalf("Expected error: %v; got: %v", nats.ErrTooManyStalledMsgs, err) |
| 8938 |
} |
| 8939 |
|
| 8940 |
select { |
| 8941 |
case <-js.PublishAsyncComplete(): |
| 8942 |
case <-time.After(2 * time.Second): |
| 8943 |
t.Fatalf("Did not receive completion signal") |
| 8944 |
} |
| 8945 |
|
| 8946 |
if _, err = js.PublishAsync("FOO.A", []byte("hello")); err != nil { |
| 8947 |
t.Fatalf("Unexpected error: %v", err) |
| 8948 |
} |
| 8949 |
if js.PublishAsyncPending() != 1 { |
| 8950 |
t.Fatalf("Expected 1 pending message; got: %d", js.PublishAsyncPending()) |
| 8951 |
} |
| 8952 |
}) |
| 8953 |
} |
| 8954 |
|
| 8955 |
func TestPublishAsyncRetryInErrHandler(t *testing.T) { |
| 8956 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 8957 |
streamCreated := make(chan struct{}) |
| 8958 |
errCB := func(js nats.JetStream, m *nats.Msg, e error) { |
| 8959 |
<-streamCreated |
| 8960 |
_, err := js.PublishMsgAsync(m) |
| 8961 |
if err != nil { |
| 8962 |
t.Fatalf("Unexpected error when republishing: %v", err) |
| 8963 |
} |
| 8964 |
} |
| 8965 |
|
| 8966 |
js, err := nc.JetStream(nats.PublishAsyncErrHandler(errCB)) |
| 8967 |
if err != nil { |
| 8968 |
t.Fatalf("Unexpected error: %v", err) |
| 8969 |
} |
| 8970 |
|
| 8971 |
errs := make(chan error, 1) |
| 8972 |
doneCh := make(chan struct{}, 1) |
| 8973 |
go func() { |
| 8974 |
for range 10 { |
| 8975 |
if _, err := js.PublishAsync("FOO.A", []byte("hello")); err != nil { |
| 8976 |
errs <- err |
| 8977 |
return |
| 8978 |
} |
| 8979 |
} |
| 8980 |
doneCh <- struct{}{} |
| 8981 |
}() |
| 8982 |
select { |
| 8983 |
case <-doneCh: |
| 8984 |
case err := <-errs: |
| 8985 |
t.Fatalf("Unexpected error during publish: %v", err) |
| 8986 |
case <-time.After(5 * time.Second): |
| 8987 |
t.Fatalf("Did not receive completion signal") |
| 8988 |
} |
| 8989 |
_, err = js.AddStream(&nats.StreamConfig{Name: "foo", Subjects: []string{"FOO.*"}}) |
| 8990 |
if err != nil { |
| 8991 |
t.Fatalf("Unexpected error: %v", err) |
| 8992 |
} |
| 8993 |
|
| 8994 |
close(streamCreated) |
| 8995 |
select { |
| 8996 |
case <-js.PublishAsyncComplete(): |
| 8997 |
case <-time.After(5 * time.Second): |
| 8998 |
t.Fatalf("Did not receive completion signal") |
| 8999 |
} |
| 9000 |
|
| 9001 |
info, err := js.StreamInfo("foo") |
| 9002 |
if err != nil { |
| 9003 |
t.Fatalf("Unexpected error: %v", err) |
| 9004 |
} |
| 9005 |
|
| 9006 |
if info.State.Msgs != 10 { |
| 9007 |
t.Fatalf("Expected 10 messages in the stream; got: %d", info.State.Msgs) |
| 9008 |
} |
| 9009 |
}) |
| 9010 |
} |
| 9011 |
|
| 9012 |
func TestJetStreamPublishAsyncPerf(t *testing.T) { |
| 9013 |
t.SkipNow() |
| 9014 |
|
| 9015 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 9016 |
// 64 byte payload. |
| 9017 |
msg := make([]byte, 64) |
| 9018 |
rand.Read(msg) |
| 9019 |
|
| 9020 |
var asyncErrs uint32 |
| 9021 |
errHandler := func(js nats.JetStream, originalMsg *nats.Msg, err error) { |
| 9022 |
t.Logf("Got an async err: %v", err) |
| 9023 |
atomic.AddUint32(&asyncErrs, 1) |
| 9024 |
} |
| 9025 |
|
| 9026 |
js, err := nc.JetStream( |
| 9027 |
nats.PublishAsyncErrHandler(errHandler), |
| 9028 |
nats.PublishAsyncMaxPending(256), |
| 9029 |
) |
| 9030 |
if err != nil { |
| 9031 |
t.Fatalf("Unexpected error: %v", err) |
| 9032 |
} |
| 9033 |
|
| 9034 |
if _, err := js.AddStream(&nats.StreamConfig{Name: "B"}); err != nil { |
| 9035 |
t.Fatalf("Unexpected error: %v", err) |
| 9036 |
} |
| 9037 |
|
| 9038 |
toSend := 1000000 |
| 9039 |
start := time.Now() |
| 9040 |
for range toSend { |
| 9041 |
if _, err = js.PublishAsync("B", msg); err != nil { |
| 9042 |
t.Fatalf("Unexpected error: %v", err) |
| 9043 |
} |
| 9044 |
} |
| 9045 |
|
| 9046 |
select { |
| 9047 |
case <-js.PublishAsyncComplete(): |
| 9048 |
if ne := atomic.LoadUint32(&asyncErrs); ne > 0 { |
| 9049 |
t.Fatalf("Got unexpected errors publishing") |
| 9050 |
} |
| 9051 |
case <-time.After(5 * time.Second): |
| 9052 |
t.Fatalf("Did not receive completion signal") |
| 9053 |
} |
| 9054 |
|
| 9055 |
tt := time.Since(start) |
| 9056 |
fmt.Printf("Took %v to send %d msgs\n", tt, toSend) |
| 9057 |
fmt.Printf("%.0f msgs/sec\n\n", float64(toSend)/tt.Seconds()) |
| 9058 |
}) |
| 9059 |
} |
| 9060 |
|
| 9061 |
func TestPublishAsyncRetry(t *testing.T) { |
| 9062 |
tests := []struct { |
| 9063 |
name string |
| 9064 |
pubOpts []nats.PubOpt |
| 9065 |
ackError error |
| 9066 |
pubErr error |
| 9067 |
}{ |
| 9068 |
{ |
| 9069 |
name: "retry until stream is ready", |
| 9070 |
pubOpts: []nats.PubOpt{ |
| 9071 |
nats.RetryAttempts(10), |
| 9072 |
nats.RetryWait(100 * time.Millisecond), |
| 9073 |
}, |
| 9074 |
}, |
| 9075 |
{ |
| 9076 |
name: "fail after max retries", |
| 9077 |
pubOpts: []nats.PubOpt{ |
| 9078 |
nats.RetryAttempts(2), |
| 9079 |
nats.RetryWait(50 * time.Millisecond), |
| 9080 |
}, |
| 9081 |
ackError: nats.ErrNoResponders, |
| 9082 |
}, |
| 9083 |
{ |
| 9084 |
name: "no retries", |
| 9085 |
pubOpts: nil, |
| 9086 |
ackError: nats.ErrNoResponders, |
| 9087 |
}, |
| 9088 |
{ |
| 9089 |
name: "invalid retry attempts", |
| 9090 |
pubOpts: []nats.PubOpt{ |
| 9091 |
nats.RetryAttempts(-1), |
| 9092 |
}, |
| 9093 |
pubErr: nats.ErrInvalidArg, |
| 9094 |
}, |
| 9095 |
} |
| 9096 |
|
| 9097 |
for _, test := range tests { |
| 9098 |
t.Run(test.name, func(t *testing.T) { |
| 9099 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 9100 |
js, err := nc.JetStream(nats.PublishAsyncMaxPending(1)) |
| 9101 |
if err != nil { |
| 9102 |
t.Fatalf("Unexpected error: %v", err) |
| 9103 |
} |
| 9104 |
|
| 9105 |
test.pubOpts = append(test.pubOpts, nats.StallWait(1*time.Nanosecond)) |
| 9106 |
ack, err := js.PublishAsync("foo", []byte("hello"), test.pubOpts...) |
| 9107 |
if !errors.Is(err, test.pubErr) { |
| 9108 |
t.Fatalf("Expected error: %v; got: %v", test.pubErr, err) |
| 9109 |
} |
| 9110 |
if err != nil { |
| 9111 |
return |
| 9112 |
} |
| 9113 |
errs := make(chan error, 1) |
| 9114 |
go func() { |
| 9115 |
time.Sleep(300 * time.Millisecond) |
| 9116 |
if _, err := js.AddStream(&nats.StreamConfig{Name: "TEST", Subjects: []string{"foo"}}); err != nil { |
| 9117 |
errs <- err |
| 9118 |
} |
| 9119 |
}() |
| 9120 |
select { |
| 9121 |
case <-ack.Ok(): |
| 9122 |
case err := <-ack.Err(): |
| 9123 |
if test.ackError != nil { |
| 9124 |
if !errors.Is(err, test.ackError) { |
| 9125 |
t.Fatalf("Expected error: %v; got: %v", test.ackError, err) |
| 9126 |
} |
| 9127 |
} else { |
| 9128 |
t.Fatalf("Unexpected ack error: %v", err) |
| 9129 |
} |
| 9130 |
case err := <-errs: |
| 9131 |
t.Fatalf("Error creating stream: %v", err) |
| 9132 |
case <-time.After(5 * time.Second): |
| 9133 |
t.Fatalf("Timeout waiting for ack") |
| 9134 |
} |
| 9135 |
}) |
| 9136 |
}) |
| 9137 |
} |
| 9138 |
} |
| 9139 |
|
| 9140 |
func TestJetStreamCleanupPublisher(t *testing.T) { |
| 9141 |
t.Run("cleanup js publisher", func(t *testing.T) { |
| 9142 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 9143 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 9144 |
if err != nil { |
| 9145 |
t.Fatalf("Unexpected error: %v", err) |
| 9146 |
} |
| 9147 |
|
| 9148 |
if _, err := js.AddStream(&nats.StreamConfig{Name: "TEST", Subjects: []string{"FOO"}}); err != nil { |
| 9149 |
t.Fatalf("Unexpected error: %v", err) |
| 9150 |
} |
| 9151 |
|
| 9152 |
numSubs := nc.NumSubscriptions() |
| 9153 |
if _, err := js.PublishAsync("FOO", []byte("hello")); err != nil { |
| 9154 |
t.Fatalf("Unexpected error: %v", err) |
| 9155 |
} |
| 9156 |
select { |
| 9157 |
case <-js.PublishAsyncComplete(): |
| 9158 |
case <-time.After(5 * time.Second): |
| 9159 |
t.Fatalf("Did not receive completion signal") |
| 9160 |
} |
| 9161 |
|
| 9162 |
if numSubs+1 != nc.NumSubscriptions() { |
| 9163 |
t.Fatalf("Expected an additional subscription after publish, got %d", nc.NumSubscriptions()) |
| 9164 |
} |
| 9165 |
|
| 9166 |
js.CleanupPublisher() |
| 9167 |
|
| 9168 |
if numSubs != nc.NumSubscriptions() { |
| 9169 |
t.Fatalf("Expected subscriptions to be back to original count") |
| 9170 |
} |
| 9171 |
}) |
| 9172 |
}) |
| 9173 |
|
| 9174 |
t.Run("cleanup js publisher, cancel pending acks", func(t *testing.T) { |
| 9175 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 9176 |
cbErr := make(chan error, 10) |
| 9177 |
js, err := nc.JetStream(nats.PublishAsyncErrHandler(func(js nats.JetStream, m *nats.Msg, err error) { |
| 9178 |
cbErr <- err |
| 9179 |
})) |
| 9180 |
if err != nil { |
| 9181 |
t.Fatalf("Unexpected error: %v", err) |
| 9182 |
} |
| 9183 |
|
| 9184 |
if _, err := js.AddStream(&nats.StreamConfig{Name: "TEST", Subjects: []string{"FOO"}, NoAck: true}); err != nil { |
| 9185 |
t.Fatalf("Unexpected error: %v", err) |
| 9186 |
} |
| 9187 |
|
| 9188 |
numSubs := nc.NumSubscriptions() |
| 9189 |
|
| 9190 |
var acks []nats.PubAckFuture |
| 9191 |
for range 10 { |
| 9192 |
ack, err := js.PublishAsync("FOO", []byte("hello")) |
| 9193 |
if err != nil { |
| 9194 |
t.Fatalf("Unexpected error: %v", err) |
| 9195 |
} |
| 9196 |
acks = append(acks, ack) |
| 9197 |
} |
| 9198 |
|
| 9199 |
asyncComplete := js.PublishAsyncComplete() |
| 9200 |
select { |
| 9201 |
case <-asyncComplete: |
| 9202 |
t.Fatalf("Should not complete, NoAck is set") |
| 9203 |
case <-time.After(200 * time.Millisecond): |
| 9204 |
} |
| 9205 |
|
| 9206 |
if numSubs+1 != nc.NumSubscriptions() { |
| 9207 |
t.Fatalf("Expected an additional subscription after publish, got %d", nc.NumSubscriptions()) |
| 9208 |
} |
| 9209 |
|
| 9210 |
js.CleanupPublisher() |
| 9211 |
|
| 9212 |
if numSubs != nc.NumSubscriptions() { |
| 9213 |
t.Fatalf("Expected subscriptions to be back to original count") |
| 9214 |
} |
| 9215 |
|
| 9216 |
select { |
| 9217 |
case <-asyncComplete: |
| 9218 |
case <-time.After(5 * time.Second): |
| 9219 |
t.Fatalf("Did not receive completion signal") |
| 9220 |
} |
| 9221 |
|
| 9222 |
for _, ack := range acks { |
| 9223 |
select { |
| 9224 |
case err := <-ack.Err(): |
| 9225 |
if !errors.Is(err, nats.ErrJetStreamPublisherClosed) { |
| 9226 |
t.Fatalf("Expected JetStreamContextClosed error, got %v", err) |
| 9227 |
} |
| 9228 |
case <-ack.Ok(): |
| 9229 |
t.Fatalf("Expected error on the ack future") |
| 9230 |
case <-time.After(200 * time.Millisecond): |
| 9231 |
t.Fatalf("Expected an error on the ack future") |
| 9232 |
} |
| 9233 |
} |
| 9234 |
|
| 9235 |
for range 10 { |
| 9236 |
select { |
| 9237 |
case err := <-cbErr: |
| 9238 |
if !errors.Is(err, nats.ErrJetStreamPublisherClosed) { |
| 9239 |
t.Fatalf("Expected JetStreamContextClosed error, got %v", err) |
| 9240 |
} |
| 9241 |
case <-time.After(200 * time.Millisecond): |
| 9242 |
t.Fatalf("Expected errors to be passed from the async handler") |
| 9243 |
} |
| 9244 |
} |
| 9245 |
}) |
| 9246 |
}) |
| 9247 |
} |
| 9248 |
|
| 9249 |
func TestJetStreamPublishExpectZero(t *testing.T) { |
| 9250 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 9251 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 9252 |
if err != nil { |
| 9253 |
t.Fatalf("Unexpected error: %v", err) |
| 9254 |
} |
| 9255 |
|
| 9256 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 9257 |
Name: "TEST", |
| 9258 |
Subjects: []string{"test", "foo", "bar"}, |
| 9259 |
}) |
| 9260 |
if err != nil { |
| 9261 |
t.Fatalf("Unexpected error: %v", err) |
| 9262 |
} |
| 9263 |
|
| 9264 |
sub, err := nc.SubscribeSync("foo") |
| 9265 |
if err != nil { |
| 9266 |
t.Errorf("Error: %s", err) |
| 9267 |
} |
| 9268 |
|
| 9269 |
_, err = js.Publish("foo", []byte("bar"), |
| 9270 |
nats.ExpectLastSequence(0), |
| 9271 |
nats.ExpectLastSequencePerSubject(0), |
| 9272 |
) |
| 9273 |
if err != nil { |
| 9274 |
t.Errorf("Error: %v", err) |
| 9275 |
} |
| 9276 |
|
| 9277 |
rawMsg, err := js.GetMsg("TEST", 1) |
| 9278 |
if err != nil { |
| 9279 |
t.Fatalf("Error: %s", err) |
| 9280 |
} |
| 9281 |
hdr, ok := rawMsg.Header["Nats-Expected-Last-Sequence"] |
| 9282 |
if !ok { |
| 9283 |
t.Fatal("Missing header") |
| 9284 |
} |
| 9285 |
got := hdr[0] |
| 9286 |
expected := "0" |
| 9287 |
if got != expected { |
| 9288 |
t.Fatalf("Expected %v, got: %v", expected, got) |
| 9289 |
} |
| 9290 |
hdr, ok = rawMsg.Header["Nats-Expected-Last-Subject-Sequence"] |
| 9291 |
if !ok { |
| 9292 |
t.Fatal("Missing header") |
| 9293 |
} |
| 9294 |
got = hdr[0] |
| 9295 |
expected = "0" |
| 9296 |
if got != expected { |
| 9297 |
t.Fatalf("Expected %v, got: %v", expected, got) |
| 9298 |
} |
| 9299 |
|
| 9300 |
msg, err := sub.NextMsg(1 * time.Second) |
| 9301 |
if err != nil { |
| 9302 |
t.Fatalf("Error: %s", err) |
| 9303 |
} |
| 9304 |
hdr, ok = msg.Header["Nats-Expected-Last-Sequence"] |
| 9305 |
if !ok { |
| 9306 |
t.Fatal("Missing header") |
| 9307 |
} |
| 9308 |
got = hdr[0] |
| 9309 |
expected = "0" |
| 9310 |
if got != expected { |
| 9311 |
t.Fatalf("Expected %v, got: %v", expected, got) |
| 9312 |
} |
| 9313 |
hdr, ok = msg.Header["Nats-Expected-Last-Subject-Sequence"] |
| 9314 |
if !ok { |
| 9315 |
t.Fatal("Missing header") |
| 9316 |
} |
| 9317 |
got = hdr[0] |
| 9318 |
expected = "0" |
| 9319 |
if got != expected { |
| 9320 |
t.Fatalf("Expected %v, got: %v", expected, got) |
| 9321 |
} |
| 9322 |
}) |
| 9323 |
} |
| 9324 |
|
| 9325 |
func TestJetStreamBindConsumer(t *testing.T) { |
| 9326 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 9327 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 9328 |
if err != nil { |
| 9329 |
t.Fatalf("Unexpected error: %v", err) |
| 9330 |
} |
| 9331 |
|
| 9332 |
if _, err := js.AddStream(nil); err == nil { |
| 9333 |
t.Fatalf("Unexpected success") |
| 9334 |
} |
| 9335 |
si, err := js.AddStream(&nats.StreamConfig{Name: "foo"}) |
| 9336 |
if err != nil { |
| 9337 |
t.Fatalf("Unexpected error: %v", err) |
| 9338 |
} |
| 9339 |
if si == nil || si.Config.Name != "foo" { |
| 9340 |
t.Fatalf("StreamInfo is not correct %+v", si) |
| 9341 |
} |
| 9342 |
|
| 9343 |
for range 25 { |
| 9344 |
js.Publish("foo", []byte("hi")) |
| 9345 |
} |
| 9346 |
|
| 9347 |
_, err = js.SubscribeSync("foo", nats.Bind("", "")) |
| 9348 |
if err != nats.ErrStreamNameRequired { |
| 9349 |
t.Fatalf("Unexpected error: %v", err) |
| 9350 |
} |
| 9351 |
_, err = js.SubscribeSync("foo", nats.Bind("foo", "")) |
| 9352 |
if err != nats.ErrConsumerNameRequired { |
| 9353 |
t.Fatalf("Unexpected error: %v", err) |
| 9354 |
} |
| 9355 |
_, err = js.SubscribeSync("foo", nats.Bind("foo", "push")) |
| 9356 |
if err == nil || !errors.Is(err, nats.ErrConsumerNotFound) { |
| 9357 |
t.Fatalf("Unexpected error: %v", err) |
| 9358 |
} |
| 9359 |
|
| 9360 |
_, err = js.PullSubscribe("foo", "pull", nats.Bind("foo", "pull")) |
| 9361 |
if err == nil || !errors.Is(err, nats.ErrConsumerNotFound) { |
| 9362 |
t.Fatalf("Unexpected error: %v", err) |
| 9363 |
} |
| 9364 |
|
| 9365 |
_, err = js.AddConsumer("foo", &nats.ConsumerConfig{ |
| 9366 |
Durable: "push", |
| 9367 |
AckPolicy: nats.AckExplicitPolicy, |
| 9368 |
DeliverSubject: nats.NewInbox(), |
| 9369 |
}) |
| 9370 |
if err != nil { |
| 9371 |
t.Fatalf("Unexpected error: %v", err) |
| 9372 |
} |
| 9373 |
|
| 9374 |
sub, err := js.SubscribeSync("foo", nats.Bind("foo", "push")) |
| 9375 |
if err != nil { |
| 9376 |
t.Fatal(err) |
| 9377 |
} |
| 9378 |
_, err = js.SubscribeSync("foo", nats.Durable("push2"), nats.Bind("foo", "push")) |
| 9379 |
if err == nil || !strings.Contains(err.Error(), `nats: duplicate consumer names (push2 and push)`) { |
| 9380 |
t.Fatalf("Unexpected error: %v", err) |
| 9381 |
} |
| 9382 |
_, err = js.SubscribeSync("foo", nats.BindStream("foo"), nats.Bind("foo2", "push")) |
| 9383 |
if err == nil || !strings.Contains(err.Error(), `nats: duplicate stream name (foo and foo2)`) { |
| 9384 |
t.Fatalf("Unexpected error: %v", err) |
| 9385 |
} |
| 9386 |
sub.Unsubscribe() |
| 9387 |
|
| 9388 |
checkConsInactive := func() { |
| 9389 |
t.Helper() |
| 9390 |
checkFor(t, time.Second, 15*time.Millisecond, func() error { |
| 9391 |
ci, _ := js.ConsumerInfo("foo", "push") |
| 9392 |
if ci != nil && !ci.PushBound { |
| 9393 |
return nil |
| 9394 |
} |
| 9395 |
return fmt.Errorf("Consumer %q still active", "push") |
| 9396 |
}) |
| 9397 |
} |
| 9398 |
checkConsInactive() |
| 9399 |
|
| 9400 |
sub, err = js.SubscribeSync("foo", nats.BindStream("foo"), nats.Bind("foo", "push")) |
| 9401 |
if err != nil { |
| 9402 |
t.Fatalf("Unexpected error: %v", err) |
| 9403 |
} |
| 9404 |
|
| 9405 |
_, err = js.SubscribeSync("foo", nats.Durable("push")) |
| 9406 |
if err == nil || !strings.Contains(err.Error(), "already bound") { |
| 9407 |
t.Fatalf("Unexpected error: %v", err) |
| 9408 |
} |
| 9409 |
_, err = js.QueueSubscribeSync("foo", "wq", nats.Durable("push")) |
| 9410 |
if err == nil || !strings.Contains(err.Error(), "without a deliver group") { |
| 9411 |
t.Fatalf("Unexpected error: %v", err) |
| 9412 |
} |
| 9413 |
sub.Unsubscribe() |
| 9414 |
checkConsInactive() |
| 9415 |
|
| 9416 |
_, err = js.QueueSubscribeSync("foo", "wq1", nats.Durable("qpush")) |
| 9417 |
if err != nil { |
| 9418 |
t.Fatalf("Unexpected error: %v", err) |
| 9419 |
} |
| 9420 |
_, err = js.SubscribeSync("foo", nats.Durable("qpush")) |
| 9421 |
if err == nil || !strings.Contains(err.Error(), "cannot create a subscription for a consumer with a deliver group") { |
| 9422 |
t.Fatalf("Unexpected error: %v", err) |
| 9423 |
} |
| 9424 |
_, err = js.QueueSubscribeSync("foo", "wq2", nats.Durable("qpush")) |
| 9425 |
if err == nil || !strings.Contains(err.Error(), "cannot create a queue subscription") { |
| 9426 |
t.Fatalf("Unexpected error: %v", err) |
| 9427 |
} |
| 9428 |
|
| 9429 |
_, err = js.AddConsumer("foo", &nats.ConsumerConfig{ |
| 9430 |
Durable: "pull", |
| 9431 |
AckPolicy: nats.AckExplicitPolicy, |
| 9432 |
}) |
| 9433 |
if err != nil { |
| 9434 |
t.Fatalf("Unexpected error: %v", err) |
| 9435 |
} |
| 9436 |
_, err = js.PullSubscribe("foo", "pull", nats.Bind("foo", "pull")) |
| 9437 |
if err != nil { |
| 9438 |
t.Fatal(err) |
| 9439 |
} |
| 9440 |
|
| 9441 |
_, err = js.PullSubscribe("foo", "push", nats.Bind("foo", "push")) |
| 9442 |
if err != nats.ErrPullSubscribeToPushConsumer { |
| 9443 |
t.Fatalf("Unexpected error: %v", err) |
| 9444 |
} |
| 9445 |
_, err = js.SubscribeSync("foo", nats.Bind("foo", "pull")) |
| 9446 |
if err != nats.ErrPullSubscribeRequired { |
| 9447 |
t.Fatalf("Unexpected error: %v", err) |
| 9448 |
} |
| 9449 |
|
| 9450 |
sub1, err := js.SubscribeSync("foo") |
| 9451 |
if err != nil { |
| 9452 |
t.Fatalf("Unexpected error: %v", err) |
| 9453 |
} |
| 9454 |
cinfo, err := sub1.ConsumerInfo() |
| 9455 |
if err != nil { |
| 9456 |
t.Fatal(err) |
| 9457 |
} |
| 9458 |
|
| 9459 |
_, err = js.SubscribeSync("foo", nats.Bind("foo", cinfo.Name)) |
| 9460 |
if err == nil || !strings.Contains(err.Error(), "already bound") { |
| 9461 |
t.Fatalf("Unexpected error: %v", err) |
| 9462 |
} |
| 9463 |
|
| 9464 |
sub2, err := js.QueueSubscribeSync("foo", "wq3") |
| 9465 |
if err != nil { |
| 9466 |
t.Fatalf("Unexpected error: %v", err) |
| 9467 |
} |
| 9468 |
for range 25 { |
| 9469 |
msg, err := sub2.NextMsg(time.Second) |
| 9470 |
if err != nil { |
| 9471 |
t.Fatalf("Error on NextMsg: %v", err) |
| 9472 |
} |
| 9473 |
msg.AckSync() |
| 9474 |
} |
| 9475 |
cinfo, _ = sub2.ConsumerInfo() |
| 9476 |
sub3, err := js.QueueSubscribeSync("foo", "wq3", nats.Bind("foo", cinfo.Name)) |
| 9477 |
if err != nil { |
| 9478 |
t.Fatalf("Unexpected error: %v", err) |
| 9479 |
} |
| 9480 |
for range 100 { |
| 9481 |
js.Publish("foo", []byte("new")) |
| 9482 |
} |
| 9483 |
if _, err := sub3.NextMsg(time.Second); err != nil { |
| 9484 |
t.Fatalf("Second member failed to get a message: %v", err) |
| 9485 |
} |
| 9486 |
}) |
| 9487 |
} |
| 9488 |
|
| 9489 |
func TestJetStreamDomain(t *testing.T) { |
| 9490 |
withJSServerInstance(t, func(t *testing.T, nc *nats.Conn, _ *testservice.Instance) { |
| 9491 |
// JS with custom domain |
| 9492 |
jsd, err := nc.JetStream(nats.Domain("ABC")) |
| 9493 |
if err != nil { |
| 9494 |
t.Fatalf("Unexpected error: %v", err) |
| 9495 |
} |
| 9496 |
info, err := jsd.AccountInfo() |
| 9497 |
if err != nil { |
| 9498 |
t.Error(err) |
| 9499 |
} |
| 9500 |
got := info.Domain |
| 9501 |
expected := "ABC" |
| 9502 |
if got != expected { |
| 9503 |
t.Errorf("Got %v, expected: %v", got, expected) |
| 9504 |
} |
| 9505 |
|
| 9506 |
if _, err = jsd.AddStream(&nats.StreamConfig{Name: "foo"}); err != nil { |
| 9507 |
t.Fatalf("Unexpected error: %v", err) |
| 9508 |
} |
| 9509 |
jsd.Publish("foo", []byte("first")) |
| 9510 |
|
| 9511 |
if _, err = jsd.AddConsumer("foo", &nats.ConsumerConfig{Durable: "c1"}); err != nil { |
| 9512 |
t.Fatalf("Unexpected error: %v", err) |
| 9513 |
} |
| 9514 |
|
| 9515 |
sub, err := jsd.SubscribeSync("foo") |
| 9516 |
if err != nil { |
| 9517 |
t.Fatal(err) |
| 9518 |
} |
| 9519 |
msg, err := sub.NextMsg(time.Second) |
| 9520 |
if err != nil { |
| 9521 |
t.Fatal(err) |
| 9522 |
} |
| 9523 |
got = string(msg.Data) |
| 9524 |
expected = "first" |
| 9525 |
if got != expected { |
| 9526 |
t.Errorf("Got %v, expected: %v", got, expected) |
| 9527 |
} |
| 9528 |
|
| 9529 |
// JS without explicit bound domain should also work. |
| 9530 |
js, err := nc.JetStream() |
| 9531 |
if err != nil { |
| 9532 |
t.Fatalf("Unexpected error: %v", err) |
| 9533 |
} |
| 9534 |
info, err = js.AccountInfo() |
| 9535 |
if err != nil { |
| 9536 |
t.Error(err) |
| 9537 |
} |
| 9538 |
got = info.Domain |
| 9539 |
expected = "ABC" |
| 9540 |
if got != expected { |
| 9541 |
t.Errorf("Got %v, expected: %v", got, expected) |
| 9542 |
} |
| 9543 |
|
| 9544 |
js.Publish("foo", []byte("second")) |
| 9545 |
|
| 9546 |
sub2, err := js.SubscribeSync("foo") |
| 9547 |
if err != nil { |
| 9548 |
t.Fatal(err) |
| 9549 |
} |
| 9550 |
msg, err = sub2.NextMsg(time.Second) |
| 9551 |
if err != nil { |
| 9552 |
t.Fatal(err) |
| 9553 |
} |
| 9554 |
got = string(msg.Data) |
| 9555 |
expected = "first" |
| 9556 |
if got != expected { |
| 9557 |
t.Errorf("Got %v, expected: %v", got, expected) |
| 9558 |
} |
| 9559 |
|
| 9560 |
msg, err = sub2.NextMsg(time.Second) |
| 9561 |
if err != nil { |
| 9562 |
t.Fatal(err) |
| 9563 |
} |
| 9564 |
got = string(msg.Data) |
| 9565 |
expected = "second" |
| 9566 |
if got != expected { |
| 9567 |
t.Errorf("Got %v, expected: %v", got, expected) |
| 9568 |
} |
| 9569 |
|
| 9570 |
// Per-request Domain option should be honored for consumer management APIs. |
| 9571 |
ci, err := js.ConsumerInfo("foo", "c1", nats.Domain("ABC")) |
| 9572 |
if err != nil { |
| 9573 |
t.Fatalf("ConsumerInfo with Domain opt failed: %v", err) |
| 9574 |
} |
| 9575 |
if ci == nil || ci.Name != "c1" { |
| 9576 |
t.Fatalf("Unexpected consumer info: %+v", ci) |
| 9577 |
} |
| 9578 |
if _, err = js.AddConsumer("foo", &nats.ConsumerConfig{Durable: "c2"}, nats.Domain("ABC")); err != nil { |
| 9579 |
t.Fatalf("AddConsumer with Domain opt failed: %v", err) |
| 9580 |
} |
| 9581 |
si, err := js.StreamInfo("foo", nats.Domain("ABC")) |
| 9582 |
if err != nil { |
| 9583 |
t.Fatalf("StreamInfo with Domain opt failed: %v", err) |
| 9584 |
} |
| 9585 |
if si == nil || si.Config.Name != "foo" { |
| 9586 |
t.Fatalf("Unexpected stream info: %+v", si) |
| 9587 |
} |
| 9588 |
if err = js.DeleteConsumer("foo", "c2", nats.Domain("ABC")); err != nil { |
| 9589 |
t.Fatalf("DeleteConsumer with Domain opt failed: %v", err) |
| 9590 |
} |
| 9591 |
|
| 9592 |
// Using different domain not configured is an error. |
| 9593 |
jsb, err := nc.JetStream(nats.Domain("XYZ")) |
| 9594 |
if err != nil { |
| 9595 |
t.Fatalf("Unexpected error: %v", err) |
| 9596 |
} |
| 9597 |
_, err = jsb.AccountInfo() |
| 9598 |
if err != nats.ErrJetStreamNotEnabled { |
| 9599 |
t.Errorf("Unexpected error: %v", err) |
| 9600 |
} |
| 9601 |
}, testservice.WithJetStream(`domain: ABC`)) |
| 9602 |
} |
| 9603 |
|
| 9604 |
func TestJetStreamMaxMsgsPerSubject(t *testing.T) { |
| 9605 |
const subjectMax = 5 |
| 9606 |
msc := nats.StreamConfig{ |
| 9607 |
Name: "TEST", |
| 9608 |
Subjects: []string{"foo", "bar", "baz.*"}, |
| 9609 |
Storage: nats.MemoryStorage, |
| 9610 |
MaxMsgsPerSubject: subjectMax, |
| 9611 |
} |
| 9612 |
fsc := msc |
| 9613 |
fsc.Storage = nats.FileStorage |
| 9614 |
|
| 9615 |
cases := []struct { |
| 9616 |
name string |
| 9617 |
mconfig *nats.StreamConfig |
| 9618 |
}{ |
| 9619 |
{"MemoryStore", &msc}, |
| 9620 |
{"FileStore", &fsc}, |
| 9621 |
} |
| 9622 |
|
| 9623 |
for _, c := range cases { |
| 9624 |
t.Run(c.name, func(t *testing.T) { |
| 9625 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 9626 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 9627 |
if err != nil { |
| 9628 |
t.Fatalf("Unexpected error: %v", err) |
| 9629 |
} |
| 9630 |
|
| 9631 |
_, err = js.AddStream(c.mconfig) |
| 9632 |
if err != nil { |
| 9633 |
t.Fatalf("Unexpected error adding stream: %v", err) |
| 9634 |
} |
| 9635 |
defer js.DeleteStream(c.mconfig.Name) |
| 9636 |
|
| 9637 |
pubAndCheck := func(subj string, num int, expectedNumMsgs uint64) { |
| 9638 |
t.Helper() |
| 9639 |
for range num { |
| 9640 |
if _, err = js.Publish(subj, []byte("TSLA")); err != nil { |
| 9641 |
t.Fatalf("Unexpected publish error: %v", err) |
| 9642 |
} |
| 9643 |
} |
| 9644 |
si, err := js.StreamInfo(c.mconfig.Name) |
| 9645 |
if err != nil { |
| 9646 |
t.Fatalf("Unexpected error: %v", err) |
| 9647 |
} |
| 9648 |
if si.State.Msgs != expectedNumMsgs { |
| 9649 |
t.Fatalf("Expected %d msgs, got %d", expectedNumMsgs, si.State.Msgs) |
| 9650 |
} |
| 9651 |
} |
| 9652 |
|
| 9653 |
pubAndCheck("foo", 1, 1) |
| 9654 |
pubAndCheck("foo", 4, 5) |
| 9655 |
pubAndCheck("foo", 2, 5) |
| 9656 |
pubAndCheck("baz.22", 5, 10) |
| 9657 |
pubAndCheck("baz.33", 5, 15) |
| 9658 |
pubAndCheck("baz.22", 5, 15) |
| 9659 |
pubAndCheck("baz.33", 5, 15) |
| 9660 |
|
| 9661 |
if err := js.PurgeStream(c.mconfig.Name); err != nil { |
| 9662 |
t.Fatalf("Unexpected purge error: %v", err) |
| 9663 |
} |
| 9664 |
pubAndCheck("foo", 1, 1) |
| 9665 |
pubAndCheck("foo", 4, 5) |
| 9666 |
pubAndCheck("baz.22", 5, 10) |
| 9667 |
pubAndCheck("baz.33", 5, 15) |
| 9668 |
}) |
| 9669 |
}) |
| 9670 |
} |
| 9671 |
} |
| 9672 |
|
| 9673 |
func TestJetStreamDrainFailsToDeleteConsumer(t *testing.T) { |
| 9674 |
c := newTester(t) |
| 9675 |
inst := c.CreateServer(t, true) |
| 9676 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 9677 |
|
| 9678 |
errCh := make(chan error, 1) |
| 9679 |
nc := dialInstance(t, inst, nats.ErrorHandler(func(_ *nats.Conn, _ *nats.Subscription, err error) { |
| 9680 |
select { |
| 9681 |
case errCh <- err: |
| 9682 |
default: |
| 9683 |
} |
| 9684 |
})) |
| 9685 |
c.WaitForJetStream(t, nc) |
| 9686 |
|
| 9687 |
js, err := nc.JetStream() |
| 9688 |
if err != nil { |
| 9689 |
t.Fatalf("Unexpected error: %v", err) |
| 9690 |
} |
| 9691 |
|
| 9692 |
if _, err := js.AddStream(&nats.StreamConfig{ |
| 9693 |
Name: "TEST", |
| 9694 |
Subjects: []string{"foo"}, |
| 9695 |
}); err != nil { |
| 9696 |
t.Fatalf("Unexpected error: %v", err) |
| 9697 |
} |
| 9698 |
|
| 9699 |
js.Publish("foo", []byte("hi")) |
| 9700 |
|
| 9701 |
blockCh := make(chan struct{}) |
| 9702 |
sub, err := js.Subscribe("foo", func(m *nats.Msg) { |
| 9703 |
<-blockCh |
| 9704 |
}, nats.Durable("dur")) |
| 9705 |
if err != nil { |
| 9706 |
t.Fatalf("Error subscribing: %v", err) |
| 9707 |
} |
| 9708 |
|
| 9709 |
sub.Drain() |
| 9710 |
|
| 9711 |
if err := js.DeleteConsumer("TEST", "dur"); err != nil { |
| 9712 |
t.Fatalf("Error deleting consumer: %v", err) |
| 9713 |
} |
| 9714 |
|
| 9715 |
close(blockCh) |
| 9716 |
|
| 9717 |
select { |
| 9718 |
case err := <-errCh: |
| 9719 |
if !strings.Contains(err.Error(), "consumer not found") { |
| 9720 |
t.Fatalf("Unexpected async error: %v", err) |
| 9721 |
} |
| 9722 |
case <-time.After(time.Second): |
| 9723 |
t.Fatal("Did not get async error") |
| 9724 |
} |
| 9725 |
} |
| 9726 |
|
| 9727 |
func TestJetStreamDomainInPubAck(t *testing.T) { |
| 9728 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 9729 |
js, err := nc.JetStream() |
| 9730 |
if err != nil { |
| 9731 |
t.Fatalf("Unexpected error: %v", err) |
| 9732 |
} |
| 9733 |
|
| 9734 |
cfg := &nats.StreamConfig{ |
| 9735 |
Name: "TEST", |
| 9736 |
Storage: nats.MemoryStorage, |
| 9737 |
Subjects: []string{"foo"}, |
| 9738 |
} |
| 9739 |
if _, err := js.AddStream(cfg); err != nil { |
| 9740 |
t.Fatalf("Unexpected error: %v", err) |
| 9741 |
} |
| 9742 |
|
| 9743 |
pa, err := js.Publish("foo", []byte("msg")) |
| 9744 |
if err != nil { |
| 9745 |
t.Fatalf("Error on publish: %v", err) |
| 9746 |
} |
| 9747 |
if pa.Domain != "HUB" { |
| 9748 |
t.Fatalf("Expected PubAck to have domain of %q, got %q", "HUB", pa.Domain) |
| 9749 |
} |
| 9750 |
}, testservice.WithJetStream(`domain: "HUB"`)) |
| 9751 |
} |
| 9752 |
|
| 9753 |
func TestJetStreamStreamAndConsumerDescription(t *testing.T) { |
| 9754 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 9755 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 9756 |
if err != nil { |
| 9757 |
t.Fatalf("Unexpected error: %v", err) |
| 9758 |
} |
| 9759 |
|
| 9760 |
streamDesc := "stream description" |
| 9761 |
si, err := js.AddStream(&nats.StreamConfig{ |
| 9762 |
Name: "TEST", |
| 9763 |
Description: streamDesc, |
| 9764 |
Subjects: []string{"foo"}, |
| 9765 |
}) |
| 9766 |
if err != nil { |
| 9767 |
t.Fatalf("Error adding stream: %v", err) |
| 9768 |
} |
| 9769 |
if si.Config.Description != streamDesc { |
| 9770 |
t.Fatalf("Invalid description: %q vs %q", streamDesc, si.Config.Description) |
| 9771 |
} |
| 9772 |
|
| 9773 |
consDesc := "consumer description" |
| 9774 |
ci, err := js.AddConsumer("TEST", &nats.ConsumerConfig{ |
| 9775 |
Description: consDesc, |
| 9776 |
Durable: "dur", |
| 9777 |
DeliverSubject: "bar", |
| 9778 |
}) |
| 9779 |
if err != nil { |
| 9780 |
t.Fatalf("Error adding consumer: %v", err) |
| 9781 |
} |
| 9782 |
if ci.Config.Description != consDesc { |
| 9783 |
t.Fatalf("Invalid description: %q vs %q", consDesc, ci.Config.Description) |
| 9784 |
} |
| 9785 |
}) |
| 9786 |
} |
| 9787 |
|
| 9788 |
func TestJetStreamMsgSubjectRewrite(t *testing.T) { |
| 9789 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 9790 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 9791 |
if err != nil { |
| 9792 |
t.Fatalf("Unexpected error: %v", err) |
| 9793 |
} |
| 9794 |
|
| 9795 |
if _, err := js.AddStream(&nats.StreamConfig{ |
| 9796 |
Name: "TEST", |
| 9797 |
Subjects: []string{"foo"}, |
| 9798 |
}); err != nil { |
| 9799 |
t.Fatalf("Error adding stream: %v", err) |
| 9800 |
} |
| 9801 |
|
| 9802 |
sub, err := nc.SubscribeSync(nats.NewInbox()) |
| 9803 |
if err != nil { |
| 9804 |
t.Fatalf("Error on subscribe: %v", err) |
| 9805 |
} |
| 9806 |
if _, err := js.AddConsumer("TEST", &nats.ConsumerConfig{ |
| 9807 |
DeliverSubject: sub.Subject, |
| 9808 |
DeliverPolicy: nats.DeliverAllPolicy, |
| 9809 |
}); err != nil { |
| 9810 |
t.Fatalf("Error adding consumer: %v", err) |
| 9811 |
} |
| 9812 |
|
| 9813 |
if _, err := js.Publish("foo", []byte("msg")); err != nil { |
| 9814 |
t.Fatalf("Error on publish: %v", err) |
| 9815 |
} |
| 9816 |
|
| 9817 |
msg, err := sub.NextMsg(time.Second) |
| 9818 |
if err != nil { |
| 9819 |
t.Fatalf("Did not get message: %v", err) |
| 9820 |
} |
| 9821 |
if msg.Subject != "foo" { |
| 9822 |
t.Fatalf("Subject should be %q, got %q", "foo", msg.Subject) |
| 9823 |
} |
| 9824 |
if string(msg.Data) != "msg" { |
| 9825 |
t.Fatalf("Unexpected data: %q", msg.Data) |
| 9826 |
} |
| 9827 |
}) |
| 9828 |
} |
| 9829 |
|
| 9830 |
func TestJetStreamPullSubscribeFetchContext(t *testing.T) { |
| 9831 |
c := newTester(t) |
| 9832 |
inst := c.CreateCluster(t, 3, true) |
| 9833 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 9834 |
|
| 9835 |
nc := dialInstance(t, inst) |
| 9836 |
c.WaitForJetStream(t, nc) |
| 9837 |
waitForJSCluster(t, nc) |
| 9838 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 9839 |
if err != nil { |
| 9840 |
t.Fatalf("Unexpected error: %v", err) |
| 9841 |
} |
| 9842 |
|
| 9843 |
subject := "WQ" |
| 9844 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 9845 |
Name: subject, |
| 9846 |
Replicas: 3, |
| 9847 |
}) |
| 9848 |
if err != nil { |
| 9849 |
t.Fatal(err) |
| 9850 |
} |
| 9851 |
|
| 9852 |
sendMsgs := func(t *testing.T, totalMsgs int) { |
| 9853 |
t.Helper() |
| 9854 |
for i := range totalMsgs { |
| 9855 |
payload := fmt.Sprintf("i:%d", i) |
| 9856 |
if _, err := js.Publish(subject, []byte(payload)); err != nil { |
| 9857 |
t.Errorf("Unexpected error: %v", err) |
| 9858 |
} |
| 9859 |
} |
| 9860 |
} |
| 9861 |
expected := 10 |
| 9862 |
sendMsgs(t, expected) |
| 9863 |
|
| 9864 |
sub, err := js.PullSubscribe(subject, "batch-ctx") |
| 9865 |
if err != nil { |
| 9866 |
t.Fatal(err) |
| 9867 |
} |
| 9868 |
defer sub.Unsubscribe() |
| 9869 |
|
| 9870 |
t.Run("ctx background", func(t *testing.T) { |
| 9871 |
_, err = sub.Fetch(expected, nats.Context(context.Background())) |
| 9872 |
if err == nil { |
| 9873 |
t.Fatal("Unexpected success") |
| 9874 |
} |
| 9875 |
if err != nats.ErrNoDeadlineContext { |
| 9876 |
t.Errorf("Expected context deadline error, got: %v", err) |
| 9877 |
} |
| 9878 |
}) |
| 9879 |
|
| 9880 |
t.Run("ctx canceled", func(t *testing.T) { |
| 9881 |
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 9882 |
cancel() |
| 9883 |
|
| 9884 |
_, err = sub.Fetch(expected, nats.Context(ctx)) |
| 9885 |
if err == nil { |
| 9886 |
t.Fatal("Unexpected success") |
| 9887 |
} |
| 9888 |
if err != context.Canceled { |
| 9889 |
t.Errorf("Expected context deadline error, got: %v", err) |
| 9890 |
} |
| 9891 |
|
| 9892 |
ctx, cancel = context.WithCancel(context.Background()) |
| 9893 |
cancel() |
| 9894 |
|
| 9895 |
_, err = sub.Fetch(expected, nats.Context(ctx)) |
| 9896 |
if err == nil { |
| 9897 |
t.Fatal("Unexpected success") |
| 9898 |
} |
| 9899 |
if err != context.Canceled { |
| 9900 |
t.Errorf("Expected context deadline error, got: %v", err) |
| 9901 |
} |
| 9902 |
}) |
| 9903 |
|
| 9904 |
t.Run("ctx timeout", func(t *testing.T) { |
| 9905 |
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 9906 |
defer cancel() |
| 9907 |
|
| 9908 |
msgs, err := sub.Fetch(expected, nats.Context(ctx)) |
| 9909 |
if err != nil { |
| 9910 |
t.Fatalf("Unexpected error: %v", err) |
| 9911 |
} |
| 9912 |
|
| 9913 |
got := len(msgs) |
| 9914 |
if got != expected { |
| 9915 |
t.Fatalf("Got %v messages, expected at least: %v", got, expected) |
| 9916 |
} |
| 9917 |
info, err := sub.ConsumerInfo() |
| 9918 |
if err != nil { |
| 9919 |
t.Error(err) |
| 9920 |
} |
| 9921 |
if info.NumAckPending != expected { |
| 9922 |
t.Errorf("Expected %d pending acks, got: %d", expected, info.NumAckPending) |
| 9923 |
} |
| 9924 |
|
| 9925 |
for _, msg := range msgs { |
| 9926 |
msg.AckSync() |
| 9927 |
} |
| 9928 |
|
| 9929 |
info, err = sub.ConsumerInfo() |
| 9930 |
if err != nil { |
| 9931 |
t.Error(err) |
| 9932 |
} |
| 9933 |
if info.NumAckPending > 0 { |
| 9934 |
t.Errorf("Expected no pending acks, got: %d", info.NumAckPending) |
| 9935 |
} |
| 9936 |
|
| 9937 |
ctx, cancel = context.WithTimeout(ctx, 250*time.Millisecond) |
| 9938 |
defer cancel() |
| 9939 |
|
| 9940 |
_, err = sub.Fetch(1, nats.Context(ctx)) |
| 9941 |
if err != context.DeadlineExceeded { |
| 9942 |
t.Errorf("Expected deadline exceeded fetching next message, got: %v", err) |
| 9943 |
} |
| 9944 |
|
| 9945 |
expected = 5 |
| 9946 |
sendMsgs(t, expected) |
| 9947 |
|
| 9948 |
ctx, cancel = context.WithTimeout(context.Background(), 1*time.Second) |
| 9949 |
defer cancel() |
| 9950 |
|
| 9951 |
msgs, err = sub.Fetch(1, nats.Context(ctx)) |
| 9952 |
if err != nil { |
| 9953 |
t.Fatalf("Unexpected error: %v", err) |
| 9954 |
} |
| 9955 |
if len(msgs) != 1 { |
| 9956 |
t.Fatalf("Expected to receive a single message, got: %d", len(msgs)) |
| 9957 |
} |
| 9958 |
for _, msg := range msgs { |
| 9959 |
msg.Ack() |
| 9960 |
} |
| 9961 |
|
| 9962 |
expected = 4 |
| 9963 |
msgs, err = sub.Fetch(expected, nats.Context(ctx)) |
| 9964 |
if err != nil { |
| 9965 |
t.Fatalf("Unexpected error: %v", err) |
| 9966 |
} |
| 9967 |
got = len(msgs) |
| 9968 |
if got != expected { |
| 9969 |
t.Fatalf("Got %v messages, expected at least: %v", got, expected) |
| 9970 |
} |
| 9971 |
for _, msg := range msgs { |
| 9972 |
msg.AckSync() |
| 9973 |
} |
| 9974 |
|
| 9975 |
info, err = sub.ConsumerInfo() |
| 9976 |
if err != nil { |
| 9977 |
t.Error(err) |
| 9978 |
} |
| 9979 |
if info.NumAckPending > 0 { |
| 9980 |
t.Errorf("Expected no pending acks, got: %d", info.NumAckPending) |
| 9981 |
} |
| 9982 |
}) |
| 9983 |
|
| 9984 |
t.Run("ctx with cancel", func(t *testing.T) { |
| 9985 |
js, err = nc.JetStream(nats.MaxWait(2 * time.Second)) |
| 9986 |
if err != nil { |
| 9987 |
t.Fatal(err) |
| 9988 |
} |
| 9989 |
|
| 9990 |
sub, err := js.PullSubscribe(subject, "batch-cancel-ctx") |
| 9991 |
if err != nil { |
| 9992 |
t.Fatal(err) |
| 9993 |
} |
| 9994 |
defer sub.Unsubscribe() |
| 9995 |
|
| 9996 |
ctx, cancel := context.WithCancel(context.Background()) |
| 9997 |
defer cancel() |
| 9998 |
|
| 9999 |
info, err := sub.ConsumerInfo() |
| 10000 |
if err != nil { |
| 10001 |
t.Fatal(err) |
| 10002 |
} |
| 10003 |
total := info.NumPending |
| 10004 |
|
| 10005 |
msgs, err := sub.Fetch(1, nats.Context(ctx)) |
| 10006 |
if err != nil { |
| 10007 |
t.Fatal(err) |
| 10008 |
} |
| 10009 |
if len(msgs) != 1 { |
| 10010 |
t.Fatalf("Expected a message, got: %d", len(msgs)) |
| 10011 |
} |
| 10012 |
for _, msg := range msgs { |
| 10013 |
msg.AckSync() |
| 10014 |
} |
| 10015 |
|
| 10016 |
expected := int(total - 1) |
| 10017 |
msgs, err = sub.Fetch(expected, nats.Context(ctx)) |
| 10018 |
if err != nil { |
| 10019 |
t.Fatal(err) |
| 10020 |
} |
| 10021 |
if len(msgs) != expected { |
| 10022 |
t.Fatalf("Expected %d messages, got: %d", expected, len(msgs)) |
| 10023 |
} |
| 10024 |
for _, msg := range msgs { |
| 10025 |
msg.AckSync() |
| 10026 |
} |
| 10027 |
|
| 10028 |
_, err = sub.Fetch(expected, nats.Context(ctx)) |
| 10029 |
if err == nil { |
| 10030 |
t.Fatal("Unexpected success") |
| 10031 |
} |
| 10032 |
if err != context.DeadlineExceeded { |
| 10033 |
t.Fatalf("Expected deadline exceeded fetching next message, got: %v", err) |
| 10034 |
} |
| 10035 |
|
| 10036 |
if ctx.Err() != nil { |
| 10037 |
t.Fatalf("Expected no errors in original cancellation context, got: %v", ctx.Err()) |
| 10038 |
} |
| 10039 |
|
| 10040 |
sendMsgs(t, 5) |
| 10041 |
|
| 10042 |
var pending uint64 = 4 |
| 10043 |
msgs, err = sub.Fetch(1, nats.Context(ctx)) |
| 10044 |
if err != nil { |
| 10045 |
t.Fatal(err) |
| 10046 |
} |
| 10047 |
if len(msgs) != 1 { |
| 10048 |
t.Fatalf("Expected a message, got: %d", len(msgs)) |
| 10049 |
} |
| 10050 |
for _, msg := range msgs { |
| 10051 |
msg.AckSync() |
| 10052 |
} |
| 10053 |
|
| 10054 |
cancel() |
| 10055 |
|
| 10056 |
_, err = sub.Fetch(1, nats.Context(ctx)) |
| 10057 |
if err == nil { |
| 10058 |
t.Fatal("Unexpected success") |
| 10059 |
} |
| 10060 |
if err != context.Canceled { |
| 10061 |
t.Fatalf("Expected deadline exceeded fetching next message, got: %v", err) |
| 10062 |
} |
| 10063 |
|
| 10064 |
info, err = sub.ConsumerInfo() |
| 10065 |
if err != nil { |
| 10066 |
t.Fatal(err) |
| 10067 |
} |
| 10068 |
total = info.NumPending |
| 10069 |
if total != pending { |
| 10070 |
t.Errorf("Expected %d pending messages, got: %d", pending, total) |
| 10071 |
} |
| 10072 |
}) |
| 10073 |
|
| 10074 |
t.Run("MaxWait timeout should return nats error", func(t *testing.T) { |
| 10075 |
_, err := sub.Fetch(1, nats.MaxWait(1*time.Nanosecond)) |
| 10076 |
if !errors.Is(err, nats.ErrTimeout) { |
| 10077 |
t.Fatalf("Expect ErrTimeout, got err=%#v", err) |
| 10078 |
} |
| 10079 |
}) |
| 10080 |
|
| 10081 |
t.Run("Context timeout should return context error", func(t *testing.T) { |
| 10082 |
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Nanosecond) |
| 10083 |
defer cancel() |
| 10084 |
_, err := sub.Fetch(1, nats.Context(ctx)) |
| 10085 |
if !errors.Is(err, context.DeadlineExceeded) { |
| 10086 |
t.Fatalf("Expect context.DeadlineExceeded, got err=%#v", err) |
| 10087 |
} |
| 10088 |
}) |
| 10089 |
} |
| 10090 |
|
| 10091 |
func TestJetStreamSubscribeContextCancel(t *testing.T) { |
| 10092 |
withJSServer(t, func(t *testing.T, nc *nats.Conn) { |
| 10093 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 10094 |
if err != nil { |
| 10095 |
t.Fatalf("Unexpected error: %v", err) |
| 10096 |
} |
| 10097 |
|
| 10098 |
_, err = js.AddStream(&nats.StreamConfig{ |
| 10099 |
Name: "TEST", |
| 10100 |
Subjects: []string{"foo", "bar", "baz", "foo.*"}, |
| 10101 |
}) |
| 10102 |
if err != nil { |
| 10103 |
t.Fatalf("Unexpected error: %v", err) |
| 10104 |
} |
| 10105 |
|
| 10106 |
toSend := 100 |
| 10107 |
for range toSend { |
| 10108 |
js.Publish("bar", []byte("foo")) |
| 10109 |
} |
| 10110 |
|
| 10111 |
t.Run("cancel unsubscribes and deletes ephemeral", func(t *testing.T) { |
| 10112 |
ctx, cancel := context.WithCancel(context.Background()) |
| 10113 |
defer cancel() |
| 10114 |
|
| 10115 |
ch := make(chan *nats.Msg, 100) |
| 10116 |
sub, err := js.Subscribe("bar", func(msg *nats.Msg) { |
| 10117 |
ch <- msg |
| 10118 |
|
| 10119 |
if len(ch) >= 50 { |
| 10120 |
cancel() |
| 10121 |
} |
| 10122 |
}, nats.Context(ctx)) |
| 10123 |
if err != nil { |
| 10124 |
t.Fatal(err) |
| 10125 |
} |
| 10126 |
|
| 10127 |
select { |
| 10128 |
case <-ctx.Done(): |
| 10129 |
case <-time.After(3 * time.Second): |
| 10130 |
t.Fatal("Timed out waiting for context to be canceled") |
| 10131 |
} |
| 10132 |
|
| 10133 |
checkFor(t, 2*time.Second, 15*time.Millisecond, func() error { |
| 10134 |
info, err := sub.ConsumerInfo() |
| 10135 |
if err != nil && err == nats.ErrConsumerNotFound { |
| 10136 |
return nil |
| 10137 |
} |
| 10138 |
return fmt.Errorf("Consumer still active, got: %v (info=%+v)", err, info) |
| 10139 |
}) |
| 10140 |
|
| 10141 |
got := len(ch) |
| 10142 |
expected := 50 |
| 10143 |
if got < expected { |
| 10144 |
t.Errorf("Expected to receive at least %d messages, got: %d", expected, got) |
| 10145 |
} |
| 10146 |
}) |
| 10147 |
|
| 10148 |
t.Run("unsubscribe cancels child context", func(t *testing.T) { |
| 10149 |
ctx, cancel := context.WithCancel(context.Background()) |
| 10150 |
defer cancel() |
| 10151 |
|
| 10152 |
sub, err := js.Subscribe("bar", func(msg *nats.Msg) {}, nats.Context(ctx)) |
| 10153 |
if err != nil { |
| 10154 |
t.Fatal(err) |
| 10155 |
} |
| 10156 |
if err := sub.Unsubscribe(); err != nil { |
| 10157 |
t.Fatal(err) |
| 10158 |
} |
| 10159 |
|
| 10160 |
checkFor(t, 2*time.Second, 15*time.Millisecond, func() error { |
| 10161 |
info, err := sub.ConsumerInfo() |
| 10162 |
if err != nil && err == nats.ErrConsumerNotFound { |
| 10163 |
return nil |
| 10164 |
} |
| 10165 |
return fmt.Errorf("Consumer still active, got: %v (info=%+v)", err, info) |
| 10166 |
}) |
| 10167 |
}) |
| 10168 |
}) |
| 10169 |
} |
| 10170 |
|
| 10171 |
// oscStream dials inst and creates the stream the ordered consumer tests use. |
| 10172 |
func oscStream(t *testing.T, inst *testservice.Instance, opts ...nats.Option) (*nats.Conn, nats.JetStreamContext) { |
| 10173 |
t.Helper() |
| 10174 |
nc := dialInstance(t, inst, opts...) |
| 10175 |
t.Cleanup(nc.Close) |
| 10176 |
js, err := nc.JetStream(nats.MaxWait(10 * time.Second)) |
| 10177 |
if err != nil { |
| 10178 |
t.Fatalf("Unexpected error: %v", err) |
| 10179 |
} |
| 10180 |
if _, err := js.AddStream(&nats.StreamConfig{Name: "OSC", Subjects: []string{"osc.>"}}); err != nil { |
| 10181 |
t.Fatalf("Error adding stream: %v", err) |
| 10182 |
} |
| 10183 |
return nc, js |
| 10184 |
} |
| 10185 |
|
| 10186 |
// countConsumerCreates watches consumer-create traffic from a separate connection. |
| 10187 |
func countConsumerCreates(t *testing.T, inst *testservice.Instance) *atomic.Int64 { |
| 10188 |
t.Helper() |
| 10189 |
mon := dialInstance(t, inst) |
| 10190 |
t.Cleanup(mon.Close) |
| 10191 |
creates := &atomic.Int64{} |
| 10192 |
if _, err := mon.Subscribe("$JS.API.CONSUMER.CREATE.>", func(*nats.Msg) { |
| 10193 |
creates.Add(1) |
| 10194 |
}); err != nil { |
| 10195 |
t.Fatalf("Error subscribing monitor: %v", err) |
| 10196 |
} |
| 10197 |
if err := mon.Flush(); err != nil { |
| 10198 |
t.Fatalf("Error flushing monitor: %v", err) |
| 10199 |
} |
| 10200 |
return creates |
| 10201 |
} |
| 10202 |
|
| 10203 |
// publishOSC publishes n messages to the stream and waits for the acks. |
| 10204 |
func publishOSC(t *testing.T, js nats.JetStreamContext, n int) { |
| 10205 |
t.Helper() |
| 10206 |
publishOSCSized(t, js, n, 1) |
| 10207 |
} |
| 10208 |
|
| 10209 |
func publishOSCSized(t *testing.T, js nats.JetStreamContext, n, size int) { |
| 10210 |
t.Helper() |
| 10211 |
payload := make([]byte, size) |
| 10212 |
for range n { |
| 10213 |
if _, err := js.PublishAsync("osc.a", payload); err != nil { |
| 10214 |
t.Fatalf("Error publishing: %v", err) |
| 10215 |
} |
| 10216 |
} |
| 10217 |
select { |
| 10218 |
case <-js.PublishAsyncComplete(): |
| 10219 |
case <-time.After(30 * time.Second): |
| 10220 |
t.Fatalf("Timed out waiting for publishes to complete") |
| 10221 |
} |
| 10222 |
} |
| 10223 |
|
| 10224 |
// seqRecorder records delivered stream sequences in arrival order. |
| 10225 |
type seqRecorder struct { |
| 10226 |
mu sync.Mutex |
| 10227 |
order []uint64 |
| 10228 |
metaErrs []error |
| 10229 |
} |
| 10230 |
|
| 10231 |
func (r *seqRecorder) add(m *nats.Msg) { |
| 10232 |
meta, err := m.Metadata() |
| 10233 |
r.mu.Lock() |
| 10234 |
defer r.mu.Unlock() |
| 10235 |
if err != nil { |
| 10236 |
r.metaErrs = append(r.metaErrs, err) |
| 10237 |
return |
| 10238 |
} |
| 10239 |
r.order = append(r.order, meta.Sequence.Stream) |
| 10240 |
} |
| 10241 |
|
| 10242 |
func (r *seqRecorder) count() int { |
| 10243 |
r.mu.Lock() |
| 10244 |
defer r.mu.Unlock() |
| 10245 |
return len(r.order) |
| 10246 |
} |
| 10247 |
|
| 10248 |
// waitFor fails unless at least want messages arrive within timeout. |
| 10249 |
func (r *seqRecorder) waitFor(t *testing.T, want int, timeout time.Duration) { |
| 10250 |
t.Helper() |
| 10251 |
checkFor(t, timeout, 10*time.Millisecond, func() error { |
| 10252 |
if n := r.count(); n < want { |
| 10253 |
return fmt.Errorf("only %d of %d messages delivered", n, want) |
| 10254 |
} |
| 10255 |
return nil |
| 10256 |
}) |
| 10257 |
} |
| 10258 |
|
| 10259 |
// readChan drains ch into the recorder until the test ends. |
| 10260 |
func (r *seqRecorder) readChan(t *testing.T, ch <-chan *nats.Msg) { |
| 10261 |
stop := make(chan struct{}) |
| 10262 |
t.Cleanup(func() { close(stop) }) |
| 10263 |
go func() { |
| 10264 |
for { |
| 10265 |
select { |
| 10266 |
case <-stop: |
| 10267 |
return |
| 10268 |
case m := <-ch: |
| 10269 |
r.add(m) |
| 10270 |
} |
| 10271 |
} |
| 10272 |
}() |
| 10273 |
} |
| 10274 |
|
| 10275 |
// verifyComplete asserts the whole stream arrived, in order and exactly once. |
| 10276 |
func (r *seqRecorder) verifyComplete(t *testing.T, want int) { |
| 10277 |
t.Helper() |
| 10278 |
r.mu.Lock() |
| 10279 |
defer r.mu.Unlock() |
| 10280 |
if len(r.metaErrs) != 0 { |
| 10281 |
t.Fatalf("Got %d metadata errors, first: %v", len(r.metaErrs), r.metaErrs[0]) |
| 10282 |
} |
| 10283 |
if len(r.order) != want { |
| 10284 |
t.Fatalf("Expected %d messages, got %d", want, len(r.order)) |
| 10285 |
} |
| 10286 |
for i, sseq := range r.order { |
| 10287 |
if sseq != uint64(i+1) { |
| 10288 |
t.Fatalf("Message %d out of order: expected stream seq %d, got %d", i, i+1, sseq) |
| 10289 |
} |
| 10290 |
} |
| 10291 |
} |
| 10292 |
|
| 10293 |
// waitForDrops fails unless the subscription actually enters slow consumer. |
| 10294 |
func waitForDrops(t *testing.T, sub *nats.Subscription) { |
| 10295 |
t.Helper() |
| 10296 |
checkFor(t, 10*time.Second, 10*time.Millisecond, func() error { |
| 10297 |
if dropped, _ := sub.Dropped(); dropped == 0 { |
| 10298 |
return fmt.Errorf("no messages dropped yet") |
| 10299 |
} |
| 10300 |
return nil |
| 10301 |
}) |
| 10302 |
} |
| 10303 |
|
| 10304 |
// Messages dropped as slow consumer must be refetched before anything newer, |
| 10305 |
// and a reset must wait until the reader has made room for the refetch. |
| 10306 |
func TestJetStreamOrderedConsumerSlowConsumer(t *testing.T) { |
| 10307 |
const ( |
| 10308 |
total = 200 |
| 10309 |
buffered = 32 |
| 10310 |
msgSize = 8 * 1024 |
| 10311 |
// Short, so that recovery through a heartbeat does not take long. |
| 10312 |
hb = 500 * time.Millisecond |
| 10313 |
) |
| 10314 |
|
| 10315 |
type nextFunc func(time.Duration) (*nats.Msg, error) |
| 10316 |
readChan := func(ch chan *nats.Msg) nextFunc { |
| 10317 |
return func(d time.Duration) (*nats.Msg, error) { |
| 10318 |
select { |
| 10319 |
case m := <-ch: |
| 10320 |
return m, nil |
| 10321 |
case <-time.After(d): |
| 10322 |
return nil, nats.ErrTimeout |
| 10323 |
} |
| 10324 |
} |
| 10325 |
} |
| 10326 |
|
| 10327 |
// Each holds exactly `buffered` messages before dropping. |
| 10328 |
subs := []struct { |
| 10329 |
name string |
| 10330 |
subscribe func(t *testing.T, js nats.JetStreamContext) (*nats.Subscription, nextFunc) |
| 10331 |
}{ |
| 10332 |
{"Subscribe", func(t *testing.T, js nats.JetStreamContext) (*nats.Subscription, nextFunc) { |
| 10333 |
// The callback hands each message to the reader, so the pending |
| 10334 |
// queue holds whatever the reader has not taken yet. |
| 10335 |
ch := make(chan *nats.Msg) |
| 10336 |
done := make(chan struct{}) |
| 10337 |
t.Cleanup(func() { close(done) }) |
| 10338 |
sub, err := js.Subscribe("osc.>", func(m *nats.Msg) { |
| 10339 |
select { |
| 10340 |
case ch <- m: |
| 10341 |
case <-done: |
| 10342 |
} |
| 10343 |
}, nats.OrderedConsumer(), nats.IdleHeartbeat(hb)) |
| 10344 |
if err != nil { |
| 10345 |
t.Fatalf("Error subscribing: %v", err) |
| 10346 |
} |
| 10347 |
if err := sub.SetPendingLimits(-1, buffered*msgSize); err != nil { |
| 10348 |
t.Fatalf("Error setting pending limits: %v", err) |
| 10349 |
} |
| 10350 |
return sub, readChan(ch) |
| 10351 |
}}, |
| 10352 |
{"SubscribeSync", func(t *testing.T, js nats.JetStreamContext) (*nats.Subscription, nextFunc) { |
| 10353 |
sub, err := js.SubscribeSync("osc.>", nats.OrderedConsumer(), nats.IdleHeartbeat(hb)) |
| 10354 |
if err != nil { |
| 10355 |
t.Fatalf("Error subscribing: %v", err) |
| 10356 |
} |
| 10357 |
if err := sub.SetPendingLimits(-1, buffered*msgSize); err != nil { |
| 10358 |
t.Fatalf("Error setting pending limits: %v", err) |
| 10359 |
} |
| 10360 |
return sub, sub.NextMsg |
| 10361 |
}}, |
| 10362 |
{"ChanSubscribe", func(t *testing.T, js nats.JetStreamContext) (*nats.Subscription, nextFunc) { |
| 10363 |
ch := make(chan *nats.Msg, buffered) |
| 10364 |
sub, err := js.ChanSubscribe("osc.>", ch, nats.OrderedConsumer(), nats.IdleHeartbeat(hb)) |
| 10365 |
if err != nil { |
| 10366 |
t.Fatalf("Error subscribing: %v", err) |
| 10367 |
} |
| 10368 |
return sub, readChan(ch) |
| 10369 |
}}, |
| 10370 |
} |
| 10371 |
|
| 10372 |
for _, test := range []struct { |
| 10373 |
name string |
| 10374 |
publishAfterDrop bool |
| 10375 |
}{ |
| 10376 |
{"NoSkippedMessages", true}, |
| 10377 |
{"HeartbeatRecovery", false}, |
| 10378 |
} { |
| 10379 |
t.Run(test.name, func(t *testing.T) { |
| 10380 |
for _, sub := range subs { |
| 10381 |
t.Run(sub.name, func(t *testing.T) { |
| 10382 |
withJSServerInstance(t, func(t *testing.T, _ *nats.Conn, inst *testservice.Instance) { |
| 10383 |
_, js := oscStream(t, inst, nats.ErrorHandler(func(*nats.Conn, *nats.Subscription, error) {})) |
| 10384 |
publishOSCSized(t, js, total, msgSize) |
| 10385 |
|
| 10386 |
sub, next := sub.subscribe(t, js) |
| 10387 |
defer sub.Unsubscribe() |
| 10388 |
waitForDrops(t, sub) |
| 10389 |
|
| 10390 |
// Reads stream sequences from through to, inclusive. |
| 10391 |
read := func(from, to uint64) { |
| 10392 |
t.Helper() |
| 10393 |
for expected := from; expected <= to; { |
| 10394 |
msg, err := next(3 * time.Second) |
| 10395 |
// Reported once after a drop. |
| 10396 |
if err == nats.ErrSlowConsumer { |
| 10397 |
continue |
| 10398 |
} |
| 10399 |
if err != nil { |
| 10400 |
t.Fatalf("Stalled waiting for stream sequence %d of %d: %v", expected, to, err) |
| 10401 |
} |
| 10402 |
meta, err := msg.Metadata() |
| 10403 |
if err != nil { |
| 10404 |
t.Fatalf("Error getting metadata: %v", err) |
| 10405 |
} |
| 10406 |
if meta.Sequence.Stream != expected { |
| 10407 |
t.Fatalf("Ordered consumer skipped a stream sequence: expected %d, got %d (consumer seq %d)", |
| 10408 |
expected, meta.Sequence.Stream, meta.Sequence.Consumer) |
| 10409 |
} |
| 10410 |
expected++ |
| 10411 |
} |
| 10412 |
} |
| 10413 |
|
| 10414 |
if test.publishAfterDrop { |
| 10415 |
// The dropped messages must be redelivered before |
| 10416 |
// the one published after them. |
| 10417 |
read(1, buffered) |
| 10418 |
if _, err := js.Publish("osc.a", []byte("last")); err != nil { |
| 10419 |
t.Fatalf("Error publishing: %v", err) |
| 10420 |
} |
| 10421 |
read(buffered+1, total+1) |
| 10422 |
return |
| 10423 |
} |
| 10424 |
|
| 10425 |
consumerName := func() string { |
| 10426 |
t.Helper() |
| 10427 |
ci, err := sub.ConsumerInfo() |
| 10428 |
if err != nil { |
| 10429 |
t.Fatalf("Error getting consumer info: %v", err) |
| 10430 |
} |
| 10431 |
return ci.Name |
| 10432 |
} |
| 10433 |
first := consumerName() |
| 10434 |
|
| 10435 |
// Heartbeats report the gap while nothing is reading, |
| 10436 |
// but a reset now would only get its refetch dropped |
| 10437 |
// into the same full buffer, again and again. It must |
| 10438 |
// wait until the reader has made room. |
| 10439 |
time.Sleep(3 * hb) |
| 10440 |
if name := consumerName(); name != first { |
| 10441 |
t.Fatalf("Ordered consumer was recreated while the buffer was full: %q -> %q", first, name) |
| 10442 |
} |
| 10443 |
read(1, buffered/2-1) |
| 10444 |
time.Sleep(2 * hb) |
| 10445 |
if name := consumerName(); name != first { |
| 10446 |
t.Fatalf("Ordered consumer was recreated while more than half the buffer was in use: %q -> %q", first, name) |
| 10447 |
} |
| 10448 |
read(buffered/2, buffered/2) |
| 10449 |
checkFor(t, 3*time.Second, 10*time.Millisecond, func() error { |
| 10450 |
ci, err := sub.ConsumerInfo() |
| 10451 |
if err != nil { |
| 10452 |
return err |
| 10453 |
} |
| 10454 |
if ci.Name == first { |
| 10455 |
return fmt.Errorf("ordered consumer not recreated after making room") |
| 10456 |
} |
| 10457 |
return nil |
| 10458 |
}) |
| 10459 |
read(buffered/2+1, total) |
| 10460 |
}) |
| 10461 |
}) |
| 10462 |
} |
| 10463 |
}) |
| 10464 |
} |
| 10465 |
} |
| 10466 |
|
| 10467 |
// An unbuffered channel has no capacity signal, so a reader that is not there |
| 10468 |
// yet cannot be told apart from one that is merely busy. While nothing has |
| 10469 |
// been delivered, resets are paced by the heartbeat and everything discarded |
| 10470 |
// in the meantime counts as dropped; once a reader shows up nothing is lost. |
| 10471 |
func TestJetStreamOrderedConsumerUnbufferedChan(t *testing.T) { |
| 10472 |
const hb = 200 * time.Millisecond |
| 10473 |
|
| 10474 |
t.Run("no reader", func(t *testing.T) { |
| 10475 |
withJSServerInstance(t, func(t *testing.T, _ *nats.Conn, inst *testservice.Instance) { |
| 10476 |
_, js := oscStream(t, inst, nats.ErrorHandler(func(*nats.Conn, *nats.Subscription, error) {})) |
| 10477 |
creates := countConsumerCreates(t, inst) |
| 10478 |
|
| 10479 |
ch := make(chan *nats.Msg) |
| 10480 |
sub, err := js.ChanSubscribe("osc.>", ch, nats.OrderedConsumer(), nats.IdleHeartbeat(hb)) |
| 10481 |
if err != nil { |
| 10482 |
t.Fatalf("Error subscribing: %v", err) |
| 10483 |
} |
| 10484 |
defer sub.Unsubscribe() |
| 10485 |
baseline := creates.Load() |
| 10486 |
|
| 10487 |
// Large enough for the server to interleave flow control messages |
| 10488 |
// with the data. |
| 10489 |
const totalMsgs = 4000 |
| 10490 |
publishOSCSized(t, js, totalMsgs, 1024) |
| 10491 |
waitForDrops(t, sub) |
| 10492 |
|
| 10493 |
const stallFor = time.Second |
| 10494 |
time.Sleep(stallFor) |
| 10495 |
// One reset per heartbeat, plus slack for a late one. |
| 10496 |
if n, max := creates.Load()-baseline, int64(stallFor/hb)+2; n > max { |
| 10497 |
t.Fatalf("Ordered consumer recreated %d times during a %v stall; expected at most %d", n, stallFor, max) |
| 10498 |
} |
| 10499 |
if dropped, _ := sub.Dropped(); dropped < totalMsgs { |
| 10500 |
t.Fatalf("Dropped() reports %d, expected at least %d", dropped, totalMsgs) |
| 10501 |
} |
| 10502 |
}) |
| 10503 |
}) |
| 10504 |
|
| 10505 |
t.Run("late reader", func(t *testing.T) { |
| 10506 |
withJSServerInstance(t, func(t *testing.T, _ *nats.Conn, inst *testservice.Instance) { |
| 10507 |
_, js := oscStream(t, inst, nats.ErrorHandler(func(*nats.Conn, *nats.Subscription, error) {})) |
| 10508 |
const totalMsgs = 100 |
| 10509 |
publishOSC(t, js, totalMsgs) |
| 10510 |
|
| 10511 |
ch := make(chan *nats.Msg) |
| 10512 |
sub, err := js.ChanSubscribe("osc.>", ch, nats.OrderedConsumer(), nats.IdleHeartbeat(hb)) |
| 10513 |
if err != nil { |
| 10514 |
t.Fatalf("Error subscribing: %v", err) |
| 10515 |
} |
| 10516 |
defer sub.Unsubscribe() |
| 10517 |
// The whole backlog is discarded before anyone reads. |
| 10518 |
waitForDrops(t, sub) |
| 10519 |
time.Sleep(2 * hb) |
| 10520 |
|
| 10521 |
var rec seqRecorder |
| 10522 |
rec.readChan(t, ch) |
| 10523 |
rec.waitFor(t, totalMsgs, 30*time.Second) |
| 10524 |
rec.verifyComplete(t, totalMsgs) |
| 10525 |
}) |
| 10526 |
}) |
| 10527 |
} |
| 10528 |
|
| 10529 |
// A healthy ordered consumer must never be recreated by the heartbeat path: the |
| 10530 |
// heartbeat carries the server's last delivered sequence while jsi.dseq is the |
| 10531 |
// next expected one, so caught up is ldseq == jsi.dseq-1, not equality. |
| 10532 |
func TestJetStreamOrderedConsumerNoResetWhenHealthy(t *testing.T) { |
| 10533 |
withJSServerInstance(t, func(t *testing.T, _ *nats.Conn, inst *testservice.Instance) { |
| 10534 |
_, js := oscStream(t, inst, nats.ErrorHandler(func(*nats.Conn, *nats.Subscription, error) {})) |
| 10535 |
creates := countConsumerCreates(t, inst) |
| 10536 |
|
| 10537 |
var rec seqRecorder |
| 10538 |
sub, err := js.Subscribe("osc.>", rec.add, nats.OrderedConsumer(), nats.IdleHeartbeat(200*time.Millisecond)) |
| 10539 |
if err != nil { |
| 10540 |
t.Fatalf("Error subscribing: %v", err) |
| 10541 |
} |
| 10542 |
defer sub.Unsubscribe() |
| 10543 |
baseline := creates.Load() |
| 10544 |
|
| 10545 |
// Six heartbeats, or the absence of resets would mean nothing. |
| 10546 |
const idleWindow = 1200 * time.Millisecond |
| 10547 |
|
| 10548 |
// Nothing delivered yet: the server reports Nats-Last-Consumer 0, jsi.dseq is 1. |
| 10549 |
time.Sleep(idleWindow) |
| 10550 |
// Steady state: the server's last delivered now equals jsi.dseq-1. |
| 10551 |
const totalMsgs = 5 |
| 10552 |
publishOSC(t, js, totalMsgs) |
| 10553 |
rec.waitFor(t, totalMsgs, 10*time.Second) |
| 10554 |
time.Sleep(idleWindow) |
| 10555 |
|
| 10556 |
// A late heartbeat trips the activity check into one reset; the bug |
| 10557 |
// would produce one per heartbeat. |
| 10558 |
if n := creates.Load() - baseline; n > 1 { |
| 10559 |
t.Fatalf("Ordered consumer recreated %d times over a healthy run; expected none", n) |
| 10560 |
} |
| 10561 |
rec.verifyComplete(t, totalMsgs) |
| 10562 |
}) |
| 10563 |
} |
| 10564 |
|
| 10565 |
// A reset before anything was delivered must resume from the consumer's first |
| 10566 |
// message, not from the start of the stream, or the deliver policy is lost. |
| 10567 |
func TestJetStreamOrderedConsumerFirstMsgDroppedKeepsStartPosition(t *testing.T) { |
| 10568 |
withJSServerInstance(t, func(t *testing.T, _ *nats.Conn, inst *testservice.Instance) { |
| 10569 |
_, js := oscStream(t, inst, nats.ErrorHandler(func(*nats.Conn, *nats.Subscription, error) {})) |
| 10570 |
publishOSC(t, js, 100) |
| 10571 |
|
| 10572 |
// No reader yet, so the first message is dropped. |
| 10573 |
ch := make(chan *nats.Msg) |
| 10574 |
sub, err := js.ChanSubscribe("osc.>", ch, nats.OrderedConsumer(), nats.DeliverNew(), nats.IdleHeartbeat(200*time.Millisecond)) |
| 10575 |
if err != nil { |
| 10576 |
t.Fatalf("Error subscribing: %v", err) |
| 10577 |
} |
| 10578 |
defer sub.Unsubscribe() |
| 10579 |
if _, err := js.Publish("osc.a", []byte("x")); err != nil { |
| 10580 |
t.Fatalf("Error publishing: %v", err) |
| 10581 |
} |
| 10582 |
waitForDrops(t, sub) |
| 10583 |
|
| 10584 |
var rec seqRecorder |
| 10585 |
rec.readChan(t, ch) |
| 10586 |
rec.waitFor(t, 1, 10*time.Second) |
| 10587 |
// Leave time for a replay of the history to show up. |
| 10588 |
time.Sleep(500 * time.Millisecond) |
| 10589 |
rec.mu.Lock() |
| 10590 |
defer rec.mu.Unlock() |
| 10591 |
if len(rec.order) != 1 || rec.order[0] != 101 { |
| 10592 |
t.Fatalf("Expected only stream seq 101, got %v", rec.order) |
| 10593 |
} |
| 10594 |
}) |
| 10595 |
} |