11/* 
2-  * Copyright 2002-2014  the original author or authors. 
2+  * Copyright 2002-2016  the original author or authors. 
33 * 
44 * Licensed under the Apache License, Version 2.0 (the "License"); 
55 * you may not use this file except in compliance with the License. 
1616
1717package  org .springframework .jms .listener ;
1818
19+ import  java .util .concurrent .CountDownLatch ;
20+ import  java .util .concurrent .TimeUnit ;
1921import  javax .jms .Connection ;
2022import  javax .jms .ConnectionFactory ;
2123import  javax .jms .Destination ;
@@ -39,55 +41,76 @@ public class DefaultMessageListenerContainerTests {
3941
4042	@ Test 
4143	public  void  applyBackOff () {
42- 		BackOff  mock  = mock (BackOff .class );
44+ 		BackOff  backOff  = mock (BackOff .class );
4345		BackOffExecution  execution  = mock (BackOffExecution .class );
4446		given (execution .nextBackOff ()).willReturn (BackOffExecution .STOP );
45- 		given (mock .start ()).willReturn (execution );
47+ 		given (backOff .start ()).willReturn (execution );
4648
47- 		DefaultMessageListenerContainer  container  = createContainer (mock , createFailingContainerFactory ());
49+ 		DefaultMessageListenerContainer  container  = createContainer (createFailingContainerFactory ());
50+ 		container .setBackOff (backOff );
4851		container .start ();
4952		assertEquals (true , container .isRunning ());
5053
5154		container .refreshConnectionUntilSuccessful ();
5255
5356		assertEquals (false , container .isRunning ());
54- 		verify (mock ).start ();
57+ 		verify (backOff ).start ();
5558		verify (execution ).nextBackOff ();
5659	}
5760
5861	@ Test 
5962	public  void  applyBackOffRetry () {
60- 		BackOff  mock  = mock (BackOff .class );
63+ 		BackOff  backOff  = mock (BackOff .class );
6164		BackOffExecution  execution  = mock (BackOffExecution .class );
6265		given (execution .nextBackOff ()).willReturn (50L , BackOffExecution .STOP );
63- 		given (mock .start ()).willReturn (execution );
66+ 		given (backOff .start ()).willReturn (execution );
6467
65- 		DefaultMessageListenerContainer  container  = createContainer (mock , createFailingContainerFactory ());
68+ 		DefaultMessageListenerContainer  container  = createContainer (createFailingContainerFactory ());
69+ 		container .setBackOff (backOff );
6670		container .start ();
6771		container .refreshConnectionUntilSuccessful ();
6872
6973		assertEquals (false , container .isRunning ());
70- 		verify (mock ).start ();
74+ 		verify (backOff ).start ();
7175		verify (execution , times (2 )).nextBackOff ();
7276	}
7377
7478	@ Test 
7579	public  void  recoverResetBackOff () {
76- 		BackOff  mock  = mock (BackOff .class );
80+ 		BackOff  backOff  = mock (BackOff .class );
7781		BackOffExecution  execution  = mock (BackOffExecution .class );
7882		given (execution .nextBackOff ()).willReturn (50L , 50L , 50L ); // 3 attempts max 
79- 		given (mock .start ()).willReturn (execution );
83+ 		given (backOff .start ()).willReturn (execution );
8084
81- 		DefaultMessageListenerContainer  container  = createContainer (mock , createRecoverableContainerFactory (1 ));
85+ 		DefaultMessageListenerContainer  container  = createContainer (createRecoverableContainerFactory (1 ));
86+ 		container .setBackOff (backOff );
8287		container .start ();
8388		container .refreshConnectionUntilSuccessful ();
8489
8590		assertEquals (true , container .isRunning ());
86- 		verify (mock ).start ();
91+ 		verify (backOff ).start ();
8792		verify (execution , times (1 )).nextBackOff (); // only on attempt as the second one lead to a recovery 
8893	}
8994
90- 	private  DefaultMessageListenerContainer  createContainer (BackOff  backOff , ConnectionFactory  connectionFactory ) {
95+ 	@ Test 
96+ 	public  void  runnableIsInvokedEvenIfContainerIsNotRunning () throws  InterruptedException  {
97+ 		DefaultMessageListenerContainer  container  = createRunningContainer ();
98+ 		container .stop ();
99+ 
100+ 		// container is stopped but should nevertheless invoke the runnable argument 
101+ 		TestRunnable  runnable2  = new  TestRunnable ();
102+ 		container .stop (runnable2 );
103+ 		runnable2 .waitForCompletion ();
104+ 	}
105+ 
106+ 	private  DefaultMessageListenerContainer  createRunningContainer () {
107+ 		DefaultMessageListenerContainer  container  = createContainer (createSuccessfulConnectionFactory ());
108+ 		container .afterPropertiesSet ();
109+ 		container .start ();
110+ 		return  container ;
111+ 	}
112+ 
113+ 	private  DefaultMessageListenerContainer  createContainer (ConnectionFactory  connectionFactory ) {
91114
92115		Destination  destination  = new  Destination () {};
93116
@@ -96,9 +119,7 @@ private DefaultMessageListenerContainer createContainer(BackOff backOff, Connect
96119		container .setConnectionFactory (connectionFactory );
97120		container .setCacheLevel (DefaultMessageListenerContainer .CACHE_NONE );
98121		container .setDestination (destination );
99- 		container .setBackOff (backOff );
100122		return  container ;
101- 
102123	}
103124
104125	private  ConnectionFactory  createFailingContainerFactory () {
@@ -141,4 +162,30 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
141162		}
142163	}
143164
165+ 	private  ConnectionFactory  createSuccessfulConnectionFactory () {
166+ 		try  {
167+ 			ConnectionFactory  connectionFactory  = mock (ConnectionFactory .class );
168+ 			given (connectionFactory .createConnection ()).willReturn (mock (Connection .class ));
169+ 			return  connectionFactory ;
170+ 		}
171+ 		catch  (JMSException  e ) {
172+ 			throw  new  IllegalStateException (); // never happen 
173+ 		}
174+ 	}
175+ 
176+ 	private  static  class  TestRunnable  implements  Runnable  {
177+ 		private  final  CountDownLatch  countDownLatch  = new  CountDownLatch (1 );
178+ 
179+ 		@ Override 
180+ 		public  void  run () {
181+ 			this .countDownLatch .countDown ();
182+ 		}
183+ 
184+ 		public  void  waitForCompletion () throws  InterruptedException  {
185+ 			this .countDownLatch .await (2 , TimeUnit .SECONDS );
186+ 			assertEquals ("callback was not invoked" , 0 , this .countDownLatch .getCount ());
187+ 		}
188+ 
189+ 	}
190+ 
144191}
0 commit comments