| 1 |
// Copyright 2012-2026 The NATS Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
//go:build go1.23 |
| 15 |
|
| 16 |
package test |
| 17 |
|
| 18 |
import ( |
| 19 |
"errors" |
| 20 |
"fmt" |
| 21 |
"sync" |
| 22 |
"testing" |
| 23 |
"time" |
| 24 |
|
| 25 |
"github.com/nats-io/nats.go" |
| 26 |
testservice "github.com/synadia-io/orbit.go/ntf-client" |
| 27 |
) |
| 28 |
|
| 29 |
const denyFooAuthSnippet = ` |
| 30 |
authorization: { |
| 31 |
users = [ |
| 32 |
{ |
| 33 |
user: test |
| 34 |
password: test |
| 35 |
permissions: { |
| 36 |
subscribe: { |
| 37 |
deny: "foo" |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
] |
| 42 |
}` |
| 43 |
|
| 44 |
func TestSubscribeIterator(t *testing.T) { |
| 45 |
t.Run("with timeout", func(t *testing.T) { |
| 46 |
withServerInstance(t, func(t *testing.T, _ *nats.Conn, inst *testservice.Instance) { |
| 47 |
nc, err := nats.Connect(inst.Servers[0].URL, nats.PermissionErrOnSubscribe(true)) |
| 48 |
if err != nil { |
| 49 |
t.Fatalf("Error on connect: %v", err) |
| 50 |
} |
| 51 |
defer nc.Close() |
| 52 |
|
| 53 |
sub, err := nc.SubscribeSync("foo") |
| 54 |
if err != nil { |
| 55 |
t.Fatal("Failed to subscribe: ", err) |
| 56 |
} |
| 57 |
defer sub.Unsubscribe() |
| 58 |
|
| 59 |
total := 100 |
| 60 |
for i := 0; i < total/2; i++ { |
| 61 |
if err := nc.Publish("foo", []byte("Hello")); err != nil { |
| 62 |
t.Fatalf("Error on publish: %v", err) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
errCh := make(chan error, 1) |
| 67 |
go func() { |
| 68 |
for i := 0; i < total/2; i++ { |
| 69 |
if err := nc.Publish("foo", []byte("Hello")); err != nil { |
| 70 |
errCh <- err |
| 71 |
return |
| 72 |
} |
| 73 |
time.Sleep(10 * time.Millisecond) |
| 74 |
} |
| 75 |
close(errCh) |
| 76 |
}() |
| 77 |
|
| 78 |
received := 0 |
| 79 |
for _, err := range sub.MsgsTimeout(100 * time.Millisecond) { |
| 80 |
if err != nil { |
| 81 |
if !errors.Is(err, nats.ErrTimeout) { |
| 82 |
t.Fatalf("Error on subscribe: %v", err) |
| 83 |
} |
| 84 |
break |
| 85 |
} else { |
| 86 |
received++ |
| 87 |
} |
| 88 |
} |
| 89 |
if received != total { |
| 90 |
t.Fatalf("Expected %d messages, got %d", total, received) |
| 91 |
} |
| 92 |
}) |
| 93 |
}) |
| 94 |
|
| 95 |
t.Run("timeout keeps yielding only ErrTimeout", func(t *testing.T) { |
| 96 |
withServerInstance(t, func(t *testing.T, _ *nats.Conn, inst *testservice.Instance) { |
| 97 |
nc, err := nats.Connect(inst.Servers[0].URL, nats.PermissionErrOnSubscribe(true)) |
| 98 |
if err != nil { |
| 99 |
t.Fatalf("Error on connect: %v", err) |
| 100 |
} |
| 101 |
defer nc.Close() |
| 102 |
|
| 103 |
sub, err := nc.SubscribeSync("foo") |
| 104 |
if err != nil { |
| 105 |
t.Fatal("Failed to subscribe: ", err) |
| 106 |
} |
| 107 |
defer sub.Unsubscribe() |
| 108 |
|
| 109 |
// No message is ever published, so every NextMsg call times out. |
| 110 |
// ErrTimeout is not fatal, so a consumer is allowed to keep iterating. |
| 111 |
// In that case the iterator must only ever yield (nil, ErrTimeout); it |
| 112 |
// must not yield a spurious (nil, nil) pair between timeouts. |
| 113 |
iterations := 0 |
| 114 |
for msg, err := range sub.MsgsTimeout(50 * time.Millisecond) { |
| 115 |
if err == nil { |
| 116 |
t.Fatalf("iteration %d: got msg=%v with nil error, expected ErrTimeout", iterations, msg) |
| 117 |
} |
| 118 |
if !errors.Is(err, nats.ErrTimeout) { |
| 119 |
t.Fatalf("iteration %d: unexpected error: %v", iterations, err) |
| 120 |
} |
| 121 |
iterations++ |
| 122 |
if iterations >= 3 { |
| 123 |
break |
| 124 |
} |
| 125 |
} |
| 126 |
}) |
| 127 |
}) |
| 128 |
|
| 129 |
t.Run("no timeout", func(t *testing.T) { |
| 130 |
withServerInstance(t, func(t *testing.T, _ *nats.Conn, inst *testservice.Instance) { |
| 131 |
nc, err := nats.Connect(inst.Servers[0].URL, nats.PermissionErrOnSubscribe(true)) |
| 132 |
if err != nil { |
| 133 |
t.Fatalf("Error on connect: %v", err) |
| 134 |
} |
| 135 |
defer nc.Close() |
| 136 |
|
| 137 |
sub, err := nc.SubscribeSync("foo") |
| 138 |
if err != nil { |
| 139 |
t.Fatal("Failed to subscribe: ", err) |
| 140 |
} |
| 141 |
defer sub.Unsubscribe() |
| 142 |
|
| 143 |
total := 100 |
| 144 |
for i := 0; i < total/2; i++ { |
| 145 |
if err := nc.Publish("foo", []byte("Hello")); err != nil { |
| 146 |
t.Fatalf("Error on publish: %v", err) |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
received := 0 |
| 151 |
|
| 152 |
errCh := make(chan error, 1) |
| 153 |
go func() { |
| 154 |
for i := 0; i < total/2; i++ { |
| 155 |
if err := nc.Publish("foo", []byte("Hello")); err != nil { |
| 156 |
errCh <- err |
| 157 |
return |
| 158 |
} |
| 159 |
time.Sleep(10 * time.Millisecond) |
| 160 |
} |
| 161 |
close(errCh) |
| 162 |
}() |
| 163 |
|
| 164 |
for _, err := range sub.Msgs() { |
| 165 |
if err != nil { |
| 166 |
t.Fatalf("Error getting msg: %v", err) |
| 167 |
} |
| 168 |
received++ |
| 169 |
if received >= total { |
| 170 |
break |
| 171 |
} |
| 172 |
} |
| 173 |
if err = <-errCh; err != nil { |
| 174 |
t.Fatalf("Error on publish: %v", err) |
| 175 |
} |
| 176 |
if _, err = sub.NextMsg(100 * time.Millisecond); !errors.Is(err, nats.ErrTimeout) { |
| 177 |
t.Fatalf("Expected timeout waiting for next message, got %v", err) |
| 178 |
} |
| 179 |
}) |
| 180 |
}) |
| 181 |
|
| 182 |
t.Run("permissions violation", func(t *testing.T) { |
| 183 |
c := newTester(t) |
| 184 |
inst := c.CreateServer(t, false, testservice.WithAuthorization(denyFooAuthSnippet)) |
| 185 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 186 |
|
| 187 |
nc, err := nats.Connect(inst.Servers[0].URL, |
| 188 |
nats.UserInfo("test", "test"), |
| 189 |
nats.PermissionErrOnSubscribe(true)) |
| 190 |
if err != nil { |
| 191 |
t.Fatalf("Error on connect: %v", err) |
| 192 |
} |
| 193 |
defer nc.Close() |
| 194 |
|
| 195 |
sub, err := nc.SubscribeSync("foo") |
| 196 |
if err != nil { |
| 197 |
t.Fatalf("Error on subscribe: %v", err) |
| 198 |
} |
| 199 |
defer sub.Unsubscribe() |
| 200 |
|
| 201 |
errs := make(chan error) |
| 202 |
go func() { |
| 203 |
var err error |
| 204 |
for _, err = range sub.Msgs() { |
| 205 |
break |
| 206 |
} |
| 207 |
errs <- err |
| 208 |
}() |
| 209 |
|
| 210 |
select { |
| 211 |
case e := <-errs: |
| 212 |
if !errors.Is(e, nats.ErrPermissionViolation) { |
| 213 |
t.Fatalf("Expected permissions error, got %v", e) |
| 214 |
} |
| 215 |
case <-time.After(2 * time.Second): |
| 216 |
t.Fatalf("Did not get the permission error") |
| 217 |
} |
| 218 |
|
| 219 |
if _, err = sub.NextMsg(100 * time.Millisecond); !errors.Is(err, nats.ErrPermissionViolation) { |
| 220 |
t.Fatalf("Expected permissions violation error, got %v", err) |
| 221 |
} |
| 222 |
}) |
| 223 |
|
| 224 |
t.Run("attempt iterator on async sub", func(t *testing.T) { |
| 225 |
withServerInstance(t, func(t *testing.T, _ *nats.Conn, inst *testservice.Instance) { |
| 226 |
nc, err := nats.Connect(inst.Servers[0].URL, nats.PermissionErrOnSubscribe(true)) |
| 227 |
if err != nil { |
| 228 |
t.Fatalf("Error on connect: %v", err) |
| 229 |
} |
| 230 |
defer nc.Close() |
| 231 |
|
| 232 |
sub, err := nc.Subscribe("foo", func(msg *nats.Msg) {}) |
| 233 |
if err != nil { |
| 234 |
t.Fatal("Failed to subscribe: ", err) |
| 235 |
} |
| 236 |
defer sub.Unsubscribe() |
| 237 |
|
| 238 |
for _, err := range sub.MsgsTimeout(100 * time.Millisecond) { |
| 239 |
if !errors.Is(err, nats.ErrSyncSubRequired) { |
| 240 |
t.Fatalf("Error on subscribe: %v", err) |
| 241 |
} |
| 242 |
} |
| 243 |
for _, err := range sub.Msgs() { |
| 244 |
if !errors.Is(err, nats.ErrSyncSubRequired) { |
| 245 |
t.Fatalf("Error on subscribe: %v", err) |
| 246 |
} |
| 247 |
} |
| 248 |
}) |
| 249 |
}) |
| 250 |
} |
| 251 |
|
| 252 |
func TestQueueSubscribeIterator(t *testing.T) { |
| 253 |
t.Run("basic", func(t *testing.T) { |
| 254 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 255 |
subs := make([]*nats.Subscription, 4) |
| 256 |
for i := 0; i < 4; i++ { |
| 257 |
sub, err := nc.QueueSubscribeSync("foo", "q") |
| 258 |
if err != nil { |
| 259 |
t.Fatal("Failed to subscribe: ", err) |
| 260 |
} |
| 261 |
subs[i] = sub |
| 262 |
defer sub.Unsubscribe() |
| 263 |
} |
| 264 |
|
| 265 |
total := 100 |
| 266 |
for i := 0; i < total; i++ { |
| 267 |
if err := nc.Publish("foo", []byte(fmt.Sprintf("%d", i))); err != nil { |
| 268 |
t.Fatalf("Error on publish: %v", err) |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
wg := sync.WaitGroup{} |
| 273 |
wg.Add(100) |
| 274 |
startWg := sync.WaitGroup{} |
| 275 |
startWg.Add(4) |
| 276 |
|
| 277 |
for i := range subs { |
| 278 |
go func(i int) { |
| 279 |
startWg.Done() |
| 280 |
for _, err := range subs[i].MsgsTimeout(100 * time.Millisecond) { |
| 281 |
if err != nil { |
| 282 |
break |
| 283 |
} |
| 284 |
wg.Done() |
| 285 |
} |
| 286 |
}(i) |
| 287 |
} |
| 288 |
|
| 289 |
startWg.Wait() |
| 290 |
|
| 291 |
wg.Wait() |
| 292 |
|
| 293 |
for _, sub := range subs { |
| 294 |
if _, err := sub.NextMsg(100 * time.Millisecond); !errors.Is(err, nats.ErrTimeout) { |
| 295 |
t.Fatalf("Expected timeout waiting for next message, got %v", err) |
| 296 |
} |
| 297 |
} |
| 298 |
}) |
| 299 |
}) |
| 300 |
|
| 301 |
t.Run("permissions violation", func(t *testing.T) { |
| 302 |
c := newTester(t) |
| 303 |
inst := c.CreateServer(t, false, testservice.WithAuthorization(denyFooAuthSnippet)) |
| 304 |
t.Cleanup(func() { inst.Destroy(t) }) |
| 305 |
|
| 306 |
nc, err := nats.Connect(inst.Servers[0].URL, |
| 307 |
nats.UserInfo("test", "test"), |
| 308 |
nats.PermissionErrOnSubscribe(true)) |
| 309 |
if err != nil { |
| 310 |
t.Fatalf("Error on connect: %v", err) |
| 311 |
} |
| 312 |
defer nc.Close() |
| 313 |
|
| 314 |
sub, err := nc.QueueSubscribeSync("foo", "q") |
| 315 |
if err != nil { |
| 316 |
t.Fatalf("Error on subscribe: %v", err) |
| 317 |
} |
| 318 |
defer sub.Unsubscribe() |
| 319 |
|
| 320 |
errs := make(chan error) |
| 321 |
go func() { |
| 322 |
var err error |
| 323 |
for _, err = range sub.MsgsTimeout(2 * time.Second) { |
| 324 |
break |
| 325 |
} |
| 326 |
errs <- err |
| 327 |
}() |
| 328 |
|
| 329 |
select { |
| 330 |
case e := <-errs: |
| 331 |
if !errors.Is(e, nats.ErrPermissionViolation) { |
| 332 |
t.Fatalf("Expected permissions error, got %v", e) |
| 333 |
} |
| 334 |
case <-time.After(2 * time.Second): |
| 335 |
t.Fatalf("Did not get the permission error") |
| 336 |
} |
| 337 |
}) |
| 338 |
|
| 339 |
t.Run("timeout does not yield phantom nil message", func(t *testing.T) { |
| 340 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 341 |
sub, err := nc.SubscribeSync("foo") |
| 342 |
if err != nil { |
| 343 |
t.Fatal("Failed to subscribe: ", err) |
| 344 |
} |
| 345 |
defer sub.Unsubscribe() |
| 346 |
|
| 347 |
// No messages are published, so every NextMsg should hit ErrTimeout. |
| 348 |
// Read three iterations and confirm each one yielded the timeout |
| 349 |
// error and not a phantom (nil, nil) following it. |
| 350 |
var got []error |
| 351 |
for _, err := range sub.MsgsTimeout(20 * time.Millisecond) { |
| 352 |
got = append(got, err) |
| 353 |
if len(got) == 3 { |
| 354 |
break |
| 355 |
} |
| 356 |
} |
| 357 |
for i, e := range got { |
| 358 |
if !errors.Is(e, nats.ErrTimeout) { |
| 359 |
t.Fatalf("yield %d: expected ErrTimeout, got %v", i, e) |
| 360 |
} |
| 361 |
} |
| 362 |
}) |
| 363 |
}) |
| 364 |
} |