DATAREDIS-245 - Prevent Excessive Thread creation in tests.

Rewritten testPubSubWithPatterns in order to make it pass reliably by keeping the original test semantics.
This commit is contained in:
Thomas Darimont
2014-01-16 09:31:17 +01:00
parent 2af23692f3
commit b5137e6dd4

View File

@@ -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<Message> messages = new LinkedBlockingDeque<Message>();
@@ -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