@@ -12,10 +12,11 @@ import { RabbitMQSetupConfig } from './types/rabbitmq-setup.types';
1212class GlobalRabbitMQSetup {
1313 private containerManager = new RabbitMQContainerManager ( ) ;
1414 private connectionManager = new RabbitMQConnectionManager ( ) ;
15- private queueManager = new RabbitMQQueueManager ( this . connectionManager ) ;
16- private spyConsumerManager = new RabbitMQSpyConsumerManager ( this . connectionManager ) ;
15+ private queueManager : RabbitMQQueueManager | null = null ;
16+ private spyConsumerManager : RabbitMQSpyConsumerManager | null = null ;
1717 private eventCollector = new EventCollector ( ) ;
1818 private isStarted = false ;
19+ private amqpUrl : string | null = null ;
1920
2021 async getOrCreateSetup ( ) : Promise < RabbitMQSetupConfig > {
2122 if ( this . isStarted ) {
@@ -33,6 +34,12 @@ class GlobalRabbitMQSetup {
3334 // Setup container and connection
3435 const container = await this . containerManager . getContainer ( ) ;
3536 const amqpUrl = container . getAmqpUrl ( ) ;
37+ this . amqpUrl = amqpUrl ;
38+
39+ // Initialize managers with the correct URL
40+ this . queueManager = new RabbitMQQueueManager ( this . connectionManager , amqpUrl ) ;
41+ this . spyConsumerManager = new RabbitMQSpyConsumerManager ( this . connectionManager , amqpUrl ) ;
42+
3643 await this . connectionManager . initialize ( amqpUrl ) ;
3744
3845 // Setup initial queues and spy consumers
@@ -50,22 +57,30 @@ class GlobalRabbitMQSetup {
5057 }
5158
5259 private async setupInitialQueues ( ) : Promise < void > {
53- await this . queueManager . clearQueue ( 'dispatcher-queue' ) ;
54- await this . spyConsumerManager . setupSpyConsumer ( {
55- queueName : 'dispatcher-queue' ,
56- eventCollector : this . eventCollector
57- } ) ;
60+ if ( this . queueManager && this . spyConsumerManager ) {
61+ await this . queueManager . clearQueue ( 'dispatcher-queue' ) ;
62+ await this . spyConsumerManager . setupSpyConsumer ( {
63+ queueName : 'dispatcher-queue' ,
64+ eventCollector : this . eventCollector
65+ } ) ;
66+ }
5867 }
5968
6069 async globalCleanup ( ) : Promise < void > {
6170 if ( ! this . isStarted ) return ;
6271
63- await this . spyConsumerManager . cleanup ( ) ;
72+ if ( this . spyConsumerManager ) {
73+ await this . spyConsumerManager . cleanup ( ) ;
74+ }
75+ if ( this . queueManager ) {
76+ await this . queueManager . cleanup ( ) ;
77+ }
6478 await this . connectionManager . cleanup ( ) ;
6579 await this . containerManager . cleanup ( ) ;
6680
6781 this . isStarted = false ;
6882 this . eventCollector . clear ( ) ;
83+ this . amqpUrl = null ;
6984 }
7085}
7186
@@ -101,8 +116,8 @@ export class RabbitMQTestSetup {
101116 this . connectionManager = new RabbitMQConnectionManager ( ) ;
102117 await this . connectionManager . initialize ( amqpUrl ) ;
103118
104- this . queueManager = new RabbitMQQueueManager ( this . connectionManager ) ;
105- this . spyConsumerManager = new RabbitMQSpyConsumerManager ( this . connectionManager ) ;
119+ this . queueManager = new RabbitMQQueueManager ( this . connectionManager , amqpUrl ) ;
120+ this . spyConsumerManager = new RabbitMQSpyConsumerManager ( this . connectionManager , amqpUrl ) ;
106121
107122 // Setup spy consumer for dispatcher queue
108123 await this . spyConsumerManager . setupSpyConsumer ( {
@@ -118,6 +133,7 @@ export class RabbitMQTestSetup {
118133
119134 if ( this . queueManager ) {
120135 await this . queueManager . clearQueue ( 'dispatcher-queue' ) ;
136+ await this . queueManager . cleanup ( ) ;
121137 }
122138
123139 if ( this . spyConsumerManager ) {
0 commit comments