Skip to content

Commit

Permalink
✅ Add RabbitMQ reconnection test
Browse files Browse the repository at this point in the history
  • Loading branch information
AgusSalvidio committed Aug 5, 2024
1 parent d3b2188 commit 9ad98b5
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 9 deletions.
101 changes: 99 additions & 2 deletions source/Ansible-RabbitMQ-Tests/RabbitMQClientTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ Class {
#package : 'Ansible-RabbitMQ-Tests'
}

{ #category : 'accessing' }
RabbitMQClientTest class >> defaultTimeLimit [

^ ( Socket standardTimeout + 120 ) seconds
]

{ #category : 'private - support' }
RabbitMQClientTest >> addTimestampRegexTo: aLogEntryCollection [

Expand All @@ -34,6 +40,35 @@ RabbitMQClientTest >> anotherRabbitMQPublisher [
]
]

{ #category : 'private - support' }
RabbitMQClientTest >> closeAllConnectionsOf: aRabbitmqContainerId for: aUsername because: aCloseReason [

^ OSPlatform current runCommand:
( 'docker exec <1s> rabbitmqctl close_all_user_connections <2s> <3s>'
expandMacrosWith: aRabbitmqContainerId
with: aUsername
with: aCloseReason )
]

{ #category : 'private - support' }
RabbitMQClientTest >> closeAllUserConnections [

| rabbitmqContainerId closeReason |

rabbitmqContainerId := self rabbitMQContainerID.

closeReason := 'CloseConnectionsTest'.

rabbitmqContainerId isEmpty
then: [ Error signal: 'Could not find a running RabbitMQ container.' ]
otherwise: [
self
closeAllConnectionsOf: rabbitmqContainerId
for: self defaultRabbitMQWorkerUsername
because: closeReason
]
]

{ #category : 'private - accessing' }
RabbitMQClientTest >> defaultRabbitMQPublisher [

Expand All @@ -42,16 +77,39 @@ RabbitMQClientTest >> defaultRabbitMQPublisher [
at: #hostname put: 'localhost';
at: #port put: 5672;
at: #username: put: 'guest';
at: #password put: 'guest'
at: #password put: 'guest';
at: #extraClientProperties put: ( Dictionary new
at: 'process' put: 'RabbitMQClientTest Publisher';
yourself )
]
]

{ #category : 'private - accessing' }
RabbitMQClientTest >> defaultRabbitMQWorkerUsername [

^ AmqpConnectionBuilder usingAMQP091Protocol credentials username
]

{ #category : 'private - support' }
RabbitMQClientTest >> largerWait [

"This delay aims to replicate the time required to successfully close all the RabbitMQ connections."

( Delay forSeconds: 120 ) wait
]

{ #category : 'private - accessing' }
RabbitMQClientTest >> queueName [

^ 'tasks-for-' , testSelector
]

{ #category : 'private - accessing' }
RabbitMQClientTest >> rabbitMQContainerID [

^ ( OSPlatform current resultOfCommand: 'docker ps -q --filter "name=rabbitmq"' ) trim
]

{ #category : 'private - accessing' }
RabbitMQClientTest >> rabbitMQPublisherWithDebuggingLogs [

Expand Down Expand Up @@ -118,7 +176,9 @@ RabbitMQClientTest >> storeText: aString [
{ #category : 'running' }
RabbitMQClientTest >> tearDown [

publisher channel queueDelete: self queueName.
[ publisher channel queueDelete: self queueName ]
on: AmqpDisconnectedError
do: [ :signal | " Handle when tests fail and the channel was never opened" signal return ].
publisher stop.
workerProcess terminate.
super tearDown
Expand Down Expand Up @@ -222,6 +282,43 @@ RabbitMQClientTest >> testPublisherConfirmationWhenMessageProcessed [
] ensure: [ worker stop ]
]

{ #category : 'tests' }
RabbitMQClientTest >> testPublishingMessageWhenConnectionIsTemporallyLost [

self resumeWorkerDuring: [
publisher
publishOnly: 'Hello' onQueueNamed: self queueName;
publishOnly: 'World' onQueueNamed: self queueName.

self wait.

self
assert: reversedTexts size equals: 2;
assert: reversedTexts first equals: 'olleH';
assert: reversedTexts last equals: 'dlroW'.

self
runMemoryLoggerDuring: [
self
closeAllUserConnections;
largerWait.
publisher publishOnly: 'Test connection restored' onQueueNamed: self queueName
]
assertingLogRecordsMatchRegexes:
{ '\[ERROR\] RabbitMQClient disconnected due to Connection closed' .
'\[INFO\] AMQP connection localhost\:(\d+)->localhost\:5672 established successfully' .
'\[ERROR\] AMQP Heartbeat failed unexpectedly \(connection closed while sending data\)\.' .
'\[WARNING\] AMQP connection localhost\:(\d+)->localhost\:5672 hard closed due to connection closed while sending data' .
'\[WARNING\] AMQP connection localhost\:(\d+)->localhost\:5672 hard closed due to connection closed while sending data' .
'\[INFO\] AMQP connection localhost\:(\d+)->localhost\:5672 established successfully' }
].


self
assert: reversedTexts size equals: 3;
assert: reversedTexts last equals: 'derotser noitcennoc tseT'
]

{ #category : 'tests' }
RabbitMQClientTest >> testPublishingMessages [

Expand Down
17 changes: 10 additions & 7 deletions source/Ansible-RabbitMQ-Tests/RabbitMQTextReverser.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ RabbitMQTextReverser class >> workingWith: aTestCase [
{ #category : 'initialization' }
RabbitMQTextReverser >> initializeWorkingWith: aTestCase [

worker := RabbitMQWorker
configuredBy: [ :options |
options
at: #hostname put: 'localhost';
at: #queueName put: aTestCase queueName
]
doingWithPayload: [ :payload | aTestCase storeText: payload utf8Decoded reversed ]
worker := RabbitMQWorker
configuredBy: [ :options |
options
at: #hostname put: 'localhost';
at: #queueName put: aTestCase queueName;
at: #extraClientProperties put: ( Dictionary new
at: 'process' put: 'Text Reverser Worker';
yourself )
]
doingWithPayload: [ :payload | aTestCase storeText: payload utf8Decoded reversed ]
]

{ #category : 'accessing' }
Expand Down

0 comments on commit 9ad98b5

Please sign in to comment.