From b5137e6dd4091edde83effa13fd3388e47fec26c Mon Sep 17 00:00:00 2001 From: Thomas Darimont Date: Thu, 16 Jan 2014 09:31:17 +0100 Subject: [PATCH] DATAREDIS-245 - Prevent Excessive Thread creation in tests. Rewritten testPubSubWithPatterns in order to make it pass reliably by keeping the original test semantics. --- .../JedisConnectionIntegrationTests.java | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/test/java/org/springframework/data/redis/connection/jedis/JedisConnectionIntegrationTests.java b/src/test/java/org/springframework/data/redis/connection/jedis/JedisConnectionIntegrationTests.java index 7b7f5d592..edda5a9ee 100644 --- a/src/test/java/org/springframework/data/redis/connection/jedis/JedisConnectionIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/connection/jedis/JedisConnectionIntegrationTests.java @@ -23,13 +23,11 @@ import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.data.redis.SettingsUtils; import org.springframework.data.redis.connection.AbstractConnectionIntegrationTests; import org.springframework.data.redis.connection.ConnectionUtils; -import org.springframework.data.redis.connection.DefaultStringRedisConnection; import org.springframework.data.redis.connection.DefaultStringTuple; import org.springframework.data.redis.connection.Message; import org.springframework.data.redis.connection.MessageListener; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.ReturnType; -import org.springframework.data.redis.connection.StringRedisConnection; import org.springframework.data.redis.connection.StringRedisConnection.StringTuple; import org.springframework.test.annotation.IfProfileValue; import org.springframework.test.context.ContextConfiguration; @@ -347,6 +345,7 @@ public class JedisConnectionIntegrationTests extends AbstractConnectionIntegrati @Test public void testPubSubWithPatterns() throws Exception { + final String expectedPattern = "channel*"; final String expectedMessage = "msg"; final BlockingDeque messages = new LinkedBlockingDeque(); @@ -359,40 +358,43 @@ public class JedisConnectionIntegrationTests extends AbstractConnectionIntegrati } }; - JedisConnectionFactory factory2 = new JedisConnectionFactory(); - factory2.setHostName(SettingsUtils.getHost()); - factory2.setPort(SettingsUtils.getPort()); - factory2.setUsePool(false); - factory2.afterPropertiesSet(); - final StringRedisConnection nonPooledConn = new DefaultStringRedisConnection(factory2.getConnection()); - - Thread th = new Thread(new Runnable() { + Thread th = new Thread(){ + { + setDaemon(true); + } public void run() { - // sleep 1/2 second to let the registration happen - try { - Thread.sleep(500); - } catch (InterruptedException ex) { - throw new RuntimeException(ex); - } - // open a new connection - RedisConnection connection2 = connectionFactory.getConnection(); - connection2.publish("channel1".getBytes(), expectedMessage.getBytes()); - connection2.publish("channel2".getBytes(), expectedMessage.getBytes()); - connection2.close(); + // open a new connection + RedisConnection con = connectionFactory.getConnection(); + try { + Thread.sleep(100); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + con.publish("channel1".getBytes(), expectedMessage.getBytes()); + con.publish("channel2".getBytes(), expectedMessage.getBytes()); + + try { + Thread.sleep(100); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + con.close(); // In some clients, unsubscribe happens async of message // receipt, so not all // messages may be received if unsubscribing now. // Connection.close in teardown // will take care of unsubscribing. if (!(ConnectionUtils.isAsync(connectionFactory))) { - nonPooledConn.getSubscription().pUnsubscribe(expectedPattern.getBytes()); + connection.getSubscription().pUnsubscribe(expectedPattern.getBytes()); } } - }); - + }; th.start(); - nonPooledConn.pSubscribe(listener, expectedPattern); + + connection.pSubscribe(listener, expectedPattern); // Not all providers block on subscribe (Lettuce does not), give some // time for messages to be received Message message = messages.poll(5, TimeUnit.SECONDS); @@ -401,8 +403,6 @@ public class JedisConnectionIntegrationTests extends AbstractConnectionIntegrati message = messages.poll(5, TimeUnit.SECONDS); assertNotNull(message); assertEquals(expectedMessage, new String(message.getBody())); - nonPooledConn.close(); - factory2.destroy(); } @Test