| 1 |
// Copyright 2016-2025 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 server |
| 15 |
|
| 16 |
import ( |
| 17 |
"errors" |
| 18 |
"flag" |
| 19 |
"fmt" |
| 20 |
"math/rand" |
| 21 |
"os" |
| 22 |
"runtime" |
| 23 |
"strconv" |
| 24 |
"strings" |
| 25 |
"sync" |
| 26 |
"testing" |
| 27 |
"time" |
| 28 |
|
| 29 |
"github.com/nats-io/nuid" |
| 30 |
) |
| 31 |
|
| 32 |
func stackFatalf(t *testing.T, f string, args ...any) { |
| 33 |
lines := make([]string, 0, 32) |
| 34 |
msg := fmt.Sprintf(f, args...) |
| 35 |
lines = append(lines, msg) |
| 36 |
|
| 37 |
// Generate the Stack of callers: Skip us and verify* frames. |
| 38 |
for i := 2; true; i++ { |
| 39 |
_, file, line, ok := runtime.Caller(i) |
| 40 |
if !ok { |
| 41 |
break |
| 42 |
} |
| 43 |
msg := fmt.Sprintf("%d - %s:%d", i, file, line) |
| 44 |
lines = append(lines, msg) |
| 45 |
} |
| 46 |
t.Fatalf("%s", strings.Join(lines, "\n")) |
| 47 |
} |
| 48 |
|
| 49 |
func verifyCount(s *Sublist, count uint32, t *testing.T) { |
| 50 |
t.Helper() |
| 51 |
if s.Count() != count { |
| 52 |
t.Fatalf("Count is %d, should be %d", s.Count(), count) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
func verifyLen(r []*subscription, l int, t *testing.T) { |
| 57 |
t.Helper() |
| 58 |
if len(r) != l { |
| 59 |
t.Fatalf("Results len is %d, should be %d", len(r), l) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
func verifyQLen(r [][]*subscription, l int, t *testing.T) { |
| 64 |
t.Helper() |
| 65 |
if len(r) != l { |
| 66 |
t.Fatalf("Queue Results len is %d, should be %d", len(r), l) |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
func verifyNumLevels(s *Sublist, expected int, t *testing.T) { |
| 71 |
t.Helper() |
| 72 |
dl := s.numLevels() |
| 73 |
if dl != expected { |
| 74 |
t.Fatalf("NumLevels is %d, should be %d", dl, expected) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
func verifyQMember(qsubs [][]*subscription, val *subscription, t *testing.T) { |
| 79 |
t.Helper() |
| 80 |
verifyMember(qsubs[findQSlot(val.queue, qsubs)], val, t) |
| 81 |
} |
| 82 |
|
| 83 |
func verifyMember(r []*subscription, val *subscription, t *testing.T) { |
| 84 |
t.Helper() |
| 85 |
for _, v := range r { |
| 86 |
if v == nil { |
| 87 |
continue |
| 88 |
} |
| 89 |
if v == val { |
| 90 |
return |
| 91 |
} |
| 92 |
} |
| 93 |
t.Fatalf("Subscription (%p) for [%s : %s] not found in results", val, val.subject, val.queue) |
| 94 |
} |
| 95 |
|
| 96 |
// Helpers to generate test subscriptions. |
| 97 |
func newSub(subject string) *subscription { |
| 98 |
c := &client{kind: CLIENT} |
| 99 |
return &subscription{client: c, subject: []byte(subject)} |
| 100 |
} |
| 101 |
|
| 102 |
func newQSub(subject, queue string) *subscription { |
| 103 |
if queue != "" { |
| 104 |
return &subscription{subject: []byte(subject), queue: []byte(queue)} |
| 105 |
} |
| 106 |
return newSub(subject) |
| 107 |
} |
| 108 |
|
| 109 |
func newRemoteQSub(subject, queue string, num int32) *subscription { |
| 110 |
if queue != "" { |
| 111 |
c := &client{kind: ROUTER} |
| 112 |
return &subscription{client: c, subject: []byte(subject), queue: []byte(queue), qw: num} |
| 113 |
} |
| 114 |
return newSub(subject) |
| 115 |
} |
| 116 |
|
| 117 |
func TestSublistInit(t *testing.T) { |
| 118 |
s := NewSublistWithCache() |
| 119 |
verifyCount(s, 0, t) |
| 120 |
} |
| 121 |
|
| 122 |
func TestSublistForServerNoOpts(t *testing.T) { |
| 123 |
s := NewSublistForServer(&Server{}) |
| 124 |
if s == nil { |
| 125 |
t.Fatal("Expected a sublist") |
| 126 |
} |
| 127 |
if s.CacheEnabled() { |
| 128 |
t.Fatal("Expected cache to be disabled when server options are nil") |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
func TestSetAccountSublistNoOpts(t *testing.T) { |
| 133 |
s := &Server{} |
| 134 |
acc := NewAccount("foo") |
| 135 |
|
| 136 |
s.setAccountSublist(acc) |
| 137 |
|
| 138 |
if acc.sl == nil { |
| 139 |
t.Fatal("Expected account sublist to be initialized") |
| 140 |
} |
| 141 |
if acc.sl.CacheEnabled() { |
| 142 |
t.Fatal("Expected cache to be disabled when server options are nil") |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
func TestSublistInsertCount(t *testing.T) { |
| 147 |
testSublistInsertCount(t, NewSublistWithCache()) |
| 148 |
} |
| 149 |
|
| 150 |
func TestSublistInsertCountNoCache(t *testing.T) { |
| 151 |
testSublistInsertCount(t, NewSublistNoCache()) |
| 152 |
} |
| 153 |
|
| 154 |
func testSublistInsertCount(t *testing.T, s *Sublist) { |
| 155 |
s.Insert(newSub("foo")) |
| 156 |
s.Insert(newSub("bar")) |
| 157 |
s.Insert(newSub("foo.bar")) |
| 158 |
verifyCount(s, 3, t) |
| 159 |
} |
| 160 |
|
| 161 |
func TestSublistSimple(t *testing.T) { |
| 162 |
testSublistSimple(t, NewSublistWithCache()) |
| 163 |
} |
| 164 |
|
| 165 |
func TestSublistSimpleNoCache(t *testing.T) { |
| 166 |
testSublistSimple(t, NewSublistNoCache()) |
| 167 |
} |
| 168 |
|
| 169 |
func testSublistSimple(t *testing.T, s *Sublist) { |
| 170 |
subject := "foo" |
| 171 |
sub := newSub(subject) |
| 172 |
s.Insert(sub) |
| 173 |
r := s.Match(subject) |
| 174 |
verifyLen(r.psubs, 1, t) |
| 175 |
verifyMember(r.psubs, sub, t) |
| 176 |
} |
| 177 |
|
| 178 |
func TestSublistSimpleMultiTokens(t *testing.T) { |
| 179 |
testSublistSimpleMultiTokens(t, NewSublistWithCache()) |
| 180 |
} |
| 181 |
|
| 182 |
func TestSublistSimpleMultiTokensNoCache(t *testing.T) { |
| 183 |
testSublistSimpleMultiTokens(t, NewSublistNoCache()) |
| 184 |
} |
| 185 |
|
| 186 |
func testSublistSimpleMultiTokens(t *testing.T, s *Sublist) { |
| 187 |
subject := "foo.bar.baz" |
| 188 |
sub := newSub(subject) |
| 189 |
s.Insert(sub) |
| 190 |
r := s.Match(subject) |
| 191 |
verifyLen(r.psubs, 1, t) |
| 192 |
verifyMember(r.psubs, sub, t) |
| 193 |
} |
| 194 |
|
| 195 |
func TestSublistPartialWildcard(t *testing.T) { |
| 196 |
testSublistPartialWildcard(t, NewSublistWithCache()) |
| 197 |
} |
| 198 |
|
| 199 |
func TestSublistPartialWildcardNoCache(t *testing.T) { |
| 200 |
testSublistPartialWildcard(t, NewSublistNoCache()) |
| 201 |
} |
| 202 |
|
| 203 |
func testSublistPartialWildcard(t *testing.T, s *Sublist) { |
| 204 |
lsub := newSub("a.b.c") |
| 205 |
psub := newSub("a.*.c") |
| 206 |
s.Insert(lsub) |
| 207 |
s.Insert(psub) |
| 208 |
r := s.Match("a.b.c") |
| 209 |
verifyLen(r.psubs, 2, t) |
| 210 |
verifyMember(r.psubs, lsub, t) |
| 211 |
verifyMember(r.psubs, psub, t) |
| 212 |
} |
| 213 |
|
| 214 |
func TestSublistPartialWildcardAtEnd(t *testing.T) { |
| 215 |
testSublistPartialWildcardAtEnd(t, NewSublistWithCache()) |
| 216 |
} |
| 217 |
|
| 218 |
func TestSublistPartialWildcardAtEndNoCache(t *testing.T) { |
| 219 |
testSublistPartialWildcardAtEnd(t, NewSublistNoCache()) |
| 220 |
} |
| 221 |
|
| 222 |
func testSublistPartialWildcardAtEnd(t *testing.T, s *Sublist) { |
| 223 |
lsub := newSub("a.b.c") |
| 224 |
psub := newSub("a.b.*") |
| 225 |
s.Insert(lsub) |
| 226 |
s.Insert(psub) |
| 227 |
r := s.Match("a.b.c") |
| 228 |
verifyLen(r.psubs, 2, t) |
| 229 |
verifyMember(r.psubs, lsub, t) |
| 230 |
verifyMember(r.psubs, psub, t) |
| 231 |
} |
| 232 |
|
| 233 |
func TestSublistFullWildcard(t *testing.T) { |
| 234 |
testSublistFullWildcard(t, NewSublistWithCache()) |
| 235 |
} |
| 236 |
|
| 237 |
func TestSublistFullWildcardNoCache(t *testing.T) { |
| 238 |
testSublistFullWildcard(t, NewSublistNoCache()) |
| 239 |
} |
| 240 |
|
| 241 |
func testSublistFullWildcard(t *testing.T, s *Sublist) { |
| 242 |
lsub := newSub("a.b.c") |
| 243 |
fsub := newSub("a.>") |
| 244 |
s.Insert(lsub) |
| 245 |
s.Insert(fsub) |
| 246 |
r := s.Match("a.b.c") |
| 247 |
verifyLen(r.psubs, 2, t) |
| 248 |
verifyMember(r.psubs, lsub, t) |
| 249 |
verifyMember(r.psubs, fsub, t) |
| 250 |
|
| 251 |
r = s.Match("a.>") |
| 252 |
verifyLen(r.psubs, 1, t) |
| 253 |
verifyMember(r.psubs, fsub, t) |
| 254 |
} |
| 255 |
|
| 256 |
func TestSublistRemove(t *testing.T) { |
| 257 |
testSublistRemove(t, NewSublistWithCache()) |
| 258 |
} |
| 259 |
|
| 260 |
func TestSublistRemoveNoCache(t *testing.T) { |
| 261 |
testSublistRemove(t, NewSublistNoCache()) |
| 262 |
} |
| 263 |
|
| 264 |
func testSublistRemove(t *testing.T, s *Sublist) { |
| 265 |
subject := "a.b.c.d" |
| 266 |
sub := newSub(subject) |
| 267 |
s.Insert(sub) |
| 268 |
verifyCount(s, 1, t) |
| 269 |
r := s.Match(subject) |
| 270 |
verifyLen(r.psubs, 1, t) |
| 271 |
s.Remove(newSub("a.b.c")) |
| 272 |
verifyCount(s, 1, t) |
| 273 |
s.Remove(sub) |
| 274 |
verifyCount(s, 0, t) |
| 275 |
r = s.Match(subject) |
| 276 |
verifyLen(r.psubs, 0, t) |
| 277 |
} |
| 278 |
|
| 279 |
func TestSublistRemoveWildcard(t *testing.T) { |
| 280 |
testSublistRemoveWildcard(t, NewSublistWithCache()) |
| 281 |
} |
| 282 |
|
| 283 |
func TestSublistRemoveWildcardNoCache(t *testing.T) { |
| 284 |
testSublistRemoveWildcard(t, NewSublistNoCache()) |
| 285 |
} |
| 286 |
|
| 287 |
func testSublistRemoveWildcard(t *testing.T, s *Sublist) { |
| 288 |
subject := "a.b.c.d" |
| 289 |
sub := newSub(subject) |
| 290 |
psub := newSub("a.b.*.d") |
| 291 |
fsub := newSub("a.b.>") |
| 292 |
s.Insert(sub) |
| 293 |
s.Insert(psub) |
| 294 |
s.Insert(fsub) |
| 295 |
verifyCount(s, 3, t) |
| 296 |
r := s.Match(subject) |
| 297 |
verifyLen(r.psubs, 3, t) |
| 298 |
s.Remove(sub) |
| 299 |
verifyCount(s, 2, t) |
| 300 |
s.Remove(fsub) |
| 301 |
verifyCount(s, 1, t) |
| 302 |
s.Remove(psub) |
| 303 |
verifyCount(s, 0, t) |
| 304 |
r = s.Match(subject) |
| 305 |
verifyLen(r.psubs, 0, t) |
| 306 |
} |
| 307 |
|
| 308 |
func TestSublistRemoveCleanup(t *testing.T) { |
| 309 |
testSublistRemoveCleanup(t, NewSublistWithCache()) |
| 310 |
} |
| 311 |
|
| 312 |
func TestSublistRemoveCleanupNoCache(t *testing.T) { |
| 313 |
testSublistRemoveCleanup(t, NewSublistNoCache()) |
| 314 |
} |
| 315 |
|
| 316 |
func testSublistRemoveCleanup(t *testing.T, s *Sublist) { |
| 317 |
literal := "a.b.c.d.e.f" |
| 318 |
depth := len(strings.Split(literal, tsep)) |
| 319 |
sub := newSub(literal) |
| 320 |
verifyNumLevels(s, 0, t) |
| 321 |
s.Insert(sub) |
| 322 |
verifyNumLevels(s, depth, t) |
| 323 |
s.Remove(sub) |
| 324 |
verifyNumLevels(s, 0, t) |
| 325 |
} |
| 326 |
|
| 327 |
func TestSublistRemoveCleanupWildcards(t *testing.T) { |
| 328 |
testSublistRemoveCleanupWildcards(t, NewSublistWithCache()) |
| 329 |
} |
| 330 |
|
| 331 |
func TestSublistRemoveCleanupWildcardsNoCache(t *testing.T) { |
| 332 |
testSublistRemoveCleanupWildcards(t, NewSublistNoCache()) |
| 333 |
} |
| 334 |
|
| 335 |
func testSublistRemoveCleanupWildcards(t *testing.T, s *Sublist) { |
| 336 |
subject := "a.b.*.d.e.>" |
| 337 |
depth := len(strings.Split(subject, tsep)) |
| 338 |
sub := newSub(subject) |
| 339 |
verifyNumLevels(s, 0, t) |
| 340 |
s.Insert(sub) |
| 341 |
verifyNumLevels(s, depth, t) |
| 342 |
s.Remove(sub) |
| 343 |
verifyNumLevels(s, 0, t) |
| 344 |
} |
| 345 |
|
| 346 |
func TestSublistRemoveWithLargeSubs(t *testing.T) { |
| 347 |
testSublistRemoveWithLargeSubs(t, NewSublistWithCache()) |
| 348 |
} |
| 349 |
|
| 350 |
func TestSublistRemoveWithLargeSubsNoCache(t *testing.T) { |
| 351 |
testSublistRemoveWithLargeSubs(t, NewSublistNoCache()) |
| 352 |
} |
| 353 |
|
| 354 |
func testSublistRemoveWithLargeSubs(t *testing.T, s *Sublist) { |
| 355 |
subject := "foo" |
| 356 |
for i := 0; i < plistMin*2; i++ { |
| 357 |
sub := newSub(subject) |
| 358 |
s.Insert(sub) |
| 359 |
} |
| 360 |
r := s.Match(subject) |
| 361 |
verifyLen(r.psubs, plistMin*2, t) |
| 362 |
// Remove one that is in the middle |
| 363 |
s.Remove(r.psubs[plistMin]) |
| 364 |
// Remove first one |
| 365 |
s.Remove(r.psubs[0]) |
| 366 |
// Remove last one |
| 367 |
s.Remove(r.psubs[len(r.psubs)-1]) |
| 368 |
// Check len again |
| 369 |
r = s.Match(subject) |
| 370 |
verifyLen(r.psubs, plistMin*2-3, t) |
| 371 |
} |
| 372 |
|
| 373 |
func TestSublistInvalidSubjectsInsert(t *testing.T) { |
| 374 |
testSublistInvalidSubjectsInsert(t, NewSublistWithCache()) |
| 375 |
} |
| 376 |
|
| 377 |
func TestSublistInvalidSubjectsInsertNoCache(t *testing.T) { |
| 378 |
testSublistInvalidSubjectsInsert(t, NewSublistNoCache()) |
| 379 |
} |
| 380 |
|
| 381 |
func TestSublistNoCacheRemoveBatch(t *testing.T) { |
| 382 |
s := NewSublistNoCache() |
| 383 |
s.Insert(newSub("foo")) |
| 384 |
sub := newSub("bar") |
| 385 |
s.Insert(sub) |
| 386 |
s.RemoveBatch([]*subscription{sub}) |
| 387 |
// Now test that this did not turn on cache |
| 388 |
for i := 0; i < 10; i++ { |
| 389 |
s.Match("foo") |
| 390 |
} |
| 391 |
if s.CacheEnabled() { |
| 392 |
t.Fatalf("Cache should not be enabled") |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
func TestSublistRemoveBatchWithError(t *testing.T) { |
| 397 |
s := NewSublistNoCache() |
| 398 |
sub1 := newSub("foo") |
| 399 |
sub2 := newSub("bar") |
| 400 |
sub3 := newSub("baz") |
| 401 |
s.Insert(sub1) |
| 402 |
s.Insert(sub2) |
| 403 |
s.Insert(sub3) |
| 404 |
subNotPresent := newSub("not.inserted") |
| 405 |
// Try to remove all subs, but include the sub that has not been inserted. |
| 406 |
err := s.RemoveBatch([]*subscription{subNotPresent, sub1, sub3}) |
| 407 |
// We expect an error to be returned, but sub1,2 and 3 to have been removed. |
| 408 |
require_Error(t, err, ErrNotFound) |
| 409 |
// Make sure that we have only sub2 present |
| 410 |
verifyCount(s, 1, t) |
| 411 |
r := s.Match("bar") |
| 412 |
verifyLen(r.psubs, 1, t) |
| 413 |
verifyMember(r.psubs, sub2, t) |
| 414 |
r = s.Match("foo") |
| 415 |
verifyLen(r.psubs, 0, t) |
| 416 |
r = s.Match("baz") |
| 417 |
verifyLen(r.psubs, 0, t) |
| 418 |
} |
| 419 |
|
| 420 |
func testSublistInvalidSubjectsInsert(t *testing.T, s *Sublist) { |
| 421 |
// Insert, or subscriptions, can have wildcards, but not empty tokens, |
| 422 |
// and can not have a FWC that is not the terminal token. |
| 423 |
|
| 424 |
// beginning empty token |
| 425 |
if err := s.Insert(newSub(".foo")); err != ErrInvalidSubject { |
| 426 |
t.Fatal("Expected invalid subject error") |
| 427 |
} |
| 428 |
|
| 429 |
// trailing empty token |
| 430 |
if err := s.Insert(newSub("foo.")); err != ErrInvalidSubject { |
| 431 |
t.Fatal("Expected invalid subject error") |
| 432 |
} |
| 433 |
// empty middle token |
| 434 |
if err := s.Insert(newSub("foo..bar")); err != ErrInvalidSubject { |
| 435 |
t.Fatal("Expected invalid subject error") |
| 436 |
} |
| 437 |
// empty middle token #2 |
| 438 |
if err := s.Insert(newSub("foo.bar..baz")); err != ErrInvalidSubject { |
| 439 |
t.Fatal("Expected invalid subject error") |
| 440 |
} |
| 441 |
// fwc not terminal |
| 442 |
if err := s.Insert(newSub("foo.>.bar")); err != ErrInvalidSubject { |
| 443 |
t.Fatal("Expected invalid subject error") |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
func TestSublistCache(t *testing.T) { |
| 448 |
s := NewSublistWithCache() |
| 449 |
|
| 450 |
// Test add a remove logistics |
| 451 |
subject := "a.b.c.d" |
| 452 |
sub := newSub(subject) |
| 453 |
psub := newSub("a.b.*.d") |
| 454 |
fsub := newSub("a.b.>") |
| 455 |
s.Insert(sub) |
| 456 |
r := s.Match(subject) |
| 457 |
verifyLen(r.psubs, 1, t) |
| 458 |
s.Insert(psub) |
| 459 |
s.Insert(fsub) |
| 460 |
verifyCount(s, 3, t) |
| 461 |
r = s.Match(subject) |
| 462 |
verifyLen(r.psubs, 3, t) |
| 463 |
s.Remove(sub) |
| 464 |
verifyCount(s, 2, t) |
| 465 |
s.Remove(fsub) |
| 466 |
verifyCount(s, 1, t) |
| 467 |
s.Remove(psub) |
| 468 |
verifyCount(s, 0, t) |
| 469 |
|
| 470 |
// Check that cache is now empty |
| 471 |
if cc := s.CacheCount(); cc != 0 { |
| 472 |
t.Fatalf("Cache should be zero, got %d\n", cc) |
| 473 |
} |
| 474 |
|
| 475 |
r = s.Match(subject) |
| 476 |
verifyLen(r.psubs, 0, t) |
| 477 |
|
| 478 |
for i := 0; i < 2*slCacheMax; i++ { |
| 479 |
s.Match(fmt.Sprintf("foo-%d\n", i)) |
| 480 |
} |
| 481 |
|
| 482 |
checkFor(t, 2*time.Second, 10*time.Millisecond, func() error { |
| 483 |
if cc := s.CacheCount(); cc > slCacheMax { |
| 484 |
return fmt.Errorf("Cache should be constrained by cacheMax, got %d for current count", cc) |
| 485 |
} |
| 486 |
return nil |
| 487 |
}) |
| 488 |
|
| 489 |
// Test that adding to a wildcard properly adds to the cache. |
| 490 |
s = NewSublistWithCache() |
| 491 |
s.Insert(newSub("foo.*")) |
| 492 |
s.Insert(newSub("foo.bar")) |
| 493 |
r = s.Match("foo.baz") |
| 494 |
verifyLen(r.psubs, 1, t) |
| 495 |
r = s.Match("foo.bar") |
| 496 |
verifyLen(r.psubs, 2, t) |
| 497 |
s.Insert(newSub("foo.>")) |
| 498 |
r = s.Match("foo.bar") |
| 499 |
verifyLen(r.psubs, 3, t) |
| 500 |
} |
| 501 |
|
| 502 |
func TestSublistBasicQueueResults(t *testing.T) { |
| 503 |
testSublistBasicQueueResults(t, NewSublistWithCache()) |
| 504 |
} |
| 505 |
|
| 506 |
func TestSublistBasicQueueResultsNoCache(t *testing.T) { |
| 507 |
testSublistBasicQueueResults(t, NewSublistNoCache()) |
| 508 |
} |
| 509 |
|
| 510 |
func testSublistBasicQueueResults(t *testing.T, s *Sublist) { |
| 511 |
// Test some basics |
| 512 |
subject := "foo" |
| 513 |
sub := newSub(subject) |
| 514 |
sub1 := newQSub(subject, "bar") |
| 515 |
sub2 := newQSub(subject, "baz") |
| 516 |
|
| 517 |
s.Insert(sub1) |
| 518 |
r := s.Match(subject) |
| 519 |
verifyLen(r.psubs, 0, t) |
| 520 |
verifyQLen(r.qsubs, 1, t) |
| 521 |
verifyLen(r.qsubs[0], 1, t) |
| 522 |
verifyQMember(r.qsubs, sub1, t) |
| 523 |
|
| 524 |
s.Insert(sub2) |
| 525 |
r = s.Match(subject) |
| 526 |
verifyLen(r.psubs, 0, t) |
| 527 |
verifyQLen(r.qsubs, 2, t) |
| 528 |
verifyLen(r.qsubs[0], 1, t) |
| 529 |
verifyLen(r.qsubs[1], 1, t) |
| 530 |
verifyQMember(r.qsubs, sub1, t) |
| 531 |
verifyQMember(r.qsubs, sub2, t) |
| 532 |
|
| 533 |
s.Insert(sub) |
| 534 |
r = s.Match(subject) |
| 535 |
verifyLen(r.psubs, 1, t) |
| 536 |
verifyQLen(r.qsubs, 2, t) |
| 537 |
verifyLen(r.qsubs[0], 1, t) |
| 538 |
verifyLen(r.qsubs[1], 1, t) |
| 539 |
verifyQMember(r.qsubs, sub1, t) |
| 540 |
verifyQMember(r.qsubs, sub2, t) |
| 541 |
verifyMember(r.psubs, sub, t) |
| 542 |
|
| 543 |
sub3 := newQSub(subject, "bar") |
| 544 |
sub4 := newQSub(subject, "baz") |
| 545 |
|
| 546 |
s.Insert(sub3) |
| 547 |
s.Insert(sub4) |
| 548 |
|
| 549 |
r = s.Match(subject) |
| 550 |
verifyLen(r.psubs, 1, t) |
| 551 |
verifyQLen(r.qsubs, 2, t) |
| 552 |
verifyLen(r.qsubs[0], 2, t) |
| 553 |
verifyLen(r.qsubs[1], 2, t) |
| 554 |
verifyQMember(r.qsubs, sub1, t) |
| 555 |
verifyQMember(r.qsubs, sub2, t) |
| 556 |
verifyQMember(r.qsubs, sub3, t) |
| 557 |
verifyQMember(r.qsubs, sub4, t) |
| 558 |
verifyMember(r.psubs, sub, t) |
| 559 |
|
| 560 |
// Now removal |
| 561 |
s.Remove(sub) |
| 562 |
|
| 563 |
r = s.Match(subject) |
| 564 |
verifyLen(r.psubs, 0, t) |
| 565 |
verifyQLen(r.qsubs, 2, t) |
| 566 |
verifyLen(r.qsubs[0], 2, t) |
| 567 |
verifyLen(r.qsubs[1], 2, t) |
| 568 |
verifyQMember(r.qsubs, sub1, t) |
| 569 |
verifyQMember(r.qsubs, sub2, t) |
| 570 |
|
| 571 |
s.Remove(sub1) |
| 572 |
r = s.Match(subject) |
| 573 |
verifyLen(r.psubs, 0, t) |
| 574 |
verifyQLen(r.qsubs, 2, t) |
| 575 |
verifyLen(r.qsubs[findQSlot(sub1.queue, r.qsubs)], 1, t) |
| 576 |
verifyLen(r.qsubs[findQSlot(sub2.queue, r.qsubs)], 2, t) |
| 577 |
verifyQMember(r.qsubs, sub2, t) |
| 578 |
verifyQMember(r.qsubs, sub3, t) |
| 579 |
verifyQMember(r.qsubs, sub4, t) |
| 580 |
|
| 581 |
s.Remove(sub3) // Last one |
| 582 |
r = s.Match(subject) |
| 583 |
verifyLen(r.psubs, 0, t) |
| 584 |
verifyQLen(r.qsubs, 1, t) |
| 585 |
verifyLen(r.qsubs[0], 2, t) // this is sub2/baz now |
| 586 |
verifyQMember(r.qsubs, sub2, t) |
| 587 |
|
| 588 |
s.Remove(sub2) |
| 589 |
s.Remove(sub4) |
| 590 |
r = s.Match(subject) |
| 591 |
verifyLen(r.psubs, 0, t) |
| 592 |
verifyQLen(r.qsubs, 0, t) |
| 593 |
} |
| 594 |
|
| 595 |
func checkBool(b, expected bool, t *testing.T) { |
| 596 |
t.Helper() |
| 597 |
if b != expected { |
| 598 |
t.Fatalf("Expected %v, but got %v\n", expected, b) |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
func checkError(err, expected error, t *testing.T) { |
| 603 |
t.Helper() |
| 604 |
if err != expected && err != nil && !errors.Is(err, expected) { |
| 605 |
t.Fatalf("Expected %v, but got %v\n", expected, err) |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
func TestSublistValidLiteralSubjects(t *testing.T) { |
| 610 |
checkBool(IsValidLiteralSubject("foo"), true, t) |
| 611 |
checkBool(IsValidLiteralSubject(".foo"), false, t) |
| 612 |
checkBool(IsValidLiteralSubject("foo."), false, t) |
| 613 |
checkBool(IsValidLiteralSubject("foo..bar"), false, t) |
| 614 |
checkBool(IsValidLiteralSubject("foo.bar.*"), false, t) |
| 615 |
checkBool(IsValidLiteralSubject("foo.bar.>"), false, t) |
| 616 |
checkBool(IsValidLiteralSubject("*"), false, t) |
| 617 |
checkBool(IsValidLiteralSubject(">"), false, t) |
| 618 |
// The followings have widlcards characters but are not |
| 619 |
// considered as such because they are not individual tokens. |
| 620 |
checkBool(IsValidLiteralSubject("foo*"), true, t) |
| 621 |
checkBool(IsValidLiteralSubject("foo**"), true, t) |
| 622 |
checkBool(IsValidLiteralSubject("foo.**"), true, t) |
| 623 |
checkBool(IsValidLiteralSubject("foo*bar"), true, t) |
| 624 |
checkBool(IsValidLiteralSubject("foo.*bar"), true, t) |
| 625 |
checkBool(IsValidLiteralSubject("foo*.bar"), true, t) |
| 626 |
checkBool(IsValidLiteralSubject("*bar"), true, t) |
| 627 |
checkBool(IsValidLiteralSubject("foo>"), true, t) |
| 628 |
checkBool(IsValidLiteralSubject("foo>>"), true, t) |
| 629 |
checkBool(IsValidLiteralSubject("foo.>>"), true, t) |
| 630 |
checkBool(IsValidLiteralSubject("foo>bar"), true, t) |
| 631 |
checkBool(IsValidLiteralSubject("foo.>bar"), true, t) |
| 632 |
checkBool(IsValidLiteralSubject("foo>.bar"), true, t) |
| 633 |
checkBool(IsValidLiteralSubject(">bar"), true, t) |
| 634 |
} |
| 635 |
|
| 636 |
func TestSublistValidSubjects(t *testing.T) { |
| 637 |
checkBool(IsValidSubject("."), false, t) |
| 638 |
checkBool(IsValidSubject(".foo"), false, t) |
| 639 |
checkBool(IsValidSubject("foo."), false, t) |
| 640 |
checkBool(IsValidSubject("foo..bar"), false, t) |
| 641 |
checkBool(IsValidSubject(">.bar"), false, t) |
| 642 |
checkBool(IsValidSubject("foo.>.bar"), false, t) |
| 643 |
checkBool(IsValidSubject("foo"), true, t) |
| 644 |
checkBool(IsValidSubject("foo.bar.*"), true, t) |
| 645 |
checkBool(IsValidSubject("foo.bar.>"), true, t) |
| 646 |
checkBool(IsValidSubject("*"), true, t) |
| 647 |
checkBool(IsValidSubject(">"), true, t) |
| 648 |
checkBool(IsValidSubject("foo*"), true, t) |
| 649 |
checkBool(IsValidSubject("foo**"), true, t) |
| 650 |
checkBool(IsValidSubject("foo.**"), true, t) |
| 651 |
checkBool(IsValidSubject("foo*bar"), true, t) |
| 652 |
checkBool(IsValidSubject("foo.*bar"), true, t) |
| 653 |
checkBool(IsValidSubject("foo*.bar"), true, t) |
| 654 |
checkBool(IsValidSubject("*bar"), true, t) |
| 655 |
checkBool(IsValidSubject("foo>"), true, t) |
| 656 |
checkBool(IsValidSubject("foo.>>"), true, t) |
| 657 |
checkBool(IsValidSubject("foo>bar"), true, t) |
| 658 |
checkBool(IsValidSubject("foo.>bar"), true, t) |
| 659 |
checkBool(IsValidSubject("foo>.bar"), true, t) |
| 660 |
checkBool(IsValidSubject(">bar"), true, t) |
| 661 |
|
| 662 |
// Check for embedded nulls. |
| 663 |
subj := []byte("foo.bar.baz.") |
| 664 |
subj = append(subj, 0) |
| 665 |
checkBool(isValidSubject(string(subj), true), false, t) |
| 666 |
} |
| 667 |
|
| 668 |
func TestSublistMatchLiterals(t *testing.T) { |
| 669 |
checkBool(matchLiteral("foo", "foo"), true, t) |
| 670 |
checkBool(matchLiteral("foo", "bar"), false, t) |
| 671 |
checkBool(matchLiteral("foo", "*"), true, t) |
| 672 |
checkBool(matchLiteral("foo", ">"), true, t) |
| 673 |
checkBool(matchLiteral("foo.bar", ">"), true, t) |
| 674 |
checkBool(matchLiteral("foo.bar", "foo.>"), true, t) |
| 675 |
checkBool(matchLiteral("foo.bar", "bar.>"), false, t) |
| 676 |
checkBool(matchLiteral("stats.test.22", "stats.>"), true, t) |
| 677 |
checkBool(matchLiteral("stats.test.22", "stats.*.*"), true, t) |
| 678 |
checkBool(matchLiteral("foo.bar", "foo"), false, t) |
| 679 |
checkBool(matchLiteral("stats.test.foos", "stats.test.foos"), true, t) |
| 680 |
checkBool(matchLiteral("stats.test.foos", "stats.test.foo"), false, t) |
| 681 |
checkBool(matchLiteral("stats.test", "stats.test.*"), false, t) |
| 682 |
checkBool(matchLiteral("stats.test.foos", "stats.*"), false, t) |
| 683 |
checkBool(matchLiteral("stats.test.foos", "stats.*.*.foos"), false, t) |
| 684 |
|
| 685 |
// These are cases where wildcards characters should not be considered |
| 686 |
// wildcards since they do not follow the rules of wildcards. |
| 687 |
checkBool(matchLiteral("*bar", "*bar"), true, t) |
| 688 |
checkBool(matchLiteral("foo*", "foo*"), true, t) |
| 689 |
checkBool(matchLiteral("foo*bar", "foo*bar"), true, t) |
| 690 |
checkBool(matchLiteral("foo.***.bar", "foo.***.bar"), true, t) |
| 691 |
checkBool(matchLiteral(">bar", ">bar"), true, t) |
| 692 |
checkBool(matchLiteral("foo>", "foo>"), true, t) |
| 693 |
checkBool(matchLiteral("foo>bar", "foo>bar"), true, t) |
| 694 |
checkBool(matchLiteral("foo.>>>.bar", "foo.>>>.bar"), true, t) |
| 695 |
} |
| 696 |
|
| 697 |
func TestSubjectIsLiteral(t *testing.T) { |
| 698 |
checkBool(subjectIsLiteral("foo"), true, t) |
| 699 |
checkBool(subjectIsLiteral("foo.bar"), true, t) |
| 700 |
checkBool(subjectIsLiteral("foo*.bar"), true, t) |
| 701 |
checkBool(subjectIsLiteral("*"), false, t) |
| 702 |
checkBool(subjectIsLiteral(">"), false, t) |
| 703 |
checkBool(subjectIsLiteral("foo.*"), false, t) |
| 704 |
checkBool(subjectIsLiteral("foo.>"), false, t) |
| 705 |
checkBool(subjectIsLiteral("foo.*.>"), false, t) |
| 706 |
checkBool(subjectIsLiteral("foo.*.bar"), false, t) |
| 707 |
checkBool(subjectIsLiteral("foo.bar.>"), false, t) |
| 708 |
} |
| 709 |
|
| 710 |
func TestValidateDestinationSubject(t *testing.T) { |
| 711 |
checkError(ValidateMapping("bar", "foo"), nil, t) |
| 712 |
checkError(ValidateMapping("foo", "foo.bar"), nil, t) |
| 713 |
checkError(ValidateMapping("*", "{{wildcard(1)}}"), nil, t) |
| 714 |
checkError(ValidateMapping("foo.>", ">"), nil, t) |
| 715 |
checkError(ValidateMapping("foo.*", "bar.{{wildcard(1)}}"), nil, t) |
| 716 |
checkError(ValidateMapping("foo.>", "bar.>"), nil, t) |
| 717 |
checkError(ValidateMapping("foo.*.>", "bar.{{wildcard(1)}}.>"), nil, t) |
| 718 |
checkError(ValidateMapping("foo.*.bar", "bar.{{wildcard(1)}}.foo"), nil, t) |
| 719 |
checkError(ValidateMapping("foo.bar.>", "foo.bar.foo.>"), nil, t) |
| 720 |
checkError(ValidateMapping("*", "foo.{{wildcard(1)}}"), nil, t) |
| 721 |
checkError(ValidateMapping("*", "foo.{{ wildcard(1) }}"), nil, t) |
| 722 |
checkError(ValidateMapping("*", "foo.{{wildcard( 1 )}}"), nil, t) |
| 723 |
checkError(ValidateMapping("*", "foo.{{partition(2,1)}}"), nil, t) |
| 724 |
checkError(ValidateMapping("*.*", "foo.{{SplitFromLeft(2,1)}}"), nil, t) |
| 725 |
checkError(ValidateMapping("*.*", "foo.{{SplitFromRight(2,1)}}"), nil, t) |
| 726 |
checkError(ValidateMapping("*", "foo.{{unknown(1)}}"), ErrInvalidMappingDestination, t) |
| 727 |
checkError(ValidateMapping("foo", "foo..}"), ErrInvalidMappingDestination, t) |
| 728 |
checkError(ValidateMapping("foo", "foo. bar}"), ErrInvalidMappingDestinationSubject, t) |
| 729 |
} |
| 730 |
|
| 731 |
func TestSubjectToken(t *testing.T) { |
| 732 |
checkToken := func(token, expected string) { |
| 733 |
t.Helper() |
| 734 |
if token != expected { |
| 735 |
t.Fatalf("Expected token of %q, got %q", expected, token) |
| 736 |
} |
| 737 |
} |
| 738 |
checkToken(tokenAt("foo.bar.baz.*", 0), "") |
| 739 |
checkToken(tokenAt("foo.bar.baz.*", 1), "foo") |
| 740 |
checkToken(tokenAt("foo.bar.baz.*", 2), "bar") |
| 741 |
checkToken(tokenAt("foo.bar.baz.*", 3), "baz") |
| 742 |
checkToken(tokenAt("foo.bar.baz.*", 4), "*") |
| 743 |
checkToken(tokenAt("foo.bar.baz.*", 5), "") |
| 744 |
} |
| 745 |
|
| 746 |
func TestSublistBadSubjectOnRemove(t *testing.T) { |
| 747 |
testSublistBadSubjectOnRemove(t, NewSublistWithCache()) |
| 748 |
} |
| 749 |
|
| 750 |
func TestSublistBadSubjectOnRemoveNoCache(t *testing.T) { |
| 751 |
testSublistBadSubjectOnRemove(t, NewSublistNoCache()) |
| 752 |
} |
| 753 |
|
| 754 |
func testSublistBadSubjectOnRemove(t *testing.T, s *Sublist) { |
| 755 |
bad := "a.b..d" |
| 756 |
sub := newSub(bad) |
| 757 |
|
| 758 |
if err := s.Insert(sub); err != ErrInvalidSubject { |
| 759 |
t.Fatalf("Expected ErrInvalidSubject, got %v\n", err) |
| 760 |
} |
| 761 |
|
| 762 |
if err := s.Remove(sub); err != ErrInvalidSubject { |
| 763 |
t.Fatalf("Expected ErrInvalidSubject, got %v\n", err) |
| 764 |
} |
| 765 |
|
| 766 |
badfwc := "a.>.b" |
| 767 |
if err := s.Remove(newSub(badfwc)); err != ErrInvalidSubject { |
| 768 |
t.Fatalf("Expected ErrInvalidSubject, got %v\n", err) |
| 769 |
} |
| 770 |
} |
| 771 |
|
| 772 |
// This is from bug report #18 |
| 773 |
func TestSublistTwoTokenPubMatchSingleTokenSub(t *testing.T) { |
| 774 |
testSublistTwoTokenPubMatchSingleTokenSub(t, NewSublistWithCache()) |
| 775 |
} |
| 776 |
|
| 777 |
func TestSublistTwoTokenPubMatchSingleTokenSubNoCache(t *testing.T) { |
| 778 |
testSublistTwoTokenPubMatchSingleTokenSub(t, NewSublistNoCache()) |
| 779 |
} |
| 780 |
|
| 781 |
func testSublistTwoTokenPubMatchSingleTokenSub(t *testing.T, s *Sublist) { |
| 782 |
sub := newSub("foo") |
| 783 |
s.Insert(sub) |
| 784 |
r := s.Match("foo") |
| 785 |
verifyLen(r.psubs, 1, t) |
| 786 |
verifyMember(r.psubs, sub, t) |
| 787 |
r = s.Match("foo.bar") |
| 788 |
verifyLen(r.psubs, 0, t) |
| 789 |
} |
| 790 |
|
| 791 |
func TestSublistInsertWithWildcardsAsLiterals(t *testing.T) { |
| 792 |
testSublistInsertWithWildcardsAsLiterals(t, NewSublistWithCache()) |
| 793 |
} |
| 794 |
|
| 795 |
func TestSublistInsertWithWildcardsAsLiteralsNoCache(t *testing.T) { |
| 796 |
testSublistInsertWithWildcardsAsLiterals(t, NewSublistNoCache()) |
| 797 |
} |
| 798 |
|
| 799 |
func testSublistInsertWithWildcardsAsLiterals(t *testing.T, s *Sublist) { |
| 800 |
subjects := []string{"foo.*-", "foo.>-"} |
| 801 |
for _, subject := range subjects { |
| 802 |
sub := newSub(subject) |
| 803 |
s.Insert(sub) |
| 804 |
// Should find no match |
| 805 |
r := s.Match("foo.bar") |
| 806 |
verifyLen(r.psubs, 0, t) |
| 807 |
// Should find a match |
| 808 |
r = s.Match(subject) |
| 809 |
verifyLen(r.psubs, 1, t) |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
func TestSublistRemoveWithWildcardsAsLiterals(t *testing.T) { |
| 814 |
testSublistRemoveWithWildcardsAsLiterals(t, NewSublistWithCache()) |
| 815 |
} |
| 816 |
|
| 817 |
func TestSublistRemoveWithWildcardsAsLiteralsNoCache(t *testing.T) { |
| 818 |
testSublistRemoveWithWildcardsAsLiterals(t, NewSublistNoCache()) |
| 819 |
} |
| 820 |
|
| 821 |
func testSublistRemoveWithWildcardsAsLiterals(t *testing.T, s *Sublist) { |
| 822 |
subjects := []string{"foo.*-", "foo.>-"} |
| 823 |
for _, subject := range subjects { |
| 824 |
sub := newSub(subject) |
| 825 |
s.Insert(sub) |
| 826 |
// Should find no match |
| 827 |
rsub := newSub("foo.bar") |
| 828 |
s.Remove(rsub) |
| 829 |
if c := s.Count(); c != 1 { |
| 830 |
t.Fatalf("Expected sublist to still contain sub, got %v", c) |
| 831 |
} |
| 832 |
s.Remove(sub) |
| 833 |
if c := s.Count(); c != 0 { |
| 834 |
t.Fatalf("Expected sublist to be empty, got %v", c) |
| 835 |
} |
| 836 |
} |
| 837 |
} |
| 838 |
|
| 839 |
func TestSublistRaceOnRemove(t *testing.T) { |
| 840 |
testSublistRaceOnRemove(t, NewSublistWithCache()) |
| 841 |
} |
| 842 |
|
| 843 |
func TestSublistRaceOnRemoveNoCache(t *testing.T) { |
| 844 |
testSublistRaceOnRemove(t, NewSublistNoCache()) |
| 845 |
} |
| 846 |
|
| 847 |
func testSublistRaceOnRemove(t *testing.T, s *Sublist) { |
| 848 |
var ( |
| 849 |
total = 100 |
| 850 |
subs = make(map[int]*subscription, total) // use map for randomness |
| 851 |
) |
| 852 |
for i := 0; i < total; i++ { |
| 853 |
sub := newQSub("foo", "bar") |
| 854 |
subs[i] = sub |
| 855 |
} |
| 856 |
|
| 857 |
for i := 0; i < 2; i++ { |
| 858 |
for _, sub := range subs { |
| 859 |
s.Insert(sub) |
| 860 |
} |
| 861 |
// Call Match() once or twice, to make sure we get from cache |
| 862 |
if i == 1 { |
| 863 |
s.Match("foo") |
| 864 |
} |
| 865 |
// This will be from cache when i==1 |
| 866 |
r := s.Match("foo") |
| 867 |
wg := sync.WaitGroup{} |
| 868 |
wg.Add(1) |
| 869 |
go func() { |
| 870 |
for _, sub := range subs { |
| 871 |
s.Remove(sub) |
| 872 |
} |
| 873 |
wg.Done() |
| 874 |
}() |
| 875 |
for _, qsub := range r.qsubs { |
| 876 |
for i := 0; i < len(qsub); i++ { |
| 877 |
sub := qsub[i] |
| 878 |
if string(sub.queue) != "bar" { |
| 879 |
t.Fatalf("Queue name should be bar, got %s", qsub[i].queue) |
| 880 |
} |
| 881 |
} |
| 882 |
} |
| 883 |
wg.Wait() |
| 884 |
} |
| 885 |
|
| 886 |
// Repeat tests with regular subs |
| 887 |
for i := 0; i < total; i++ { |
| 888 |
sub := newSub("foo") |
| 889 |
subs[i] = sub |
| 890 |
} |
| 891 |
|
| 892 |
for i := 0; i < 2; i++ { |
| 893 |
for _, sub := range subs { |
| 894 |
s.Insert(sub) |
| 895 |
} |
| 896 |
// Call Match() once or twice, to make sure we get from cache |
| 897 |
if i == 1 { |
| 898 |
s.Match("foo") |
| 899 |
} |
| 900 |
// This will be from cache when i==1 |
| 901 |
r := s.Match("foo") |
| 902 |
wg := sync.WaitGroup{} |
| 903 |
wg.Add(1) |
| 904 |
go func() { |
| 905 |
for _, sub := range subs { |
| 906 |
s.Remove(sub) |
| 907 |
} |
| 908 |
wg.Done() |
| 909 |
}() |
| 910 |
for i := 0; i < len(r.psubs); i++ { |
| 911 |
sub := r.psubs[i] |
| 912 |
if string(sub.subject) != "foo" { |
| 913 |
t.Fatalf("Subject should be foo, got %s", sub.subject) |
| 914 |
} |
| 915 |
} |
| 916 |
wg.Wait() |
| 917 |
} |
| 918 |
} |
| 919 |
|
| 920 |
func TestSublistRaceOnInsert(t *testing.T) { |
| 921 |
testSublistRaceOnInsert(t, NewSublistWithCache()) |
| 922 |
} |
| 923 |
|
| 924 |
func TestSublistRaceOnInsertNoCache(t *testing.T) { |
| 925 |
testSublistRaceOnInsert(t, NewSublistNoCache()) |
| 926 |
} |
| 927 |
|
| 928 |
func testSublistRaceOnInsert(t *testing.T, s *Sublist) { |
| 929 |
var ( |
| 930 |
total = 100 |
| 931 |
subs = make(map[int]*subscription, total) // use map for randomness |
| 932 |
wg sync.WaitGroup |
| 933 |
) |
| 934 |
for i := 0; i < total; i++ { |
| 935 |
sub := newQSub("foo", "bar") |
| 936 |
subs[i] = sub |
| 937 |
} |
| 938 |
wg.Add(1) |
| 939 |
go func() { |
| 940 |
for _, sub := range subs { |
| 941 |
s.Insert(sub) |
| 942 |
} |
| 943 |
wg.Done() |
| 944 |
}() |
| 945 |
for i := 0; i < 1000; i++ { |
| 946 |
r := s.Match("foo") |
| 947 |
for _, qsubs := range r.qsubs { |
| 948 |
for _, qsub := range qsubs { |
| 949 |
if string(qsub.queue) != "bar" { |
| 950 |
t.Fatalf("Expected queue name to be bar, got %v", string(qsub.queue)) |
| 951 |
} |
| 952 |
} |
| 953 |
} |
| 954 |
} |
| 955 |
wg.Wait() |
| 956 |
|
| 957 |
// Repeat the test with plain subs |
| 958 |
for i := 0; i < total; i++ { |
| 959 |
sub := newSub("foo") |
| 960 |
subs[i] = sub |
| 961 |
} |
| 962 |
wg.Add(1) |
| 963 |
go func() { |
| 964 |
for _, sub := range subs { |
| 965 |
s.Insert(sub) |
| 966 |
} |
| 967 |
wg.Done() |
| 968 |
}() |
| 969 |
for i := 0; i < 1000; i++ { |
| 970 |
r := s.Match("foo") |
| 971 |
for _, sub := range r.psubs { |
| 972 |
if string(sub.subject) != "foo" { |
| 973 |
t.Fatalf("Expected subject to be foo, got %v", string(sub.subject)) |
| 974 |
} |
| 975 |
} |
| 976 |
} |
| 977 |
wg.Wait() |
| 978 |
} |
| 979 |
|
| 980 |
func TestSublistRaceOnMatch(t *testing.T) { |
| 981 |
s := NewSublistNoCache() |
| 982 |
s.Insert(newQSub("foo.*", "workers")) |
| 983 |
s.Insert(newQSub("foo.bar", "workers")) |
| 984 |
s.Insert(newSub("foo.*")) |
| 985 |
s.Insert(newSub("foo.bar")) |
| 986 |
|
| 987 |
wg := sync.WaitGroup{} |
| 988 |
wg.Add(2) |
| 989 |
errCh := make(chan error, 2) |
| 990 |
f := func() { |
| 991 |
defer wg.Done() |
| 992 |
for i := 0; i < 10; i++ { |
| 993 |
r := s.Match("foo.bar") |
| 994 |
for _, sub := range r.psubs { |
| 995 |
if !strings.HasPrefix(string(sub.subject), "foo.") { |
| 996 |
errCh <- fmt.Errorf("Wrong subject: %s", sub.subject) |
| 997 |
return |
| 998 |
} |
| 999 |
} |
| 1000 |
for _, qsub := range r.qsubs { |
| 1001 |
for _, sub := range qsub { |
| 1002 |
if string(sub.queue) != "workers" { |
| 1003 |
errCh <- fmt.Errorf("Wrong queue name: %s", sub.queue) |
| 1004 |
return |
| 1005 |
} |
| 1006 |
} |
| 1007 |
} |
| 1008 |
} |
| 1009 |
} |
| 1010 |
go f() |
| 1011 |
go f() |
| 1012 |
wg.Wait() |
| 1013 |
select { |
| 1014 |
case e := <-errCh: |
| 1015 |
t.Fatal(e.Error()) |
| 1016 |
default: |
| 1017 |
} |
| 1018 |
} |
| 1019 |
|
| 1020 |
// Remote subscriptions for queue subscribers will be weighted such that a single subscription |
| 1021 |
// is received, but represents all of the queue subscribers on the remote side. |
| 1022 |
func TestSublistRemoteQueueSubscriptions(t *testing.T) { |
| 1023 |
testSublistRemoteQueueSubscriptions(t, NewSublistWithCache()) |
| 1024 |
} |
| 1025 |
|
| 1026 |
func TestSublistRemoteQueueSubscriptionsNoCache(t *testing.T) { |
| 1027 |
testSublistRemoteQueueSubscriptions(t, NewSublistNoCache()) |
| 1028 |
} |
| 1029 |
|
| 1030 |
func testSublistRemoteQueueSubscriptions(t *testing.T, s *Sublist) { |
| 1031 |
// Normals |
| 1032 |
s1 := newQSub("foo", "bar") |
| 1033 |
s2 := newQSub("foo", "bar") |
| 1034 |
s.Insert(s1) |
| 1035 |
s.Insert(s2) |
| 1036 |
|
| 1037 |
// Now do weighted remotes. |
| 1038 |
rs1 := newRemoteQSub("foo", "bar", 10) |
| 1039 |
s.Insert(rs1) |
| 1040 |
rs2 := newRemoteQSub("foo", "bar", 10) |
| 1041 |
s.Insert(rs2) |
| 1042 |
|
| 1043 |
// These are just shadowed in results, so should appear as 4 subs. |
| 1044 |
verifyCount(s, 4, t) |
| 1045 |
|
| 1046 |
r := s.Match("foo") |
| 1047 |
verifyLen(r.psubs, 0, t) |
| 1048 |
verifyQLen(r.qsubs, 1, t) |
| 1049 |
verifyLen(r.qsubs[0], 22, t) |
| 1050 |
|
| 1051 |
s.Remove(s1) |
| 1052 |
s.Remove(rs1) |
| 1053 |
|
| 1054 |
verifyCount(s, 2, t) |
| 1055 |
|
| 1056 |
// Now make sure our shadowed results are correct after a removal. |
| 1057 |
r = s.Match("foo") |
| 1058 |
verifyLen(r.psubs, 0, t) |
| 1059 |
verifyQLen(r.qsubs, 1, t) |
| 1060 |
verifyLen(r.qsubs[0], 11, t) |
| 1061 |
|
| 1062 |
// Now do an update to an existing remote sub to update its weight. |
| 1063 |
rs2.qw = 1 |
| 1064 |
s.UpdateRemoteQSub(rs2) |
| 1065 |
|
| 1066 |
// Results should reflect new weight. |
| 1067 |
r = s.Match("foo") |
| 1068 |
verifyLen(r.psubs, 0, t) |
| 1069 |
verifyQLen(r.qsubs, 1, t) |
| 1070 |
verifyLen(r.qsubs[0], 2, t) |
| 1071 |
} |
| 1072 |
|
| 1073 |
func TestSublistSharedEmptyResult(t *testing.T) { |
| 1074 |
s := NewSublistWithCache() |
| 1075 |
r1 := s.Match("foo") |
| 1076 |
verifyLen(r1.psubs, 0, t) |
| 1077 |
verifyQLen(r1.qsubs, 0, t) |
| 1078 |
|
| 1079 |
r2 := s.Match("bar") |
| 1080 |
verifyLen(r2.psubs, 0, t) |
| 1081 |
verifyQLen(r2.qsubs, 0, t) |
| 1082 |
|
| 1083 |
if r1 != r2 { |
| 1084 |
t.Fatalf("Expected empty result to be a shared result set") |
| 1085 |
} |
| 1086 |
} |
| 1087 |
|
| 1088 |
func TestSublistNoCacheStats(t *testing.T) { |
| 1089 |
s := NewSublistNoCache() |
| 1090 |
s.Insert(newSub("foo")) |
| 1091 |
s.Insert(newSub("bar")) |
| 1092 |
s.Insert(newSub("baz")) |
| 1093 |
s.Insert(newSub("foo.bar.baz")) |
| 1094 |
s.Match("a.b.c") |
| 1095 |
s.Match("bar") |
| 1096 |
stats := s.Stats() |
| 1097 |
if stats.NumCache != 0 { |
| 1098 |
t.Fatalf("Expected 0 for NumCache stat, got %d", stats.NumCache) |
| 1099 |
} |
| 1100 |
} |
| 1101 |
|
| 1102 |
func TestSublistAll(t *testing.T) { |
| 1103 |
s := NewSublistNoCache() |
| 1104 |
subs := []*subscription{ |
| 1105 |
newSub("foo.bar.baz"), |
| 1106 |
newSub("foo"), |
| 1107 |
newSub("baz"), |
| 1108 |
} |
| 1109 |
// alter client's kind |
| 1110 |
subs[0].client.kind = LEAF |
| 1111 |
for _, sub := range subs { |
| 1112 |
s.Insert(sub) |
| 1113 |
} |
| 1114 |
|
| 1115 |
var buf [32]*subscription |
| 1116 |
output := buf[:0] |
| 1117 |
s.All(&output) |
| 1118 |
if len(output) != len(subs) { |
| 1119 |
t.Fatalf("Expected %d for All, got %d", len(subs), len(output)) |
| 1120 |
} |
| 1121 |
} |
| 1122 |
|
| 1123 |
func TestIsSubsetMatch(t *testing.T) { |
| 1124 |
for _, test := range []struct { |
| 1125 |
subject string |
| 1126 |
test string |
| 1127 |
result bool |
| 1128 |
}{ |
| 1129 |
{"foo.bar", "foo.bar", true}, |
| 1130 |
{"foo.*", ">", true}, |
| 1131 |
{"foo.*", "*.*", true}, |
| 1132 |
{"foo.*", "foo.*", true}, |
| 1133 |
{"foo.*", "foo.bar", false}, |
| 1134 |
{"foo.>", ">", true}, |
| 1135 |
{"foo.>", "*.>", true}, |
| 1136 |
{"foo.>", "foo.>", true}, |
| 1137 |
{"foo.>", "foo.bar", false}, |
| 1138 |
{"foo..bar", "foo.*", false}, // Bad subject, we return false |
| 1139 |
{"foo.*", "foo..bar", false}, // Bad subject, we return false |
| 1140 |
} { |
| 1141 |
t.Run("", func(t *testing.T) { |
| 1142 |
if res := subjectIsSubsetMatch(test.subject, test.test); res != test.result { |
| 1143 |
t.Fatalf("Subject %q subset match of %q, should be %v, got %v", |
| 1144 |
test.test, test.subject, test.result, res) |
| 1145 |
} |
| 1146 |
}) |
| 1147 |
} |
| 1148 |
} |
| 1149 |
|
| 1150 |
func TestSublistRegisterInterestNotification(t *testing.T) { |
| 1151 |
s := NewSublistWithCache() |
| 1152 |
ch := make(chan bool, 1) |
| 1153 |
|
| 1154 |
expectErr := func(subject string) { |
| 1155 |
if err := s.RegisterNotification(subject, ch); err != ErrInvalidSubject { |
| 1156 |
t.Fatalf("Expected err, got %v", err) |
| 1157 |
} |
| 1158 |
} |
| 1159 |
|
| 1160 |
// Test that we require a literal subject. |
| 1161 |
expectErr("foo.*") |
| 1162 |
expectErr(">") |
| 1163 |
|
| 1164 |
// Chan needs to be non-nil |
| 1165 |
if err := s.RegisterNotification("foo", nil); err != ErrNilChan { |
| 1166 |
t.Fatalf("Expected err, got %v", err) |
| 1167 |
} |
| 1168 |
|
| 1169 |
// Clearing one that is not there will return false. |
| 1170 |
if s.ClearNotification("foo", ch) { |
| 1171 |
t.Fatalf("Expected to return false on non-existent notification entry") |
| 1172 |
} |
| 1173 |
|
| 1174 |
// This should work properly. |
| 1175 |
if err := s.RegisterNotification("foo", ch); err != nil { |
| 1176 |
t.Fatalf("Unexpected error: %v", err) |
| 1177 |
} |
| 1178 |
|
| 1179 |
tt := time.NewTimer(time.Second) |
| 1180 |
expectBoolWithCh := func(ch chan bool, b bool) { |
| 1181 |
t.Helper() |
| 1182 |
tt.Reset(time.Second) |
| 1183 |
defer tt.Stop() |
| 1184 |
select { |
| 1185 |
case v := <-ch: |
| 1186 |
if v != b { |
| 1187 |
t.Fatalf("Expected %v, got %v", b, v) |
| 1188 |
} |
| 1189 |
case <-tt.C: |
| 1190 |
t.Fatalf("Timeout waiting for expected value") |
| 1191 |
} |
| 1192 |
} |
| 1193 |
expectBool := func(b bool) { |
| 1194 |
t.Helper() |
| 1195 |
expectBoolWithCh(ch, b) |
| 1196 |
} |
| 1197 |
expectFalse := func() { |
| 1198 |
t.Helper() |
| 1199 |
expectBool(false) |
| 1200 |
} |
| 1201 |
expectTrue := func() { |
| 1202 |
t.Helper() |
| 1203 |
expectBool(true) |
| 1204 |
} |
| 1205 |
expectNone := func() { |
| 1206 |
t.Helper() |
| 1207 |
if lch := len(ch); lch != 0 { |
| 1208 |
t.Fatalf("Expected no notifications, had %d and first was %v", lch, <-ch) |
| 1209 |
} |
| 1210 |
} |
| 1211 |
expectOneWithCh := func(ch chan bool) { |
| 1212 |
t.Helper() |
| 1213 |
if len(ch) != 1 { |
| 1214 |
t.Fatalf("Expected 1 notification") |
| 1215 |
} |
| 1216 |
} |
| 1217 |
expectOne := func() { |
| 1218 |
t.Helper() |
| 1219 |
expectOneWithCh(ch) |
| 1220 |
} |
| 1221 |
|
| 1222 |
expectOne() |
| 1223 |
expectFalse() |
| 1224 |
sub := newSub("foo") |
| 1225 |
s.Insert(sub) |
| 1226 |
expectTrue() |
| 1227 |
|
| 1228 |
sub2 := newSub("foo") |
| 1229 |
s.Insert(sub2) |
| 1230 |
expectNone() |
| 1231 |
|
| 1232 |
if err := s.RegisterNotification("bar", ch); err != nil { |
| 1233 |
t.Fatalf("Unexpected error: %v", err) |
| 1234 |
} |
| 1235 |
expectFalse() |
| 1236 |
|
| 1237 |
sub3 := newSub("foo") |
| 1238 |
s.Insert(sub3) |
| 1239 |
expectNone() |
| 1240 |
|
| 1241 |
// Now remove literals. |
| 1242 |
s.Remove(sub) |
| 1243 |
expectNone() |
| 1244 |
s.Remove(sub2) |
| 1245 |
expectNone() |
| 1246 |
s.Remove(sub3) |
| 1247 |
expectFalse() |
| 1248 |
|
| 1249 |
if err := s.RegisterNotification("test.node", ch); err != nil { |
| 1250 |
t.Fatalf("Unexpected error: %v", err) |
| 1251 |
} |
| 1252 |
expectOne() |
| 1253 |
expectFalse() |
| 1254 |
|
| 1255 |
tnSub1 := newSub("test.node.already.exist") |
| 1256 |
s.Insert(tnSub1) |
| 1257 |
expectNone() |
| 1258 |
|
| 1259 |
tnSub2 := newSub("test.node") |
| 1260 |
s.Insert(tnSub2) |
| 1261 |
expectTrue() |
| 1262 |
|
| 1263 |
tnSub3 := newSub("test.node") |
| 1264 |
s.Insert(tnSub3) |
| 1265 |
expectNone() |
| 1266 |
|
| 1267 |
s.Remove(tnSub1) |
| 1268 |
expectNone() |
| 1269 |
s.Remove(tnSub2) |
| 1270 |
expectNone() |
| 1271 |
s.Remove(tnSub3) |
| 1272 |
expectFalse() |
| 1273 |
|
| 1274 |
if !s.ClearNotification("test.node", ch) { |
| 1275 |
t.Fatalf("Expected to return true") |
| 1276 |
} |
| 1277 |
|
| 1278 |
sub4 := newSub("bar") |
| 1279 |
s.Insert(sub4) |
| 1280 |
expectTrue() |
| 1281 |
|
| 1282 |
if !s.ClearNotification("bar", ch) { |
| 1283 |
t.Fatalf("Expected to return true") |
| 1284 |
} |
| 1285 |
s.RLock() |
| 1286 |
lnr := len(s.notify.remove) |
| 1287 |
s.RUnlock() |
| 1288 |
if lnr != 0 { |
| 1289 |
t.Fatalf("Expected zero entries for remove notify, got %d", lnr) |
| 1290 |
} |
| 1291 |
if !s.ClearNotification("foo", ch) { |
| 1292 |
t.Fatalf("Expected to return true") |
| 1293 |
} |
| 1294 |
s.RLock() |
| 1295 |
notifyMap := s.notify |
| 1296 |
s.RUnlock() |
| 1297 |
if notifyMap != nil { |
| 1298 |
t.Fatalf("Expected the notify map to be nil") |
| 1299 |
} |
| 1300 |
|
| 1301 |
// Let's do some wildcard checks. |
| 1302 |
// Wildcards will not trigger interest. |
| 1303 |
subpwc := newSub("*") |
| 1304 |
s.Insert(subpwc) |
| 1305 |
expectNone() |
| 1306 |
|
| 1307 |
if err := s.RegisterNotification("foo", ch); err != nil { |
| 1308 |
t.Fatalf("Unexpected error: %v", err) |
| 1309 |
} |
| 1310 |
expectFalse() |
| 1311 |
|
| 1312 |
s.Insert(sub) |
| 1313 |
expectTrue() |
| 1314 |
|
| 1315 |
s.Remove(sub) |
| 1316 |
expectFalse() |
| 1317 |
|
| 1318 |
s.Remove(subpwc) |
| 1319 |
expectNone() |
| 1320 |
|
| 1321 |
subfwc := newSub(">") |
| 1322 |
s.Insert(subfwc) |
| 1323 |
expectNone() |
| 1324 |
|
| 1325 |
s.Insert(subpwc) |
| 1326 |
expectNone() |
| 1327 |
|
| 1328 |
s.Remove(subpwc) |
| 1329 |
expectNone() |
| 1330 |
|
| 1331 |
s.Remove(subfwc) |
| 1332 |
expectNone() |
| 1333 |
|
| 1334 |
// Test batch |
| 1335 |
subs := []*subscription{sub, sub2, sub3, sub4, subpwc, subfwc} |
| 1336 |
for _, sub := range subs { |
| 1337 |
s.Insert(sub) |
| 1338 |
} |
| 1339 |
expectTrue() |
| 1340 |
|
| 1341 |
s.RemoveBatch(subs) |
| 1342 |
expectOne() |
| 1343 |
expectFalse() |
| 1344 |
|
| 1345 |
// Test queue subs |
| 1346 |
// We know make sure that if you have qualified a queue group it has to match, etc. |
| 1347 |
// Also if you do not specify one they will not trigger. |
| 1348 |
qsub := newQSub("foo.bar.baz", "1") |
| 1349 |
s.Insert(qsub) |
| 1350 |
expectNone() |
| 1351 |
|
| 1352 |
if err := s.RegisterNotification("foo.bar.baz", ch); err != nil { |
| 1353 |
t.Fatalf("Unexpected error: %v", err) |
| 1354 |
} |
| 1355 |
expectFalse() |
| 1356 |
|
| 1357 |
wcqsub := newQSub("foo.bar.>", "1") |
| 1358 |
s.Insert(wcqsub) |
| 1359 |
expectNone() |
| 1360 |
|
| 1361 |
s.Remove(qsub) |
| 1362 |
expectNone() |
| 1363 |
|
| 1364 |
s.Remove(wcqsub) |
| 1365 |
expectNone() |
| 1366 |
|
| 1367 |
s.Insert(wcqsub) |
| 1368 |
expectNone() |
| 1369 |
|
| 1370 |
if err := s.RegisterQueueNotification("queue.test.node", "q22", ch); err != nil { |
| 1371 |
t.Fatalf("Unexpected error: %v", err) |
| 1372 |
} |
| 1373 |
expectOne() |
| 1374 |
expectFalse() |
| 1375 |
|
| 1376 |
qsub1 := newQSub("queue.test.node.already.exist", "queue") |
| 1377 |
s.Insert(qsub1) |
| 1378 |
expectNone() |
| 1379 |
|
| 1380 |
qsub2 := newQSub("queue.test.node", "q22") |
| 1381 |
s.Insert(qsub2) |
| 1382 |
expectTrue() |
| 1383 |
|
| 1384 |
qsub3 := newQSub("queue.test.node", "otherqueue") |
| 1385 |
s.Insert(qsub3) |
| 1386 |
expectNone() |
| 1387 |
|
| 1388 |
qsub4 := newQSub("queue.different.node", "q22") |
| 1389 |
s.Insert(qsub4) |
| 1390 |
expectNone() |
| 1391 |
|
| 1392 |
qsub5 := newQSub("queue.test.node", "q22") |
| 1393 |
s.Insert(qsub5) |
| 1394 |
expectNone() |
| 1395 |
|
| 1396 |
s.Remove(qsub3) |
| 1397 |
expectNone() |
| 1398 |
s.Remove(qsub1) |
| 1399 |
expectNone() |
| 1400 |
s.Remove(qsub2) |
| 1401 |
expectNone() |
| 1402 |
s.Remove(qsub4) |
| 1403 |
expectNone() |
| 1404 |
s.Remove(qsub5) |
| 1405 |
expectFalse() |
| 1406 |
|
| 1407 |
if !s.ClearQueueNotification("queue.test.node", "q22", ch) { |
| 1408 |
t.Fatalf("Expected to return true") |
| 1409 |
} |
| 1410 |
|
| 1411 |
if err := s.RegisterQueueNotification("some.subject", "queue1", ch); err != nil { |
| 1412 |
t.Fatalf("Unexpected error: %v", err) |
| 1413 |
} |
| 1414 |
expectOne() |
| 1415 |
expectFalse() |
| 1416 |
|
| 1417 |
qsub1 = newQSub("some.subject", "queue1") |
| 1418 |
s.Insert(qsub1) |
| 1419 |
expectTrue() |
| 1420 |
|
| 1421 |
// Create a second channel for this other queue |
| 1422 |
ch2 := make(chan bool, 1) |
| 1423 |
if err := s.RegisterQueueNotification("some.subject", "queue2", ch2); err != nil { |
| 1424 |
t.Fatalf("Unexpected error: %v", err) |
| 1425 |
} |
| 1426 |
expectOneWithCh(ch2) |
| 1427 |
expectBoolWithCh(ch2, false) |
| 1428 |
|
| 1429 |
qsub2 = newQSub("some.subject", "queue2") |
| 1430 |
s.Insert(qsub2) |
| 1431 |
expectBoolWithCh(ch2, true) |
| 1432 |
|
| 1433 |
// But we should not get notification on queue1 |
| 1434 |
expectNone() |
| 1435 |
|
| 1436 |
s.Remove(qsub1) |
| 1437 |
expectFalse() |
| 1438 |
s.Remove(qsub2) |
| 1439 |
expectBoolWithCh(ch2, false) |
| 1440 |
|
| 1441 |
if !s.ClearQueueNotification("some.subject", "queue1", ch) { |
| 1442 |
t.Fatalf("Expected to return true") |
| 1443 |
} |
| 1444 |
if !s.ClearQueueNotification("some.subject", "queue2", ch2) { |
| 1445 |
t.Fatalf("Expected to return true") |
| 1446 |
} |
| 1447 |
|
| 1448 |
// Test non-blocking notifications. |
| 1449 |
if err := s.RegisterNotification("bar", ch); err != nil { |
| 1450 |
t.Fatalf("Unexpected error: %v", err) |
| 1451 |
} |
| 1452 |
|
| 1453 |
if err := s.RegisterNotification("baz", ch); err != nil { |
| 1454 |
t.Fatalf("Unexpected error: %v", err) |
| 1455 |
} |
| 1456 |
|
| 1457 |
s.Insert(newSub("baz")) |
| 1458 |
s.Insert(newSub("bar")) |
| 1459 |
s.Insert(subpwc) |
| 1460 |
expectOne() |
| 1461 |
expectFalse() |
| 1462 |
} |
| 1463 |
|
| 1464 |
func TestSublistReverseMatch(t *testing.T) { |
| 1465 |
s := NewSublistWithCache() |
| 1466 |
fooSub := newSub("foo") |
| 1467 |
barSub := newSub("bar") |
| 1468 |
fooBarSub := newSub("foo.bar") |
| 1469 |
fooBazSub := newSub("foo.baz") |
| 1470 |
fooBarBazSub := newSub("foo.bar.baz") |
| 1471 |
s.Insert(fooSub) |
| 1472 |
s.Insert(barSub) |
| 1473 |
s.Insert(fooBarSub) |
| 1474 |
s.Insert(fooBazSub) |
| 1475 |
s.Insert(fooBarBazSub) |
| 1476 |
|
| 1477 |
r := s.ReverseMatch("foo") |
| 1478 |
verifyLen(r.psubs, 1, t) |
| 1479 |
verifyMember(r.psubs, fooSub, t) |
| 1480 |
|
| 1481 |
r = s.ReverseMatch("bar") |
| 1482 |
verifyLen(r.psubs, 1, t) |
| 1483 |
verifyMember(r.psubs, barSub, t) |
| 1484 |
|
| 1485 |
r = s.ReverseMatch("*") |
| 1486 |
verifyLen(r.psubs, 2, t) |
| 1487 |
verifyMember(r.psubs, fooSub, t) |
| 1488 |
verifyMember(r.psubs, barSub, t) |
| 1489 |
|
| 1490 |
r = s.ReverseMatch("baz") |
| 1491 |
verifyLen(r.psubs, 0, t) |
| 1492 |
|
| 1493 |
r = s.ReverseMatch("foo.*") |
| 1494 |
verifyLen(r.psubs, 2, t) |
| 1495 |
verifyMember(r.psubs, fooBarSub, t) |
| 1496 |
verifyMember(r.psubs, fooBazSub, t) |
| 1497 |
|
| 1498 |
r = s.ReverseMatch("*.*") |
| 1499 |
verifyLen(r.psubs, 2, t) |
| 1500 |
verifyMember(r.psubs, fooBarSub, t) |
| 1501 |
verifyMember(r.psubs, fooBazSub, t) |
| 1502 |
|
| 1503 |
r = s.ReverseMatch("*.bar") |
| 1504 |
verifyLen(r.psubs, 1, t) |
| 1505 |
verifyMember(r.psubs, fooBarSub, t) |
| 1506 |
|
| 1507 |
r = s.ReverseMatch("*.baz") |
| 1508 |
verifyLen(r.psubs, 1, t) |
| 1509 |
verifyMember(r.psubs, fooBazSub, t) |
| 1510 |
|
| 1511 |
r = s.ReverseMatch("bar.*") |
| 1512 |
verifyLen(r.psubs, 0, t) |
| 1513 |
|
| 1514 |
r = s.ReverseMatch("*.bat") |
| 1515 |
verifyLen(r.psubs, 0, t) |
| 1516 |
|
| 1517 |
r = s.ReverseMatch("foo.>") |
| 1518 |
verifyLen(r.psubs, 3, t) |
| 1519 |
verifyMember(r.psubs, fooBarSub, t) |
| 1520 |
verifyMember(r.psubs, fooBazSub, t) |
| 1521 |
verifyMember(r.psubs, fooBarBazSub, t) |
| 1522 |
|
| 1523 |
r = s.ReverseMatch(">") |
| 1524 |
verifyLen(r.psubs, 5, t) |
| 1525 |
verifyMember(r.psubs, fooSub, t) |
| 1526 |
verifyMember(r.psubs, barSub, t) |
| 1527 |
verifyMember(r.psubs, fooBarSub, t) |
| 1528 |
verifyMember(r.psubs, fooBazSub, t) |
| 1529 |
verifyMember(r.psubs, fooBarBazSub, t) |
| 1530 |
} |
| 1531 |
|
| 1532 |
func TestSublistReverseMatchWider(t *testing.T) { |
| 1533 |
s := NewSublistWithCache() |
| 1534 |
sub := newSub("uplink.*.*.>") |
| 1535 |
s.Insert(sub) |
| 1536 |
|
| 1537 |
r := s.ReverseMatch("uplink.1.*.*.>") |
| 1538 |
verifyLen(r.psubs, 1, t) |
| 1539 |
verifyMember(r.psubs, sub, t) |
| 1540 |
|
| 1541 |
r = s.ReverseMatch("uplink.1.2.3.>") |
| 1542 |
verifyLen(r.psubs, 1, t) |
| 1543 |
verifyMember(r.psubs, sub, t) |
| 1544 |
} |
| 1545 |
|
| 1546 |
func TestSublistMatchWithEmptyTokens(t *testing.T) { |
| 1547 |
for _, test := range []struct { |
| 1548 |
name string |
| 1549 |
cache bool |
| 1550 |
}{ |
| 1551 |
{"cache", true}, |
| 1552 |
{"no cache", false}, |
| 1553 |
} { |
| 1554 |
t.Run(test.name, func(t *testing.T) { |
| 1555 |
sl := NewSublist(true) |
| 1556 |
sub1 := newSub(">") |
| 1557 |
sub2 := newQSub(">", "queue") |
| 1558 |
sl.Insert(sub1) |
| 1559 |
sl.Insert(sub2) |
| 1560 |
|
| 1561 |
for _, subj := range []string{".foo", "..foo", "foo..", "foo.", "foo..bar", "foo...bar"} { |
| 1562 |
t.Run(subj, func(t *testing.T) { |
| 1563 |
r := sl.Match(subj) |
| 1564 |
verifyLen(r.psubs, 0, t) |
| 1565 |
verifyQLen(r.qsubs, 0, t) |
| 1566 |
}) |
| 1567 |
} |
| 1568 |
}) |
| 1569 |
} |
| 1570 |
} |
| 1571 |
|
| 1572 |
func TestSublistSubjectCollide(t *testing.T) { |
| 1573 |
require_False(t, SubjectsCollide("foo.*", "foo.*.bar.>")) |
| 1574 |
require_False(t, SubjectsCollide("foo.*.bar.>", "foo.*")) |
| 1575 |
require_True(t, SubjectsCollide("foo.*", "foo.foo")) |
| 1576 |
require_True(t, SubjectsCollide("foo.*", "*.foo")) |
| 1577 |
require_True(t, SubjectsCollide("foo.bar.>", "*.bar.foo")) |
| 1578 |
} |
| 1579 |
|
| 1580 |
func TestSublistAddCacheHitRate(t *testing.T) { |
| 1581 |
sl1 := NewSublistWithCache() |
| 1582 |
fooSub := newSub("foo") |
| 1583 |
sl1.Insert(fooSub) |
| 1584 |
for i := 0; i < 4; i++ { |
| 1585 |
sl1.Match("foo") |
| 1586 |
} |
| 1587 |
stats1 := sl1.Stats() |
| 1588 |
require_True(t, stats1.CacheHitRate == 0.75) |
| 1589 |
|
| 1590 |
sl2 := NewSublistWithCache() |
| 1591 |
barSub := newSub("bar") |
| 1592 |
sl2.Insert(barSub) |
| 1593 |
for i := 0; i < 4; i++ { |
| 1594 |
sl2.Match("bar") |
| 1595 |
} |
| 1596 |
stats2 := sl2.Stats() |
| 1597 |
require_True(t, stats2.CacheHitRate == 0.75) |
| 1598 |
|
| 1599 |
ts := &SublistStats{} |
| 1600 |
ts.add(stats1) |
| 1601 |
ts.add(stats2) |
| 1602 |
require_True(t, ts.CacheHitRate == 0.75) |
| 1603 |
} |
| 1604 |
|
| 1605 |
// -- Benchmarks Setup -- |
| 1606 |
|
| 1607 |
var benchSublistSubs []*subscription |
| 1608 |
var benchSublistSl = NewSublistWithCache() |
| 1609 |
|
| 1610 |
// https://github.com/golang/go/issues/31859 |
| 1611 |
func TestMain(m *testing.M) { |
| 1612 |
flag.StringVar(&testDefaultClusterCompression, "cluster_compression", _EMPTY_, "Test with this compression level as the default") |
| 1613 |
flag.StringVar(&testDefaultLeafNodeCompression, "leafnode_compression", _EMPTY_, "Test with this compression level as the default") |
| 1614 |
flag.Parse() |
| 1615 |
initSublist := false |
| 1616 |
flag.Visit(func(f *flag.Flag) { |
| 1617 |
if f.Name == "test.bench" { |
| 1618 |
initSublist = true |
| 1619 |
} |
| 1620 |
}) |
| 1621 |
if initSublist { |
| 1622 |
benchSublistSubs = make([]*subscription, 0, 256*1024) |
| 1623 |
toks := []string{"synadia", "nats", "jetstream", "nkeys", "jwt", "deny", "auth", "drain"} |
| 1624 |
subsInit("", toks) |
| 1625 |
for i := 0; i < len(benchSublistSubs); i++ { |
| 1626 |
benchSublistSl.Insert(benchSublistSubs[i]) |
| 1627 |
} |
| 1628 |
addWildcards() |
| 1629 |
} |
| 1630 |
os.Exit(m.Run()) |
| 1631 |
} |
| 1632 |
|
| 1633 |
func TestSublistHasInterest(t *testing.T) { |
| 1634 |
sl := NewSublistWithCache() |
| 1635 |
fooSub := newSub("foo") |
| 1636 |
sl.Insert(fooSub) |
| 1637 |
|
| 1638 |
// Expect to find that "foo" matches but "bar" doesn't. |
| 1639 |
// At this point nothing should be in the cache. |
| 1640 |
require_True(t, sl.HasInterest("foo")) |
| 1641 |
require_False(t, sl.HasInterest("bar")) |
| 1642 |
require_Equal(t, sl.cacheHits, 0) |
| 1643 |
|
| 1644 |
// Now call Match(), which will populate the cache. |
| 1645 |
sl.Match("foo") |
| 1646 |
require_Equal(t, sl.cacheHits, 0) |
| 1647 |
|
| 1648 |
// Future calls to HasInterest() should hit the cache now. |
| 1649 |
for i := uint64(1); i <= 5; i++ { |
| 1650 |
require_True(t, sl.HasInterest("foo")) |
| 1651 |
require_Equal(t, sl.cacheHits, i) |
| 1652 |
} |
| 1653 |
|
| 1654 |
// Call Match on a subject we know there is no match. |
| 1655 |
hits := sl.cacheHits |
| 1656 |
sl.Match("bar") |
| 1657 |
require_False(t, sl.HasInterest("bar")) |
| 1658 |
// The sublist caches "negative" results as well, expect a cache hit. |
| 1659 |
require_Equal(t, sl.cacheHits, hits+1) |
| 1660 |
|
| 1661 |
// Remove fooSub and check interest again |
| 1662 |
sl.Remove(fooSub) |
| 1663 |
require_False(t, sl.HasInterest("foo")) |
| 1664 |
|
| 1665 |
// Try with some wildcards |
| 1666 |
sub := newSub("foo.*") |
| 1667 |
sl.Insert(sub) |
| 1668 |
require_False(t, sl.HasInterest("foo")) |
| 1669 |
require_True(t, sl.HasInterest("foo.bar")) |
| 1670 |
require_False(t, sl.HasInterest("foo.bar.baz")) |
| 1671 |
|
| 1672 |
// Remove sub, there should be no interest |
| 1673 |
sl.Remove(sub) |
| 1674 |
require_False(t, sl.HasInterest("foo")) |
| 1675 |
require_False(t, sl.HasInterest("foo.bar")) |
| 1676 |
require_False(t, sl.HasInterest("foo.bar.baz")) |
| 1677 |
|
| 1678 |
sub = newSub("foo.>") |
| 1679 |
sl.Insert(sub) |
| 1680 |
require_False(t, sl.HasInterest("foo")) |
| 1681 |
require_True(t, sl.HasInterest("foo.bar")) |
| 1682 |
require_True(t, sl.HasInterest("foo.bar.baz")) |
| 1683 |
|
| 1684 |
sl.Remove(sub) |
| 1685 |
require_False(t, sl.HasInterest("foo")) |
| 1686 |
require_False(t, sl.HasInterest("foo.bar")) |
| 1687 |
require_False(t, sl.HasInterest("foo.bar.baz")) |
| 1688 |
|
| 1689 |
sub = newSub("*.>") |
| 1690 |
sl.Insert(sub) |
| 1691 |
require_False(t, sl.HasInterest("foo")) |
| 1692 |
require_True(t, sl.HasInterest("foo.bar")) |
| 1693 |
require_True(t, sl.HasInterest("foo.baz")) |
| 1694 |
sl.Remove(sub) |
| 1695 |
|
| 1696 |
sub = newSub("*.bar") |
| 1697 |
sl.Insert(sub) |
| 1698 |
require_False(t, sl.HasInterest("foo")) |
| 1699 |
require_True(t, sl.HasInterest("foo.bar")) |
| 1700 |
require_False(t, sl.HasInterest("foo.baz")) |
| 1701 |
sl.Remove(sub) |
| 1702 |
|
| 1703 |
sub = newSub("*") |
| 1704 |
sl.Insert(sub) |
| 1705 |
require_True(t, sl.HasInterest("foo")) |
| 1706 |
require_False(t, sl.HasInterest("foo.bar")) |
| 1707 |
sl.Remove(sub) |
| 1708 |
|
| 1709 |
// Try with queues now. |
| 1710 |
qsub := newQSub("foo", "bar") |
| 1711 |
sl.Insert(qsub) |
| 1712 |
require_True(t, sl.HasInterest("foo")) |
| 1713 |
require_False(t, sl.HasInterest("foo.bar")) |
| 1714 |
|
| 1715 |
qsub2 := newQSub("foo", "baz") |
| 1716 |
sl.Insert(qsub2) |
| 1717 |
require_True(t, sl.HasInterest("foo")) |
| 1718 |
require_False(t, sl.HasInterest("foo.bar")) |
| 1719 |
|
| 1720 |
// Remove first queue |
| 1721 |
sl.Remove(qsub) |
| 1722 |
require_True(t, sl.HasInterest("foo")) |
| 1723 |
require_False(t, sl.HasInterest("foo.bar")) |
| 1724 |
|
| 1725 |
// Remove last. |
| 1726 |
sl.Remove(qsub2) |
| 1727 |
require_False(t, sl.HasInterest("foo")) |
| 1728 |
require_False(t, sl.HasInterest("foo.bar")) |
| 1729 |
|
| 1730 |
// With wildcards now |
| 1731 |
qsub = newQSub("foo.*", "bar") |
| 1732 |
sl.Insert(qsub) |
| 1733 |
require_False(t, sl.HasInterest("foo")) |
| 1734 |
require_True(t, sl.HasInterest("foo.bar")) |
| 1735 |
require_False(t, sl.HasInterest("foo.bar.baz")) |
| 1736 |
|
| 1737 |
// Add another queue to the group |
| 1738 |
qsub2 = newQSub("foo.*", "baz") |
| 1739 |
sl.Insert(qsub2) |
| 1740 |
require_False(t, sl.HasInterest("foo")) |
| 1741 |
require_True(t, sl.HasInterest("foo.bar")) |
| 1742 |
require_False(t, sl.HasInterest("foo.bar.baz")) |
| 1743 |
|
| 1744 |
// Remove first queue |
| 1745 |
sl.Remove(qsub) |
| 1746 |
require_False(t, sl.HasInterest("foo")) |
| 1747 |
require_True(t, sl.HasInterest("foo.bar")) |
| 1748 |
require_False(t, sl.HasInterest("foo.bar.baz")) |
| 1749 |
|
| 1750 |
// Remove last |
| 1751 |
sl.Remove(qsub2) |
| 1752 |
require_False(t, sl.HasInterest("foo")) |
| 1753 |
require_False(t, sl.HasInterest("foo.bar")) |
| 1754 |
require_False(t, sl.HasInterest("foo.bar.baz")) |
| 1755 |
|
| 1756 |
qsub = newQSub("foo.>", "bar") |
| 1757 |
sl.Insert(qsub) |
| 1758 |
require_False(t, sl.HasInterest("foo")) |
| 1759 |
require_True(t, sl.HasInterest("foo.bar")) |
| 1760 |
require_True(t, sl.HasInterest("foo.bar.baz")) |
| 1761 |
|
| 1762 |
// Add another queue to the group |
| 1763 |
qsub2 = newQSub("foo.>", "baz") |
| 1764 |
sl.Insert(qsub2) |
| 1765 |
require_False(t, sl.HasInterest("foo")) |
| 1766 |
require_True(t, sl.HasInterest("foo.bar")) |
| 1767 |
require_True(t, sl.HasInterest("foo.bar.baz")) |
| 1768 |
|
| 1769 |
// Remove first queue |
| 1770 |
sl.Remove(qsub) |
| 1771 |
require_False(t, sl.HasInterest("foo")) |
| 1772 |
require_True(t, sl.HasInterest("foo.bar")) |
| 1773 |
require_True(t, sl.HasInterest("foo.bar.baz")) |
| 1774 |
|
| 1775 |
// Remove last |
| 1776 |
sl.Remove(qsub2) |
| 1777 |
require_False(t, sl.HasInterest("foo")) |
| 1778 |
require_False(t, sl.HasInterest("foo.bar")) |
| 1779 |
require_False(t, sl.HasInterest("foo.bar.baz")) |
| 1780 |
|
| 1781 |
qsub = newQSub("*.>", "bar") |
| 1782 |
sl.Insert(qsub) |
| 1783 |
require_False(t, sl.HasInterest("foo")) |
| 1784 |
require_True(t, sl.HasInterest("foo.bar")) |
| 1785 |
require_True(t, sl.HasInterest("foo.baz")) |
| 1786 |
sl.Remove(qsub) |
| 1787 |
|
| 1788 |
qsub = newQSub("*.bar", "bar") |
| 1789 |
sl.Insert(qsub) |
| 1790 |
require_False(t, sl.HasInterest("foo")) |
| 1791 |
require_True(t, sl.HasInterest("foo.bar")) |
| 1792 |
require_False(t, sl.HasInterest("foo.baz")) |
| 1793 |
sl.Remove(qsub) |
| 1794 |
|
| 1795 |
qsub = newQSub("*", "bar") |
| 1796 |
sl.Insert(qsub) |
| 1797 |
require_True(t, sl.HasInterest("foo")) |
| 1798 |
require_False(t, sl.HasInterest("foo.bar")) |
| 1799 |
sl.Remove(qsub) |
| 1800 |
} |
| 1801 |
|
| 1802 |
func TestSublistHasInterestOverlapping(t *testing.T) { |
| 1803 |
sl := NewSublistWithCache() |
| 1804 |
require_NoError(t, sl.Insert(newSub("stream.A.child"))) |
| 1805 |
require_NoError(t, sl.Insert(newSub("stream.*"))) |
| 1806 |
require_True(t, sl.HasInterest("stream.A.child")) |
| 1807 |
require_True(t, sl.HasInterest("stream.A")) |
| 1808 |
} |
| 1809 |
|
| 1810 |
func TestSublistNumInterest(t *testing.T) { |
| 1811 |
sl := NewSublistWithCache() |
| 1812 |
fooSub := newSub("foo") |
| 1813 |
sl.Insert(fooSub) |
| 1814 |
|
| 1815 |
require_NumInterest := func(t *testing.T, subj string, wnp, wnq int) { |
| 1816 |
t.Helper() |
| 1817 |
np, nq := sl.NumInterest(subj) |
| 1818 |
require_Equal(t, np, wnp) |
| 1819 |
require_Equal(t, nq, wnq) |
| 1820 |
} |
| 1821 |
|
| 1822 |
// Expect to find that "foo" matches but "bar" doesn't. |
| 1823 |
// At this point nothing should be in the cache. |
| 1824 |
require_NumInterest(t, "foo", 1, 0) |
| 1825 |
require_NumInterest(t, "bar", 0, 0) |
| 1826 |
require_Equal(t, sl.cacheHits, 0) |
| 1827 |
|
| 1828 |
// Now call Match(), which will populate the cache. |
| 1829 |
sl.Match("foo") |
| 1830 |
require_Equal(t, sl.cacheHits, 0) |
| 1831 |
|
| 1832 |
// Future calls to HasInterest() should hit the cache now. |
| 1833 |
for i := uint64(1); i <= 5; i++ { |
| 1834 |
require_NumInterest(t, "foo", 1, 0) |
| 1835 |
require_Equal(t, sl.cacheHits, i) |
| 1836 |
} |
| 1837 |
|
| 1838 |
// Call Match on a subject we know there is no match. |
| 1839 |
hits := sl.cacheHits |
| 1840 |
sl.Match("bar") |
| 1841 |
require_NumInterest(t, "bar", 0, 0) |
| 1842 |
// The sublist caches "negative" results as well, expect a cache hit. |
| 1843 |
require_Equal(t, sl.cacheHits, hits+1) |
| 1844 |
|
| 1845 |
// Remove fooSub and check interest again |
| 1846 |
sl.Remove(fooSub) |
| 1847 |
require_NumInterest(t, "foo", 0, 0) |
| 1848 |
|
| 1849 |
// Try with some wildcards |
| 1850 |
sub := newSub("foo.*") |
| 1851 |
sl.Insert(sub) |
| 1852 |
require_NumInterest(t, "foo", 0, 0) |
| 1853 |
require_NumInterest(t, "foo.bar", 1, 0) |
| 1854 |
require_NumInterest(t, "foo.bar.baz", 0, 0) |
| 1855 |
|
| 1856 |
// Remove sub, there should be no interest |
| 1857 |
sl.Remove(sub) |
| 1858 |
require_NumInterest(t, "foo", 0, 0) |
| 1859 |
require_NumInterest(t, "foo.bar", 0, 0) |
| 1860 |
require_NumInterest(t, "foo.bar.baz", 0, 0) |
| 1861 |
|
| 1862 |
sub = newSub("foo.>") |
| 1863 |
sl.Insert(sub) |
| 1864 |
require_NumInterest(t, "foo", 0, 0) |
| 1865 |
require_NumInterest(t, "foo.bar", 1, 0) |
| 1866 |
require_NumInterest(t, "foo.bar.baz", 1, 0) |
| 1867 |
|
| 1868 |
sl.Remove(sub) |
| 1869 |
require_NumInterest(t, "foo", 0, 0) |
| 1870 |
require_NumInterest(t, "foo.bar", 0, 0) |
| 1871 |
require_NumInterest(t, "foo.bar.baz", 0, 0) |
| 1872 |
|
| 1873 |
sub = newSub("*.>") |
| 1874 |
sl.Insert(sub) |
| 1875 |
require_NumInterest(t, "foo", 0, 0) |
| 1876 |
require_NumInterest(t, "foo.bar", 1, 0) |
| 1877 |
require_NumInterest(t, "foo.bar.baz", 1, 0) |
| 1878 |
sl.Remove(sub) |
| 1879 |
|
| 1880 |
sub = newSub("*.bar") |
| 1881 |
sl.Insert(sub) |
| 1882 |
require_NumInterest(t, "foo", 0, 0) |
| 1883 |
require_NumInterest(t, "foo.bar", 1, 0) |
| 1884 |
require_NumInterest(t, "foo.bar.baz", 0, 0) |
| 1885 |
sl.Remove(sub) |
| 1886 |
|
| 1887 |
sub = newSub("*") |
| 1888 |
sl.Insert(sub) |
| 1889 |
require_NumInterest(t, "foo", 1, 0) |
| 1890 |
require_NumInterest(t, "foo.bar", 0, 0) |
| 1891 |
sl.Remove(sub) |
| 1892 |
|
| 1893 |
// Try with queues now. |
| 1894 |
qsub := newQSub("foo", "bar") |
| 1895 |
sl.Insert(qsub) |
| 1896 |
require_NumInterest(t, "foo", 0, 1) |
| 1897 |
require_NumInterest(t, "foo.bar", 0, 0) |
| 1898 |
|
| 1899 |
qsub2 := newQSub("foo", "baz") |
| 1900 |
sl.Insert(qsub2) |
| 1901 |
require_NumInterest(t, "foo", 0, 2) |
| 1902 |
require_NumInterest(t, "foo.bar", 0, 0) |
| 1903 |
|
| 1904 |
// Add a second qsub to the second queue group |
| 1905 |
qsub3 := newQSub("foo", "baz") |
| 1906 |
sl.Insert(qsub3) |
| 1907 |
require_NumInterest(t, "foo", 0, 3) |
| 1908 |
require_NumInterest(t, "foo.bar", 0, 0) |
| 1909 |
|
| 1910 |
// Remove first queue |
| 1911 |
sl.Remove(qsub) |
| 1912 |
require_NumInterest(t, "foo", 0, 2) |
| 1913 |
require_NumInterest(t, "foo.bar", 0, 0) |
| 1914 |
|
| 1915 |
// Remove second |
| 1916 |
sl.Remove(qsub2) |
| 1917 |
require_NumInterest(t, "foo", 0, 1) |
| 1918 |
require_NumInterest(t, "foo.bar", 0, 0) |
| 1919 |
|
| 1920 |
// Remove last. |
| 1921 |
sl.Remove(qsub3) |
| 1922 |
require_NumInterest(t, "foo", 0, 0) |
| 1923 |
require_NumInterest(t, "foo.bar", 0, 0) |
| 1924 |
|
| 1925 |
// With wildcards now |
| 1926 |
qsub = newQSub("foo.*", "bar") |
| 1927 |
sl.Insert(qsub) |
| 1928 |
require_NumInterest(t, "foo", 0, 0) |
| 1929 |
require_NumInterest(t, "foo.bar", 0, 1) |
| 1930 |
require_NumInterest(t, "foo.bar.baz", 0, 0) |
| 1931 |
|
| 1932 |
// Add another queue to the group |
| 1933 |
qsub2 = newQSub("foo.*", "baz") |
| 1934 |
sl.Insert(qsub2) |
| 1935 |
require_NumInterest(t, "foo", 0, 0) |
| 1936 |
require_NumInterest(t, "foo.bar", 0, 2) |
| 1937 |
require_NumInterest(t, "foo.bar.baz", 0, 0) |
| 1938 |
|
| 1939 |
qsub3 = newQSub("foo.*", "baz") |
| 1940 |
sl.Insert(qsub3) |
| 1941 |
require_NumInterest(t, "foo", 0, 0) |
| 1942 |
require_NumInterest(t, "foo.bar", 0, 3) |
| 1943 |
require_NumInterest(t, "foo.bar.baz", 0, 0) |
| 1944 |
|
| 1945 |
// Remove first queue |
| 1946 |
sl.Remove(qsub) |
| 1947 |
require_NumInterest(t, "foo", 0, 0) |
| 1948 |
require_NumInterest(t, "foo.bar", 0, 2) |
| 1949 |
require_NumInterest(t, "foo.bar.baz", 0, 0) |
| 1950 |
|
| 1951 |
// Remove second |
| 1952 |
sl.Remove(qsub2) |
| 1953 |
require_NumInterest(t, "foo", 0, 0) |
| 1954 |
require_NumInterest(t, "foo.bar", 0, 1) |
| 1955 |
require_NumInterest(t, "foo.bar.baz", 0, 0) |
| 1956 |
|
| 1957 |
// Remove last |
| 1958 |
sl.Remove(qsub3) |
| 1959 |
require_NumInterest(t, "foo", 0, 0) |
| 1960 |
require_NumInterest(t, "foo.bar", 0, 0) |
| 1961 |
require_NumInterest(t, "foo.bar.baz", 0, 0) |
| 1962 |
|
| 1963 |
// With > wildcard |
| 1964 |
qsub = newQSub("foo.>", "bar") |
| 1965 |
sl.Insert(qsub) |
| 1966 |
require_NumInterest(t, "foo", 0, 0) |
| 1967 |
require_NumInterest(t, "foo.bar", 0, 1) |
| 1968 |
require_NumInterest(t, "foo.bar.baz", 0, 1) |
| 1969 |
|
| 1970 |
// Add another queue to the group |
| 1971 |
qsub2 = newQSub("foo.>", "baz") |
| 1972 |
sl.Insert(qsub2) |
| 1973 |
require_NumInterest(t, "foo", 0, 0) |
| 1974 |
require_NumInterest(t, "foo.bar", 0, 2) |
| 1975 |
require_NumInterest(t, "foo.bar.baz", 0, 2) |
| 1976 |
|
| 1977 |
// Add another queue to second group. |
| 1978 |
qsub3 = newQSub("foo.>", "baz") |
| 1979 |
sl.Insert(qsub3) |
| 1980 |
require_NumInterest(t, "foo", 0, 0) |
| 1981 |
require_NumInterest(t, "foo.bar", 0, 3) |
| 1982 |
require_NumInterest(t, "foo.bar.baz", 0, 3) |
| 1983 |
|
| 1984 |
// Remove first queue |
| 1985 |
sl.Remove(qsub) |
| 1986 |
require_NumInterest(t, "foo", 0, 0) |
| 1987 |
require_NumInterest(t, "foo.bar", 0, 2) |
| 1988 |
require_NumInterest(t, "foo.bar.baz", 0, 2) |
| 1989 |
|
| 1990 |
// Remove second |
| 1991 |
sl.Remove(qsub2) |
| 1992 |
require_NumInterest(t, "foo", 0, 0) |
| 1993 |
require_NumInterest(t, "foo.bar", 0, 1) |
| 1994 |
require_NumInterest(t, "foo.bar.baz", 0, 1) |
| 1995 |
|
| 1996 |
// Remove last |
| 1997 |
sl.Remove(qsub3) |
| 1998 |
require_NumInterest(t, "foo", 0, 0) |
| 1999 |
require_NumInterest(t, "foo.bar", 0, 0) |
| 2000 |
require_NumInterest(t, "foo.bar.baz", 0, 0) |
| 2001 |
|
| 2002 |
qsub = newQSub("*.>", "bar") |
| 2003 |
sl.Insert(qsub) |
| 2004 |
require_NumInterest(t, "foo", 0, 0) |
| 2005 |
require_NumInterest(t, "foo.bar", 0, 1) |
| 2006 |
require_NumInterest(t, "foo.bar.baz", 0, 1) |
| 2007 |
sl.Remove(qsub) |
| 2008 |
|
| 2009 |
qsub = newQSub("*.bar", "bar") |
| 2010 |
sl.Insert(qsub) |
| 2011 |
require_NumInterest(t, "foo", 0, 0) |
| 2012 |
require_NumInterest(t, "foo.bar", 0, 1) |
| 2013 |
require_NumInterest(t, "foo.bar.baz", 0, 0) |
| 2014 |
sl.Remove(qsub) |
| 2015 |
|
| 2016 |
qsub = newQSub("*", "bar") |
| 2017 |
sl.Insert(qsub) |
| 2018 |
require_NumInterest(t, "foo", 0, 1) |
| 2019 |
require_NumInterest(t, "foo.bar", 0, 0) |
| 2020 |
sl.Remove(qsub) |
| 2021 |
} |
| 2022 |
|
| 2023 |
func subsInit(pre string, toks []string) { |
| 2024 |
var sub string |
| 2025 |
for _, t := range toks { |
| 2026 |
if len(pre) > 0 { |
| 2027 |
sub = pre + tsep + t |
| 2028 |
} else { |
| 2029 |
sub = t |
| 2030 |
} |
| 2031 |
benchSublistSubs = append(benchSublistSubs, newSub(sub)) |
| 2032 |
if len(strings.Split(sub, tsep)) < 5 { |
| 2033 |
subsInit(sub, toks) |
| 2034 |
} |
| 2035 |
} |
| 2036 |
} |
| 2037 |
|
| 2038 |
func addWildcards() { |
| 2039 |
benchSublistSl.Insert(newSub("cloud.>")) |
| 2040 |
benchSublistSl.Insert(newSub("cloud.nats.component.>")) |
| 2041 |
benchSublistSl.Insert(newSub("cloud.*.*.nkeys.*")) |
| 2042 |
} |
| 2043 |
|
| 2044 |
// -- Benchmarks Setup End -- |
| 2045 |
|
| 2046 |
func Benchmark______________________SublistInsert(b *testing.B) { |
| 2047 |
s := NewSublistWithCache() |
| 2048 |
for i, l := 0, len(benchSublistSubs); i < b.N; i++ { |
| 2049 |
index := i % l |
| 2050 |
s.Insert(benchSublistSubs[index]) |
| 2051 |
} |
| 2052 |
} |
| 2053 |
|
| 2054 |
func Benchmark_______________SublistInsertNoCache(b *testing.B) { |
| 2055 |
s := NewSublistNoCache() |
| 2056 |
for i, l := 0, len(benchSublistSubs); i < b.N; i++ { |
| 2057 |
index := i % l |
| 2058 |
s.Insert(benchSublistSubs[index]) |
| 2059 |
} |
| 2060 |
} |
| 2061 |
|
| 2062 |
func benchSublistTokens(b *testing.B, tokens string) { |
| 2063 |
for i := 0; i < b.N; i++ { |
| 2064 |
benchSublistSl.Match(tokens) |
| 2065 |
} |
| 2066 |
} |
| 2067 |
|
| 2068 |
func Benchmark____________SublistMatchSingleToken(b *testing.B) { |
| 2069 |
benchSublistTokens(b, "synadia") |
| 2070 |
} |
| 2071 |
|
| 2072 |
func Benchmark______________SublistMatchTwoTokens(b *testing.B) { |
| 2073 |
benchSublistTokens(b, "synadia.nats") |
| 2074 |
} |
| 2075 |
|
| 2076 |
func Benchmark____________SublistMatchThreeTokens(b *testing.B) { |
| 2077 |
benchSublistTokens(b, "synadia.nats.jetstream") |
| 2078 |
} |
| 2079 |
|
| 2080 |
func Benchmark_____________SublistMatchFourTokens(b *testing.B) { |
| 2081 |
benchSublistTokens(b, "synadia.nats.jetstream.nkeys") |
| 2082 |
} |
| 2083 |
|
| 2084 |
func Benchmark_SublistMatchFourTokensSingleResult(b *testing.B) { |
| 2085 |
benchSublistTokens(b, "synadia.nats.jetstream.nkeys") |
| 2086 |
} |
| 2087 |
|
| 2088 |
func Benchmark_SublistMatchFourTokensMultiResults(b *testing.B) { |
| 2089 |
benchSublistTokens(b, "cloud.nats.component.router") |
| 2090 |
} |
| 2091 |
|
| 2092 |
func Benchmark_______SublistMissOnLastTokenOfFive(b *testing.B) { |
| 2093 |
benchSublistTokens(b, "synadia.nats.jetstream.nkeys.ZZZZ") |
| 2094 |
} |
| 2095 |
|
| 2096 |
func multiRead(b *testing.B, num int) { |
| 2097 |
var swg, fwg sync.WaitGroup |
| 2098 |
swg.Add(num) |
| 2099 |
fwg.Add(num) |
| 2100 |
s := "synadia.nats.jetstream.nkeys" |
| 2101 |
for i := 0; i < num; i++ { |
| 2102 |
go func() { |
| 2103 |
swg.Done() |
| 2104 |
swg.Wait() |
| 2105 |
n := b.N / num |
| 2106 |
for i := 0; i < n; i++ { |
| 2107 |
benchSublistSl.Match(s) |
| 2108 |
} |
| 2109 |
fwg.Done() |
| 2110 |
}() |
| 2111 |
} |
| 2112 |
swg.Wait() |
| 2113 |
b.ResetTimer() |
| 2114 |
fwg.Wait() |
| 2115 |
} |
| 2116 |
|
| 2117 |
func Benchmark____________Sublist10XMultipleReads(b *testing.B) { |
| 2118 |
multiRead(b, 10) |
| 2119 |
} |
| 2120 |
|
| 2121 |
func Benchmark___________Sublist100XMultipleReads(b *testing.B) { |
| 2122 |
multiRead(b, 100) |
| 2123 |
} |
| 2124 |
|
| 2125 |
func Benchmark__________Sublist1000XMultipleReads(b *testing.B) { |
| 2126 |
multiRead(b, 1000) |
| 2127 |
} |
| 2128 |
|
| 2129 |
func Benchmark________________SublistMatchLiteral(b *testing.B) { |
| 2130 |
cachedSubj := "foo.foo.foo.foo.foo.foo.foo.foo.foo.foo" |
| 2131 |
subjects := []string{ |
| 2132 |
"foo.foo.foo.foo.foo.foo.foo.foo.foo.foo", |
| 2133 |
"foo.foo.foo.foo.foo.foo.foo.foo.foo.>", |
| 2134 |
"foo.foo.foo.foo.foo.foo.foo.foo.>", |
| 2135 |
"foo.foo.foo.foo.foo.foo.foo.>", |
| 2136 |
"foo.foo.foo.foo.foo.foo.>", |
| 2137 |
"foo.foo.foo.foo.foo.>", |
| 2138 |
"foo.foo.foo.foo.>", |
| 2139 |
"foo.foo.foo.>", |
| 2140 |
"foo.foo.>", |
| 2141 |
"foo.>", |
| 2142 |
">", |
| 2143 |
"foo.foo.foo.foo.foo.foo.foo.foo.foo.*", |
| 2144 |
"foo.foo.foo.foo.foo.foo.foo.foo.*.*", |
| 2145 |
"foo.foo.foo.foo.foo.foo.foo.*.*.*", |
| 2146 |
"foo.foo.foo.foo.foo.foo.*.*.*.*", |
| 2147 |
"foo.foo.foo.foo.foo.*.*.*.*.*", |
| 2148 |
"foo.foo.foo.foo.*.*.*.*.*.*", |
| 2149 |
"foo.foo.foo.*.*.*.*.*.*.*", |
| 2150 |
"foo.foo.*.*.*.*.*.*.*.*", |
| 2151 |
"foo.*.*.*.*.*.*.*.*.*", |
| 2152 |
"*.*.*.*.*.*.*.*.*.*", |
| 2153 |
} |
| 2154 |
b.ResetTimer() |
| 2155 |
for i := 0; i < b.N; i++ { |
| 2156 |
for _, subject := range subjects { |
| 2157 |
if !matchLiteral(cachedSubj, subject) { |
| 2158 |
b.Fatalf("Subject %q no match with %q", cachedSubj, subject) |
| 2159 |
} |
| 2160 |
} |
| 2161 |
} |
| 2162 |
} |
| 2163 |
|
| 2164 |
func Benchmark_____SublistMatch10kSubsWithNoCache(b *testing.B) { |
| 2165 |
var nsubs = 512 |
| 2166 |
s := NewSublistNoCache() |
| 2167 |
subject := "foo" |
| 2168 |
for i := 0; i < nsubs; i++ { |
| 2169 |
s.Insert(newSub(subject)) |
| 2170 |
} |
| 2171 |
b.ResetTimer() |
| 2172 |
for i := 0; i < b.N; i++ { |
| 2173 |
r := s.Match(subject) |
| 2174 |
if len(r.psubs) != nsubs { |
| 2175 |
b.Fatalf("Results len is %d, should be %d", len(r.psubs), nsubs) |
| 2176 |
} |
| 2177 |
} |
| 2178 |
} |
| 2179 |
|
| 2180 |
func removeTest(b *testing.B, singleSubject, doBatch bool, qgroup string) { |
| 2181 |
s := NewSublistWithCache() |
| 2182 |
subject := "foo" |
| 2183 |
|
| 2184 |
subs := make([]*subscription, 0, b.N) |
| 2185 |
for i := 0; i < b.N; i++ { |
| 2186 |
var sub *subscription |
| 2187 |
if singleSubject { |
| 2188 |
sub = newQSub(subject, qgroup) |
| 2189 |
} else { |
| 2190 |
sub = newQSub(fmt.Sprintf("%s.%d\n", subject, i), qgroup) |
| 2191 |
} |
| 2192 |
s.Insert(sub) |
| 2193 |
subs = append(subs, sub) |
| 2194 |
} |
| 2195 |
|
| 2196 |
// Actual test on Remove |
| 2197 |
b.ResetTimer() |
| 2198 |
if doBatch { |
| 2199 |
s.RemoveBatch(subs) |
| 2200 |
} else { |
| 2201 |
for _, sub := range subs { |
| 2202 |
s.Remove(sub) |
| 2203 |
} |
| 2204 |
} |
| 2205 |
} |
| 2206 |
|
| 2207 |
func Benchmark__________SublistRemove1TokenSingle(b *testing.B) { |
| 2208 |
removeTest(b, true, false, "") |
| 2209 |
} |
| 2210 |
|
| 2211 |
func Benchmark___________SublistRemove1TokenBatch(b *testing.B) { |
| 2212 |
removeTest(b, true, true, "") |
| 2213 |
} |
| 2214 |
|
| 2215 |
func Benchmark_________SublistRemove2TokensSingle(b *testing.B) { |
| 2216 |
removeTest(b, false, false, "") |
| 2217 |
} |
| 2218 |
|
| 2219 |
func Benchmark__________SublistRemove2TokensBatch(b *testing.B) { |
| 2220 |
removeTest(b, false, true, "") |
| 2221 |
} |
| 2222 |
|
| 2223 |
func Benchmark________SublistRemove1TokenQGSingle(b *testing.B) { |
| 2224 |
removeTest(b, true, false, "bar") |
| 2225 |
} |
| 2226 |
|
| 2227 |
func Benchmark_________SublistRemove1TokenQGBatch(b *testing.B) { |
| 2228 |
removeTest(b, true, true, "bar") |
| 2229 |
} |
| 2230 |
|
| 2231 |
func removeMultiTest(b *testing.B, singleSubject, doBatch bool) { |
| 2232 |
s := NewSublistWithCache() |
| 2233 |
subject := "foo" |
| 2234 |
var swg, fwg sync.WaitGroup |
| 2235 |
swg.Add(b.N) |
| 2236 |
fwg.Add(b.N) |
| 2237 |
|
| 2238 |
// We will have b.N go routines each with 1k subscriptions. |
| 2239 |
sc := 1000 |
| 2240 |
|
| 2241 |
for i := 0; i < b.N; i++ { |
| 2242 |
go func() { |
| 2243 |
subs := make([]*subscription, 0, sc) |
| 2244 |
for n := 0; n < sc; n++ { |
| 2245 |
var sub *subscription |
| 2246 |
if singleSubject { |
| 2247 |
sub = newSub(subject) |
| 2248 |
} else { |
| 2249 |
sub = newSub(fmt.Sprintf("%s.%d\n", subject, n)) |
| 2250 |
} |
| 2251 |
s.Insert(sub) |
| 2252 |
subs = append(subs, sub) |
| 2253 |
} |
| 2254 |
// Wait to start test |
| 2255 |
swg.Done() |
| 2256 |
swg.Wait() |
| 2257 |
// Actual test on Remove |
| 2258 |
if doBatch { |
| 2259 |
s.RemoveBatch(subs) |
| 2260 |
} else { |
| 2261 |
for _, sub := range subs { |
| 2262 |
s.Remove(sub) |
| 2263 |
} |
| 2264 |
} |
| 2265 |
fwg.Done() |
| 2266 |
}() |
| 2267 |
} |
| 2268 |
swg.Wait() |
| 2269 |
b.ResetTimer() |
| 2270 |
fwg.Wait() |
| 2271 |
} |
| 2272 |
|
| 2273 |
// Check contention rates for remove from multiple Go routines. |
| 2274 |
// Reason for BatchRemove. |
| 2275 |
func Benchmark_________SublistRemove1kSingleMulti(b *testing.B) { |
| 2276 |
removeMultiTest(b, true, false) |
| 2277 |
} |
| 2278 |
|
| 2279 |
// Batch version |
| 2280 |
func Benchmark__________SublistRemove1kBatchMulti(b *testing.B) { |
| 2281 |
removeMultiTest(b, true, true) |
| 2282 |
} |
| 2283 |
|
| 2284 |
func Benchmark__SublistRemove1kSingle2TokensMulti(b *testing.B) { |
| 2285 |
removeMultiTest(b, false, false) |
| 2286 |
} |
| 2287 |
|
| 2288 |
// Batch version |
| 2289 |
func Benchmark___SublistRemove1kBatch2TokensMulti(b *testing.B) { |
| 2290 |
removeMultiTest(b, false, true) |
| 2291 |
} |
| 2292 |
|
| 2293 |
// Cache contention tests |
| 2294 |
func cacheContentionTest(b *testing.B, numMatchers, numAdders, numRemovers int) { |
| 2295 |
var swg, fwg, mwg sync.WaitGroup |
| 2296 |
total := numMatchers + numAdders + numRemovers |
| 2297 |
swg.Add(total) |
| 2298 |
fwg.Add(total) |
| 2299 |
mwg.Add(numMatchers) |
| 2300 |
|
| 2301 |
mu := sync.RWMutex{} |
| 2302 |
subs := make([]*subscription, 0, 8192) |
| 2303 |
|
| 2304 |
quitCh := make(chan struct{}) |
| 2305 |
|
| 2306 |
// Set up a new sublist. subjects will be foo.bar.baz.N |
| 2307 |
s := NewSublistWithCache() |
| 2308 |
mu.Lock() |
| 2309 |
for i := 0; i < 10000; i++ { |
| 2310 |
sub := newSub(fmt.Sprintf("foo.bar.baz.%d", i)) |
| 2311 |
s.Insert(sub) |
| 2312 |
subs = append(subs, sub) |
| 2313 |
} |
| 2314 |
mu.Unlock() |
| 2315 |
|
| 2316 |
// Now warm up the cache |
| 2317 |
for i := 0; i < slCacheMax; i++ { |
| 2318 |
s.Match(fmt.Sprintf("foo.bar.baz.%d", i)) |
| 2319 |
} |
| 2320 |
|
| 2321 |
// Setup go routines. |
| 2322 |
|
| 2323 |
// Adders |
| 2324 |
for i := 0; i < numAdders; i++ { |
| 2325 |
go func() { |
| 2326 |
swg.Done() |
| 2327 |
swg.Wait() |
| 2328 |
for { |
| 2329 |
select { |
| 2330 |
case <-quitCh: |
| 2331 |
fwg.Done() |
| 2332 |
return |
| 2333 |
default: |
| 2334 |
mu.Lock() |
| 2335 |
next := len(subs) |
| 2336 |
subj := "foo.bar.baz." + strconv.FormatInt(int64(next), 10) |
| 2337 |
sub := newSub(subj) |
| 2338 |
subs = append(subs, sub) |
| 2339 |
mu.Unlock() |
| 2340 |
s.Insert(sub) |
| 2341 |
} |
| 2342 |
} |
| 2343 |
}() |
| 2344 |
} |
| 2345 |
|
| 2346 |
// Removers |
| 2347 |
for i := 0; i < numRemovers; i++ { |
| 2348 |
go func() { |
| 2349 |
prand := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 2350 |
swg.Done() |
| 2351 |
swg.Wait() |
| 2352 |
for { |
| 2353 |
select { |
| 2354 |
case <-quitCh: |
| 2355 |
fwg.Done() |
| 2356 |
return |
| 2357 |
default: |
| 2358 |
mu.RLock() |
| 2359 |
lh := len(subs) - 1 |
| 2360 |
index := prand.Intn(lh) |
| 2361 |
sub := subs[index] |
| 2362 |
mu.RUnlock() |
| 2363 |
s.Remove(sub) |
| 2364 |
} |
| 2365 |
} |
| 2366 |
}() |
| 2367 |
} |
| 2368 |
|
| 2369 |
// Matchers |
| 2370 |
for i := 0; i < numMatchers; i++ { |
| 2371 |
go func() { |
| 2372 |
id := nuid.New() |
| 2373 |
swg.Done() |
| 2374 |
swg.Wait() |
| 2375 |
|
| 2376 |
// We will miss on purpose to blow the cache. |
| 2377 |
n := b.N / numMatchers |
| 2378 |
for i := 0; i < n; i++ { |
| 2379 |
subj := "foo.bar.baz." + id.Next() |
| 2380 |
s.Match(subj) |
| 2381 |
} |
| 2382 |
mwg.Done() |
| 2383 |
fwg.Done() |
| 2384 |
}() |
| 2385 |
} |
| 2386 |
|
| 2387 |
swg.Wait() |
| 2388 |
b.ResetTimer() |
| 2389 |
mwg.Wait() |
| 2390 |
b.StopTimer() |
| 2391 |
close(quitCh) |
| 2392 |
fwg.Wait() |
| 2393 |
} |
| 2394 |
|
| 2395 |
func Benchmark____SublistCacheContention10M10A10R(b *testing.B) { |
| 2396 |
cacheContentionTest(b, 10, 10, 10) |
| 2397 |
} |
| 2398 |
|
| 2399 |
func Benchmark_SublistCacheContention100M100A100R(b *testing.B) { |
| 2400 |
cacheContentionTest(b, 100, 100, 100) |
| 2401 |
} |
| 2402 |
|
| 2403 |
func Benchmark____SublistCacheContention1kM1kA1kR(b *testing.B) { |
| 2404 |
cacheContentionTest(b, 1024, 1024, 1024) |
| 2405 |
} |
| 2406 |
|
| 2407 |
func Benchmark_SublistCacheContention10kM10kA10kR(b *testing.B) { |
| 2408 |
cacheContentionTest(b, 10*1024, 10*1024, 10*1024) |
| 2409 |
} |
| 2410 |
|
| 2411 |
func Benchmark______________IsValidLiteralSubject(b *testing.B) { |
| 2412 |
for i := 0; i < b.N; i++ { |
| 2413 |
IsValidLiteralSubject("foo.bar.baz.22") |
| 2414 |
} |
| 2415 |
} |
| 2416 |
|
| 2417 |
func Benchmark___________________subjectIsLiteral(b *testing.B) { |
| 2418 |
for i := 0; i < b.N; i++ { |
| 2419 |
subjectIsLiteral("foo.bar.baz.22") |
| 2420 |
} |
| 2421 |
} |
| 2422 |
|
| 2423 |
// Test to determine the fastest way to match a filter |
| 2424 |
// with wildcards against a literal subject. |
| 2425 |
func Benchmark_SubjectFilterMatchers(b *testing.B) { |
| 2426 |
cases := []struct { |
| 2427 |
name string |
| 2428 |
subject string |
| 2429 |
filter string |
| 2430 |
want bool |
| 2431 |
}{ |
| 2432 |
{ |
| 2433 |
name: "short_single_wc_match", |
| 2434 |
subject: "foo.baz.12345", |
| 2435 |
filter: "foo.baz.*", |
| 2436 |
want: true, |
| 2437 |
}, |
| 2438 |
{ |
| 2439 |
name: "short_mixed_wc_match", |
| 2440 |
subject: "foo.baz.12345", |
| 2441 |
filter: "foo.*.>", |
| 2442 |
want: true, |
| 2443 |
}, |
| 2444 |
{ |
| 2445 |
name: "long_single_wc_match", |
| 2446 |
subject: "foo.alpha.beta.gamma.delta.epsilon.zeta.12345", |
| 2447 |
filter: "foo.alpha.beta.gamma.delta.epsilon.zeta.*", |
| 2448 |
want: true, |
| 2449 |
}, |
| 2450 |
{ |
| 2451 |
name: "long_many_wc_late_mismatch", |
| 2452 |
subject: "foo.alpha.beta.gamma.delta.epsilon.zeta.12345", |
| 2453 |
filter: "foo.*.*.*.*.*.*.99999", |
| 2454 |
want: false, |
| 2455 |
}, |
| 2456 |
} |
| 2457 |
|
| 2458 |
for _, tc := range cases { |
| 2459 |
b.Run(tc.name, func(b *testing.B) { |
| 2460 |
b.Run("subjectIsSubsetMatch", func(b *testing.B) { |
| 2461 |
var matched bool |
| 2462 |
for i := 0; i < b.N; i++ { |
| 2463 |
matched = subjectIsSubsetMatch(tc.subject, tc.filter) |
| 2464 |
} |
| 2465 |
if matched != tc.want { |
| 2466 |
b.Fatalf("unexpected result: got %v, want %v", matched, tc.want) |
| 2467 |
} |
| 2468 |
}) |
| 2469 |
b.Run("matchLiteral", func(b *testing.B) { |
| 2470 |
var matched bool |
| 2471 |
for i := 0; i < b.N; i++ { |
| 2472 |
matched = matchLiteral(tc.subject, tc.filter) |
| 2473 |
} |
| 2474 |
if matched != tc.want { |
| 2475 |
b.Fatalf("unexpected result: got %v, want %v", matched, tc.want) |
| 2476 |
} |
| 2477 |
}) |
| 2478 |
}) |
| 2479 |
} |
| 2480 |
} |