@@ -17,6 +17,7 @@ describe("RabbitMQ Single Node", {
1717 rabbitmqNodes : JSON . stringify ( [ connectionString ] ) ,
1818 rabbitmqUsername : "guest" ,
1919 rabbitmqPassword : "guest" ,
20+ timeout : 10 ,
2021 } ;
2122
2223 const heartbeat = {
@@ -27,7 +28,7 @@ describe("RabbitMQ Single Node", {
2728 try {
2829 await rabbitMQMonitor . check ( monitor , heartbeat , { } ) ;
2930 assert . strictEqual ( heartbeat . status , UP ) ;
30- assert . strictEqual ( heartbeat . msg , "OK " ) ;
31+ assert . strictEqual ( heartbeat . msg , "Node is reachable and there are no alerts in the cluster " ) ;
3132 } finally {
3233 rabbitMQContainer . stop ( ) ;
3334 }
@@ -39,6 +40,7 @@ describe("RabbitMQ Single Node", {
3940 rabbitmqNodes : JSON . stringify ( [ "http://localhost:15672" ] ) ,
4041 rabbitmqUsername : "rabbitmqUser" ,
4142 rabbitmqPassword : "rabbitmqPass" ,
43+ timeout : 10 ,
4244 } ;
4345
4446 const heartbeat = {
@@ -55,4 +57,193 @@ describe("RabbitMQ Single Node", {
5557 ) ;
5658 } ) ;
5759
60+ test ( "checkSingleNode() succeeds when node is healthy" , async ( ) => {
61+ const rabbitMQContainer = await new RabbitMQContainer ( ) . withStartupTimeout ( 60000 ) . start ( ) ;
62+ const rabbitMQMonitor = new RabbitMqMonitorType ( ) ;
63+ const connectionString = `http://${ rabbitMQContainer . getHost ( ) } :${ rabbitMQContainer . getMappedPort ( 15672 ) } ` ;
64+
65+ const monitor = {
66+ name : "Test Monitor" ,
67+ rabbitmqUsername : "guest" ,
68+ rabbitmqPassword : "guest" ,
69+ timeout : 10 ,
70+ } ;
71+
72+ try {
73+ // Should not throw - just validates the node is healthy
74+ await rabbitMQMonitor . checkSingleNode ( monitor , connectionString , "1/1" ) ;
75+ } finally {
76+ rabbitMQContainer . stop ( ) ;
77+ }
78+ } ) ;
79+
80+ test ( "checkSingleNode() throws error when node is unreachable" , async ( ) => {
81+ const rabbitMQMonitor = new RabbitMqMonitorType ( ) ;
82+ const monitor = {
83+ name : "Test Monitor" ,
84+ rabbitmqUsername : "guest" ,
85+ rabbitmqPassword : "guest" ,
86+ timeout : 10 ,
87+ } ;
88+
89+ // Should reject with any error (connection refused, timeout, etc.)
90+ await assert . rejects (
91+ rabbitMQMonitor . checkSingleNode ( monitor , "http://localhost:15672" , "1/1" ) ,
92+ Error
93+ ) ;
94+ } ) ;
95+ } ) ;
96+
97+ describe ( "RabbitMQ Multi-Node (Mocked)" , ( ) => {
98+ test ( "check() succeeds when first node is healthy" , async ( ) => {
99+ const rabbitMQMonitor = new RabbitMqMonitorType ( ) ;
100+ const monitor = {
101+ rabbitmqNodes : JSON . stringify ( [ "http://node1:15672" , "http://node2:15672" ] ) ,
102+ rabbitmqUsername : "guest" ,
103+ rabbitmqPassword : "guest" ,
104+ timeout : 10 ,
105+ } ;
106+
107+ const heartbeat = {
108+ msg : "" ,
109+ status : PENDING ,
110+ } ;
111+
112+ // Mock checkSingleNode to succeed on first call (just don't throw)
113+ let callCount = 0 ;
114+ rabbitMQMonitor . checkSingleNode = async ( mon , url , nodeInfo ) => {
115+ callCount ++ ;
116+ // Success - don't throw
117+ } ;
118+
119+ await rabbitMQMonitor . check ( monitor , heartbeat , { } ) ;
120+ assert . strictEqual ( heartbeat . status , UP ) ;
121+ assert . strictEqual ( heartbeat . msg , "One of the 2 nodes is reachable and there are no alerts in the cluster" ) ;
122+ assert . strictEqual ( callCount , 1 , "Should only check first node" ) ;
123+ } ) ;
124+
125+ test ( "check() succeeds when second node is healthy after first fails" , async ( ) => {
126+ const rabbitMQMonitor = new RabbitMqMonitorType ( ) ;
127+ const monitor = {
128+ rabbitmqNodes : JSON . stringify ( [ "http://node1:15672" , "http://node2:15672" ] ) ,
129+ rabbitmqUsername : "guest" ,
130+ rabbitmqPassword : "guest" ,
131+ timeout : 10 ,
132+ } ;
133+
134+ const heartbeat = {
135+ msg : "" ,
136+ status : PENDING ,
137+ } ;
138+
139+ // Mock checkSingleNode to fail first, succeed second
140+ let callCount = 0 ;
141+ rabbitMQMonitor . checkSingleNode = async ( mon , url , nodeInfo ) => {
142+ callCount ++ ;
143+ if ( callCount === 1 ) {
144+ throw new Error ( "Node 1 connection failed" ) ;
145+ }
146+ // Second call succeeds - don't throw
147+ } ;
148+
149+ await rabbitMQMonitor . check ( monitor , heartbeat , { } ) ;
150+ assert . strictEqual ( heartbeat . status , UP ) ;
151+ assert . strictEqual ( heartbeat . msg , "One of the 2 nodes is reachable and there are no alerts in the cluster" ) ;
152+ assert . strictEqual ( callCount , 2 , "Should check both nodes" ) ;
153+ } ) ;
154+
155+ test ( "check() fails with consolidated error when all nodes are down" , async ( ) => {
156+ const rabbitMQMonitor = new RabbitMqMonitorType ( ) ;
157+ const monitor = {
158+ rabbitmqNodes : JSON . stringify ( [
159+ "http://node1:15672" ,
160+ "http://node2:15672" ,
161+ "http://node3:15672"
162+ ] ) ,
163+ rabbitmqUsername : "guest" ,
164+ rabbitmqPassword : "guest" ,
165+ timeout : 10 ,
166+ } ;
167+
168+ const heartbeat = {
169+ msg : "" ,
170+ status : PENDING ,
171+ } ;
172+
173+ // Mock checkSingleNode to always fail
174+ let callCount = 0 ;
175+ rabbitMQMonitor . checkSingleNode = async ( mon , url , nodeInfo ) => {
176+ callCount ++ ;
177+ throw new Error ( `Connection failed to node ${ callCount } ` ) ;
178+ } ;
179+
180+ await assert . rejects (
181+ rabbitMQMonitor . check ( monitor , heartbeat , { } ) ,
182+ ( error ) => {
183+ assert . match ( error . message , / A l l 3 n o d e s f a i l e d / ) ;
184+ assert . match ( error . message , / N o d e 1 : / ) ;
185+ assert . match ( error . message , / N o d e 2 : / ) ;
186+ assert . match ( error . message , / N o d e 3 : / ) ;
187+ return true ;
188+ }
189+ ) ;
190+ assert . strictEqual ( callCount , 3 , "Should check all three nodes" ) ;
191+ } ) ;
192+
193+ test ( "check() fails when no nodes are configured" , async ( ) => {
194+ const rabbitMQMonitor = new RabbitMqMonitorType ( ) ;
195+ const monitor = {
196+ rabbitmqNodes : JSON . stringify ( [ ] ) ,
197+ rabbitmqUsername : "guest" ,
198+ rabbitmqPassword : "guest" ,
199+ timeout : 10 ,
200+ } ;
201+
202+ const heartbeat = {
203+ msg : "" ,
204+ status : PENDING ,
205+ } ;
206+
207+ await assert . rejects (
208+ rabbitMQMonitor . check ( monitor , heartbeat , { } ) ,
209+ / N o R a b b i t M Q n o d e s c o n f i g u r e d /
210+ ) ;
211+ } ) ;
212+
213+ test ( "check() tries all nodes before failing" , async ( ) => {
214+ const rabbitMQMonitor = new RabbitMqMonitorType ( ) ;
215+ const monitor = {
216+ rabbitmqNodes : JSON . stringify ( [
217+ "http://node1:15672" ,
218+ "http://node2:15672" ,
219+ "http://node3:15672" ,
220+ "http://node4:15672"
221+ ] ) ,
222+ rabbitmqUsername : "guest" ,
223+ rabbitmqPassword : "guest" ,
224+ timeout : 10 ,
225+ } ;
226+
227+ const heartbeat = {
228+ msg : "" ,
229+ status : PENDING ,
230+ } ;
231+
232+ const checkedNodes = [ ] ;
233+ rabbitMQMonitor . checkSingleNode = async ( mon , url , nodeInfo ) => {
234+ checkedNodes . push ( url ) ;
235+ throw new Error ( `Failed: ${ url } ` ) ;
236+ } ;
237+
238+ await assert . rejects (
239+ rabbitMQMonitor . check ( monitor , heartbeat , { } ) ,
240+ / A l l 4 n o d e s f a i l e d /
241+ ) ;
242+
243+ assert . strictEqual ( checkedNodes . length , 4 , "Should check all 4 nodes" ) ;
244+ assert . strictEqual ( checkedNodes [ 0 ] , "http://node1:15672" ) ;
245+ assert . strictEqual ( checkedNodes [ 1 ] , "http://node2:15672" ) ;
246+ assert . strictEqual ( checkedNodes [ 2 ] , "http://node3:15672" ) ;
247+ assert . strictEqual ( checkedNodes [ 3 ] , "http://node4:15672" ) ;
248+ } ) ;
58249} ) ;
0 commit comments