11package redis .clients .jedis ;
22
3+ import static org .hamcrest .MatcherAssert .assertThat ;
4+ import static org .hamcrest .Matchers .equalTo ;
5+ import static org .hamcrest .Matchers .greaterThanOrEqualTo ;
36import static org .junit .Assert .assertEquals ;
47import static org .junit .Assert .assertNull ;
58import static org .junit .Assert .assertTrue ;
69import static org .junit .Assert .fail ;
710
811import java .net .URI ;
912import java .net .URISyntaxException ;
13+ import java .util .concurrent .atomic .AtomicBoolean ;
1014import java .util .concurrent .atomic .AtomicInteger ;
1115import org .apache .commons .pool2 .impl .GenericObjectPoolConfig ;
1216import org .junit .Test ;
@@ -196,9 +200,9 @@ public void testResetValidCredentials() {
196200 public void testCredentialsProvider () {
197201 final AtomicInteger prepareCount = new AtomicInteger ();
198202 final AtomicInteger cleanupCount = new AtomicInteger ();
203+ final AtomicBoolean validPassword = new AtomicBoolean (false );
199204
200205 RedisCredentialsProvider credentialsProvider = new RedisCredentialsProvider () {
201- boolean firstCall = true ;
202206
203207 @ Override
204208 public void prepare () {
@@ -207,8 +211,7 @@ public void prepare() {
207211
208212 @ Override
209213 public RedisCredentials get () {
210- if (firstCall ) {
211- firstCall = false ;
214+ if (!validPassword .get ()) {
212215 return new RedisCredentials () { };
213216 }
214217
@@ -244,12 +247,13 @@ public void cleanUp() {
244247 } catch (JedisException e ) {
245248 }
246249 assertEquals (0 , pool .getPool ().getNumActive () + pool .getPool ().getNumIdle () + pool .getPool ().getNumWaiters ());
247- assertEquals ( 1 , prepareCount .get ( ));
248- assertEquals ( 1 , cleanupCount .get ( ));
250+ assertThat ( prepareCount .getAndSet ( 0 ), greaterThanOrEqualTo ( 1 ));
251+ assertThat ( cleanupCount .getAndSet ( 0 ), greaterThanOrEqualTo ( 1 ));
249252
253+ validPassword .set (true );
250254 assertNull (pool .get ("foo" ));
251- assertEquals ( 2 , prepareCount .get ());
252- assertEquals ( 2 , cleanupCount .get ());
255+ assertThat ( prepareCount .get (), equalTo ( 1 ));
256+ assertThat ( cleanupCount .get (), equalTo ( 1 ));
253257 }
254258 }
255259}
0 commit comments