| 1 |
|
| 2 |
ved. |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package amqp091 |
| 7 |
|
| 8 |
import ( |
| 9 |
"context" |
| 10 |
"net/url" |
| 11 |
"testing" |
| 12 |
"time" |
| 13 |
|
| 14 |
"github.com/rabbitmq/amqp091-go/internal/utils" |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
func TestConnectionRecoveryPublish(t *testing.T) { |
| 19 |
connectionName := "test-connection-recovery-publish" |
| 20 |
|
| 21 |
properties := NewConnectionProperties() |
| 22 |
properties.SetClientConnectionName(connectionName) |
| 23 |
conn, err := DialConfig(amqpURL, Config{ |
| 24 |
Recovery: &Recovery{}, |
| 25 |
Locale: defaultLocale, |
| 26 |
Properties: properties, |
| 27 |
}) |
| 28 |
if err != nil { |
| 29 |
t.Fatalf("DialConfig failed: %v", err) |
| 30 |
} |
| 31 |
defer conn.Close() |
| 32 |
|
| 33 |
ch, err := conn.Channel() |
| 34 |
if err != nil { |
| 35 |
t.Fatalf("Channel creation failed: %v", err) |
| 36 |
} |
| 37 |
defer ch.Close() |
| 38 |
|
| 39 |
exchangeName := "recovery_exchange" |
| 40 |
err = ch.ExchangeDeclare( |
| 41 |
exchangeName, |
| 42 |
"direct", |
| 43 |
false, |
| 44 |
true, |
| 45 |
false, |
| 46 |
false, |
| 47 |
nil, |
| 48 |
) |
| 49 |
if err != nil { |
| 50 |
t.Fatalf("ExchangeDeclare failed: %v", err) |
| 51 |
} |
| 52 |
defer func() { |
| 53 |
_ = ch.ExchangeDelete(exchangeName, false, false) |
| 54 |
}() |
| 55 |
|
| 56 |
queueName := "recovery_publish_queue" |
| 57 |
_, err = ch.QueueDeclare( |
| 58 |
queueName, |
| 59 |
true, |
| 60 |
false, |
| 61 |
false, |
| 62 |
false, |
| 63 |
nil, |
| 64 |
) |
| 65 |
if err != nil { |
| 66 |
t.Fatalf("QueueDeclare failed: %v", err) |
| 67 |
} |
| 68 |
defer func() { |
| 69 |
_, _ = ch.QueueDelete(queueName, false, false, false) |
| 70 |
}() |
| 71 |
|
| 72 |
routingKey := "recovery_routing_key" |
| 73 |
err = ch.QueueBind( |
| 74 |
queueName, |
| 75 |
routingKey, |
| 76 |
exchangeName, |
| 77 |
false, |
| 78 |
nil, |
| 79 |
) |
| 80 |
if err != nil { |
| 81 |
t.Fatalf("QueueBind failed: %v", err) |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
preRecoveryMessage := "hello recovery 1" |
| 86 |
err = ch.PublishWithContext( |
| 87 |
context.Background(), |
| 88 |
exchangeName, |
| 89 |
routingKey, |
| 90 |
false, |
| 91 |
false, |
| 92 |
Publishing{ |
| 93 |
ContentType: "text/plain", |
| 94 |
Body: []byte(preRecoveryMessage), |
| 95 |
}, |
| 96 |
) |
| 97 |
if err != nil { |
| 98 |
t.Fatalf("Publish pre-recovery message failed: %v", err) |
| 99 |
} |
| 100 |
t.Logf("Published message pre-recovery: %s", preRecoveryMessage) |
| 101 |
|
| 102 |
|
| 103 |
msgs, err := ch.Consume( |
| 104 |
queueName, |
| 105 |
"recovery_publish_consumer", |
| 106 |
true, |
| 107 |
false, |
| 108 |
false, |
| 109 |
false, |
| 110 |
nil, |
| 111 |
) |
| 112 |
if err != nil { |
| 113 |
t.Fatalf("Consume failed: %v", err) |
| 114 |
} |
| 115 |
|
| 116 |
select { |
| 117 |
case d, ok := <-msgs: |
| 118 |
if !ok { |
| 119 |
t.Fatalf("Consume channel closed prematurely") |
| 120 |
} |
| 121 |
if string(d.Body) != preRecoveryMessage { |
| 122 |
t.Fatalf("Expected message '%s', got: %s", preRecoveryMessage, string(d.Body)) |
| 123 |
} |
| 124 |
t.Logf("Received message pre-recovery: %s", string(d.Body)) |
| 125 |
case <-time.After(5 * time.Second): |
| 126 |
t.Fatalf("Timeout waiting for receive message pre-recovery: %s", preRecoveryMessage) |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
stateChanged := make(chan *StateChanged, 10) |
| 131 |
conn.NotifyStateChange(stateChanged) |
| 132 |
|
| 133 |
|
| 134 |
chanStateChanged := make(chan *StateChanged, 10) |
| 135 |
ch.NotifyStateChange(chanStateChanged) |
| 136 |
|
| 137 |
|
| 138 |
dropConnection(t, connectionName) |
| 139 |
|
| 140 |
|
| 141 |
waitForConnectionOpen(t, stateChanged) |
| 142 |
|
| 143 |
|
| 144 |
waitForChannelOpen(t, chanStateChanged) |
| 145 |
|
| 146 |
|
| 147 |
postRecoveryMessage := "hello recovery 2" |
| 148 |
err = ch.PublishWithContext( |
| 149 |
context.Background(), |
| 150 |
exchangeName, |
| 151 |
routingKey, |
| 152 |
false, |
| 153 |
false, |
| 154 |
Publishing{ |
| 155 |
ContentType: "text/plain", |
| 156 |
Body: []byte(postRecoveryMessage), |
| 157 |
}, |
| 158 |
) |
| 159 |
if err != nil { |
| 160 |
t.Fatalf("Publish post-recovery message failed: %v", err) |
| 161 |
} |
| 162 |
t.Logf("Published message post-recovery: %s", postRecoveryMessage) |
| 163 |
|
| 164 |
|
| 165 |
select { |
| 166 |
case d, ok := <-msgs: |
| 167 |
if !ok { |
| 168 |
t.Fatalf("Consume channel closed after recovery") |
| 169 |
} |
| 170 |
if string(d.Body) != postRecoveryMessage { |
| 171 |
t.Fatalf("Expected message '%s', got: %s", postRecoveryMessage, string(d.Body)) |
| 172 |
} |
| 173 |
t.Logf("Received message post-recovery: %s", string(d.Body)) |
| 174 |
case <-time.After(10 * time.Second): |
| 175 |
t.Fatalf("Timeout waiting for receive message post-recovery: %s", postRecoveryMessage) |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
func TestConnectionRecoveryConsume(t *testing.T) { |
| 181 |
connectionName := "test-connection-recovery-consume" |
| 182 |
|
| 183 |
properties := NewConnectionProperties() |
| 184 |
properties.SetClientConnectionName(connectionName) |
| 185 |
conn, err := DialConfig(amqpURL, Config{ |
| 186 |
Recovery: &Recovery{}, |
| 187 |
Locale: defaultLocale, |
| 188 |
Properties: properties, |
| 189 |
}) |
| 190 |
if err != nil { |
| 191 |
t.Fatalf("DialConfig failed: %v", err) |
| 192 |
} |
| 193 |
defer conn.Close() |
| 194 |
|
| 195 |
ch, err := conn.Channel() |
| 196 |
if err != nil { |
| 197 |
t.Fatalf("Channel creation failed: %v", err) |
| 198 |
} |
| 199 |
defer ch.Close() |
| 200 |
|
| 201 |
queueName := "recovery_consume_test_queue" |
| 202 |
_, err = ch.QueueDeclare( |
| 203 |
queueName, |
| 204 |
true, |
| 205 |
false, |
| 206 |
false, |
| 207 |
false, |
| 208 |
nil, |
| 209 |
) |
| 210 |
if err != nil { |
| 211 |
t.Fatalf("QueueDeclare failed: %v", err) |
| 212 |
} |
| 213 |
defer func() { |
| 214 |
_, _ = ch.QueueDelete(queueName, false, false, false) |
| 215 |
}() |
| 216 |
|
| 217 |
|
| 218 |
msgs, err := ch.Consume( |
| 219 |
queueName, |
| 220 |
"consume-recovery-test", |
| 221 |
false, |
| 222 |
false, |
| 223 |
false, |
| 224 |
false, |
| 225 |
nil, |
| 226 |
) |
| 227 |
if err != nil { |
| 228 |
t.Fatalf("Consume failed: %v", err) |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
err = ch.PublishWithContext( |
| 233 |
context.Background(), |
| 234 |
"", |
| 235 |
queueName, |
| 236 |
false, |
| 237 |
false, |
| 238 |
Publishing{ |
| 239 |
ContentType: "text/plain", |
| 240 |
Body: []byte("hello recovery consume"), |
| 241 |
}, |
| 242 |
) |
| 243 |
if err != nil { |
| 244 |
t.Fatalf("Publish failed: %v", err) |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
select { |
| 249 |
case msg, ok := <-msgs: |
| 250 |
if !ok { |
| 251 |
t.Fatalf("Consume channel closed prematurely") |
| 252 |
} |
| 253 |
if string(msg.Body) != "hello recovery consume" { |
| 254 |
t.Fatalf("Expected message 'hello recovery consume', got: %s", string(msg.Body)) |
| 255 |
} |
| 256 |
t.Logf("Received message pre-recovery: %s (Redelivered: %t). Intentional no ACK.", string(msg.Body), msg.Redelivered) |
| 257 |
case <-time.After(5 * time.Second): |
| 258 |
t.Fatalf("Timeout waiting for message delivery pre-recovery") |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
stateChanged := make(chan *StateChanged, 10) |
| 263 |
conn.NotifyStateChange(stateChanged) |
| 264 |
|
| 265 |
|
| 266 |
chanStateChanged := make(chan *StateChanged, 10) |
| 267 |
ch.NotifyStateChange(chanStateChanged) |
| 268 |
|
| 269 |
|
| 270 |
dropConnection(t, connectionName) |
| 271 |
|
| 272 |
|
| 273 |
waitForConnectionOpen(t, stateChanged) |
| 274 |
|
| 275 |
|
| 276 |
waitForChannelOpen(t, chanStateChanged) |
| 277 |
|
| 278 |
|
| 279 |
select { |
| 280 |
case msg, ok := <-msgs: |
| 281 |
if !ok { |
| 282 |
t.Fatalf("Consume channel closed after recovery") |
| 283 |
} |
| 284 |
if string(msg.Body) != "hello recovery consume" { |
| 285 |
t.Fatalf("Expected message 'hello recovery consume', got: %s", string(msg.Body)) |
| 286 |
} |
| 287 |
t.Logf("Received message post-recovery: %s (Redelivered: %t). Sending ACK.", string(msg.Body), msg.Redelivered) |
| 288 |
|
| 289 |
err = msg.Ack(false) |
| 290 |
if err != nil { |
| 291 |
t.Fatalf("Acking redelivered message post-recovery failed: %v", err) |
| 292 |
} |
| 293 |
case <-time.After(10 * time.Second): |
| 294 |
t.Fatalf("Timeout waiting for message redelivery post-recovery") |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
func dropConnection(t *testing.T, name string) { |
| 299 |
var targetConnName string |
| 300 |
loopDeadline := time.Now().Add(10 * time.Second) |
| 301 |
for time.Now().Before(loopDeadline) { |
| 302 |
connection, err := utils.GetConnectionByName(name) |
| 303 |
if err != nil { |
| 304 |
t.Logf("Failure getting connection by name (will retry): %v", err) |
| 305 |
time.Sleep(2 * time.Second) |
| 306 |
continue |
| 307 |
} |
| 308 |
targetConnName = connection.Name |
| 309 |
break |
| 310 |
} |
| 311 |
|
| 312 |
if targetConnName == "" { |
| 313 |
conns, _ := utils.Connections() |
| 314 |
t.Fatalf("Could not find connection by name: %s in connections: %+v", name, conns) |
| 315 |
} |
| 316 |
|
| 317 |
t.Logf("Dropping connection: %s", targetConnName) |
| 318 |
err := utils.DropConnection(url.PathEscape(targetConnName), "15672") |
| 319 |
if err != nil { |
| 320 |
t.Fatalf("DropConnection failed: %v", err) |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
func waitForConnectionOpen(t *testing.T, stateChanged chan *StateChanged) { |
| 325 |
var connReconnectingSeen bool |
| 326 |
var connOpenSeen bool |
| 327 |
for !connOpenSeen { |
| 328 |
select { |
| 329 |
case sc := <-stateChanged: |
| 330 |
t.Logf("Connection state changed: %s", sc) |
| 331 |
if sc.To == StateReconnecting { |
| 332 |
connReconnectingSeen = true |
| 333 |
} |
| 334 |
if sc.To == StateOpen { |
| 335 |
connOpenSeen = true |
| 336 |
} |
| 337 |
case <-time.After(10 * time.Second): |
| 338 |
t.Fatalf("Timeout waiting for connection recovery state changes. Reconnecting seen: %t, Open seen: %t", connReconnectingSeen, connOpenSeen) |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
func waitForChannelOpen(t *testing.T, chanStateChanged chan *StateChanged) { |
| 344 |
var chanReconnectingSeen bool |
| 345 |
var chanOpenSeen bool |
| 346 |
|
| 347 |
for !chanOpenSeen { |
| 348 |
select { |
| 349 |
case sc := <-chanStateChanged: |
| 350 |
t.Logf("Channel state changed: %s", sc) |
| 351 |
if sc.To == StateReconnecting { |
| 352 |
chanReconnectingSeen = true |
| 353 |
} |
| 354 |
if sc.To == StateOpen { |
| 355 |
chanOpenSeen = true |
| 356 |
} |
| 357 |
case <-time.After(10 * time.Second): |
| 358 |
t.Fatalf("Timeout waiting for channel recovery state changes. Reconnecting seen: %t, Open seen: %t", chanReconnectingSeen, chanOpenSeen) |
| 359 |
} |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
func waitForChannelClose(t *testing.T, chanStateChanged chan *StateChanged, chanID int, reason string) { |
| 364 |
var chanClosedSeen bool |
| 365 |
var chanReconnectingSeen bool |
| 366 |
var chanOpenSeen bool |
| 367 |
|
| 368 |
loopDeadline := time.After(3 * time.Second) |
| 369 |
for { |
| 370 |
select { |
| 371 |
case sc, ok := <-chanStateChanged: |
| 372 |
if !ok { |
| 373 |
if !chanClosedSeen { |
| 374 |
t.Fatalf("Expected Channel %d to transition to StateClosed before channel closed", chanID) |
| 375 |
} |
| 376 |
if chanReconnectingSeen || chanOpenSeen { |
| 377 |
t.Fatalf("Channel %d recovery was triggered for %s!", chanID, reason) |
| 378 |
} |
| 379 |
return |
| 380 |
} |
| 381 |
t.Logf("Channel %d state changed: %s", chanID, sc) |
| 382 |
if sc.To == StateClosed { |
| 383 |
chanClosedSeen = true |
| 384 |
} |
| 385 |
if sc.To == StateReconnecting { |
| 386 |
chanReconnectingSeen = true |
| 387 |
} |
| 388 |
if sc.To == StateOpen { |
| 389 |
chanOpenSeen = true |
| 390 |
} |
| 391 |
case <-loopDeadline: |
| 392 |
if !chanClosedSeen { |
| 393 |
t.Fatalf("Expected Channel %d to transition to StateClosed", chanID) |
| 394 |
} |
| 395 |
if chanReconnectingSeen || chanOpenSeen { |
| 396 |
t.Fatalf("Channel %d recovery was triggered for %s!", chanID, reason) |
| 397 |
} |
| 398 |
return |
| 399 |
} |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
func waitForStateChangeClose(t *testing.T, ch chan *StateChanged, name, listenerName string) { |
| 404 |
done := make(chan struct{}) |
| 405 |
go func() { |
| 406 |
for range ch { |
| 407 |
} |
| 408 |
close(done) |
| 409 |
}() |
| 410 |
select { |
| 411 |
case <-done: |
| 412 |
t.Logf("State change channel for %s %s cleanly closed", name, listenerName) |
| 413 |
case <-time.After(5 * time.Second): |
| 414 |
t.Fatalf("Timeout waiting for state change channel for %s %s to close", name, listenerName) |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
func TestConnectionRecoveryNonRecoverableChannelClose(t *testing.T) { |
| 423 |
connectionName := "test-non-recoverable-channel-close" |
| 424 |
properties := NewConnectionProperties() |
| 425 |
properties.SetClientConnectionName(connectionName) |
| 426 |
conn, err := DialConfig(amqpURL, Config{ |
| 427 |
Recovery: &Recovery{}, |
| 428 |
Locale: defaultLocale, |
| 429 |
Properties: properties, |
| 430 |
}) |
| 431 |
if err != nil { |
| 432 |
t.Fatalf("DialConfig failed: %v", err) |
| 433 |
} |
| 434 |
defer conn.Close() |
| 435 |
|
| 436 |
|
| 437 |
ch1, err := conn.Channel() |
| 438 |
if err != nil { |
| 439 |
t.Fatalf("First Channel creation failed: %v", err) |
| 440 |
} |
| 441 |
defer ch1.Close() |
| 442 |
|
| 443 |
chanStateChanged1 := make(chan *StateChanged, 10) |
| 444 |
ch1.NotifyStateChange(chanStateChanged1) |
| 445 |
|
| 446 |
queueNamePrecondition := "precondition_failed_test_queue" |
| 447 |
_, _ = ch1.QueueDelete(queueNamePrecondition, false, false, false) |
| 448 |
|
| 449 |
|
| 450 |
_, err = ch1.QueueDeclare( |
| 451 |
queueNamePrecondition, |
| 452 |
true, |
| 453 |
false, |
| 454 |
false, |
| 455 |
false, |
| 456 |
nil, |
| 457 |
) |
| 458 |
if err != nil { |
| 459 |
t.Fatalf("QueueDeclare durable:true failed: %v", err) |
| 460 |
} |
| 461 |
defer func() { |
| 462 |
|
| 463 |
if !conn.IsClosed() { |
| 464 |
if cleanCh, err := conn.Channel(); err == nil { |
| 465 |
_, _ = cleanCh.QueueDelete(queueNamePrecondition, false, false, false) |
| 466 |
cleanCh.Close() |
| 467 |
} |
| 468 |
} |
| 469 |
}() |
| 470 |
|
| 471 |
|
| 472 |
_, err = ch1.QueueDeclare( |
| 473 |
queueNamePrecondition, |
| 474 |
false, |
| 475 |
false, |
| 476 |
false, |
| 477 |
false, |
| 478 |
nil, |
| 479 |
) |
| 480 |
if err == nil { |
| 481 |
t.Fatalf("Expected PreconditionFailed error but second QueueDeclare succeeded") |
| 482 |
} |
| 483 |
|
| 484 |
amqpErr, ok := err.(*Error) |
| 485 |
if !ok { |
| 486 |
t.Fatalf("Expected amqp.Error, got: %T (%v)", err, err) |
| 487 |
} |
| 488 |
if amqpErr.Code != PreconditionFailed { |
| 489 |
t.Fatalf("Expected PreconditionFailed (406), got code: %d", amqpErr.Code) |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
waitForChannelClose(t, chanStateChanged1, int(ch1.id), "PreconditionFailed") |
| 494 |
|
| 495 |
if conn.IsClosed() { |
| 496 |
t.Fatalf("Expected connection to remain open after PRECONDITION_FAILED soft exception") |
| 497 |
} |
| 498 |
t.Log("Verified connection remains open after PRECONDITION_FAILED") |
| 499 |
|
| 500 |
|
| 501 |
ch2, err := conn.Channel() |
| 502 |
if err != nil { |
| 503 |
t.Fatalf("Second Channel creation failed: %v", err) |
| 504 |
} |
| 505 |
defer ch2.Close() |
| 506 |
|
| 507 |
chanStateChanged2 := make(chan *StateChanged, 10) |
| 508 |
ch2.NotifyStateChange(chanStateChanged2) |
| 509 |
|
| 510 |
queueNameResource := "resource_locked_test_queue" |
| 511 |
_, _ = ch2.QueueDelete(queueNameResource, false, false, false) |
| 512 |
|
| 513 |
|
| 514 |
_, err = ch2.QueueDeclare( |
| 515 |
queueNameResource, |
| 516 |
false, |
| 517 |
true, |
| 518 |
true, |
| 519 |
false, |
| 520 |
nil, |
| 521 |
) |
| 522 |
if err != nil { |
| 523 |
t.Fatalf("QueueDeclare exclusive:true failed: %v", err) |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
_, err = ch2.QueueDeclare( |
| 528 |
queueNameResource, |
| 529 |
false, |
| 530 |
true, |
| 531 |
false, |
| 532 |
false, |
| 533 |
nil, |
| 534 |
) |
| 535 |
if err == nil { |
| 536 |
t.Fatalf("Expected ResourceLocked error but second QueueDeclare succeeded") |
| 537 |
} |
| 538 |
|
| 539 |
amqpErr, ok = err.(*Error) |
| 540 |
if !ok { |
| 541 |
t.Fatalf("Expected amqp.Error, got: %T (%v)", err, err) |
| 542 |
} |
| 543 |
if amqpErr.Code != ResourceLocked { |
| 544 |
t.Fatalf("Expected ResourceLocked (405), got code: %d", amqpErr.Code) |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
waitForChannelClose(t, chanStateChanged2, int(ch2.id), "ResourceLocked") |
| 549 |
|
| 550 |
if conn.IsClosed() { |
| 551 |
t.Fatalf("Expected connection to remain open after RESOURCE_LOCKED soft exception") |
| 552 |
} |
| 553 |
t.Log("Verified connection remains open after RESOURCE_LOCKED") |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
func TestConnectionRecoveryChannelIDReservation(t *testing.T) { |
| 560 |
connectionName := "test-channel-id-reservation-recovery" |
| 561 |
properties := NewConnectionProperties() |
| 562 |
properties.SetClientConnectionName(connectionName) |
| 563 |
conn, err := DialConfig(amqpURL, Config{ |
| 564 |
Recovery: &Recovery{}, |
| 565 |
Locale: defaultLocale, |
| 566 |
Properties: properties, |
| 567 |
}) |
| 568 |
if err != nil { |
| 569 |
t.Fatalf("DialConfig failed: %v", err) |
| 570 |
} |
| 571 |
defer conn.Close() |
| 572 |
|
| 573 |
|
| 574 |
ch1, err := conn.Channel() |
| 575 |
if err != nil { |
| 576 |
t.Fatalf("Channel 1 creation failed: %v", err) |
| 577 |
} |
| 578 |
defer ch1.Close() |
| 579 |
|
| 580 |
ch2, err := conn.Channel() |
| 581 |
if err != nil { |
| 582 |
t.Fatalf("Channel 2 creation failed: %v", err) |
| 583 |
} |
| 584 |
defer ch2.Close() |
| 585 |
|
| 586 |
ch3, err := conn.Channel() |
| 587 |
if err != nil { |
| 588 |
t.Fatalf("Channel 3 creation failed: %v", err) |
| 589 |
} |
| 590 |
defer ch3.Close() |
| 591 |
|
| 592 |
ch4, err := conn.Channel() |
| 593 |
if err != nil { |
| 594 |
t.Fatalf("Channel 4 creation failed: %v", err) |
| 595 |
} |
| 596 |
defer ch4.Close() |
| 597 |
|
| 598 |
id1, id2, id3, id4 := ch1.id, ch2.id, ch3.id, ch4.id |
| 599 |
t.Logf("Opened channels with IDs: %d, %d, %d, %d", id1, id2, id3, id4) |
| 600 |
|
| 601 |
|
| 602 |
if err := ch2.Close(); err != nil { |
| 603 |
t.Fatalf("Failed to close channel 2: %v", err) |
| 604 |
} |
| 605 |
if err := ch4.Close(); err != nil { |
| 606 |
t.Fatalf("Failed to close channel 4: %v", err) |
| 607 |
} |
| 608 |
t.Logf("Closed channels with IDs: %d, %d", id2, id4) |
| 609 |
|
| 610 |
|
| 611 |
stateChanged := make(chan *StateChanged, 10) |
| 612 |
conn.NotifyStateChange(stateChanged) |
| 613 |
|
| 614 |
chanStateChanged1 := make(chan *StateChanged, 10) |
| 615 |
ch1.NotifyStateChange(chanStateChanged1) |
| 616 |
|
| 617 |
chanStateChanged3 := make(chan *StateChanged, 10) |
| 618 |
ch3.NotifyStateChange(chanStateChanged3) |
| 619 |
|
| 620 |
|
| 621 |
dropConnection(t, connectionName) |
| 622 |
|
| 623 |
|
| 624 |
waitForConnectionOpen(t, stateChanged) |
| 625 |
waitForChannelOpen(t, chanStateChanged1) |
| 626 |
waitForChannelOpen(t, chanStateChanged3) |
| 627 |
|
| 628 |
|
| 629 |
conn.m.Lock() |
| 630 |
if conn.allocator == nil { |
| 631 |
conn.m.Unlock() |
| 632 |
t.Fatalf("Expected allocator to be initialized post-recovery") |
| 633 |
} |
| 634 |
isReserved1 := conn.allocator.reserved(int(id1)) |
| 635 |
isReserved3 := conn.allocator.reserved(int(id3)) |
| 636 |
isReserved2 := conn.allocator.reserved(int(id2)) |
| 637 |
isReserved4 := conn.allocator.reserved(int(id4)) |
| 638 |
conn.m.Unlock() |
| 639 |
|
| 640 |
if !isReserved1 { |
| 641 |
t.Fatalf("Expected recovered channel ID %d to be reserved in the allocator", id1) |
| 642 |
} |
| 643 |
if !isReserved3 { |
| 644 |
t.Fatalf("Expected recovered channel ID %d to be reserved in the allocator", id3) |
| 645 |
} |
| 646 |
if isReserved2 { |
| 647 |
t.Fatalf("Expected closed channel ID %d to NOT be reserved in the allocator", id2) |
| 648 |
} |
| 649 |
if isReserved4 { |
| 650 |
t.Fatalf("Expected closed channel ID %d to NOT be reserved in the allocator", id4) |
| 651 |
} |
| 652 |
t.Logf("Verified channel IDs %d and %d are correctly reserved, and %d and %d are free in the allocator", id1, id3, id2, id4) |
| 653 |
|
| 654 |
|
| 655 |
var activeChannels []*Channel |
| 656 |
for i := 0; i < 5; i++ { |
| 657 |
ch, err := conn.Channel() |
| 658 |
if err != nil { |
| 659 |
t.Fatalf("Failed to create channel during churn at iteration %d: %v", i, err) |
| 660 |
} |
| 661 |
activeChannels = append(activeChannels, ch) |
| 662 |
t.Logf("Opened new channel with ID: %d", ch.id) |
| 663 |
|
| 664 |
if ch.id == id1 || ch.id == id3 { |
| 665 |
t.Fatalf("Conflict detected! New channel allocated with recovered channel ID: %d", ch.id) |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
for _, ch := range activeChannels { |
| 671 |
ch.Close() |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
func TestConnectionRecoveryLifeCycleNotifyStateChange(t *testing.T) { |
| 678 |
connectionName := "test-connection-recovery-lifecycle-notify-state-change" |
| 679 |
|
| 680 |
properties := NewConnectionProperties() |
| 681 |
properties.SetClientConnectionName(connectionName) |
| 682 |
conn, err := DialConfig(amqpURL, Config{ |
| 683 |
Recovery: &Recovery{}, |
| 684 |
Locale: defaultLocale, |
| 685 |
Properties: properties, |
| 686 |
}) |
| 687 |
if err != nil { |
| 688 |
t.Fatalf("DialConfig failed: %v", err) |
| 689 |
} |
| 690 |
defer conn.Close() |
| 691 |
|
| 692 |
ch1Name := "channel 1" |
| 693 |
ch1, err := conn.Channel() |
| 694 |
if err != nil { |
| 695 |
t.Fatalf("%s creation failed: %v", ch1Name, err) |
| 696 |
} |
| 697 |
defer ch1.Close() |
| 698 |
|
| 699 |
ch2Name := "channel 2" |
| 700 |
ch2, err := conn.Channel() |
| 701 |
if err != nil { |
| 702 |
t.Fatalf("%s creation failed: %v", ch2Name, err) |
| 703 |
} |
| 704 |
defer ch2.Close() |
| 705 |
|
| 706 |
|
| 707 |
stateChanged := make(chan *StateChanged, 10) |
| 708 |
conn.NotifyStateChange(stateChanged) |
| 709 |
|
| 710 |
chanStateChanged11 := make(chan *StateChanged, 10) |
| 711 |
ch1.NotifyStateChange(chanStateChanged11) |
| 712 |
|
| 713 |
|
| 714 |
chanStateChanged12 := make(chan *StateChanged, 10) |
| 715 |
ch1.NotifyStateChange(chanStateChanged12) |
| 716 |
|
| 717 |
chanStateChanged21 := make(chan *StateChanged, 10) |
| 718 |
ch2.NotifyStateChange(chanStateChanged21) |
| 719 |
|
| 720 |
|
| 721 |
chanStateChanged22 := make(chan *StateChanged, 10) |
| 722 |
ch2.NotifyStateChange(chanStateChanged22) |
| 723 |
|
| 724 |
|
| 725 |
dropConnection(t, connectionName) |
| 726 |
|
| 727 |
|
| 728 |
waitForConnectionOpen(t, stateChanged) |
| 729 |
waitForChannelOpen(t, chanStateChanged11) |
| 730 |
waitForChannelOpen(t, chanStateChanged21) |
| 731 |
|
| 732 |
|
| 733 |
if err := ch1.Close(); err != nil { |
| 734 |
t.Fatalf("Failed to close %s: %v", ch1Name, err) |
| 735 |
} |
| 736 |
if err := ch2.Close(); err != nil { |
| 737 |
t.Fatalf("Failed to close %s: %v", ch2Name, err) |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
waitForStateChangeClose(t, chanStateChanged11, ch1Name, "listener 1") |
| 742 |
|
| 743 |
|
| 744 |
waitForStateChangeClose(t, chanStateChanged12, ch1Name, "listener 2") |
| 745 |
|
| 746 |
|
| 747 |
waitForStateChangeClose(t, chanStateChanged21, ch2Name, "listener 1") |
| 748 |
|
| 749 |
|
| 750 |
waitForStateChangeClose(t, chanStateChanged22, ch2Name, "listener 2") |
| 751 |
|
| 752 |
|
| 753 |
if err := conn.Close(); err != nil { |
| 754 |
t.Fatalf("Failed to close %s: %v", connectionName, err) |
| 755 |
} |
| 756 |
|
| 757 |
|
| 758 |
waitForStateChangeClose(t, stateChanged, connectionName, "listener 1") |
| 759 |
} |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
func TestConnectionRecoveryCancelInterrupt(t *testing.T) { |
| 764 |
connectionName := "test-connection-recovery-cancel-interrupt" |
| 765 |
properties := NewConnectionProperties() |
| 766 |
properties.SetClientConnectionName(connectionName) |
| 767 |
conn, err := DialConfig(amqpURL, Config{ |
| 768 |
Recovery: &Recovery{ |
| 769 |
ReconnectionConfig: &ReconnectionConfig{ |
| 770 |
MaxRetryCount: 5, |
| 771 |
RetryInterval: 5 * time.Second, |
| 772 |
}, |
| 773 |
}, |
| 774 |
Locale: defaultLocale, |
| 775 |
Properties: properties, |
| 776 |
}) |
| 777 |
if err != nil { |
| 778 |
t.Fatalf("DialConfig failed: %v", err) |
| 779 |
} |
| 780 |
defer conn.Close() |
| 781 |
|
| 782 |
ch, err := conn.Channel() |
| 783 |
if err != nil { |
| 784 |
t.Fatalf("Channel creation failed: %v", err) |
| 785 |
} |
| 786 |
defer ch.Close() |
| 787 |
|
| 788 |
|
| 789 |
connCancelCh := conn.NotifyRecoveryCancel(make(chan struct{})) |
| 790 |
chCancelCh := ch.NotifyRecoveryCancel(make(chan struct{})) |
| 791 |
|
| 792 |
|
| 793 |
connStateCh := make(chan *StateChanged, 10) |
| 794 |
conn.NotifyStateChange(connStateCh) |
| 795 |
|
| 796 |
chanStateCh := make(chan *StateChanged, 10) |
| 797 |
ch.NotifyStateChange(chanStateCh) |
| 798 |
|
| 799 |
|
| 800 |
dropConnection(t, connectionName) |
| 801 |
|
| 802 |
|
| 803 |
var connReconnectingSeen bool |
| 804 |
var chanReconnectingSeen bool |
| 805 |
|
| 806 |
timeout := time.After(10 * time.Second) |
| 807 |
for !connReconnectingSeen || !chanReconnectingSeen { |
| 808 |
select { |
| 809 |
case sc := <-connStateCh: |
| 810 |
t.Logf("Connection state changed: %s", sc) |
| 811 |
if sc.To == StateReconnecting { |
| 812 |
connReconnectingSeen = true |
| 813 |
} |
| 814 |
case sc := <-chanStateCh: |
| 815 |
t.Logf("Channel state changed: %s", sc) |
| 816 |
if sc.To == StateReconnecting { |
| 817 |
chanReconnectingSeen = true |
| 818 |
} |
| 819 |
case <-timeout: |
| 820 |
t.Fatalf("Timeout waiting for connection and channel to enter Reconnecting state") |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
|
| 825 |
t.Log("Closing channel during recovery to trigger abort...") |
| 826 |
if err := ch.Close(); err != nil { |
| 827 |
t.Fatalf("Channel Close failed: %v", err) |
| 828 |
} |
| 829 |
|
| 830 |
|
| 831 |
t.Log("Closing connection during recovery to trigger abort...") |
| 832 |
if err := conn.Close(); err != nil { |
| 833 |
t.Fatalf("Connection Close failed: %v", err) |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
select { |
| 838 |
case <-chCancelCh: |
| 839 |
t.Log("Channel recovery cancel event received successfully") |
| 840 |
case <-time.After(2 * time.Second): |
| 841 |
t.Error("Timeout waiting for channel recovery cancel event") |
| 842 |
} |
| 843 |
|
| 844 |
select { |
| 845 |
case <-connCancelCh: |
| 846 |
t.Log("Connection recovery cancel event received successfully") |
| 847 |
case <-time.After(2 * time.Second): |
| 848 |
t.Error("Timeout waiting for connection recovery cancel event") |
| 849 |
} |
| 850 |
|
| 851 |
|
| 852 |
var connClosedSeen bool |
| 853 |
var chanClosedSeen bool |
| 854 |
|
| 855 |
timeout = time.After(2 * time.Second) |
| 856 |
for !connClosedSeen || !chanClosedSeen { |
| 857 |
select { |
| 858 |
case sc, ok := <-connStateCh: |
| 859 |
if !ok { |
| 860 |
connClosedSeen = true |
| 861 |
continue |
| 862 |
} |
| 863 |
t.Logf("Connection state changed post-close: %s", sc) |
| 864 |
if sc.To == StateClosed { |
| 865 |
connClosedSeen = true |
| 866 |
} |
| 867 |
case sc, ok := <-chanStateCh: |
| 868 |
if !ok { |
| 869 |
chanClosedSeen = true |
| 870 |
continue |
| 871 |
} |
| 872 |
t.Logf("Channel state changed post-close: %s", sc) |
| 873 |
if sc.To == StateClosed { |
| 874 |
chanClosedSeen = true |
| 875 |
} |
| 876 |
case <-timeout: |
| 877 |
t.Fatalf("Timeout waiting for connection and channel state to change to Closed. connClosedSeen=%t, chanClosedSeen=%t", connClosedSeen, chanClosedSeen) |
| 878 |
} |
| 879 |
} |
| 880 |
} |