| 1 |
// Copyright 2022-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 |
"errors" |
| 19 |
"fmt" |
| 20 |
"reflect" |
| 21 |
"testing" |
| 22 |
"time" |
| 23 |
|
| 24 |
"github.com/nats-io/nats.go" |
| 25 |
"github.com/nats-io/nats.go/jetstream" |
| 26 |
testservice "github.com/synadia-io/orbit.go/ntf-client" |
| 27 |
) |
| 28 |
|
| 29 |
func TestNewWithAPIPrefix(t *testing.T) { |
| 30 |
t.Run("import subject from another account", func(t *testing.T) { |
| 31 |
c := newTester(t) |
| 32 |
accounts := `accounts: { |
| 33 |
JS: { |
| 34 |
jetstream: enabled |
| 35 |
users: [ {user: main, password: foo} ] |
| 36 |
exports [ { service: "$JS.API.>" }, { service: "foo" }] |
| 37 |
}, |
| 38 |
U: { |
| 39 |
users: [ {user: test_user, password: bar} ] |
| 40 |
imports [ |
| 41 |
{ service: { subject: "$JS.API.>", account: JS } , to: "main.>" } |
| 42 |
{ service: { subject: "foo", account: JS } } |
| 43 |
] |
| 44 |
}, |
| 45 |
}` |
| 46 |
inst := c.CreateServer(t, true, |
| 47 |
testservice.WithAccounts(accounts), |
| 48 |
testservice.WithAuthorization("no_auth_user: test_user"), |
| 49 |
testservice.WithSystemAccount("# system_account intentionally unset"), |
| 50 |
) |
| 51 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 52 |
|
| 53 |
ncMain := dialInstance(t, inst, nats.UserInfo("main", "foo")) |
| 54 |
|
| 55 |
ctx := newTesterCtx(t, 20*time.Second) |
| 56 |
jsMain, err := jetstream.New(ncMain) |
| 57 |
if err != nil { |
| 58 |
t.Fatalf("Unexpected error: %v", err) |
| 59 |
} |
| 60 |
|
| 61 |
_, err = jsMain.CreateStream(ctx, jetstream.StreamConfig{ |
| 62 |
Name: "TEST", |
| 63 |
Subjects: []string{"foo"}, |
| 64 |
}) |
| 65 |
if err != nil { |
| 66 |
t.Fatalf("Unexpected error: %v", err) |
| 67 |
} |
| 68 |
|
| 69 |
ncTest := dialInstance(t, inst) |
| 70 |
|
| 71 |
jsTest, err := jetstream.NewWithAPIPrefix(ncTest, "main") |
| 72 |
if err != nil { |
| 73 |
t.Fatalf("Unexpected error: %v", err) |
| 74 |
} |
| 75 |
opts := jsTest.Options() |
| 76 |
if opts.APIPrefix != "main" { |
| 77 |
t.Fatalf("Invalid API prefix; want: %v, got: %v", "main", opts.APIPrefix) |
| 78 |
} |
| 79 |
if opts.Domain != "" { |
| 80 |
t.Fatalf("Invalid domain; want: %v, got: %v", "", opts.Domain) |
| 81 |
} |
| 82 |
|
| 83 |
_, err = jsTest.Publish(ctx, "foo", []byte("msg")) |
| 84 |
if err != nil { |
| 85 |
t.Fatalf("Unexpected error: %v", err) |
| 86 |
} |
| 87 |
}) |
| 88 |
|
| 89 |
t.Run("empty API prefix", func(t *testing.T) { |
| 90 |
withJSServer(t, func(t *testing.T, nc *nats.Conn, _ jetstream.JetStream) { |
| 91 |
_, err := jetstream.NewWithAPIPrefix(nc, "") |
| 92 |
if err == nil || err.Error() != "API prefix cannot be empty" { |
| 93 |
t.Fatalf(`Expected error: "API prefix cannot be empty"; got: %v`, err) |
| 94 |
} |
| 95 |
}) |
| 96 |
}) |
| 97 |
|
| 98 |
t.Run("invalid options", func(t *testing.T) { |
| 99 |
withJSServer(t, func(t *testing.T, nc *nats.Conn, _ jetstream.JetStream) { |
| 100 |
_, err := jetstream.New(nc, jetstream.WithPublishAsyncMaxPending(0)) |
| 101 |
if !errors.Is(err, jetstream.ErrInvalidOption) { |
| 102 |
t.Fatalf("Expected error: %v; got: %v", jetstream.ErrInvalidOption, err) |
| 103 |
} |
| 104 |
|
| 105 |
_, err = jetstream.NewWithAPIPrefix(nc, "main", jetstream.WithPublishAsyncMaxPending(0)) |
| 106 |
if !errors.Is(err, jetstream.ErrInvalidOption) { |
| 107 |
t.Fatalf("Expected error: %v; got: %v", jetstream.ErrInvalidOption, err) |
| 108 |
} |
| 109 |
|
| 110 |
_, err = jetstream.NewWithDomain(nc, "main", jetstream.WithPublishAsyncMaxPending(0)) |
| 111 |
if !errors.Is(err, jetstream.ErrInvalidOption) { |
| 112 |
t.Fatalf("Expected error: %v; got: %v", jetstream.ErrInvalidOption, err) |
| 113 |
} |
| 114 |
}) |
| 115 |
}) |
| 116 |
} |
| 117 |
|
| 118 |
func TestNewWithDomain(t *testing.T) { |
| 119 |
t.Run("jetstream account with domain", func(t *testing.T) { |
| 120 |
withJSServer(t, func(t *testing.T, nc *nats.Conn, _ jetstream.JetStream) { |
| 121 |
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) |
| 122 |
defer cancel() |
| 123 |
js, err := jetstream.NewWithDomain(nc, "ABC") |
| 124 |
if err != nil { |
| 125 |
t.Fatalf("Unexpected error: %v", err) |
| 126 |
} |
| 127 |
|
| 128 |
accInfo, err := js.AccountInfo(ctx) |
| 129 |
if err != nil { |
| 130 |
t.Fatalf("Unexpected error: %v", err) |
| 131 |
} |
| 132 |
if accInfo.Domain != "ABC" { |
| 133 |
t.Errorf("Invalid domain; want %v, got: %v", "ABC", accInfo.Domain) |
| 134 |
} |
| 135 |
|
| 136 |
opts := js.Options() |
| 137 |
if opts.APIPrefix != "" { |
| 138 |
t.Fatalf("Invalid API prefix; want: %v, got: %v", "", opts.APIPrefix) |
| 139 |
} |
| 140 |
if opts.Domain != "ABC" { |
| 141 |
t.Fatalf("Invalid domain; want: %v, got: %v", "ABC", opts.Domain) |
| 142 |
} |
| 143 |
|
| 144 |
_, err = js.CreateStream(ctx, jetstream.StreamConfig{ |
| 145 |
Name: "TEST", |
| 146 |
Subjects: []string{"foo"}, |
| 147 |
}) |
| 148 |
if err != nil { |
| 149 |
t.Fatalf("Unexpected error: %v", err) |
| 150 |
} |
| 151 |
|
| 152 |
_, err = js.Publish(ctx, "foo", []byte("msg")) |
| 153 |
if err != nil { |
| 154 |
t.Fatalf("Unexpected error: %v", err) |
| 155 |
} |
| 156 |
}, testservice.WithJetStream(`domain: ABC`)) |
| 157 |
}) |
| 158 |
|
| 159 |
t.Run("empty domain", func(t *testing.T) { |
| 160 |
withJSServer(t, func(t *testing.T, nc *nats.Conn, _ jetstream.JetStream) { |
| 161 |
_, err := jetstream.NewWithDomain(nc, "") |
| 162 |
if err == nil || err.Error() != "domain cannot be empty" { |
| 163 |
t.Fatalf(`Expected error: "domain cannot be empty"; got: %v`, err) |
| 164 |
} |
| 165 |
}) |
| 166 |
}) |
| 167 |
} |
| 168 |
|
| 169 |
func TestJetStreamOptionsReadOnly(t *testing.T) { |
| 170 |
withJSServer(t, func(t *testing.T, nc *nats.Conn, _ jetstream.JetStream) { |
| 171 |
// Test with ClientTrace |
| 172 |
var originalCalled bool |
| 173 |
clientTrace := &jetstream.ClientTrace{ |
| 174 |
RequestSent: func(subj string, payload []byte) { |
| 175 |
originalCalled = true |
| 176 |
}, |
| 177 |
} |
| 178 |
|
| 179 |
js, err := jetstream.New(nc, jetstream.WithClientTrace(clientTrace)) |
| 180 |
if err != nil { |
| 181 |
t.Fatalf("Unexpected error: %v", err) |
| 182 |
} |
| 183 |
|
| 184 |
// Test that modifying basic options doesn't affect the original |
| 185 |
opts := js.Options() |
| 186 |
opts.APIPrefix = "foo" |
| 187 |
opts.Domain = "bar" |
| 188 |
|
| 189 |
opts = js.Options() |
| 190 |
if opts.APIPrefix != "" { |
| 191 |
t.Fatalf("Invalid API prefix; want: %v, got: %v", "", opts.APIPrefix) |
| 192 |
} |
| 193 |
if opts.Domain != "" { |
| 194 |
t.Fatalf("Invalid domain; want: %v, got: %v", "", opts.Domain) |
| 195 |
} |
| 196 |
|
| 197 |
if opts.ClientTrace == nil { |
| 198 |
t.Fatal("Expected ClientTrace to be set in Options()") |
| 199 |
} |
| 200 |
if opts.ClientTrace == clientTrace { |
| 201 |
t.Fatal("ClientTrace should be a copy, not the same instance") |
| 202 |
} |
| 203 |
|
| 204 |
var modifiedCalled bool |
| 205 |
opts.ClientTrace.RequestSent = func(subj string, payload []byte) { |
| 206 |
modifiedCalled = true |
| 207 |
} |
| 208 |
|
| 209 |
ctx := newTesterCtx(t, 5*time.Second) |
| 210 |
_, err = js.CreateStream(ctx, jetstream.StreamConfig{Name: "test", Subjects: []string{"test.*"}}) |
| 211 |
if err != nil { |
| 212 |
t.Fatalf("Unexpected error: %v", err) |
| 213 |
} |
| 214 |
|
| 215 |
if !originalCalled { |
| 216 |
t.Fatal("Original callback should have been called") |
| 217 |
} |
| 218 |
if modifiedCalled { |
| 219 |
t.Fatal("Modified callback should not have been called") |
| 220 |
} |
| 221 |
}) |
| 222 |
} |
| 223 |
|
| 224 |
func TestWithClientTrace(t *testing.T) { |
| 225 |
withJSServer(t, func(t *testing.T, nc *nats.Conn, _ jetstream.JetStream) { |
| 226 |
ctx := newTesterCtx(t, 20*time.Second) |
| 227 |
var sent, received string |
| 228 |
clientTrace := &jetstream.ClientTrace{ |
| 229 |
RequestSent: func(subj string, _ []byte) { |
| 230 |
sent = fmt.Sprintf("Request sent: %s", subj) |
| 231 |
}, |
| 232 |
ResponseReceived: func(subj string, _ []byte, _ nats.Header) { |
| 233 |
received = fmt.Sprintf("Response received: %s", subj) |
| 234 |
}, |
| 235 |
} |
| 236 |
js, err := jetstream.New(nc, jetstream.WithClientTrace(clientTrace)) |
| 237 |
if err != nil { |
| 238 |
t.Fatalf("Unexpected error: %v", err) |
| 239 |
} |
| 240 |
|
| 241 |
_, err = js.CreateStream(ctx, jetstream.StreamConfig{Name: "foo", Subjects: []string{"FOO.123"}}) |
| 242 |
if err != nil { |
| 243 |
t.Fatalf("Unexpected error: %v", err) |
| 244 |
} |
| 245 |
if sent != "Request sent: $JS.API.STREAM.CREATE.foo" { |
| 246 |
t.Fatalf(`Invalid value on sent request trace; want: "Request sent: $JS.API.STREAM.CREATE.foo"; got: %s`, sent) |
| 247 |
} |
| 248 |
if received != "Response received: $JS.API.STREAM.CREATE.foo" { |
| 249 |
t.Fatalf(`Invalid value on response receive trace; want: "Response received: $JS.API.STREAM.CREATE.foo"; got: %s`, received) |
| 250 |
} |
| 251 |
}) |
| 252 |
} |
| 253 |
|
| 254 |
func TestCreateStream(t *testing.T) { |
| 255 |
tests := []struct { |
| 256 |
name string |
| 257 |
stream string |
| 258 |
subject string |
| 259 |
metadata map[string]string |
| 260 |
timeout time.Duration |
| 261 |
withError error |
| 262 |
}{ |
| 263 |
{ |
| 264 |
name: "create stream, ok", |
| 265 |
stream: "foo", |
| 266 |
timeout: 10 * time.Second, |
| 267 |
subject: "FOO.123", |
| 268 |
}, |
| 269 |
{ |
| 270 |
name: "create stream with metadata", |
| 271 |
stream: "foo_meta", |
| 272 |
metadata: map[string]string{ |
| 273 |
"foo": "bar", |
| 274 |
"name": "test", |
| 275 |
}, |
| 276 |
timeout: 10 * time.Second, |
| 277 |
subject: "FOO.meta", |
| 278 |
}, |
| 279 |
{ |
| 280 |
name: "create stream with metadata, reserved prefix", |
| 281 |
stream: "foo_meta1", |
| 282 |
metadata: map[string]string{ |
| 283 |
"foo": "bar", |
| 284 |
"_nats_version": "2.10.0", |
| 285 |
}, |
| 286 |
timeout: 10 * time.Second, |
| 287 |
subject: "FOO.meta1", |
| 288 |
}, |
| 289 |
{ |
| 290 |
name: "with empty context", |
| 291 |
stream: "foo_empty_ctx", |
| 292 |
subject: "FOO.ctx", |
| 293 |
}, |
| 294 |
{ |
| 295 |
name: "invalid stream name", |
| 296 |
stream: "foo.123", |
| 297 |
subject: "FOO.123", |
| 298 |
timeout: 10 * time.Second, |
| 299 |
withError: jetstream.ErrInvalidStreamName, |
| 300 |
}, |
| 301 |
{ |
| 302 |
name: "stream name required", |
| 303 |
stream: "", |
| 304 |
subject: "FOO.123", |
| 305 |
timeout: 10 * time.Second, |
| 306 |
withError: jetstream.ErrStreamNameRequired, |
| 307 |
}, |
| 308 |
{ |
| 309 |
name: "stream name already in use", |
| 310 |
stream: "foo", |
| 311 |
subject: "BAR.123", |
| 312 |
timeout: 10 * time.Second, |
| 313 |
withError: jetstream.ErrStreamNameAlreadyInUse, |
| 314 |
}, |
| 315 |
{ |
| 316 |
name: "context timeout", |
| 317 |
stream: "foo", |
| 318 |
subject: "BAR.123", |
| 319 |
timeout: 1 * time.Microsecond, |
| 320 |
withError: context.DeadlineExceeded, |
| 321 |
}, |
| 322 |
} |
| 323 |
|
| 324 |
withJSServer(t, func(t *testing.T, _ *nats.Conn, js jetstream.JetStream) { |
| 325 |
for _, test := range tests { |
| 326 |
t.Run(test.name, func(t *testing.T) { |
| 327 |
ctx := context.Background() |
| 328 |
if test.timeout > 0 { |
| 329 |
ctx = newTesterCtx(t, test.timeout) |
| 330 |
} |
| 331 |
s, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: test.stream, Subjects: []string{test.subject}, Metadata: test.metadata}) |
| 332 |
if test.withError != nil { |
| 333 |
if !errors.Is(err, test.withError) { |
| 334 |
t.Fatalf("Expected error: %v; got: %v", test.withError, err) |
| 335 |
} |
| 336 |
return |
| 337 |
} |
| 338 |
if err != nil { |
| 339 |
t.Fatalf("Unexpected error: %v", err) |
| 340 |
} |
| 341 |
for k, v := range test.metadata { |
| 342 |
if s.CachedInfo().Config.Metadata[k] != v { |
| 343 |
t.Fatalf("Invalid metadata; want: %v, got: %v", test.metadata, s.CachedInfo().Config.Metadata) |
| 344 |
} |
| 345 |
} |
| 346 |
}) |
| 347 |
} |
| 348 |
}) |
| 349 |
} |
| 350 |
|
| 351 |
func TestCreateStreamMirrorCrossDomains(t *testing.T) { |
| 352 |
test := []struct { |
| 353 |
name string |
| 354 |
streamConfig *jetstream.StreamConfig |
| 355 |
}{ |
| 356 |
{ |
| 357 |
name: "create stream mirror cross domains", |
| 358 |
streamConfig: &jetstream.StreamConfig{ |
| 359 |
Name: "MIRROR", |
| 360 |
Mirror: &jetstream.StreamSource{ |
| 361 |
Name: "TEST", |
| 362 |
Domain: "HUB", |
| 363 |
}, |
| 364 |
}, |
| 365 |
}, |
| 366 |
{ |
| 367 |
name: "create stream with source cross domains", |
| 368 |
streamConfig: &jetstream.StreamConfig{ |
| 369 |
Name: "MIRROR", |
| 370 |
Sources: []*jetstream.StreamSource{ |
| 371 |
{ |
| 372 |
Name: "TEST", |
| 373 |
Domain: "HUB", |
| 374 |
}, |
| 375 |
}, |
| 376 |
}, |
| 377 |
}, |
| 378 |
} |
| 379 |
|
| 380 |
for _, test := range test { |
| 381 |
t.Run(test.name, func(t *testing.T) { |
| 382 |
c := newTester(t) |
| 383 |
host := testserviceHost(t) |
| 384 |
|
| 385 |
hubInst := c.CreateServer(t, true, |
| 386 |
testservice.WithJetStream(`domain: HUB`), |
| 387 |
testservice.WithLeafNode(`leafnodes { port: {{ .Ports.leafnode }} }`), |
| 388 |
testservice.WithAccounts(emptyAccountsBody()), |
| 389 |
testservice.WithSystemAccount(noSystemAccountBody()), |
| 390 |
testservice.WithAuthorization(`# no authorization required`), |
| 391 |
) |
| 392 |
t.Cleanup(func() { hubInst.Destroy(t) }) |
| 393 |
|
| 394 |
hubLeafPort := hubInst.Servers[0].Ports["leafnode"] |
| 395 |
if hubLeafPort == 0 { |
| 396 |
t.Fatalf("hub leafnode port not reserved") |
| 397 |
} |
| 398 |
|
| 399 |
leafInst := c.CreateServer(t, true, |
| 400 |
testservice.WithJetStream(`domain: LEAF`), |
| 401 |
testservice.WithLeafNode(fmt.Sprintf(`leafnodes { remotes = [ { url: "leaf://%s:%d" } ] }`, host, hubLeafPort)), |
| 402 |
testservice.WithAccounts(emptyAccountsBody()), |
| 403 |
testservice.WithSystemAccount(noSystemAccountBody()), |
| 404 |
testservice.WithAuthorization(`# no authorization required`), |
| 405 |
) |
| 406 |
t.Cleanup(func() { leafInst.Destroy(t) }) |
| 407 |
|
| 408 |
nc := dialInstance(t, hubInst) |
| 409 |
c.WaitForJetStream(t, nc) |
| 410 |
|
| 411 |
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) |
| 412 |
defer cancel() |
| 413 |
js, err := jetstream.New(nc) |
| 414 |
if err != nil { |
| 415 |
t.Fatalf("Unexpected error: %v", err) |
| 416 |
} |
| 417 |
|
| 418 |
_, err = js.CreateStream(ctx, jetstream.StreamConfig{ |
| 419 |
Name: "TEST", |
| 420 |
Subjects: []string{"foo"}, |
| 421 |
}) |
| 422 |
if err != nil { |
| 423 |
t.Fatalf("Unexpected error: %v", err) |
| 424 |
} |
| 425 |
if _, err := js.Publish(ctx, "foo", []byte("msg1")); err != nil { |
| 426 |
t.Fatalf("Unexpected error: %v", err) |
| 427 |
} |
| 428 |
if _, err := js.Publish(ctx, "foo", []byte("msg2")); err != nil { |
| 429 |
t.Fatalf("Unexpected error: %v", err) |
| 430 |
} |
| 431 |
|
| 432 |
lnc := dialInstance(t, leafInst) |
| 433 |
c.WaitForJetStream(t, lnc) |
| 434 |
ljs, err := jetstream.New(lnc) |
| 435 |
if err != nil { |
| 436 |
t.Fatalf("Unexpected error: %v", err) |
| 437 |
} |
| 438 |
|
| 439 |
ccfg := *test.streamConfig |
| 440 |
_, err = ljs.CreateStream(ctx, ccfg) |
| 441 |
if err != nil { |
| 442 |
t.Fatalf("Unexpected error: %v", err) |
| 443 |
} |
| 444 |
if !reflect.DeepEqual(test.streamConfig, &ccfg) { |
| 445 |
t.Fatalf("Did not expect config to be altered: %+v vs %+v", test.streamConfig, ccfg) |
| 446 |
} |
| 447 |
|
| 448 |
// Make sure we sync. |
| 449 |
checkFor(t, 5*time.Second, 15*time.Millisecond, func() error { |
| 450 |
lStream, err := ljs.Stream(ctx, "MIRROR") |
| 451 |
if err != nil { |
| 452 |
t.Fatalf("Unexpected error: %v", err) |
| 453 |
} |
| 454 |
|
| 455 |
if lStream.CachedInfo().State.Msgs == 2 { |
| 456 |
return nil |
| 457 |
} |
| 458 |
return fmt.Errorf("Did not get synced messages: %d", lStream.CachedInfo().State.Msgs) |
| 459 |
}) |
| 460 |
if _, err := ljs.Publish(ctx, "foo", []byte("msg3")); err != nil { |
| 461 |
t.Fatalf("Unexpected error: %v", err) |
| 462 |
} |
| 463 |
lStream, err := ljs.Stream(ctx, "MIRROR") |
| 464 |
if err != nil { |
| 465 |
t.Fatalf("Unexpected error: %v", err) |
| 466 |
} |
| 467 |
checkFor(t, 5*time.Second, 15*time.Millisecond, func() error { |
| 468 |
info, err := lStream.Info(ctx) |
| 469 |
if err != nil { |
| 470 |
return fmt.Errorf("Unexpected error when getting stream info: %v", err) |
| 471 |
} |
| 472 |
if info.State.Msgs != 3 { |
| 473 |
return fmt.Errorf("Expected 3 msgs in stream; got: %d", lStream.CachedInfo().State.Msgs) |
| 474 |
} |
| 475 |
return nil |
| 476 |
}) |
| 477 |
|
| 478 |
rjs, err := jetstream.NewWithDomain(lnc, "HUB") |
| 479 |
if err != nil { |
| 480 |
t.Fatalf("Unexpected error: %v", err) |
| 481 |
} |
| 482 |
|
| 483 |
_, err = rjs.Stream(ctx, "TEST") |
| 484 |
if err != nil { |
| 485 |
t.Fatalf("Unexpected error: %v", err) |
| 486 |
} |
| 487 |
if _, err := rjs.Publish(ctx, "foo", []byte("msg4")); err != nil { |
| 488 |
t.Fatalf("Unexpected error: %v", err) |
| 489 |
} |
| 490 |
rStream, err := rjs.Stream(ctx, "TEST") |
| 491 |
if err != nil { |
| 492 |
t.Fatalf("Unexpected error: %v", err) |
| 493 |
} |
| 494 |
|
| 495 |
if rStream.CachedInfo().State.Msgs != 4 { |
| 496 |
t.Fatalf("Expected 4 msgs in stream; got: %d", rStream.CachedInfo().State.Msgs) |
| 497 |
} |
| 498 |
}) |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
func TestCreateOrUpdateStream(t *testing.T) { |
| 503 |
tests := []struct { |
| 504 |
name string |
| 505 |
stream string |
| 506 |
subject string |
| 507 |
timeout time.Duration |
| 508 |
withError error |
| 509 |
withInfoCheck bool |
| 510 |
}{ |
| 511 |
{ |
| 512 |
name: "create stream ok", |
| 513 |
stream: "foo", |
| 514 |
timeout: 10 * time.Second, |
| 515 |
subject: "FOO.1", |
| 516 |
withInfoCheck: false, |
| 517 |
}, |
| 518 |
{ |
| 519 |
name: "create stream empty context", |
| 520 |
stream: "foo-o", |
| 521 |
subject: "FOO.12", |
| 522 |
withInfoCheck: false, |
| 523 |
}, |
| 524 |
{ |
| 525 |
name: "create stream invalid stream name", |
| 526 |
stream: "foo.123", |
| 527 |
subject: "FOO-123", |
| 528 |
timeout: 10 * time.Second, |
| 529 |
withError: jetstream.ErrInvalidStreamName, |
| 530 |
withInfoCheck: false, |
| 531 |
}, |
| 532 |
{ |
| 533 |
name: "create stream stream name required", |
| 534 |
stream: "", |
| 535 |
subject: "FOO-1234", |
| 536 |
timeout: 10 * time.Second, |
| 537 |
withError: jetstream.ErrStreamNameRequired, |
| 538 |
withInfoCheck: false, |
| 539 |
}, |
| 540 |
{ |
| 541 |
name: "update stream ok", |
| 542 |
stream: "foo", |
| 543 |
subject: "BAR-123", |
| 544 |
timeout: 10 * time.Second, |
| 545 |
withInfoCheck: true, |
| 546 |
}, |
| 547 |
{ |
| 548 |
name: "create stream context timeout", |
| 549 |
stream: "foo", |
| 550 |
subject: "BAR-1234", |
| 551 |
timeout: 1 * time.Microsecond, |
| 552 |
withError: context.DeadlineExceeded, |
| 553 |
withInfoCheck: false, |
| 554 |
}, |
| 555 |
{ |
| 556 |
name: "update stream with empty context", |
| 557 |
stream: "sample-foo-1", |
| 558 |
subject: "SAMPLE-FOO-123", |
| 559 |
withInfoCheck: true, |
| 560 |
}, |
| 561 |
{ |
| 562 |
name: "update stream invalid stream name", |
| 563 |
stream: "sample-foo.123", |
| 564 |
subject: "SAMPLE-FOO-1234", |
| 565 |
timeout: 10 * time.Second, |
| 566 |
withError: jetstream.ErrInvalidStreamName, |
| 567 |
withInfoCheck: true, |
| 568 |
}, |
| 569 |
{ |
| 570 |
name: "update stream stream name required", |
| 571 |
stream: "", |
| 572 |
subject: "SAMPLE-FOO-123", |
| 573 |
timeout: 10 * time.Second, |
| 574 |
withError: jetstream.ErrStreamNameRequired, |
| 575 |
withInfoCheck: true, |
| 576 |
}, |
| 577 |
{ |
| 578 |
name: "update stream context timeout", |
| 579 |
stream: "sample-foo-2", |
| 580 |
subject: "SAMPLE-FOO-123456", |
| 581 |
timeout: 1 * time.Microsecond, |
| 582 |
withError: context.DeadlineExceeded, |
| 583 |
withInfoCheck: true, |
| 584 |
}, |
| 585 |
} |
| 586 |
|
| 587 |
withJSServer(t, func(t *testing.T, _ *nats.Conn, js jetstream.JetStream) { |
| 588 |
for _, test := range tests { |
| 589 |
t.Run(test.name, func(t *testing.T) { |
| 590 |
ctx := context.Background() |
| 591 |
if test.timeout > 0 { |
| 592 |
ctx = newTesterCtx(t, test.timeout) |
| 593 |
} |
| 594 |
s, err := js.CreateOrUpdateStream(ctx, jetstream.StreamConfig{Name: test.stream, Subjects: []string{test.subject}}) |
| 595 |
if test.withError != nil { |
| 596 |
if !errors.Is(err, test.withError) { |
| 597 |
t.Fatalf("Expected error: %v; got: %v", test.withError, err) |
| 598 |
} |
| 599 |
return |
| 600 |
} |
| 601 |
if err != nil { |
| 602 |
t.Fatalf("Unexpected error: %v", err) |
| 603 |
} |
| 604 |
|
| 605 |
if test.withInfoCheck { |
| 606 |
info, err := s.Info(ctx) |
| 607 |
if err != nil { |
| 608 |
t.Fatalf("Unexpected error: %v", err) |
| 609 |
} |
| 610 |
if len(info.Config.Subjects) != 1 || info.Config.Subjects[0] != test.subject { |
| 611 |
t.Fatalf("Invalid stream subjects after update: %v", info.Config.Subjects) |
| 612 |
} |
| 613 |
} |
| 614 |
}) |
| 615 |
} |
| 616 |
}) |
| 617 |
} |
| 618 |
|
| 619 |
func TestUpdateStream(t *testing.T) { |
| 620 |
tests := []struct { |
| 621 |
name string |
| 622 |
stream string |
| 623 |
subject string |
| 624 |
metadata map[string]string |
| 625 |
timeout time.Duration |
| 626 |
withError error |
| 627 |
}{ |
| 628 |
{ |
| 629 |
name: "update existing stream", |
| 630 |
stream: "foo", |
| 631 |
subject: "BAR.123", |
| 632 |
timeout: 10 * time.Second, |
| 633 |
}, |
| 634 |
{ |
| 635 |
name: "with empty context", |
| 636 |
stream: "foo", |
| 637 |
subject: "FOO.123", |
| 638 |
}, |
| 639 |
{ |
| 640 |
name: "update existing, add metadata", |
| 641 |
stream: "foo", |
| 642 |
subject: "BAR.123", |
| 643 |
metadata: map[string]string{ |
| 644 |
"foo": "bar", |
| 645 |
"name": "test", |
| 646 |
}, |
| 647 |
timeout: 10 * time.Second, |
| 648 |
}, |
| 649 |
{ |
| 650 |
name: "invalid stream name", |
| 651 |
stream: "foo.123", |
| 652 |
subject: "FOO.123", |
| 653 |
timeout: 10 * time.Second, |
| 654 |
withError: jetstream.ErrInvalidStreamName, |
| 655 |
}, |
| 656 |
{ |
| 657 |
name: "stream name required", |
| 658 |
stream: "", |
| 659 |
subject: "FOO.123", |
| 660 |
timeout: 10 * time.Second, |
| 661 |
withError: jetstream.ErrStreamNameRequired, |
| 662 |
}, |
| 663 |
{ |
| 664 |
name: "stream not found", |
| 665 |
stream: "bar", |
| 666 |
subject: "FOO.123", |
| 667 |
timeout: 10 * time.Second, |
| 668 |
withError: jetstream.ErrStreamNotFound, |
| 669 |
}, |
| 670 |
{ |
| 671 |
name: "context timeout", |
| 672 |
stream: "foo", |
| 673 |
subject: "FOO.123", |
| 674 |
timeout: 1 * time.Microsecond, |
| 675 |
withError: context.DeadlineExceeded, |
| 676 |
}, |
| 677 |
} |
| 678 |
|
| 679 |
withJSServer(t, func(t *testing.T, _ *nats.Conn, js jetstream.JetStream) { |
| 680 |
_, err := js.CreateStream(context.Background(), jetstream.StreamConfig{Name: "foo", Subjects: []string{"FOO.123"}}) |
| 681 |
if err != nil { |
| 682 |
t.Fatalf("Unexpected error: %v", err) |
| 683 |
} |
| 684 |
for _, test := range tests { |
| 685 |
t.Run(test.name, func(t *testing.T) { |
| 686 |
ctx := context.Background() |
| 687 |
if test.timeout > 0 { |
| 688 |
ctx = newTesterCtx(t, test.timeout) |
| 689 |
} |
| 690 |
s, err := js.UpdateStream(ctx, jetstream.StreamConfig{Name: test.stream, Subjects: []string{test.subject}, Metadata: test.metadata}) |
| 691 |
if test.withError != nil { |
| 692 |
if !errors.Is(err, test.withError) { |
| 693 |
t.Fatalf("Expected error: %v; got: %v", test.withError, err) |
| 694 |
} |
| 695 |
return |
| 696 |
} |
| 697 |
if err != nil { |
| 698 |
t.Fatalf("Unexpected error: %v", err) |
| 699 |
} |
| 700 |
info, err := s.Info(ctx) |
| 701 |
if err != nil { |
| 702 |
t.Fatalf("Unexpected error: %v", err) |
| 703 |
} |
| 704 |
if len(info.Config.Subjects) != 1 || info.Config.Subjects[0] != test.subject { |
| 705 |
t.Fatalf("Invalid stream subjects after update: %v", info.Config.Subjects) |
| 706 |
} |
| 707 |
for k, v := range test.metadata { |
| 708 |
if info.Config.Metadata[k] != v { |
| 709 |
t.Fatalf("Invalid metadata; want: %v, got: %v", test.metadata, info.Config.Metadata) |
| 710 |
} |
| 711 |
} |
| 712 |
}) |
| 713 |
} |
| 714 |
}) |
| 715 |
} |
| 716 |
|
| 717 |
func TestStream(t *testing.T) { |
| 718 |
tests := []struct { |
| 719 |
name string |
| 720 |
stream string |
| 721 |
subject string |
| 722 |
timeout time.Duration |
| 723 |
withError error |
| 724 |
}{ |
| 725 |
{ |
| 726 |
name: "get existing stream", |
| 727 |
stream: "foo", |
| 728 |
timeout: 10 * time.Second, |
| 729 |
}, |
| 730 |
{ |
| 731 |
name: "with empty context", |
| 732 |
stream: "foo", |
| 733 |
}, |
| 734 |
{ |
| 735 |
name: "invalid stream name", |
| 736 |
stream: "foo.123", |
| 737 |
timeout: 10 * time.Second, |
| 738 |
withError: jetstream.ErrInvalidStreamName, |
| 739 |
}, |
| 740 |
{ |
| 741 |
name: "stream name required", |
| 742 |
stream: "", |
| 743 |
timeout: 10 * time.Second, |
| 744 |
withError: jetstream.ErrStreamNameRequired, |
| 745 |
}, |
| 746 |
{ |
| 747 |
name: "stream not found", |
| 748 |
stream: "bar", |
| 749 |
timeout: 10 * time.Second, |
| 750 |
withError: jetstream.ErrStreamNotFound, |
| 751 |
}, |
| 752 |
{ |
| 753 |
name: "context timeout", |
| 754 |
stream: "foo", |
| 755 |
timeout: 1 * time.Microsecond, |
| 756 |
withError: context.DeadlineExceeded, |
| 757 |
}, |
| 758 |
} |
| 759 |
withJSServer(t, func(t *testing.T, _ *nats.Conn, js jetstream.JetStream) { |
| 760 |
_, err := js.CreateStream(context.Background(), jetstream.StreamConfig{Name: "foo", Subjects: []string{"FOO.123"}}) |
| 761 |
if err != nil { |
| 762 |
t.Fatalf("Unexpected error: %v", err) |
| 763 |
} |
| 764 |
|
| 765 |
for _, test := range tests { |
| 766 |
t.Run(test.name, func(t *testing.T) { |
| 767 |
ctx := context.Background() |
| 768 |
if test.timeout > 0 { |
| 769 |
ctx = newTesterCtx(t, test.timeout) |
| 770 |
} |
| 771 |
s, err := js.Stream(ctx, test.stream) |
| 772 |
if test.withError != nil { |
| 773 |
if !errors.Is(err, test.withError) { |
| 774 |
t.Fatalf("Expected error: %v; got: %v", test.withError, err) |
| 775 |
} |
| 776 |
return |
| 777 |
} |
| 778 |
if err != nil { |
| 779 |
t.Fatalf("Unexpected error: %v", err) |
| 780 |
} |
| 781 |
if s.CachedInfo().Config.Name != test.stream { |
| 782 |
t.Fatalf("Invalid stream fetched; want: foo; got: %s", s.CachedInfo().Config.Name) |
| 783 |
} |
| 784 |
}) |
| 785 |
} |
| 786 |
}) |
| 787 |
} |
| 788 |
|
| 789 |
func TestDeleteStream(t *testing.T) { |
| 790 |
tests := []struct { |
| 791 |
name string |
| 792 |
stream string |
| 793 |
subject string |
| 794 |
timeout time.Duration |
| 795 |
withError error |
| 796 |
}{ |
| 797 |
{ |
| 798 |
name: "delete existing stream", |
| 799 |
stream: "foo", |
| 800 |
timeout: 10 * time.Second, |
| 801 |
}, |
| 802 |
{ |
| 803 |
name: "with empty context", |
| 804 |
stream: "bar", |
| 805 |
}, |
| 806 |
{ |
| 807 |
name: "invalid stream name", |
| 808 |
stream: "foo.123", |
| 809 |
timeout: 10 * time.Second, |
| 810 |
withError: jetstream.ErrInvalidStreamName, |
| 811 |
}, |
| 812 |
{ |
| 813 |
name: "stream name required", |
| 814 |
stream: "", |
| 815 |
timeout: 10 * time.Second, |
| 816 |
withError: jetstream.ErrStreamNameRequired, |
| 817 |
}, |
| 818 |
{ |
| 819 |
name: "stream not found", |
| 820 |
stream: "foo", |
| 821 |
timeout: 10 * time.Second, |
| 822 |
withError: jetstream.ErrStreamNotFound, |
| 823 |
}, |
| 824 |
} |
| 825 |
withJSServer(t, func(t *testing.T, _ *nats.Conn, js jetstream.JetStream) { |
| 826 |
_, err := js.CreateStream(context.Background(), jetstream.StreamConfig{Name: "foo", Subjects: []string{"FOO.123"}}) |
| 827 |
if err != nil { |
| 828 |
t.Fatalf("Unexpected error: %v", err) |
| 829 |
} |
| 830 |
|
| 831 |
_, err = js.CreateStream(context.Background(), jetstream.StreamConfig{Name: "bar", Subjects: []string{"BAR.123"}}) |
| 832 |
if err != nil { |
| 833 |
t.Fatalf("Unexpected error: %v", err) |
| 834 |
} |
| 835 |
|
| 836 |
for _, test := range tests { |
| 837 |
t.Run(test.name, func(t *testing.T) { |
| 838 |
ctx := context.Background() |
| 839 |
if test.timeout > 0 { |
| 840 |
ctx = newTesterCtx(t, 20*time.Second) |
| 841 |
} |
| 842 |
err := js.DeleteStream(ctx, test.stream) |
| 843 |
if test.withError != nil { |
| 844 |
if !errors.Is(err, test.withError) { |
| 845 |
t.Fatalf("Expected error: %v; got: %v", test.withError, err) |
| 846 |
} |
| 847 |
return |
| 848 |
} |
| 849 |
if err != nil { |
| 850 |
t.Fatalf("Unexpected error: %v", err) |
| 851 |
} |
| 852 |
}) |
| 853 |
} |
| 854 |
}) |
| 855 |
} |
| 856 |
|
| 857 |
func TestAccountInfo(t *testing.T) { |
| 858 |
t.Run("fetch account info", func(t *testing.T) { |
| 859 |
withJSServer(t, func(t *testing.T, _ *nats.Conn, js jetstream.JetStream) { |
| 860 |
ctx := newTesterCtx(t, 20*time.Second) |
| 861 |
_, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: "foo", Subjects: []string{"FOO.123"}}) |
| 862 |
if err != nil { |
| 863 |
t.Fatalf("Unexpected error: %v", err) |
| 864 |
} |
| 865 |
info, err := js.AccountInfo(ctx) |
| 866 |
if err != nil { |
| 867 |
t.Fatalf("Unexpected error: %v", err) |
| 868 |
} |
| 869 |
|
| 870 |
if info.Streams != 1 { |
| 871 |
t.Fatalf("Invalid number of streams; want: 1; got: %d", info.Streams) |
| 872 |
} |
| 873 |
}) |
| 874 |
}) |
| 875 |
|
| 876 |
t.Run("account limits fields", func(t *testing.T) { |
| 877 |
withJSServer(t, func(t *testing.T, _ *nats.Conn, js jetstream.JetStream) { |
| 878 |
ctx := newTesterCtx(t, 20*time.Second) |
| 879 |
|
| 880 |
info, err := js.AccountInfo(ctx) |
| 881 |
if err != nil { |
| 882 |
t.Fatalf("Unexpected error: %v", err) |
| 883 |
} |
| 884 |
|
| 885 |
expectedLimits := jetstream.AccountLimits{ |
| 886 |
MaxMemory: -1, |
| 887 |
MaxStore: -1, |
| 888 |
MaxStreams: -1, |
| 889 |
MaxConsumers: -1, |
| 890 |
MaxAckPending: -1, |
| 891 |
MemoryMaxStreamBytes: -1, |
| 892 |
StoreMaxStreamBytes: -1, |
| 893 |
MaxBytesRequired: false, |
| 894 |
} |
| 895 |
if info.Limits != expectedLimits { |
| 896 |
t.Fatalf("Expected limits %+v, got: %+v", expectedLimits, info.Limits) |
| 897 |
} |
| 898 |
}) |
| 899 |
}) |
| 900 |
|
| 901 |
t.Run("jetstream not enabled on server", func(t *testing.T) { |
| 902 |
c := newTester(t) |
| 903 |
inst := c.CreateServer(t, false) |
| 904 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 905 |
|
| 906 |
nc := dialInstance(t, inst) |
| 907 |
js, err := jetstream.New(nc) |
| 908 |
if err != nil { |
| 909 |
t.Fatalf("Unexpected error: %v", err) |
| 910 |
} |
| 911 |
|
| 912 |
ctx := newTesterCtx(t, 20*time.Second) |
| 913 |
_, err = js.AccountInfo(ctx) |
| 914 |
if err == nil || !errors.Is(err, jetstream.ErrJetStreamNotEnabled) { |
| 915 |
t.Fatalf("Expected error: %v; got: %v", jetstream.ErrJetStreamNotEnabled, err) |
| 916 |
} |
| 917 |
}) |
| 918 |
|
| 919 |
t.Run("jetstream not enabled for account", func(t *testing.T) { |
| 920 |
c := newTester(t) |
| 921 |
accounts := `accounts: { |
| 922 |
JS: { |
| 923 |
jetstream: disabled |
| 924 |
users: [ {user: foo, password: bar} ] |
| 925 |
}, |
| 926 |
}` |
| 927 |
inst := c.CreateServer(t, true, |
| 928 |
testservice.WithAccounts(accounts), |
| 929 |
testservice.WithAuthorization("no_auth_user: foo"), |
| 930 |
testservice.WithSystemAccount("# system_account intentionally unset"), |
| 931 |
) |
| 932 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 933 |
|
| 934 |
nc := dialInstance(t, inst) |
| 935 |
js, err := jetstream.New(nc) |
| 936 |
if err != nil { |
| 937 |
t.Fatalf("Unexpected error: %v", err) |
| 938 |
} |
| 939 |
|
| 940 |
ctx := newTesterCtx(t, 20*time.Second) |
| 941 |
_, err = js.AccountInfo(ctx) |
| 942 |
if err == nil || !errors.Is(err, jetstream.ErrJetStreamNotEnabledForAccount) { |
| 943 |
t.Fatalf("Expected error: %v; got: %v", jetstream.ErrJetStreamNotEnabledForAccount, err) |
| 944 |
} |
| 945 |
}) |
| 946 |
} |
| 947 |
|
| 948 |
func TestListStreams(t *testing.T) { |
| 949 |
tests := []struct { |
| 950 |
name string |
| 951 |
streamsNum int |
| 952 |
timeout time.Duration |
| 953 |
subject string |
| 954 |
expected int |
| 955 |
withError error |
| 956 |
}{ |
| 957 |
{ |
| 958 |
name: "list streams", |
| 959 |
streamsNum: 260, |
| 960 |
timeout: 10 * time.Second, |
| 961 |
expected: 260, |
| 962 |
}, |
| 963 |
{ |
| 964 |
name: "with empty context", |
| 965 |
streamsNum: 260, |
| 966 |
expected: 260, |
| 967 |
}, |
| 968 |
{ |
| 969 |
name: "no stream available", |
| 970 |
timeout: 10 * time.Second, |
| 971 |
streamsNum: 0, |
| 972 |
expected: 0, |
| 973 |
}, |
| 974 |
{ |
| 975 |
name: "list streams with subject filter", |
| 976 |
subject: "FOO.123", |
| 977 |
streamsNum: 260, |
| 978 |
expected: 1, |
| 979 |
}, |
| 980 |
{ |
| 981 |
name: "list streams with subject filter, no match", |
| 982 |
subject: "FOO.500", |
| 983 |
streamsNum: 100, |
| 984 |
expected: 0, |
| 985 |
}, |
| 986 |
{ |
| 987 |
name: "context timeout", |
| 988 |
streamsNum: 260, |
| 989 |
timeout: 1 * time.Microsecond, |
| 990 |
withError: context.DeadlineExceeded, |
| 991 |
}, |
| 992 |
} |
| 993 |
|
| 994 |
for _, test := range tests { |
| 995 |
t.Run(test.name, func(t *testing.T) { |
| 996 |
withJSServer(t, func(t *testing.T, _ *nats.Conn, js jetstream.JetStream) { |
| 997 |
ctx := context.Background() |
| 998 |
if test.timeout > 0 { |
| 999 |
ctx = newTesterCtx(t, test.timeout) |
| 1000 |
} |
| 1001 |
for i := range test.streamsNum { |
| 1002 |
_, err := js.CreateStream(context.Background(), jetstream.StreamConfig{Name: fmt.Sprintf("foo%d", i), Subjects: []string{fmt.Sprintf("FOO.%d", i)}}) |
| 1003 |
if err != nil { |
| 1004 |
t.Fatalf("Unexpected error: %v", err) |
| 1005 |
} |
| 1006 |
} |
| 1007 |
opts := []jetstream.StreamListOpt{} |
| 1008 |
if test.subject != "" { |
| 1009 |
opts = append(opts, jetstream.WithStreamListSubject(test.subject)) |
| 1010 |
} |
| 1011 |
streamsList := js.ListStreams(ctx, opts...) |
| 1012 |
streams := make([]*jetstream.StreamInfo, 0) |
| 1013 |
for si := range streamsList.Info() { |
| 1014 |
streams = append(streams, si) |
| 1015 |
} |
| 1016 |
if test.withError != nil { |
| 1017 |
if !errors.Is(streamsList.Err(), test.withError) { |
| 1018 |
t.Fatalf("Expected error: %v; got: %v", test.withError, streamsList.Err()) |
| 1019 |
} |
| 1020 |
return |
| 1021 |
} |
| 1022 |
if streamsList.Err() != nil { |
| 1023 |
t.Fatalf("Unexpected error: %s", streamsList.Err()) |
| 1024 |
} |
| 1025 |
if len(streams) != test.expected { |
| 1026 |
t.Fatalf("Wrong number of streams; want: %d; got: %d", test.streamsNum, len(streams)) |
| 1027 |
} |
| 1028 |
}) |
| 1029 |
}) |
| 1030 |
} |
| 1031 |
} |
| 1032 |
|
| 1033 |
func TestStreamNames(t *testing.T) { |
| 1034 |
tests := []struct { |
| 1035 |
name string |
| 1036 |
streamsNum int |
| 1037 |
subject string |
| 1038 |
expected int |
| 1039 |
timeout time.Duration |
| 1040 |
withError error |
| 1041 |
}{ |
| 1042 |
{ |
| 1043 |
name: "list streams", |
| 1044 |
streamsNum: 500, |
| 1045 |
timeout: 10 * time.Second, |
| 1046 |
expected: 500, |
| 1047 |
}, |
| 1048 |
{ |
| 1049 |
name: "with empty context", |
| 1050 |
streamsNum: 500, |
| 1051 |
expected: 500, |
| 1052 |
}, |
| 1053 |
{ |
| 1054 |
name: "no stream available", |
| 1055 |
streamsNum: 0, |
| 1056 |
expected: 0, |
| 1057 |
timeout: 10 * time.Second, |
| 1058 |
}, |
| 1059 |
{ |
| 1060 |
name: "list streams with subject filter", |
| 1061 |
subject: "FOO.123", |
| 1062 |
streamsNum: 260, |
| 1063 |
expected: 1, |
| 1064 |
}, |
| 1065 |
{ |
| 1066 |
name: "list streams with subject filter, no match", |
| 1067 |
subject: "FOO.500", |
| 1068 |
streamsNum: 100, |
| 1069 |
expected: 0, |
| 1070 |
timeout: 10 * time.Second, |
| 1071 |
}, |
| 1072 |
{ |
| 1073 |
name: "context timeout", |
| 1074 |
streamsNum: 500, |
| 1075 |
timeout: 1 * time.Microsecond, |
| 1076 |
withError: context.DeadlineExceeded, |
| 1077 |
}, |
| 1078 |
} |
| 1079 |
|
| 1080 |
for _, test := range tests { |
| 1081 |
t.Run(test.name, func(t *testing.T) { |
| 1082 |
withJSServer(t, func(t *testing.T, _ *nats.Conn, js jetstream.JetStream) { |
| 1083 |
ctx := context.Background() |
| 1084 |
if test.timeout > 0 { |
| 1085 |
ctx = newTesterCtx(t, test.timeout) |
| 1086 |
} |
| 1087 |
for i := range test.streamsNum { |
| 1088 |
_, err := js.CreateStream(context.Background(), jetstream.StreamConfig{Name: fmt.Sprintf("foo%d", i), Subjects: []string{fmt.Sprintf("FOO.%d", i)}}) |
| 1089 |
if err != nil { |
| 1090 |
t.Fatalf("Unexpected error: %v", err) |
| 1091 |
} |
| 1092 |
} |
| 1093 |
opts := []jetstream.StreamListOpt{} |
| 1094 |
if test.subject != "" { |
| 1095 |
opts = append(opts, jetstream.WithStreamListSubject(test.subject)) |
| 1096 |
} |
| 1097 |
streamsList := js.StreamNames(ctx, opts...) |
| 1098 |
streams := make([]string, 0) |
| 1099 |
for s := range streamsList.Name() { |
| 1100 |
streams = append(streams, s) |
| 1101 |
} |
| 1102 |
if test.withError != nil { |
| 1103 |
if !errors.Is(streamsList.Err(), test.withError) { |
| 1104 |
t.Fatalf("Expected error: %v; got: %v", test.withError, streamsList.Err()) |
| 1105 |
} |
| 1106 |
return |
| 1107 |
} |
| 1108 |
if streamsList.Err() != nil { |
| 1109 |
t.Fatalf("Unexpected error: %s", streamsList.Err()) |
| 1110 |
} |
| 1111 |
if len(streams) != test.expected { |
| 1112 |
t.Fatalf("Wrong number of streams; want: %d; got: %d", test.streamsNum, len(streams)) |
| 1113 |
} |
| 1114 |
}) |
| 1115 |
}) |
| 1116 |
} |
| 1117 |
} |
| 1118 |
|
| 1119 |
func TestJetStream_CreateOrUpdateConsumer(t *testing.T) { |
| 1120 |
tests := []struct { |
| 1121 |
name string |
| 1122 |
stream string |
| 1123 |
consumerConfig jetstream.ConsumerConfig |
| 1124 |
shouldCreate bool |
| 1125 |
timeout time.Duration |
| 1126 |
withError error |
| 1127 |
}{ |
| 1128 |
{ |
| 1129 |
name: "create durable pull consumer", |
| 1130 |
stream: "foo", |
| 1131 |
consumerConfig: jetstream.ConsumerConfig{Durable: "dur", AckPolicy: jetstream.AckExplicitPolicy}, |
| 1132 |
timeout: 10 * time.Second, |
| 1133 |
shouldCreate: true, |
| 1134 |
}, |
| 1135 |
{ |
| 1136 |
name: "create ephemeral pull consumer", |
| 1137 |
stream: "foo", |
| 1138 |
consumerConfig: jetstream.ConsumerConfig{AckPolicy: jetstream.AckExplicitPolicy}, |
| 1139 |
timeout: 10 * time.Second, |
| 1140 |
shouldCreate: true, |
| 1141 |
}, |
| 1142 |
{ |
| 1143 |
name: "with empty context", |
| 1144 |
consumerConfig: jetstream.ConsumerConfig{AckPolicy: jetstream.AckExplicitPolicy}, |
| 1145 |
stream: "foo", |
| 1146 |
shouldCreate: true, |
| 1147 |
}, |
| 1148 |
{ |
| 1149 |
name: "consumer already exists, update", |
| 1150 |
stream: "foo", |
| 1151 |
consumerConfig: jetstream.ConsumerConfig{Durable: "dur", AckPolicy: jetstream.AckExplicitPolicy, Description: "test consumer"}, |
| 1152 |
}, |
| 1153 |
{ |
| 1154 |
name: "consumer already exists, illegal update", |
| 1155 |
stream: "foo", |
| 1156 |
consumerConfig: jetstream.ConsumerConfig{Durable: "dur", AckPolicy: jetstream.AckNonePolicy, Description: "test consumer"}, |
| 1157 |
withError: jetstream.ErrConsumerCreate, |
| 1158 |
}, |
| 1159 |
{ |
| 1160 |
name: "stream does not exist", |
| 1161 |
stream: "abc", |
| 1162 |
withError: jetstream.ErrStreamNotFound, |
| 1163 |
}, |
| 1164 |
{ |
| 1165 |
name: "invalid stream name", |
| 1166 |
stream: "foo.1", |
| 1167 |
withError: jetstream.ErrInvalidStreamName, |
| 1168 |
}, |
| 1169 |
{ |
| 1170 |
name: "invalid durable name", |
| 1171 |
stream: "foo", |
| 1172 |
consumerConfig: jetstream.ConsumerConfig{Durable: "dur.123", AckPolicy: jetstream.AckExplicitPolicy}, |
| 1173 |
withError: jetstream.ErrInvalidConsumerName, |
| 1174 |
}, |
| 1175 |
{ |
| 1176 |
name: "context timeout", |
| 1177 |
consumerConfig: jetstream.ConsumerConfig{AckPolicy: jetstream.AckExplicitPolicy}, |
| 1178 |
stream: "foo", |
| 1179 |
timeout: 1 * time.Microsecond, |
| 1180 |
withError: context.DeadlineExceeded, |
| 1181 |
}, |
| 1182 |
} |
| 1183 |
|
| 1184 |
withJSServer(t, func(t *testing.T, nc *nats.Conn, js jetstream.JetStream) { |
| 1185 |
for _, consType := range []string{"pull", "push"} { |
| 1186 |
_, err := js.CreateStream(context.Background(), jetstream.StreamConfig{Name: "foo", Subjects: []string{"FOO.*"}}) |
| 1187 |
if err != nil { |
| 1188 |
t.Fatalf("Unexpected error: %v", err) |
| 1189 |
} |
| 1190 |
|
| 1191 |
for _, test := range tests { |
| 1192 |
t.Run(fmt.Sprintf("%s %s", consType, test.name), func(t *testing.T) { |
| 1193 |
ctx := context.Background() |
| 1194 |
if test.timeout > 0 { |
| 1195 |
ctx = newTesterCtx(t, test.timeout) |
| 1196 |
} |
| 1197 |
var sub *nats.Subscription |
| 1198 |
if test.consumerConfig.FilterSubject != "" { |
| 1199 |
sub, err = nc.SubscribeSync(fmt.Sprintf("$JS.API.CONSUMER.CREATE.foo.*.%s", test.consumerConfig.FilterSubject)) |
| 1200 |
} else { |
| 1201 |
sub, err = nc.SubscribeSync("$JS.API.CONSUMER.CREATE.foo.*") |
| 1202 |
} |
| 1203 |
var c consInfoConstraint |
| 1204 |
if consType == "pull" { |
| 1205 |
c, err = js.CreateOrUpdateConsumer(ctx, test.stream, test.consumerConfig) |
| 1206 |
} else { |
| 1207 |
test.consumerConfig.DeliverSubject = "inbox" |
| 1208 |
c, err = js.CreateOrUpdatePushConsumer(ctx, test.stream, test.consumerConfig) |
| 1209 |
} |
| 1210 |
if test.withError != nil { |
| 1211 |
if err == nil || !errors.Is(err, test.withError) { |
| 1212 |
t.Fatalf("Expected error: %v; got: %v", test.withError, err) |
| 1213 |
} |
| 1214 |
return |
| 1215 |
} |
| 1216 |
if err != nil { |
| 1217 |
t.Fatalf("Unexpected error: %v", err) |
| 1218 |
} |
| 1219 |
if test.shouldCreate { |
| 1220 |
if _, err := sub.NextMsgWithContext(ctx); err != nil { |
| 1221 |
t.Fatalf("Expected request on %s; got %s", sub.Subject, err) |
| 1222 |
} |
| 1223 |
} |
| 1224 |
if consType == "pull" { |
| 1225 |
_, err = js.Consumer(ctx, test.stream, c.CachedInfo().Name) |
| 1226 |
} else { |
| 1227 |
_, err = js.PushConsumer(ctx, test.stream, c.CachedInfo().Name) |
| 1228 |
} |
| 1229 |
if err != nil { |
| 1230 |
t.Fatalf("Unexpected error: %v", err) |
| 1231 |
} |
| 1232 |
}) |
| 1233 |
} |
| 1234 |
if err := js.DeleteStream(context.Background(), "foo"); err != nil { |
| 1235 |
t.Fatalf("Unexpected error: %v", err) |
| 1236 |
} |
| 1237 |
} |
| 1238 |
}) |
| 1239 |
} |
| 1240 |
|
| 1241 |
func TestJetStream_CreateConsumer(t *testing.T) { |
| 1242 |
tests := []struct { |
| 1243 |
name string |
| 1244 |
consumerConfig jetstream.ConsumerConfig |
| 1245 |
shouldCreate bool |
| 1246 |
stream string |
| 1247 |
withError error |
| 1248 |
}{ |
| 1249 |
{ |
| 1250 |
name: "create consumer", |
| 1251 |
consumerConfig: jetstream.ConsumerConfig{Durable: "dur"}, |
| 1252 |
stream: "foo", |
| 1253 |
shouldCreate: true, |
| 1254 |
}, |
| 1255 |
{ |
| 1256 |
name: "consumer already exists, error", |
| 1257 |
consumerConfig: jetstream.ConsumerConfig{Durable: "dur", Description: "test consumer"}, |
| 1258 |
stream: "foo", |
| 1259 |
withError: jetstream.ErrConsumerExists, |
| 1260 |
}, |
| 1261 |
{ |
| 1262 |
name: "stream does not exist", |
| 1263 |
stream: "abc", |
| 1264 |
consumerConfig: jetstream.ConsumerConfig{Durable: "dur"}, |
| 1265 |
withError: jetstream.ErrStreamNotFound, |
| 1266 |
}, |
| 1267 |
{ |
| 1268 |
name: "invalid stream name", |
| 1269 |
stream: "foo.1", |
| 1270 |
withError: jetstream.ErrInvalidStreamName, |
| 1271 |
}, |
| 1272 |
} |
| 1273 |
|
| 1274 |
withJSServer(t, func(t *testing.T, nc *nats.Conn, js jetstream.JetStream) { |
| 1275 |
for _, consType := range []string{"pull", "push"} { |
| 1276 |
ctx := newTesterCtx(t, 5*time.Second) |
| 1277 |
s, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: "foo", Subjects: []string{"FOO.*"}}) |
| 1278 |
if err != nil { |
| 1279 |
t.Fatalf("Unexpected error: %v", err) |
| 1280 |
} |
| 1281 |
|
| 1282 |
for _, test := range tests { |
| 1283 |
t.Run(fmt.Sprintf("%s %s", consType, test.name), func(t *testing.T) { |
| 1284 |
var sub *nats.Subscription |
| 1285 |
if test.consumerConfig.FilterSubject != "" { |
| 1286 |
sub, err = nc.SubscribeSync(fmt.Sprintf("$JS.API.CONSUMER.CREATE.foo.*.%s", test.consumerConfig.FilterSubject)) |
| 1287 |
} else { |
| 1288 |
sub, err = nc.SubscribeSync("$JS.API.CONSUMER.CREATE.foo.*") |
| 1289 |
} |
| 1290 |
var c consInfoConstraint |
| 1291 |
var err error |
| 1292 |
if consType == "pull" { |
| 1293 |
c, err = js.CreateConsumer(ctx, test.stream, test.consumerConfig) |
| 1294 |
} else { |
| 1295 |
test.consumerConfig.DeliverSubject = "inbox" |
| 1296 |
c, err = js.CreatePushConsumer(ctx, test.stream, test.consumerConfig) |
| 1297 |
} |
| 1298 |
if test.withError != nil { |
| 1299 |
if !errors.Is(err, test.withError) { |
| 1300 |
t.Fatalf("Expected error: %v; got: %v", test.withError, err) |
| 1301 |
} |
| 1302 |
return |
| 1303 |
} |
| 1304 |
if err != nil { |
| 1305 |
t.Fatalf("Unexpected error: %v", err) |
| 1306 |
} |
| 1307 |
if test.shouldCreate { |
| 1308 |
if _, err := sub.NextMsgWithContext(ctx); err != nil { |
| 1309 |
t.Fatalf("Expected request on %s; got %s", sub.Subject, err) |
| 1310 |
} |
| 1311 |
} |
| 1312 |
var ci consInfoConstraint |
| 1313 |
if consType == "pull" { |
| 1314 |
ci, err = s.Consumer(ctx, c.CachedInfo().Name) |
| 1315 |
} else { |
| 1316 |
ci, err = s.PushConsumer(ctx, c.CachedInfo().Name) |
| 1317 |
} |
| 1318 |
if err != nil { |
| 1319 |
t.Fatalf("Unexpected error: %v", err) |
| 1320 |
} |
| 1321 |
if ci.CachedInfo().Config.AckPolicy != test.consumerConfig.AckPolicy { |
| 1322 |
t.Fatalf("Invalid ack policy; want: %s; got: %s", test.consumerConfig.AckPolicy, ci.CachedInfo().Config.AckPolicy) |
| 1323 |
} |
| 1324 |
if !reflect.DeepEqual(test.consumerConfig.FilterSubjects, ci.CachedInfo().Config.FilterSubjects) { |
| 1325 |
t.Fatalf("Invalid filter subjects; want: %v; got: %v", test.consumerConfig.FilterSubjects, ci.CachedInfo().Config.FilterSubjects) |
| 1326 |
} |
| 1327 |
}) |
| 1328 |
} |
| 1329 |
if err := js.DeleteStream(context.Background(), "foo"); err != nil { |
| 1330 |
t.Fatalf("Unexpected error: %v", err) |
| 1331 |
} |
| 1332 |
} |
| 1333 |
}) |
| 1334 |
} |
| 1335 |
|
| 1336 |
func TestJetStream_UpdateConsumer(t *testing.T) { |
| 1337 |
tests := []struct { |
| 1338 |
name string |
| 1339 |
consumerConfig jetstream.ConsumerConfig |
| 1340 |
shouldUpdate bool |
| 1341 |
stream string |
| 1342 |
withError error |
| 1343 |
}{ |
| 1344 |
{ |
| 1345 |
name: "update consumer", |
| 1346 |
consumerConfig: jetstream.ConsumerConfig{Name: "testcons", Description: "updated consumer"}, |
| 1347 |
stream: "foo", |
| 1348 |
shouldUpdate: true, |
| 1349 |
}, |
| 1350 |
{ |
| 1351 |
name: "illegal update", |
| 1352 |
consumerConfig: jetstream.ConsumerConfig{Name: "testcons", AckPolicy: jetstream.AckNonePolicy}, |
| 1353 |
stream: "foo", |
| 1354 |
withError: jetstream.ErrConsumerCreate, |
| 1355 |
}, |
| 1356 |
{ |
| 1357 |
name: "consumer does not exist", |
| 1358 |
consumerConfig: jetstream.ConsumerConfig{Name: "abc"}, |
| 1359 |
stream: "foo", |
| 1360 |
withError: jetstream.ErrConsumerDoesNotExist, |
| 1361 |
}, |
| 1362 |
{ |
| 1363 |
name: "stream does not exist", |
| 1364 |
consumerConfig: jetstream.ConsumerConfig{Name: "testcons"}, |
| 1365 |
stream: "abc", |
| 1366 |
withError: jetstream.ErrStreamNotFound, |
| 1367 |
}, |
| 1368 |
{ |
| 1369 |
name: "invalid stream name", |
| 1370 |
stream: "foo.1", |
| 1371 |
withError: jetstream.ErrInvalidStreamName, |
| 1372 |
}, |
| 1373 |
} |
| 1374 |
|
| 1375 |
withJSServer(t, func(t *testing.T, nc *nats.Conn, js jetstream.JetStream) { |
| 1376 |
for _, consType := range []string{"pull", "push"} { |
| 1377 |
ctx := newTesterCtx(t, 5*time.Second) |
| 1378 |
s, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: "foo", Subjects: []string{"FOO.*"}}) |
| 1379 |
if err != nil { |
| 1380 |
t.Fatalf("Unexpected error: %v", err) |
| 1381 |
} |
| 1382 |
|
| 1383 |
if consType == "pull" { |
| 1384 |
_, err = s.CreateConsumer(ctx, jetstream.ConsumerConfig{Name: "testcons"}) |
| 1385 |
} else { |
| 1386 |
_, err = s.CreatePushConsumer(ctx, jetstream.ConsumerConfig{Name: "testcons", DeliverSubject: "inbox"}) |
| 1387 |
} |
| 1388 |
if err != nil { |
| 1389 |
t.Fatalf("Unexpected error: %v", err) |
| 1390 |
} |
| 1391 |
|
| 1392 |
for _, test := range tests { |
| 1393 |
t.Run(fmt.Sprintf("%s %s", consType, test.name), func(t *testing.T) { |
| 1394 |
var sub *nats.Subscription |
| 1395 |
if test.consumerConfig.FilterSubject != "" { |
| 1396 |
sub, err = nc.SubscribeSync(fmt.Sprintf("$JS.API.CONSUMER.CREATE.foo.*.%s", test.consumerConfig.FilterSubject)) |
| 1397 |
} else { |
| 1398 |
sub, err = nc.SubscribeSync("$JS.API.CONSUMER.CREATE.foo.*") |
| 1399 |
} |
| 1400 |
var c consInfoConstraint |
| 1401 |
var err error |
| 1402 |
if consType == "pull" { |
| 1403 |
c, err = js.UpdateConsumer(ctx, test.stream, test.consumerConfig) |
| 1404 |
} else { |
| 1405 |
test.consumerConfig.DeliverSubject = "inbox" |
| 1406 |
c, err = js.UpdatePushConsumer(ctx, test.stream, test.consumerConfig) |
| 1407 |
} |
| 1408 |
if test.withError != nil { |
| 1409 |
if !errors.Is(err, test.withError) { |
| 1410 |
t.Fatalf("Expected error: %v; got: %v", test.withError, err) |
| 1411 |
} |
| 1412 |
return |
| 1413 |
} |
| 1414 |
if err != nil { |
| 1415 |
t.Fatalf("Unexpected error: %v", err) |
| 1416 |
} |
| 1417 |
if test.shouldUpdate { |
| 1418 |
if _, err := sub.NextMsgWithContext(ctx); err != nil { |
| 1419 |
t.Fatalf("Expected request on %s; got %s", sub.Subject, err) |
| 1420 |
} |
| 1421 |
} |
| 1422 |
var ci consInfoConstraint |
| 1423 |
if consType == "pull" { |
| 1424 |
ci, err = s.Consumer(ctx, c.CachedInfo().Name) |
| 1425 |
} else { |
| 1426 |
ci, err = s.PushConsumer(ctx, c.CachedInfo().Name) |
| 1427 |
} |
| 1428 |
if err != nil { |
| 1429 |
t.Fatalf("Unexpected error: %v", err) |
| 1430 |
} |
| 1431 |
if ci.CachedInfo().Config.AckPolicy != test.consumerConfig.AckPolicy { |
| 1432 |
t.Fatalf("Invalid ack policy; want: %s; got: %s", test.consumerConfig.AckPolicy, ci.CachedInfo().Config.AckPolicy) |
| 1433 |
} |
| 1434 |
if !reflect.DeepEqual(test.consumerConfig.FilterSubjects, ci.CachedInfo().Config.FilterSubjects) { |
| 1435 |
t.Fatalf("Invalid filter subjects; want: %v; got: %v", test.consumerConfig.FilterSubjects, ci.CachedInfo().Config.FilterSubjects) |
| 1436 |
} |
| 1437 |
}) |
| 1438 |
} |
| 1439 |
if err := js.DeleteStream(context.Background(), "foo"); err != nil { |
| 1440 |
t.Fatalf("Unexpected error: %v", err) |
| 1441 |
} |
| 1442 |
} |
| 1443 |
}) |
| 1444 |
} |
| 1445 |
|
| 1446 |
func TestJetStream_Consumer(t *testing.T) { |
| 1447 |
tests := []struct { |
| 1448 |
name string |
| 1449 |
stream string |
| 1450 |
durable string |
| 1451 |
timeout time.Duration |
| 1452 |
withError error |
| 1453 |
}{ |
| 1454 |
{ |
| 1455 |
name: "get existing consumer", |
| 1456 |
stream: "foo", |
| 1457 |
durable: "dur", |
| 1458 |
timeout: 10 * time.Second, |
| 1459 |
}, |
| 1460 |
{ |
| 1461 |
name: "with empty context", |
| 1462 |
stream: "foo", |
| 1463 |
durable: "dur", |
| 1464 |
}, |
| 1465 |
{ |
| 1466 |
name: "consumer does not exist", |
| 1467 |
stream: "foo", |
| 1468 |
durable: "abc", |
| 1469 |
timeout: 10 * time.Second, |
| 1470 |
withError: jetstream.ErrConsumerNotFound, |
| 1471 |
}, |
| 1472 |
{ |
| 1473 |
name: "invalid durable name", |
| 1474 |
stream: "foo", |
| 1475 |
durable: "dur.123", |
| 1476 |
withError: jetstream.ErrInvalidConsumerName, |
| 1477 |
}, |
| 1478 |
{ |
| 1479 |
name: "stream does not exist", |
| 1480 |
stream: "abc", |
| 1481 |
durable: "dur", |
| 1482 |
withError: jetstream.ErrStreamNotFound, |
| 1483 |
}, |
| 1484 |
{ |
| 1485 |
name: "invalid stream name", |
| 1486 |
stream: "foo.1", |
| 1487 |
durable: "dur", |
| 1488 |
withError: jetstream.ErrInvalidStreamName, |
| 1489 |
}, |
| 1490 |
{ |
| 1491 |
name: "context timeout", |
| 1492 |
stream: "foo", |
| 1493 |
durable: "dur", |
| 1494 |
timeout: 1 * time.Microsecond, |
| 1495 |
withError: context.DeadlineExceeded, |
| 1496 |
}, |
| 1497 |
} |
| 1498 |
|
| 1499 |
withJSServer(t, func(t *testing.T, _ *nats.Conn, js jetstream.JetStream) { |
| 1500 |
for _, consType := range []string{"pull", "push"} { |
| 1501 |
s, err := js.CreateStream(context.Background(), jetstream.StreamConfig{Name: "foo", Subjects: []string{"FOO.*"}}) |
| 1502 |
if err != nil { |
| 1503 |
t.Fatalf("Unexpected error: %v", err) |
| 1504 |
} |
| 1505 |
|
| 1506 |
if consType == "pull" { |
| 1507 |
_, err = s.CreateOrUpdateConsumer(context.Background(), jetstream.ConsumerConfig{Durable: "dur", AckPolicy: jetstream.AckAllPolicy, Description: "desc"}) |
| 1508 |
} else { |
| 1509 |
_, err = s.CreateOrUpdateConsumer(context.Background(), jetstream.ConsumerConfig{Durable: "dur", AckPolicy: jetstream.AckAllPolicy, Description: "desc", DeliverSubject: "inbox"}) |
| 1510 |
} |
| 1511 |
if err != nil { |
| 1512 |
t.Fatalf("Unexpected error: %v", err) |
| 1513 |
} |
| 1514 |
|
| 1515 |
for _, test := range tests { |
| 1516 |
t.Run(fmt.Sprintf("%s %s", consType, test.name), func(t *testing.T) { |
| 1517 |
ctx := context.Background() |
| 1518 |
if test.timeout > 0 { |
| 1519 |
ctx = newTesterCtx(t, test.timeout) |
| 1520 |
} |
| 1521 |
var c consInfoConstraint |
| 1522 |
var err error |
| 1523 |
if consType == "pull" { |
| 1524 |
c, err = js.Consumer(ctx, test.stream, test.durable) |
| 1525 |
} else { |
| 1526 |
c, err = js.PushConsumer(ctx, test.stream, test.durable) |
| 1527 |
} |
| 1528 |
if test.withError != nil { |
| 1529 |
if err == nil || !errors.Is(err, test.withError) { |
| 1530 |
t.Fatalf("Expected error: %v; got: %v", test.withError, err) |
| 1531 |
} |
| 1532 |
return |
| 1533 |
} |
| 1534 |
if err != nil { |
| 1535 |
t.Fatalf("Unexpected error: %v", err) |
| 1536 |
} |
| 1537 |
if c.CachedInfo().Name != test.durable { |
| 1538 |
t.Fatalf("Unexpected consumer fetched; want: %s; got: %s", test.durable, c.CachedInfo().Name) |
| 1539 |
} |
| 1540 |
}) |
| 1541 |
} |
| 1542 |
if err := js.DeleteStream(context.Background(), "foo"); err != nil { |
| 1543 |
t.Fatalf("Unexpected error: %v", err) |
| 1544 |
} |
| 1545 |
} |
| 1546 |
}) |
| 1547 |
} |
| 1548 |
|
| 1549 |
func TestJetStream_DeleteConsumer(t *testing.T) { |
| 1550 |
tests := []struct { |
| 1551 |
name string |
| 1552 |
stream string |
| 1553 |
durable string |
| 1554 |
timeout time.Duration |
| 1555 |
withError error |
| 1556 |
}{ |
| 1557 |
{ |
| 1558 |
name: "delete existing consumer", |
| 1559 |
stream: "foo", |
| 1560 |
durable: "dur", |
| 1561 |
timeout: 10 * time.Second, |
| 1562 |
}, |
| 1563 |
{ |
| 1564 |
name: "with empty context", |
| 1565 |
stream: "foo", |
| 1566 |
durable: "dur2", |
| 1567 |
}, |
| 1568 |
{ |
| 1569 |
name: "consumer does not exist", |
| 1570 |
stream: "foo", |
| 1571 |
durable: "dur", |
| 1572 |
timeout: 10 * time.Second, |
| 1573 |
withError: jetstream.ErrConsumerNotFound, |
| 1574 |
}, |
| 1575 |
{ |
| 1576 |
name: "invalid durable name", |
| 1577 |
stream: "foo", |
| 1578 |
durable: "dur.123", |
| 1579 |
withError: jetstream.ErrInvalidConsumerName, |
| 1580 |
}, |
| 1581 |
{ |
| 1582 |
name: "stream not found", |
| 1583 |
stream: "abc", |
| 1584 |
durable: "dur", |
| 1585 |
withError: jetstream.ErrStreamNotFound, |
| 1586 |
}, |
| 1587 |
{ |
| 1588 |
name: "invalid stream name", |
| 1589 |
stream: "foo.1", |
| 1590 |
durable: "dur", |
| 1591 |
withError: jetstream.ErrInvalidStreamName, |
| 1592 |
}, |
| 1593 |
{ |
| 1594 |
name: "context timeout", |
| 1595 |
stream: "foo", |
| 1596 |
durable: "dur", |
| 1597 |
timeout: 1 * time.Microsecond, |
| 1598 |
withError: context.DeadlineExceeded, |
| 1599 |
}, |
| 1600 |
} |
| 1601 |
|
| 1602 |
withJSServer(t, func(t *testing.T, _ *nats.Conn, js jetstream.JetStream) { |
| 1603 |
s, err := js.CreateStream(context.Background(), jetstream.StreamConfig{Name: "foo", Subjects: []string{"FOO.*"}}) |
| 1604 |
if err != nil { |
| 1605 |
t.Fatalf("Unexpected error: %v", err) |
| 1606 |
} |
| 1607 |
_, err = s.CreateOrUpdateConsumer(context.Background(), jetstream.ConsumerConfig{Durable: "dur", AckPolicy: jetstream.AckAllPolicy, Description: "desc"}) |
| 1608 |
if err != nil { |
| 1609 |
t.Fatalf("Unexpected error: %v", err) |
| 1610 |
} |
| 1611 |
_, err = s.CreateOrUpdateConsumer(context.Background(), jetstream.ConsumerConfig{Durable: "dur2", AckPolicy: jetstream.AckAllPolicy, Description: "desc"}) |
| 1612 |
if err != nil { |
| 1613 |
t.Fatalf("Unexpected error: %v", err) |
| 1614 |
} |
| 1615 |
|
| 1616 |
for _, test := range tests { |
| 1617 |
t.Run(test.name, func(t *testing.T) { |
| 1618 |
ctx := context.Background() |
| 1619 |
if test.timeout > 0 { |
| 1620 |
ctx = newTesterCtx(t, test.timeout) |
| 1621 |
} |
| 1622 |
err := js.DeleteConsumer(ctx, test.stream, test.durable) |
| 1623 |
if test.withError != nil { |
| 1624 |
if err == nil || !errors.Is(err, test.withError) { |
| 1625 |
t.Fatalf("Expected error: %v; got: %v", test.withError, err) |
| 1626 |
} |
| 1627 |
return |
| 1628 |
} |
| 1629 |
if err != nil { |
| 1630 |
t.Fatalf("Unexpected error: %v", err) |
| 1631 |
} |
| 1632 |
_, err = s.Consumer(ctx, test.durable) |
| 1633 |
if err == nil || !errors.Is(err, jetstream.ErrConsumerNotFound) { |
| 1634 |
t.Fatalf("Expected error: %v; got: %v", jetstream.ErrConsumerNotFound, err) |
| 1635 |
} |
| 1636 |
}) |
| 1637 |
} |
| 1638 |
}) |
| 1639 |
} |
| 1640 |
|
| 1641 |
func TestStreamNameBySubject(t *testing.T) { |
| 1642 |
tests := []struct { |
| 1643 |
name string |
| 1644 |
subject string |
| 1645 |
withError error |
| 1646 |
timeout time.Duration |
| 1647 |
expected string |
| 1648 |
}{ |
| 1649 |
{ |
| 1650 |
name: "get stream name by subject explicit", |
| 1651 |
subject: "FOO.123", |
| 1652 |
timeout: 10 * time.Second, |
| 1653 |
expected: "foo", |
| 1654 |
}, |
| 1655 |
{ |
| 1656 |
name: "with empty context", |
| 1657 |
subject: "FOO.123", |
| 1658 |
expected: "foo", |
| 1659 |
}, |
| 1660 |
{ |
| 1661 |
name: "get stream name by subject with wildcard", |
| 1662 |
subject: "BAR.*", |
| 1663 |
expected: "bar", |
| 1664 |
}, |
| 1665 |
{ |
| 1666 |
name: "match more than one stream, return the first one", |
| 1667 |
subject: ">", |
| 1668 |
expected: "", |
| 1669 |
}, |
| 1670 |
{ |
| 1671 |
name: "stream not found", |
| 1672 |
subject: "BAR.XYZ", |
| 1673 |
withError: jetstream.ErrStreamNotFound, |
| 1674 |
}, |
| 1675 |
{ |
| 1676 |
name: "invalid subject", |
| 1677 |
subject: "FOO.>.123", |
| 1678 |
withError: jetstream.ErrInvalidSubject, |
| 1679 |
}, |
| 1680 |
{ |
| 1681 |
name: "context timeout", |
| 1682 |
subject: "FOO.123", |
| 1683 |
timeout: 1 * time.Microsecond, |
| 1684 |
withError: context.DeadlineExceeded, |
| 1685 |
}, |
| 1686 |
} |
| 1687 |
|
| 1688 |
withJSServer(t, func(t *testing.T, _ *nats.Conn, js jetstream.JetStream) { |
| 1689 |
_, err := js.CreateStream(context.Background(), jetstream.StreamConfig{Name: "foo", Subjects: []string{"FOO.*"}}) |
| 1690 |
if err != nil { |
| 1691 |
t.Fatalf("Unexpected error: %v", err) |
| 1692 |
} |
| 1693 |
_, err = js.CreateStream(context.Background(), jetstream.StreamConfig{Name: "bar", Subjects: []string{"BAR.ABC"}}) |
| 1694 |
if err != nil { |
| 1695 |
t.Fatalf("Unexpected error: %v", err) |
| 1696 |
} |
| 1697 |
|
| 1698 |
for _, test := range tests { |
| 1699 |
t.Run(test.name, func(t *testing.T) { |
| 1700 |
ctx := context.Background() |
| 1701 |
if test.timeout > 0 { |
| 1702 |
ctx = newTesterCtx(t, test.timeout) |
| 1703 |
} |
| 1704 |
name, err := js.StreamNameBySubject(ctx, test.subject) |
| 1705 |
if test.withError != nil { |
| 1706 |
if err == nil || !errors.Is(err, test.withError) { |
| 1707 |
t.Fatalf("Expected error: %v; got: %v", test.withError, err) |
| 1708 |
} |
| 1709 |
return |
| 1710 |
} |
| 1711 |
if err != nil { |
| 1712 |
t.Fatalf("Unexpected error: %v", err) |
| 1713 |
} |
| 1714 |
if test.expected != "" && name != test.expected { |
| 1715 |
t.Fatalf("Unexpected stream name; want: %s; got: %s", test.expected, name) |
| 1716 |
} |
| 1717 |
}) |
| 1718 |
} |
| 1719 |
}) |
| 1720 |
} |
| 1721 |
|
| 1722 |
func TestJetStreamTransform(t *testing.T) { |
| 1723 |
withJSServer(t, func(t *testing.T, nc *nats.Conn, js jetstream.JetStream) { |
| 1724 |
ctx := context.Background() |
| 1725 |
_, err := js.CreateStream(ctx, jetstream.StreamConfig{ |
| 1726 |
Name: "ORIGIN", |
| 1727 |
Subjects: []string{"test"}, |
| 1728 |
SubjectTransform: &jetstream.SubjectTransformConfig{Source: ">", Destination: "transformed.>"}, |
| 1729 |
Storage: jetstream.MemoryStorage, |
| 1730 |
}) |
| 1731 |
if err != nil { |
| 1732 |
t.Fatalf("Unexpected error: %v", err) |
| 1733 |
} |
| 1734 |
err = nc.Publish("test", []byte("1")) |
| 1735 |
if err != nil { |
| 1736 |
t.Fatalf("Unexpected error: %v", err) |
| 1737 |
} |
| 1738 |
sourcingStream, err := js.CreateStream(ctx, jetstream.StreamConfig{ |
| 1739 |
Subjects: []string{}, |
| 1740 |
Name: "SOURCING", |
| 1741 |
Sources: []*jetstream.StreamSource{{Name: "ORIGIN", SubjectTransforms: []jetstream.SubjectTransformConfig{{Source: ">", Destination: "fromtest.>"}}}}, |
| 1742 |
Storage: jetstream.MemoryStorage, |
| 1743 |
}) |
| 1744 |
if err != nil { |
| 1745 |
t.Fatalf("Unexpected error: %v", err) |
| 1746 |
} |
| 1747 |
|
| 1748 |
cons, err := sourcingStream.CreateConsumer(ctx, jetstream.ConsumerConfig{FilterSubject: "fromtest.>", MemoryStorage: true}) |
| 1749 |
if err != nil { |
| 1750 |
t.Fatalf("Unexpected error: %v", err) |
| 1751 |
} |
| 1752 |
m, err := cons.Next() |
| 1753 |
if err != nil { |
| 1754 |
t.Fatalf("Unexpected error: %v", err) |
| 1755 |
} |
| 1756 |
if m.Subject() != "fromtest.transformed.test" { |
| 1757 |
t.Fatalf("the subject of the message doesn't match the expected fromtest.transformed.test: %s", m.Subject()) |
| 1758 |
} |
| 1759 |
}) |
| 1760 |
} |
| 1761 |
|
| 1762 |
func TestStreamConfigMatches(t *testing.T) { |
| 1763 |
withJSServer(t, func(t *testing.T, _ *nats.Conn, js jetstream.JetStream) { |
| 1764 |
cfg := jetstream.StreamConfig{ |
| 1765 |
Name: "stream", |
| 1766 |
Description: "desc", |
| 1767 |
Subjects: []string{"foo.*"}, |
| 1768 |
Retention: jetstream.WorkQueuePolicy, |
| 1769 |
MaxConsumers: 10, |
| 1770 |
MaxMsgs: 100, |
| 1771 |
MaxBytes: 1000, |
| 1772 |
Discard: jetstream.DiscardNew, |
| 1773 |
DiscardNewPerSubject: true, |
| 1774 |
MaxAge: 100 * time.Second, |
| 1775 |
MaxMsgsPerSubject: 1000, |
| 1776 |
MaxMsgSize: 10000, |
| 1777 |
Storage: jetstream.MemoryStorage, |
| 1778 |
Replicas: 1, |
| 1779 |
NoAck: true, |
| 1780 |
Duplicates: 10 * time.Second, |
| 1781 |
Sealed: false, |
| 1782 |
DenyDelete: true, |
| 1783 |
DenyPurge: false, |
| 1784 |
AllowRollup: true, |
| 1785 |
Compression: jetstream.S2Compression, |
| 1786 |
FirstSeq: 5, |
| 1787 |
SubjectTransform: &jetstream.SubjectTransformConfig{Source: ">", Destination: "transformed.>"}, |
| 1788 |
RePublish: &jetstream.RePublish{ |
| 1789 |
Source: ">", |
| 1790 |
Destination: "RP.>", |
| 1791 |
HeadersOnly: true, |
| 1792 |
}, |
| 1793 |
AllowDirect: true, |
| 1794 |
ConsumerLimits: jetstream.StreamConsumerLimits{ |
| 1795 |
InactiveThreshold: 10 * time.Second, |
| 1796 |
MaxAckPending: 500, |
| 1797 |
}, |
| 1798 |
} |
| 1799 |
|
| 1800 |
s, err := js.CreateStream(context.Background(), cfg) |
| 1801 |
if err != nil { |
| 1802 |
t.Fatalf("Unexpected error: %v", err) |
| 1803 |
} |
| 1804 |
// server will set metadata values, so we need to clear them |
| 1805 |
s.CachedInfo().Config.Metadata = nil |
| 1806 |
if !reflect.DeepEqual(s.CachedInfo().Config, cfg) { |
| 1807 |
t.Fatalf("StreamConfig doesn't match: %#v", s.CachedInfo().Config) |
| 1808 |
} |
| 1809 |
|
| 1810 |
cfgMirror := jetstream.StreamConfig{ |
| 1811 |
Name: "mirror", |
| 1812 |
MaxConsumers: 10, |
| 1813 |
MaxMsgs: 100, |
| 1814 |
MaxBytes: 1000, |
| 1815 |
MaxAge: 100 * time.Second, |
| 1816 |
MaxMsgsPerSubject: 1000, |
| 1817 |
MaxMsgSize: 10000, |
| 1818 |
Replicas: 1, |
| 1819 |
Duplicates: 10 * time.Second, |
| 1820 |
Mirror: &jetstream.StreamSource{ |
| 1821 |
Name: "stream", |
| 1822 |
OptStartSeq: 10, |
| 1823 |
SubjectTransforms: []jetstream.SubjectTransformConfig{ |
| 1824 |
{Source: ">", Destination: "transformed.>"}, |
| 1825 |
}, |
| 1826 |
}, |
| 1827 |
MirrorDirect: true, |
| 1828 |
SubjectTransform: &jetstream.SubjectTransformConfig{Source: ">", Destination: "transformed.>"}, |
| 1829 |
} |
| 1830 |
|
| 1831 |
s, err = js.CreateStream(context.Background(), cfgMirror) |
| 1832 |
if err != nil { |
| 1833 |
t.Fatalf("Unexpected error: %v", err) |
| 1834 |
} |
| 1835 |
// server will set metadata values, so we need to clear them |
| 1836 |
s.CachedInfo().Config.Metadata = nil |
| 1837 |
if !reflect.DeepEqual(s.CachedInfo().Config, cfgMirror) { |
| 1838 |
t.Fatalf("StreamConfig doesn't match: %#v", s.CachedInfo().Config) |
| 1839 |
} |
| 1840 |
|
| 1841 |
cfgSourcing := jetstream.StreamConfig{ |
| 1842 |
Name: "sourcing", |
| 1843 |
Subjects: []string{"BAR"}, |
| 1844 |
MaxConsumers: 10, |
| 1845 |
MaxMsgs: 100, |
| 1846 |
MaxBytes: 1000, |
| 1847 |
MaxAge: 100 * time.Second, |
| 1848 |
MaxMsgsPerSubject: 1000, |
| 1849 |
MaxMsgSize: 10000, |
| 1850 |
Replicas: 1, |
| 1851 |
Duplicates: 10 * time.Second, |
| 1852 |
Sources: []*jetstream.StreamSource{ |
| 1853 |
{ |
| 1854 |
Name: "stream", |
| 1855 |
OptStartSeq: 10, |
| 1856 |
SubjectTransforms: []jetstream.SubjectTransformConfig{ |
| 1857 |
{Source: ">", Destination: "transformed.>"}, |
| 1858 |
}, |
| 1859 |
}, |
| 1860 |
}, |
| 1861 |
SubjectTransform: &jetstream.SubjectTransformConfig{Source: ">", Destination: "transformed.>"}, |
| 1862 |
} |
| 1863 |
|
| 1864 |
s, err = js.CreateStream(context.Background(), cfgSourcing) |
| 1865 |
if err != nil { |
| 1866 |
t.Fatalf("Unexpected error: %v", err) |
| 1867 |
} |
| 1868 |
// server will set metadata values, so we need to clear them |
| 1869 |
s.CachedInfo().Config.Metadata = nil |
| 1870 |
if !reflect.DeepEqual(s.CachedInfo().Config, cfgSourcing) { |
| 1871 |
t.Fatalf("StreamConfig doesn't match: %#v", s.CachedInfo().Config) |
| 1872 |
} |
| 1873 |
}) |
| 1874 |
} |
| 1875 |
|
| 1876 |
func TestConsumerConfigMatches(t *testing.T) { |
| 1877 |
withJSServer(t, func(t *testing.T, _ *nats.Conn, js jetstream.JetStream) { |
| 1878 |
s, err := js.CreateStream(context.Background(), jetstream.StreamConfig{ |
| 1879 |
Name: "FOO", |
| 1880 |
Subjects: []string{"foo.*"}, |
| 1881 |
}) |
| 1882 |
if err != nil { |
| 1883 |
t.Fatalf("Unexpected error: %v", err) |
| 1884 |
} |
| 1885 |
cfg := jetstream.ConsumerConfig{ |
| 1886 |
Name: "cons", |
| 1887 |
Durable: "cons", |
| 1888 |
Description: "test", |
| 1889 |
DeliverPolicy: jetstream.DeliverByStartSequencePolicy, |
| 1890 |
OptStartSeq: 5, |
| 1891 |
AckPolicy: jetstream.AckAllPolicy, |
| 1892 |
AckWait: 1 * time.Second, |
| 1893 |
MaxDeliver: 5, |
| 1894 |
BackOff: []time.Duration{1 * time.Second, 2 * time.Second, 3 * time.Second}, |
| 1895 |
ReplayPolicy: jetstream.ReplayOriginalPolicy, |
| 1896 |
SampleFrequency: "50%", |
| 1897 |
MaxWaiting: 100, |
| 1898 |
MaxAckPending: 1000, |
| 1899 |
HeadersOnly: true, |
| 1900 |
MaxRequestBatch: 100, |
| 1901 |
MaxRequestExpires: 10 * time.Second, |
| 1902 |
MaxRequestMaxBytes: 1000, |
| 1903 |
InactiveThreshold: 20 * time.Second, |
| 1904 |
Replicas: 1, |
| 1905 |
MemoryStorage: true, |
| 1906 |
FilterSubjects: []string{"foo.1", "foo.2"}, |
| 1907 |
} |
| 1908 |
|
| 1909 |
c, err := s.CreateConsumer(context.Background(), cfg) |
| 1910 |
if err != nil { |
| 1911 |
t.Fatalf("Unexpected error: %v", err) |
| 1912 |
} |
| 1913 |
// server will set metadata values, so we need to clear them |
| 1914 |
c.CachedInfo().Config.Metadata = nil |
| 1915 |
if !reflect.DeepEqual(c.CachedInfo().Config, cfg) { |
| 1916 |
t.Fatalf("ConsumerConfig doesn't match") |
| 1917 |
} |
| 1918 |
}) |
| 1919 |
} |
| 1920 |
|
| 1921 |
func TestJetStreamCleanupPublisher(t *testing.T) { |
| 1922 |
t.Run("cleanup js publisher", func(t *testing.T) { |
| 1923 |
withJSServer(t, func(t *testing.T, nc *nats.Conn, js jetstream.JetStream) { |
| 1924 |
if _, err := js.CreateStream(context.Background(), jetstream.StreamConfig{Name: "TEST", Subjects: []string{"FOO"}}); err != nil { |
| 1925 |
t.Fatalf("Unexpected error: %v", err) |
| 1926 |
} |
| 1927 |
|
| 1928 |
numSubs := nc.NumSubscriptions() |
| 1929 |
if _, err := js.PublishAsync("FOO", []byte("hello")); err != nil { |
| 1930 |
t.Fatalf("Unexpected error: %v", err) |
| 1931 |
} |
| 1932 |
select { |
| 1933 |
case <-js.PublishAsyncComplete(): |
| 1934 |
case <-time.After(5 * time.Second): |
| 1935 |
t.Fatalf("Did not receive completion signal") |
| 1936 |
} |
| 1937 |
|
| 1938 |
if numSubs+1 != nc.NumSubscriptions() { |
| 1939 |
t.Fatalf("Expected an additional subscription after publish, got %d", nc.NumSubscriptions()) |
| 1940 |
} |
| 1941 |
|
| 1942 |
js.CleanupPublisher() |
| 1943 |
|
| 1944 |
if numSubs != nc.NumSubscriptions() { |
| 1945 |
t.Fatalf("Expected subscriptions to be back to original count") |
| 1946 |
} |
| 1947 |
}) |
| 1948 |
}) |
| 1949 |
|
| 1950 |
t.Run("cleanup js publisher, cancel pending acks", func(t *testing.T) { |
| 1951 |
c := newTester(t) |
| 1952 |
inst := c.CreateServer(t, true) |
| 1953 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 1954 |
|
| 1955 |
nc := dialInstance(t, inst) |
| 1956 |
|
| 1957 |
cbErr := make(chan error, 10) |
| 1958 |
js, err := jetstream.New(nc, jetstream.WithPublishAsyncErrHandler(func(js jetstream.JetStream, m *nats.Msg, err error) { |
| 1959 |
cbErr <- err |
| 1960 |
})) |
| 1961 |
if err != nil { |
| 1962 |
t.Fatalf("Unexpected error: %v", err) |
| 1963 |
} |
| 1964 |
|
| 1965 |
// Create a stream with NoAck so that we can test that we cancel ack futures. |
| 1966 |
if _, err := js.CreateStream(context.Background(), jetstream.StreamConfig{Name: "TEST", Subjects: []string{"FOO"}, NoAck: true}); err != nil { |
| 1967 |
t.Fatalf("Unexpected error: %v", err) |
| 1968 |
} |
| 1969 |
|
| 1970 |
numSubs := nc.NumSubscriptions() |
| 1971 |
|
| 1972 |
var acks []jetstream.PubAckFuture |
| 1973 |
for range 10 { |
| 1974 |
ack, err := js.PublishAsync("FOO", []byte("hello")) |
| 1975 |
if err != nil { |
| 1976 |
t.Fatalf("Unexpected error: %v", err) |
| 1977 |
} |
| 1978 |
acks = append(acks, ack) |
| 1979 |
} |
| 1980 |
|
| 1981 |
asyncComplete := js.PublishAsyncComplete() |
| 1982 |
select { |
| 1983 |
case <-asyncComplete: |
| 1984 |
t.Fatalf("Should not complete, NoAck is set") |
| 1985 |
case <-time.After(200 * time.Millisecond): |
| 1986 |
} |
| 1987 |
|
| 1988 |
if numSubs+1 != nc.NumSubscriptions() { |
| 1989 |
t.Fatalf("Expected an additional subscription after publish, got %d", nc.NumSubscriptions()) |
| 1990 |
} |
| 1991 |
|
| 1992 |
js.CleanupPublisher() |
| 1993 |
|
| 1994 |
if numSubs != nc.NumSubscriptions() { |
| 1995 |
t.Fatalf("Expected subscriptions to be back to original count") |
| 1996 |
} |
| 1997 |
|
| 1998 |
// check that PublishAsyncComplete channel is closed |
| 1999 |
select { |
| 2000 |
case <-asyncComplete: |
| 2001 |
case <-time.After(5 * time.Second): |
| 2002 |
t.Fatalf("Did not receive completion signal") |
| 2003 |
} |
| 2004 |
|
| 2005 |
// check that all ack futures are canceled |
| 2006 |
for _, ack := range acks { |
| 2007 |
select { |
| 2008 |
case err := <-ack.Err(): |
| 2009 |
if !errors.Is(err, jetstream.ErrJetStreamPublisherClosed) { |
| 2010 |
t.Fatalf("Expected JetStreamContextClosed error, got %v", err) |
| 2011 |
} |
| 2012 |
case <-ack.Ok(): |
| 2013 |
t.Fatalf("Expected error on the ack future") |
| 2014 |
case <-time.After(200 * time.Millisecond): |
| 2015 |
t.Fatalf("Expected an error on the ack future") |
| 2016 |
} |
| 2017 |
} |
| 2018 |
|
| 2019 |
// check that async error handler is called for each pending ack |
| 2020 |
for range 10 { |
| 2021 |
select { |
| 2022 |
case err := <-cbErr: |
| 2023 |
if !errors.Is(err, jetstream.ErrJetStreamPublisherClosed) { |
| 2024 |
t.Fatalf("Expected JetStreamContextClosed error, got %v", err) |
| 2025 |
} |
| 2026 |
case <-time.After(200 * time.Millisecond): |
| 2027 |
t.Fatalf("Expected errors to be passed from the async handler") |
| 2028 |
} |
| 2029 |
} |
| 2030 |
}) |
| 2031 |
} |
| 2032 |
|
| 2033 |
func TestCreateOrUpdateStreamCrossDomains(t *testing.T) { |
| 2034 |
c := newTester(t) |
| 2035 |
host := testserviceHost(t) |
| 2036 |
|
| 2037 |
hubInst := c.CreateServer(t, true, |
| 2038 |
testservice.WithJetStream(`domain: HUB`), |
| 2039 |
testservice.WithLeafNode(`leafnodes { port: {{ .Ports.leafnode }} }`), |
| 2040 |
testservice.WithAccounts(emptyAccountsBody()), |
| 2041 |
testservice.WithSystemAccount(noSystemAccountBody()), |
| 2042 |
testservice.WithAuthorization(`# no authorization required`), |
| 2043 |
) |
| 2044 |
t.Cleanup(func() { hubInst.Destroy(t) }) |
| 2045 |
|
| 2046 |
hubLeafPort := hubInst.Servers[0].Ports["leafnode"] |
| 2047 |
if hubLeafPort == 0 { |
| 2048 |
t.Fatalf("hub leafnode port not reserved") |
| 2049 |
} |
| 2050 |
|
| 2051 |
leafInst := c.CreateServer(t, true, |
| 2052 |
testservice.WithJetStream(`domain: LEAF`), |
| 2053 |
testservice.WithLeafNode(fmt.Sprintf(`leafnodes { remotes = [ { url: "leaf://%s:%d" } ] }`, host, hubLeafPort)), |
| 2054 |
testservice.WithAccounts(emptyAccountsBody()), |
| 2055 |
testservice.WithSystemAccount(noSystemAccountBody()), |
| 2056 |
testservice.WithAuthorization(`# no authorization required`), |
| 2057 |
) |
| 2058 |
t.Cleanup(func() { leafInst.Destroy(t) }) |
| 2059 |
|
| 2060 |
ncHub := dialInstance(t, hubInst) |
| 2061 |
c.WaitForJetStream(t, ncHub) |
| 2062 |
jsHub, err := jetstream.New(ncHub) |
| 2063 |
if err != nil { |
| 2064 |
t.Fatalf("Unexpected error: %v", err) |
| 2065 |
} |
| 2066 |
|
| 2067 |
// Create stream on HUB domain |
| 2068 |
_, err = jsHub.CreateStream(context.Background(), jetstream.StreamConfig{ |
| 2069 |
Name: "TEST", |
| 2070 |
Subjects: []string{"foo"}, |
| 2071 |
}) |
| 2072 |
if err != nil { |
| 2073 |
t.Fatalf("Failed to create source stream: %v", err) |
| 2074 |
} |
| 2075 |
|
| 2076 |
ncLeaf := dialInstance(t, leafInst) |
| 2077 |
c.WaitForJetStream(t, ncLeaf) |
| 2078 |
jsLeaf, err := jetstream.New(ncLeaf) |
| 2079 |
if err != nil { |
| 2080 |
t.Fatalf("Unexpected error: %v", err) |
| 2081 |
} |
| 2082 |
|
| 2083 |
// Create stream with cross-domain source using CreateOrUpdateStream |
| 2084 |
streamConfig := jetstream.StreamConfig{ |
| 2085 |
Name: "SOURCE", |
| 2086 |
Sources: []*jetstream.StreamSource{ |
| 2087 |
{ |
| 2088 |
Name: "TEST", |
| 2089 |
Domain: "HUB", |
| 2090 |
}, |
| 2091 |
}, |
| 2092 |
} |
| 2093 |
|
| 2094 |
// First call should create the stream |
| 2095 |
stream, err := jsLeaf.CreateOrUpdateStream(context.Background(), streamConfig) |
| 2096 |
if err != nil { |
| 2097 |
t.Fatalf("Failed to create stream: %v", err) |
| 2098 |
} |
| 2099 |
|
| 2100 |
// Verify the stream was created with external configuration |
| 2101 |
info, err := stream.Info(context.Background()) |
| 2102 |
if err != nil { |
| 2103 |
t.Fatalf("Failed to get stream info: %v", err) |
| 2104 |
} |
| 2105 |
|
| 2106 |
if len(info.Config.Sources) != 1 { |
| 2107 |
t.Fatalf("Expected 1 source, got %d", len(info.Config.Sources)) |
| 2108 |
} |
| 2109 |
|
| 2110 |
firstSource := info.Config.Sources[0] |
| 2111 |
if firstSource.External == nil { |
| 2112 |
t.Fatal("Expected external configuration to be set after create") |
| 2113 |
} |
| 2114 |
if firstSource.External.APIPrefix == "" { |
| 2115 |
t.Fatal("Expected external APIPrefix to be set after create") |
| 2116 |
} |
| 2117 |
expectedAPIPrefix := firstSource.External.APIPrefix |
| 2118 |
|
| 2119 |
// Second call should update the stream |
| 2120 |
// Add a subject to force an actual update |
| 2121 |
streamConfig.Subjects = []string{"bar"} |
| 2122 |
stream2, err := jsLeaf.CreateOrUpdateStream(context.Background(), streamConfig) |
| 2123 |
if err != nil { |
| 2124 |
t.Fatalf("Failed to update stream: %v", err) |
| 2125 |
} |
| 2126 |
|
| 2127 |
// Verify the external configuration is preserved during update |
| 2128 |
info2, err := stream2.Info(context.Background()) |
| 2129 |
if err != nil { |
| 2130 |
t.Fatalf("Failed to get updated stream info: %v", err) |
| 2131 |
} |
| 2132 |
|
| 2133 |
if len(info2.Config.Sources) != 1 { |
| 2134 |
t.Fatalf("Expected 1 source after update, got %d", len(info2.Config.Sources)) |
| 2135 |
} |
| 2136 |
|
| 2137 |
updatedSource := info2.Config.Sources[0] |
| 2138 |
if updatedSource.External == nil { |
| 2139 |
t.Fatal("External configuration was removed during update - this is the bug!") |
| 2140 |
} |
| 2141 |
if updatedSource.External.APIPrefix != expectedAPIPrefix { |
| 2142 |
t.Fatalf("Expected APIPrefix %q to be preserved, got %q", expectedAPIPrefix, updatedSource.External.APIPrefix) |
| 2143 |
} |
| 2144 |
|
| 2145 |
// Verify the subject was actually updated |
| 2146 |
if len(info2.Config.Subjects) != 1 { |
| 2147 |
t.Fatalf("Expected 1 subject after update, got %d", len(info2.Config.Subjects)) |
| 2148 |
} |
| 2149 |
if info2.Config.Subjects[0] != "bar" { |
| 2150 |
t.Fatalf("Expected subject to be updated to 'bar', got %q", info2.Config.Subjects[0]) |
| 2151 |
} |
| 2152 |
} |
| 2153 |
|
| 2154 |
func TestPromoteMirrorToStream(t *testing.T) { |
| 2155 |
withJSServer(t, func(t *testing.T, _ *nats.Conn, js jetstream.JetStream) { |
| 2156 |
ctx := context.Background() |
| 2157 |
// Create source stream |
| 2158 |
_, err := js.CreateStream(ctx, jetstream.StreamConfig{ |
| 2159 |
Name: "SOURCE", |
| 2160 |
Subjects: []string{"foo"}, |
| 2161 |
}) |
| 2162 |
if err != nil { |
| 2163 |
t.Fatalf("Unexpected error: %v", err) |
| 2164 |
} |
| 2165 |
|
| 2166 |
// Publish some messages to source |
| 2167 |
for range 10 { |
| 2168 |
if _, err := js.Publish(ctx, "foo", []byte("hello")); err != nil { |
| 2169 |
t.Fatalf("Unexpected error: %v", err) |
| 2170 |
} |
| 2171 |
} |
| 2172 |
|
| 2173 |
// Create mirror stream |
| 2174 |
mirror, err := js.CreateStream(ctx, jetstream.StreamConfig{ |
| 2175 |
Name: "MIRROR", |
| 2176 |
Mirror: &jetstream.StreamSource{Name: "SOURCE"}, |
| 2177 |
}) |
| 2178 |
if err != nil { |
| 2179 |
t.Fatalf("Unexpected error: %v", err) |
| 2180 |
} |
| 2181 |
|
| 2182 |
// Wait for mirror to catch up |
| 2183 |
checkFor(t, 5*time.Second, 100*time.Millisecond, func() error { |
| 2184 |
info, err := mirror.Info(ctx) |
| 2185 |
if err != nil { |
| 2186 |
return err |
| 2187 |
} |
| 2188 |
if info.State.Msgs != 10 { |
| 2189 |
return fmt.Errorf("not caught up") |
| 2190 |
} |
| 2191 |
return nil |
| 2192 |
}) |
| 2193 |
|
| 2194 |
// Remove mirror configuration |
| 2195 |
updatedMirror, err := js.UpdateStream(ctx, jetstream.StreamConfig{ |
| 2196 |
Name: "MIRROR", |
| 2197 |
Subjects: []string{"bar"}, |
| 2198 |
}) |
| 2199 |
if err != nil { |
| 2200 |
t.Fatalf("Unexpected error: %v", err) |
| 2201 |
} |
| 2202 |
|
| 2203 |
info, err := updatedMirror.Info(ctx) |
| 2204 |
if err != nil { |
| 2205 |
t.Fatalf("Unexpected error: %v", err) |
| 2206 |
} |
| 2207 |
|
| 2208 |
if info.Config.Mirror != nil { |
| 2209 |
t.Fatal("Expected mirror to be nil after promote") |
| 2210 |
} |
| 2211 |
|
| 2212 |
if info.Config.Subjects[0] != "bar" { |
| 2213 |
t.Fatalf("Expected subject to be 'bar', got %q", info.Config.Subjects[0]) |
| 2214 |
} |
| 2215 |
}) |
| 2216 |
} |
| 2217 |
|
| 2218 |
func TestJetStreamConn(t *testing.T) { |
| 2219 |
withJSServer(t, func(t *testing.T, nc *nats.Conn, js jetstream.JetStream) { |
| 2220 |
conn := js.Conn() |
| 2221 |
if conn != nc { |
| 2222 |
t.Fatal("Expected Conn() to return the same connection") |
| 2223 |
} |
| 2224 |
}) |
| 2225 |
} |
| 2226 |
|
| 2227 |
func TestStreamSourceWithConsumer(t *testing.T) { |
| 2228 |
withJSServer(t, func(t *testing.T, _ *nats.Conn, js jetstream.JetStream) { |
| 2229 |
ctx := newTesterCtx(t, 10*time.Second) |
| 2230 |
|
| 2231 |
upstream, err := js.CreateStream(ctx, jetstream.StreamConfig{ |
| 2232 |
Name: "UP", |
| 2233 |
Subjects: []string{"up"}, |
| 2234 |
}) |
| 2235 |
if err != nil { |
| 2236 |
t.Fatalf("CreateStream upstream: %v", err) |
| 2237 |
} |
| 2238 |
|
| 2239 |
if _, err := upstream.CreateOrUpdateConsumer(ctx, jetstream.ConsumerConfig{ |
| 2240 |
Durable: "C", |
| 2241 |
DeliverSubject: "deliver", |
| 2242 |
AckPolicy: jetstream.AckFlowControlPolicy, |
| 2243 |
IdleHeartbeat: time.Second, |
| 2244 |
}); err != nil { |
| 2245 |
t.Fatalf("CreateOrUpdateConsumer: %v", err) |
| 2246 |
} |
| 2247 |
|
| 2248 |
down, err := js.CreateStream(ctx, jetstream.StreamConfig{ |
| 2249 |
Name: "DOWN", |
| 2250 |
Sources: []*jetstream.StreamSource{{ |
| 2251 |
Name: "UP", |
| 2252 |
Consumer: &jetstream.StreamConsumerSource{ |
| 2253 |
Name: "C", |
| 2254 |
DeliverSubject: "deliver", |
| 2255 |
}, |
| 2256 |
}}, |
| 2257 |
}) |
| 2258 |
if err != nil { |
| 2259 |
t.Fatalf("CreateStream down: %v", err) |
| 2260 |
} |
| 2261 |
|
| 2262 |
if _, err := js.Publish(ctx, "up", []byte("hello")); err != nil { |
| 2263 |
t.Fatalf("Publish: %v", err) |
| 2264 |
} |
| 2265 |
|
| 2266 |
checkFor(t, 5*time.Second, 100*time.Millisecond, func() error { |
| 2267 |
info, err := down.Info(ctx) |
| 2268 |
if err != nil { |
| 2269 |
return err |
| 2270 |
} |
| 2271 |
if info.State.Msgs != 1 { |
| 2272 |
return fmt.Errorf("expected 1 msg in DOWN, got %d", info.State.Msgs) |
| 2273 |
} |
| 2274 |
return nil |
| 2275 |
}) |
| 2276 |
}) |
| 2277 |
} |