99import org .apache .commons .pool2 .impl .DefaultPooledObject ;
1010import org .apache .commons .pool2 .impl .GenericObjectPoolConfig ;
1111
12+ import org .junit .jupiter .api .BeforeEach ;
1213import org .junit .jupiter .api .Tag ;
1314import org .junit .jupiter .api .Test ;
15+ import org .junit .jupiter .api .TestInfo ;
1416import redis .clients .jedis .exceptions .InvalidURIException ;
1517import redis .clients .jedis .exceptions .JedisAccessControlException ;
1618import redis .clients .jedis .exceptions .JedisConnectionException ;
1719import redis .clients .jedis .exceptions .JedisException ;
1820
1921import static java .util .concurrent .TimeUnit .MILLISECONDS ;
2022import static org .awaitility .Awaitility .await ;
23+ import static org .hamcrest .MatcherAssert .assertThat ;
24+ import static org .hamcrest .Matchers .equalTo ;
2125import static org .junit .jupiter .api .Assertions .assertEquals ;
2226import static org .junit .jupiter .api .Assertions .assertInstanceOf ;
2327import static org .junit .jupiter .api .Assertions .assertNull ;
@@ -33,6 +37,15 @@ public class JedisPoolTest {
3337
3438private static final EndpointConfig endpointStandalone1 =HostAndPorts .getRedisEndpoint ("standalone1" );
3539
40+ private String testKey ;
41+ private String testValue ;
42+
43+ @ BeforeEach
44+ public void setUpTestKey (TestInfo testInfo ) {
45+ testKey =testInfo .getDisplayName () +"-key" ;
46+ testValue =testInfo .getDisplayName () +"-value" ;
47+ }
48+
3649@ Test
3750public void checkConnections () {
3851JedisPool pool =new JedisPool (new JedisPoolConfig (),endpointStandalone0 .getHost (),endpointStandalone0 .getPort (),2000 );
@@ -462,4 +475,64 @@ public void testResetValidCredentials() {
462475 }
463476 }
464477 }
478+
479+ @ Test
480+ public void testWithResource () {
481+ try (JedisPool pool =new JedisPool (new JedisPoolConfig (),endpointStandalone0 .getHostAndPort (),
482+ endpointStandalone0 .getClientConfigBuilder ().build ())) {
483+
484+ pool .withResource (jedis -> {
485+ jedis .set (testKey ,testValue );
486+ });
487+
488+ pool .withResource (jedis -> {
489+ assertEquals (testValue ,jedis .get (testKey ));
490+ });
491+ }
492+ }
493+
494+ @ Test
495+ public void testWithResourceReturnsConnectionToPool () {
496+ try (JedisPool pool =new JedisPool (new JedisPoolConfig (),endpointStandalone0 .getHostAndPort (),
497+ endpointStandalone0 .getClientConfigBuilder ().build ())) {
498+
499+ pool .withResource (jedis -> {
500+ assertThat (pool .getNumActive (),equalTo (1 ));
501+ jedis .set ("foo" ,"bar" );
502+ });
503+
504+ assertThat (pool .getNumActive (),equalTo (0 ));
505+ }
506+ }
507+
508+ @ Test
509+ public void testWithResourceGet () {
510+ try (JedisPool pool =new JedisPool (new JedisPoolConfig (),endpointStandalone0 .getHostAndPort (),
511+ endpointStandalone0 .getClientConfigBuilder ().build ())) {
512+
513+ String result =pool .withResourceGet (jedis -> {
514+ jedis .set (testKey ,testValue );
515+ return jedis .get (testKey );
516+ });
517+
518+ assertEquals (testValue ,result );
519+ }
520+ }
521+
522+ @ Test
523+ public void testWithResourceGetReturnsConnectionToPool () {
524+ try (JedisPool pool =new JedisPool (new JedisPoolConfig (),endpointStandalone0 .getHostAndPort (),
525+ endpointStandalone0 .getClientConfigBuilder ().build ())) {
526+
527+ String result =pool .withResourceGet (jedis -> {
528+ assertThat (pool .getNumActive (),equalTo (1 ));
529+ jedis .set ("foo" ,"bar" );
530+ return jedis .get ("foo" );
531+ });
532+
533+ assertThat (result ,equalTo ("bar" ));
534+ assertThat (pool .getNumActive (),equalTo (0 ));
535+ }
536+ }
537+
465538}