| 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 |
package test |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
"strings" |
| 19 |
"sync" |
| 20 |
"testing" |
| 21 |
"time" |
| 22 |
|
| 23 |
"github.com/nats-io/nats.go" |
| 24 |
testservice "github.com/synadia-io/orbit.go/ntf-client" |
| 25 |
) |
| 26 |
|
| 27 |
func TestContextRequestWithNilConnection(t *testing.T) { |
| 28 |
var nc *nats.Conn |
| 29 |
|
| 30 |
ctx, cancelCB := context.WithTimeout(context.Background(), 100*time.Millisecond) |
| 31 |
defer cancelCB() // should always be called, not discarded, to prevent context leak |
| 32 |
|
| 33 |
_, err := nc.RequestWithContext(ctx, "fast", []byte("")) |
| 34 |
if err == nil { |
| 35 |
t.Fatal("Expected request with context and nil connection to fail") |
| 36 |
} |
| 37 |
if err != nats.ErrInvalidConnection { |
| 38 |
t.Fatalf("Expected nats.ErrInvalidConnection, got %v\n", err) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
func testContextRequestWithTimeout(t *testing.T, nc *nats.Conn) { |
| 43 |
nc.Subscribe("slow", func(m *nats.Msg) { |
| 44 |
// Simulates latency into the client so that timeout is hit. |
| 45 |
time.Sleep(200 * time.Millisecond) |
| 46 |
nc.Publish(m.Reply, []byte("NG")) |
| 47 |
}) |
| 48 |
nc.Subscribe("fast", func(m *nats.Msg) { |
| 49 |
nc.Publish(m.Reply, []byte("OK")) |
| 50 |
}) |
| 51 |
nc.Subscribe("hdrs", func(m *nats.Msg) { |
| 52 |
if m.Header.Get("Hdr-Test") != "1" { |
| 53 |
m.Respond([]byte("-ERR")) |
| 54 |
} |
| 55 |
|
| 56 |
r := nats.NewMsg(m.Reply) |
| 57 |
r.Header = m.Header |
| 58 |
r.Data = []byte("+OK") |
| 59 |
m.RespondMsg(r) |
| 60 |
}) |
| 61 |
|
| 62 |
ctx, cancelCB := context.WithTimeout(context.Background(), 100*time.Millisecond) |
| 63 |
defer cancelCB() // should always be called, not discarded, to prevent context leak |
| 64 |
|
| 65 |
// Fast request should not fail at this point. |
| 66 |
resp, err := nc.RequestWithContext(ctx, "fast", []byte("")) |
| 67 |
if err != nil { |
| 68 |
t.Fatalf("Expected request with context to not fail on fast response: %s", err) |
| 69 |
} |
| 70 |
got := string(resp.Data) |
| 71 |
expected := "OK" |
| 72 |
if got != expected { |
| 73 |
t.Errorf("Expected to receive %s, got: %s", expected, got) |
| 74 |
} |
| 75 |
|
| 76 |
// Slow request hits timeout so expected to fail. |
| 77 |
_, err = nc.RequestWithContext(ctx, "slow", []byte("world")) |
| 78 |
if err == nil { |
| 79 |
t.Fatal("Expected request with timeout context to fail") |
| 80 |
} |
| 81 |
|
| 82 |
// Reported error is "context deadline exceeded" from Context package, |
| 83 |
// which implements net.Error interface. |
| 84 |
type timeoutError interface { |
| 85 |
Timeout() bool |
| 86 |
} |
| 87 |
timeoutErr, ok := err.(timeoutError) |
| 88 |
if !ok || !timeoutErr.Timeout() { |
| 89 |
t.Error("Expected to have a timeout error") |
| 90 |
} |
| 91 |
expected = `context deadline exceeded` |
| 92 |
if !strings.Contains(err.Error(), expected) { |
| 93 |
t.Errorf("Expected %q error, got: %q", expected, err.Error()) |
| 94 |
} |
| 95 |
|
| 96 |
// 2nd request should fail again even if they would be fast because context |
| 97 |
// has already timed out. |
| 98 |
_, err = nc.RequestWithContext(ctx, "fast", []byte("world")) |
| 99 |
if err == nil { |
| 100 |
t.Fatal("Expected request with context to fail") |
| 101 |
} |
| 102 |
|
| 103 |
// now test headers make it all the way back |
| 104 |
msg := nats.NewMsg("hdrs") |
| 105 |
msg.Header.Add("Hdr-Test", "1") |
| 106 |
resp, err = nc.RequestMsgWithContext(context.Background(), msg) |
| 107 |
if err != nil { |
| 108 |
t.Fatalf("Expected request to be published: %v", err) |
| 109 |
} |
| 110 |
if string(resp.Data) != "+OK" { |
| 111 |
t.Fatalf("Headers were not published to the requestor") |
| 112 |
} |
| 113 |
if resp.Header.Get("Hdr-Test") != "1" { |
| 114 |
t.Fatalf("Did not receive header in response") |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
func TestContextRequestWithTimeout(t *testing.T) { |
| 119 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 120 |
testContextRequestWithTimeout(t, nc) |
| 121 |
}) |
| 122 |
} |
| 123 |
|
| 124 |
func TestOldContextRequestWithTimeout(t *testing.T) { |
| 125 |
withServerInstance(t, func(t *testing.T, _ *nats.Conn, inst *testservice.Instance) { |
| 126 |
nc := dialInstance(t, inst, nats.UseOldRequestStyle()) |
| 127 |
testContextRequestWithTimeout(t, nc) |
| 128 |
}) |
| 129 |
} |
| 130 |
|
| 131 |
func testContextRequestWithTimeoutCanceled(t *testing.T, nc *nats.Conn) { |
| 132 |
ctx, cancelCB := context.WithTimeout(context.Background(), 100*time.Millisecond) |
| 133 |
defer cancelCB() |
| 134 |
|
| 135 |
nc.Subscribe("fast", func(m *nats.Msg) { |
| 136 |
nc.Publish(m.Reply, []byte("OK")) |
| 137 |
}) |
| 138 |
|
| 139 |
// Fast request should not fail |
| 140 |
resp, err := nc.RequestWithContext(ctx, "fast", []byte("")) |
| 141 |
if err != nil { |
| 142 |
t.Fatalf("Expected request with context to not fail on fast response: %s", err) |
| 143 |
} |
| 144 |
got := string(resp.Data) |
| 145 |
expected := "OK" |
| 146 |
if got != expected { |
| 147 |
t.Errorf("Expected to receive %s, got: %s", expected, got) |
| 148 |
} |
| 149 |
|
| 150 |
// Cancel the context already so that rest of requests fail. |
| 151 |
cancelCB() |
| 152 |
|
| 153 |
// Context is already canceled so requests should immediately fail. |
| 154 |
_, err = nc.RequestWithContext(ctx, "fast", []byte("world")) |
| 155 |
if err == nil { |
| 156 |
t.Fatal("Expected request with timeout context to fail") |
| 157 |
} |
| 158 |
|
| 159 |
// Reported error is "context canceled" from Context package, |
| 160 |
// which is not a timeout error. |
| 161 |
type timeoutError interface { |
| 162 |
Timeout() bool |
| 163 |
} |
| 164 |
if _, ok := err.(timeoutError); ok { |
| 165 |
t.Errorf("Expected to not have a timeout error") |
| 166 |
} |
| 167 |
expected = `context canceled` |
| 168 |
if !strings.Contains(err.Error(), expected) { |
| 169 |
t.Errorf("Expected %q error, got: %q", expected, err.Error()) |
| 170 |
} |
| 171 |
|
| 172 |
// 2nd request should fail again even if fast because context has already been canceled |
| 173 |
_, err = nc.RequestWithContext(ctx, "fast", []byte("world")) |
| 174 |
if err == nil { |
| 175 |
t.Fatal("Expected request with context to fail") |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
func TestContextRequestWithTimeoutCanceled(t *testing.T) { |
| 180 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 181 |
testContextRequestWithTimeoutCanceled(t, nc) |
| 182 |
}) |
| 183 |
} |
| 184 |
|
| 185 |
func TestOldContextRequestWithTimeoutCanceled(t *testing.T) { |
| 186 |
withServerInstance(t, func(t *testing.T, _ *nats.Conn, inst *testservice.Instance) { |
| 187 |
nc := dialInstance(t, inst, nats.UseOldRequestStyle()) |
| 188 |
testContextRequestWithTimeoutCanceled(t, nc) |
| 189 |
}) |
| 190 |
} |
| 191 |
|
| 192 |
func testContextRequestWithCancel(t *testing.T, nc *nats.Conn) { |
| 193 |
ctx, cancelCB := context.WithCancel(context.Background()) |
| 194 |
defer cancelCB() // should always be called, not discarded, to prevent context leak |
| 195 |
|
| 196 |
// timer which cancels the context though can also be arbitrarily extended |
| 197 |
expirationTimer := time.AfterFunc(100*time.Millisecond, func() { |
| 198 |
cancelCB() |
| 199 |
}) |
| 200 |
|
| 201 |
sub1, err := nc.Subscribe("slow", func(m *nats.Msg) { |
| 202 |
// simulates latency into the client so that timeout is hit. |
| 203 |
time.Sleep(40 * time.Millisecond) |
| 204 |
nc.Publish(m.Reply, []byte("OK")) |
| 205 |
}) |
| 206 |
if err != nil { |
| 207 |
t.Fatalf("Expected to be able to subscribe: %s", err) |
| 208 |
} |
| 209 |
defer sub1.Unsubscribe() |
| 210 |
sub2, err := nc.Subscribe("slower", func(m *nats.Msg) { |
| 211 |
// we know this request will take longer so extend the timeout |
| 212 |
expirationTimer.Reset(100 * time.Millisecond) |
| 213 |
|
| 214 |
// slower reply which would have hit original timeout |
| 215 |
time.Sleep(70 * time.Millisecond) |
| 216 |
|
| 217 |
nc.Publish(m.Reply, []byte("Also OK")) |
| 218 |
}) |
| 219 |
if err != nil { |
| 220 |
t.Fatalf("Expected to be able to subscribe: %s", err) |
| 221 |
} |
| 222 |
defer sub2.Unsubscribe() |
| 223 |
|
| 224 |
for i := 0; i < 2; i++ { |
| 225 |
resp, err := nc.RequestWithContext(ctx, "slow", []byte("")) |
| 226 |
if err != nil { |
| 227 |
t.Fatalf("Expected request with context to not fail: %s", err) |
| 228 |
} |
| 229 |
got := string(resp.Data) |
| 230 |
expected := "OK" |
| 231 |
if got != expected { |
| 232 |
t.Errorf("Expected to receive %s, got: %s", expected, got) |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
// A third request with latency would make the context |
| 237 |
// get canceled, but these reset the timer so deadline |
| 238 |
// gets extended: |
| 239 |
for i := 0; i < 10; i++ { |
| 240 |
resp, err := nc.RequestWithContext(ctx, "slower", []byte("")) |
| 241 |
if err != nil { |
| 242 |
t.Fatalf("Expected request with context to not fail: %s", err) |
| 243 |
} |
| 244 |
got := string(resp.Data) |
| 245 |
expected := "Also OK" |
| 246 |
if got != expected { |
| 247 |
t.Errorf("Expected to receive %s, got: %s", expected, got) |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
// One more slow request will expire the timer and cause an error... |
| 252 |
_, err = nc.RequestWithContext(ctx, "slow", []byte("")) |
| 253 |
if err == nil { |
| 254 |
t.Fatal("Expected request with cancellation context to fail") |
| 255 |
} |
| 256 |
|
| 257 |
// ...though reported error is "context canceled" from Context package, |
| 258 |
// which is not a timeout error. |
| 259 |
type timeoutError interface { |
| 260 |
Timeout() bool |
| 261 |
} |
| 262 |
if _, ok := err.(timeoutError); ok { |
| 263 |
t.Errorf("Expected to not have a timeout error") |
| 264 |
} |
| 265 |
expected := `context canceled` |
| 266 |
if !strings.Contains(err.Error(), expected) { |
| 267 |
t.Errorf("Expected %q error, got: %q", expected, err.Error()) |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
func TestContextOldRequestClosed(t *testing.T) { |
| 272 |
withServerInstance(t, func(t *testing.T, _ *nats.Conn, inst *testservice.Instance) { |
| 273 |
nc := dialInstance(t, inst, nats.UseOldRequestStyle()) |
| 274 |
|
| 275 |
ctx, cancelCB := context.WithTimeout(context.Background(), time.Second) |
| 276 |
defer cancelCB() // should always be called, not discarded, to prevent context leak |
| 277 |
|
| 278 |
errCh := make(chan error, 1) |
| 279 |
start := time.Now() |
| 280 |
go func() { |
| 281 |
sub, _ := nc.SubscribeSync("checkClose") |
| 282 |
defer sub.Unsubscribe() |
| 283 |
_, err := nc.RequestWithContext(ctx, "checkClose", []byte("should be kicked out on close")) |
| 284 |
errCh <- err |
| 285 |
}() |
| 286 |
time.Sleep(100 * time.Millisecond) |
| 287 |
nc.Close() |
| 288 |
if e := <-errCh; e != nats.ErrConnectionClosed { |
| 289 |
t.Fatalf("Unexpected error: %v", e) |
| 290 |
} |
| 291 |
if dur := time.Since(start); dur >= time.Second { |
| 292 |
t.Fatalf("Request took too long to bail out: %v", dur) |
| 293 |
} |
| 294 |
}) |
| 295 |
} |
| 296 |
|
| 297 |
func TestContextRequestWithCancel(t *testing.T) { |
| 298 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 299 |
testContextRequestWithCancel(t, nc) |
| 300 |
}) |
| 301 |
} |
| 302 |
|
| 303 |
func TestOldContextRequestWithCancel(t *testing.T) { |
| 304 |
withServerInstance(t, func(t *testing.T, _ *nats.Conn, inst *testservice.Instance) { |
| 305 |
nc := dialInstance(t, inst, nats.UseOldRequestStyle()) |
| 306 |
testContextRequestWithCancel(t, nc) |
| 307 |
}) |
| 308 |
} |
| 309 |
|
| 310 |
func testContextRequestWithDeadline(t *testing.T, nc *nats.Conn) { |
| 311 |
deadline := time.Now().Add(100 * time.Millisecond) |
| 312 |
ctx, cancelCB := context.WithDeadline(context.Background(), deadline) |
| 313 |
defer cancelCB() // should always be called, not discarded, to prevent context leak |
| 314 |
|
| 315 |
nc.Subscribe("slow", func(m *nats.Msg) { |
| 316 |
// simulates latency into the client so that timeout is hit. |
| 317 |
time.Sleep(40 * time.Millisecond) |
| 318 |
nc.Publish(m.Reply, []byte("OK")) |
| 319 |
}) |
| 320 |
|
| 321 |
for i := 0; i < 2; i++ { |
| 322 |
resp, err := nc.RequestWithContext(ctx, "slow", []byte("")) |
| 323 |
if err != nil { |
| 324 |
t.Fatalf("Expected request with context to not fail: %s", err) |
| 325 |
} |
| 326 |
got := string(resp.Data) |
| 327 |
expected := "OK" |
| 328 |
if got != expected { |
| 329 |
t.Errorf("Expected to receive %s, got: %s", expected, got) |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
// A third request with latency would make the context |
| 334 |
// reach the deadline. |
| 335 |
_, err := nc.RequestWithContext(ctx, "slow", []byte("")) |
| 336 |
if err == nil { |
| 337 |
t.Fatal("Expected request with context to reach deadline") |
| 338 |
} |
| 339 |
|
| 340 |
// Reported error is "context deadline exceeded" from Context package, |
| 341 |
// which implements net.Error Timeout interface. |
| 342 |
type timeoutError interface { |
| 343 |
Timeout() bool |
| 344 |
} |
| 345 |
timeoutErr, ok := err.(timeoutError) |
| 346 |
if !ok || !timeoutErr.Timeout() { |
| 347 |
t.Errorf("Expected to have a timeout error") |
| 348 |
} |
| 349 |
expected := `context deadline exceeded` |
| 350 |
if !strings.Contains(err.Error(), expected) { |
| 351 |
t.Errorf("Expected %q error, got: %q", expected, err.Error()) |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
func TestContextRequestWithDeadline(t *testing.T) { |
| 356 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 357 |
testContextRequestWithDeadline(t, nc) |
| 358 |
}) |
| 359 |
} |
| 360 |
|
| 361 |
func TestOldContextRequestWithDeadline(t *testing.T) { |
| 362 |
withServerInstance(t, func(t *testing.T, _ *nats.Conn, inst *testservice.Instance) { |
| 363 |
nc := dialInstance(t, inst, nats.UseOldRequestStyle()) |
| 364 |
testContextRequestWithDeadline(t, nc) |
| 365 |
}) |
| 366 |
} |
| 367 |
|
| 368 |
func TestContextSubNextMsgWithTimeout(t *testing.T) { |
| 369 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 370 |
ctx, cancelCB := context.WithTimeout(context.Background(), 100*time.Millisecond) |
| 371 |
defer cancelCB() // should always be called, not discarded, to prevent context leak |
| 372 |
|
| 373 |
sub, err := nc.SubscribeSync("slow") |
| 374 |
if err != nil { |
| 375 |
t.Fatalf("Expected to be able to subscribe: %s", err) |
| 376 |
} |
| 377 |
|
| 378 |
for i := 0; i < 2; i++ { |
| 379 |
err := nc.Publish("slow", []byte("OK")) |
| 380 |
if err != nil { |
| 381 |
t.Fatalf("Expected publish to not fail: %s", err) |
| 382 |
} |
| 383 |
// Enough time to get a couple of messages |
| 384 |
time.Sleep(40 * time.Millisecond) |
| 385 |
|
| 386 |
msg, err := sub.NextMsgWithContext(ctx) |
| 387 |
if err != nil { |
| 388 |
t.Fatalf("Expected to receive message: %s", err) |
| 389 |
} |
| 390 |
got := string(msg.Data) |
| 391 |
expected := "OK" |
| 392 |
if got != expected { |
| 393 |
t.Errorf("Expected to receive %s, got: %s", expected, got) |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
// Third message will fail because the context will be canceled by now |
| 398 |
_, err = sub.NextMsgWithContext(ctx) |
| 399 |
if err == nil { |
| 400 |
t.Fatal("Expected to fail receiving a message") |
| 401 |
} |
| 402 |
|
| 403 |
// Reported error is "context deadline exceeded" from Context package, |
| 404 |
// which implements net.Error Timeout interface. |
| 405 |
type timeoutError interface { |
| 406 |
Timeout() bool |
| 407 |
} |
| 408 |
timeoutErr, ok := err.(timeoutError) |
| 409 |
if !ok || !timeoutErr.Timeout() { |
| 410 |
t.Errorf("Expected to have a timeout error") |
| 411 |
} |
| 412 |
expected := `context deadline exceeded` |
| 413 |
if !strings.Contains(err.Error(), expected) { |
| 414 |
t.Errorf("Expected %q error, got: %q", expected, err.Error()) |
| 415 |
} |
| 416 |
}) |
| 417 |
} |
| 418 |
|
| 419 |
func TestContextSubNextMsgWithTimeoutCanceled(t *testing.T) { |
| 420 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 421 |
ctx, cancelCB := context.WithTimeout(context.Background(), 100*time.Millisecond) |
| 422 |
defer cancelCB() // should always be called, not discarded, to prevent context leak |
| 423 |
|
| 424 |
sub, err := nc.SubscribeSync("fast") |
| 425 |
if err != nil { |
| 426 |
t.Fatalf("Expected to be able to subscribe: %s", err) |
| 427 |
} |
| 428 |
|
| 429 |
for i := 0; i < 2; i++ { |
| 430 |
err := nc.Publish("fast", []byte("OK")) |
| 431 |
if err != nil { |
| 432 |
t.Fatalf("Expected publish to not fail: %s", err) |
| 433 |
} |
| 434 |
// Enough time to get a couple of messages |
| 435 |
time.Sleep(40 * time.Millisecond) |
| 436 |
|
| 437 |
msg, err := sub.NextMsgWithContext(ctx) |
| 438 |
if err != nil { |
| 439 |
t.Fatalf("Expected to receive message: %s", err) |
| 440 |
} |
| 441 |
got := string(msg.Data) |
| 442 |
expected := "OK" |
| 443 |
if got != expected { |
| 444 |
t.Errorf("Expected to receive %s, got: %s", expected, got) |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
// Cancel the context already so that rest of NextMsg calls fail. |
| 449 |
cancelCB() |
| 450 |
|
| 451 |
_, err = sub.NextMsgWithContext(ctx) |
| 452 |
if err == nil { |
| 453 |
t.Fatal("Expected request with timeout context to fail") |
| 454 |
} |
| 455 |
|
| 456 |
// Reported error is "context canceled" from Context package, |
| 457 |
// which is not a timeout error. |
| 458 |
type timeoutError interface { |
| 459 |
Timeout() bool |
| 460 |
} |
| 461 |
if _, ok := err.(timeoutError); ok { |
| 462 |
t.Errorf("Expected to not have a timeout error") |
| 463 |
} |
| 464 |
expected := `context canceled` |
| 465 |
if !strings.Contains(err.Error(), expected) { |
| 466 |
t.Errorf("Expected %q error, got: %q", expected, err.Error()) |
| 467 |
} |
| 468 |
}) |
| 469 |
} |
| 470 |
|
| 471 |
func TestContextSubNextMsgWithCancel(t *testing.T) { |
| 472 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 473 |
ctx, cancelCB := context.WithCancel(context.Background()) |
| 474 |
defer cancelCB() // should always be called, not discarded, to prevent context leak |
| 475 |
|
| 476 |
// timer which cancels the context though can also be arbitrarily extended |
| 477 |
time.AfterFunc(100*time.Millisecond, func() { |
| 478 |
cancelCB() |
| 479 |
}) |
| 480 |
|
| 481 |
sub1, err := nc.SubscribeSync("foo") |
| 482 |
if err != nil { |
| 483 |
t.Fatalf("Expected to be able to subscribe: %s", err) |
| 484 |
} |
| 485 |
sub2, err := nc.SubscribeSync("bar") |
| 486 |
if err != nil { |
| 487 |
t.Fatalf("Expected to be able to subscribe: %s", err) |
| 488 |
} |
| 489 |
|
| 490 |
for i := 0; i < 2; i++ { |
| 491 |
err := nc.Publish("foo", []byte("OK")) |
| 492 |
if err != nil { |
| 493 |
t.Fatalf("Expected publish to not fail: %s", err) |
| 494 |
} |
| 495 |
resp, err := sub1.NextMsgWithContext(ctx) |
| 496 |
if err != nil { |
| 497 |
t.Fatalf("Expected request with context to not fail: %s", err) |
| 498 |
} |
| 499 |
got := string(resp.Data) |
| 500 |
expected := "OK" |
| 501 |
if got != expected { |
| 502 |
t.Errorf("Expected to receive %s, got: %s", expected, got) |
| 503 |
} |
| 504 |
} |
| 505 |
err = nc.Publish("bar", []byte("Also OK")) |
| 506 |
if err != nil { |
| 507 |
t.Fatalf("Expected publish to not fail: %s", err) |
| 508 |
} |
| 509 |
|
| 510 |
resp, err := sub2.NextMsgWithContext(ctx) |
| 511 |
if err != nil { |
| 512 |
t.Fatalf("Expected request with context to not fail: %s", err) |
| 513 |
} |
| 514 |
got := string(resp.Data) |
| 515 |
expected := "Also OK" |
| 516 |
if got != expected { |
| 517 |
t.Errorf("Expected to receive %s, got: %s", expected, got) |
| 518 |
} |
| 519 |
|
| 520 |
// We do not have another message pending so timer will |
| 521 |
// cancel the context. |
| 522 |
_, err = sub2.NextMsgWithContext(ctx) |
| 523 |
if err == nil { |
| 524 |
t.Fatal("Expected request with context to fail") |
| 525 |
} |
| 526 |
|
| 527 |
// Reported error is "context canceled" from Context package, |
| 528 |
// which is not a timeout error. |
| 529 |
type timeoutError interface { |
| 530 |
Timeout() bool |
| 531 |
} |
| 532 |
if _, ok := err.(timeoutError); ok { |
| 533 |
t.Errorf("Expected to not have a timeout error") |
| 534 |
} |
| 535 |
expected = `context canceled` |
| 536 |
if !strings.Contains(err.Error(), expected) { |
| 537 |
t.Errorf("Expected %q error, got: %q", expected, err.Error()) |
| 538 |
} |
| 539 |
}) |
| 540 |
} |
| 541 |
|
| 542 |
func TestContextSubNextMsgWithDeadline(t *testing.T) { |
| 543 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 544 |
deadline := time.Now().Add(100 * time.Millisecond) |
| 545 |
ctx, cancelCB := context.WithDeadline(context.Background(), deadline) |
| 546 |
defer cancelCB() // should always be called, not discarded, to prevent context leak |
| 547 |
|
| 548 |
sub, err := nc.SubscribeSync("slow") |
| 549 |
if err != nil { |
| 550 |
t.Fatalf("Expected to be able to subscribe: %s", err) |
| 551 |
} |
| 552 |
|
| 553 |
for i := 0; i < 2; i++ { |
| 554 |
err := nc.Publish("slow", []byte("OK")) |
| 555 |
if err != nil { |
| 556 |
t.Fatalf("Expected publish to not fail: %s", err) |
| 557 |
} |
| 558 |
// Enough time to get a couple of messages |
| 559 |
time.Sleep(40 * time.Millisecond) |
| 560 |
|
| 561 |
msg, err := sub.NextMsgWithContext(ctx) |
| 562 |
if err != nil { |
| 563 |
t.Fatalf("Expected to receive message: %s", err) |
| 564 |
} |
| 565 |
got := string(msg.Data) |
| 566 |
expected := "OK" |
| 567 |
if got != expected { |
| 568 |
t.Errorf("Expected to receive %s, got: %s", expected, got) |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
// Third message will fail because the context will be canceled by now |
| 573 |
_, err = sub.NextMsgWithContext(ctx) |
| 574 |
if err == nil { |
| 575 |
t.Fatal("Expected to fail receiving a message") |
| 576 |
} |
| 577 |
|
| 578 |
// Reported error is "context deadline exceeded" from Context package, |
| 579 |
// which implements net.Error Timeout interface. |
| 580 |
type timeoutError interface { |
| 581 |
Timeout() bool |
| 582 |
} |
| 583 |
timeoutErr, ok := err.(timeoutError) |
| 584 |
if !ok || !timeoutErr.Timeout() { |
| 585 |
t.Errorf("Expected to have a timeout error") |
| 586 |
} |
| 587 |
expected := `context deadline exceeded` |
| 588 |
if !strings.Contains(err.Error(), expected) { |
| 589 |
t.Errorf("Expected %q error, got: %q", expected, err.Error()) |
| 590 |
} |
| 591 |
}) |
| 592 |
} |
| 593 |
|
| 594 |
func TestContextRequestConnClosed(t *testing.T) { |
| 595 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 596 |
ctx, cancelCB := context.WithCancel(context.Background()) |
| 597 |
defer cancelCB() |
| 598 |
|
| 599 |
time.AfterFunc(100*time.Millisecond, func() { |
| 600 |
cancelCB() |
| 601 |
}) |
| 602 |
|
| 603 |
nc.Close() |
| 604 |
_, err := nc.RequestWithContext(ctx, "foo", []byte("")) |
| 605 |
if err == nil { |
| 606 |
t.Fatal("Expected request to fail with error") |
| 607 |
} |
| 608 |
if err != nats.ErrConnectionClosed { |
| 609 |
t.Errorf("Expected request to fail with connection closed error: %s", err) |
| 610 |
} |
| 611 |
}) |
| 612 |
} |
| 613 |
|
| 614 |
func TestContextBadSubscription(t *testing.T) { |
| 615 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 616 |
ctx, cancelCB := context.WithCancel(context.Background()) |
| 617 |
defer cancelCB() |
| 618 |
time.AfterFunc(100*time.Millisecond, func() { |
| 619 |
cancelCB() |
| 620 |
}) |
| 621 |
|
| 622 |
sub, err := nc.Subscribe("foo", func(_ *nats.Msg) {}) |
| 623 |
if err != nil { |
| 624 |
t.Fatalf("Expected to be able to subscribe: %s", err) |
| 625 |
} |
| 626 |
|
| 627 |
err = sub.Unsubscribe() |
| 628 |
if err != nil { |
| 629 |
t.Fatalf("Expected to be able to unsubscribe: %s", err) |
| 630 |
} |
| 631 |
|
| 632 |
_, err = sub.NextMsgWithContext(ctx) |
| 633 |
if err == nil { |
| 634 |
t.Fatal("Expected to fail getting next message with context") |
| 635 |
} |
| 636 |
|
| 637 |
if err != nats.ErrBadSubscription { |
| 638 |
t.Errorf("Expected request to fail with connection closed error: %s", err) |
| 639 |
} |
| 640 |
}) |
| 641 |
} |
| 642 |
|
| 643 |
func TestFlushWithContext(t *testing.T) { |
| 644 |
withServerInstance(t, func(t *testing.T, nc *nats.Conn, inst *testservice.Instance) { |
| 645 |
ctx := context.Background() |
| 646 |
|
| 647 |
// No context should error. |
| 648 |
//lint:ignore SA1012 testing that passing nil fails |
| 649 |
if err := nc.FlushWithContext(nil); err != nats.ErrInvalidContext { |
| 650 |
t.Fatalf("Expected '%v', got '%v'", nats.ErrInvalidContext, err) |
| 651 |
} |
| 652 |
// A context with no deadline set should error also. |
| 653 |
if err := nc.FlushWithContext(ctx); err != nats.ErrNoDeadlineContext { |
| 654 |
t.Fatalf("Expected '%v', got '%v'", nats.ErrNoDeadlineContext, err) |
| 655 |
} |
| 656 |
|
| 657 |
dctx, cancel := context.WithTimeout(ctx, 10*time.Second) |
| 658 |
cancel() |
| 659 |
|
| 660 |
// A closed context should error. |
| 661 |
if err := nc.FlushWithContext(dctx); err != context.Canceled { |
| 662 |
t.Fatalf("Expected '%v', got '%v'", context.Canceled, err) |
| 663 |
} |
| 664 |
|
| 665 |
t.Run("flush on closed connection", func(t *testing.T) { |
| 666 |
nc2 := dialInstance(t, inst) |
| 667 |
nc2.Close() |
| 668 |
|
| 669 |
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) |
| 670 |
defer cancel() |
| 671 |
|
| 672 |
err := nc2.FlushWithContext(ctx) |
| 673 |
if err != nats.ErrConnectionClosed { |
| 674 |
t.Fatalf("Expected '%v', got '%v'", nats.ErrConnectionClosed, err) |
| 675 |
} |
| 676 |
}) |
| 677 |
}) |
| 678 |
} |
| 679 |
|
| 680 |
func TestUnsubscribeAndNextMsgWithContext(t *testing.T) { |
| 681 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 682 |
ctx, cancelCB := context.WithCancel(context.Background()) |
| 683 |
defer cancelCB() // should always be called, not discarded, to prevent context leak |
| 684 |
|
| 685 |
sub, err := nc.SubscribeSync("foo") |
| 686 |
if err != nil { |
| 687 |
t.Fatalf("Expected to be able to subscribe: %s", err) |
| 688 |
} |
| 689 |
sub.Unsubscribe() |
| 690 |
if _, err = sub.NextMsgWithContext(ctx); err != nats.ErrBadSubscription { |
| 691 |
t.Fatalf("Expected '%v', but got: '%v'", nats.ErrBadSubscription, err) |
| 692 |
} |
| 693 |
|
| 694 |
ctx, cancelCB = context.WithCancel(context.Background()) |
| 695 |
defer cancelCB() // should always be called, not discarded, to prevent context leak |
| 696 |
|
| 697 |
sub, err = nc.SubscribeSync("foo") |
| 698 |
if err != nil { |
| 699 |
t.Fatalf("Expected to be able to subscribe: %s", err) |
| 700 |
} |
| 701 |
|
| 702 |
// Now make sure we get same error when unsubscribing from separate routine |
| 703 |
// while in the call. |
| 704 |
wg := sync.WaitGroup{} |
| 705 |
wg.Add(1) |
| 706 |
go func() { |
| 707 |
time.Sleep(100 * time.Millisecond) |
| 708 |
sub.Unsubscribe() |
| 709 |
wg.Done() |
| 710 |
}() |
| 711 |
|
| 712 |
if _, err = sub.NextMsgWithContext(ctx); err != nats.ErrBadSubscription { |
| 713 |
t.Fatalf("Expected '%v', but got: '%v'", nats.ErrBadSubscription, err) |
| 714 |
} |
| 715 |
wg.Wait() |
| 716 |
}) |
| 717 |
} |
| 718 |
|
| 719 |
func TestContextInvalid(t *testing.T) { |
| 720 |
withServer(t, func(t *testing.T, nc *nats.Conn) { |
| 721 |
//lint:ignore SA1012 testing that passing nil fails |
| 722 |
_, err := nc.RequestWithContext(nil, "foo", []byte("")) |
| 723 |
if err == nil { |
| 724 |
t.Fatal("Expected request to fail with error") |
| 725 |
} |
| 726 |
if err != nats.ErrInvalidContext { |
| 727 |
t.Errorf("Expected request to fail with connection closed error: %s", err) |
| 728 |
} |
| 729 |
|
| 730 |
sub, err := nc.Subscribe("foo", func(_ *nats.Msg) {}) |
| 731 |
if err != nil { |
| 732 |
t.Fatalf("Expected to be able to subscribe: %s", err) |
| 733 |
} |
| 734 |
|
| 735 |
//lint:ignore SA1012 testing that passing nil fails |
| 736 |
_, err = sub.NextMsgWithContext(nil) |
| 737 |
if err == nil { |
| 738 |
t.Fatal("Expected request to fail with error") |
| 739 |
} |
| 740 |
if err != nats.ErrInvalidContext { |
| 741 |
t.Errorf("Expected request to fail with connection closed error: %s", err) |
| 742 |
} |
| 743 |
}) |
| 744 |
} |