Change string size to make test fail faster

This commit is contained in:
Jonathan Leibiusky
2013-12-03 08:31:24 -05:00
parent fa614bd2b8
commit efbb710343

View File

@@ -3,6 +3,7 @@ package redis.clients.jedis.tests.commands;
import java.io.IOException; import java.io.IOException;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.Arrays; import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
@@ -246,8 +247,8 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
try { try {
Jedis j = createJedis(); Jedis j = createJedis();
Thread.sleep(1000); Thread.sleep(1000);
j.publish(SafeEncoder.encode("foo"), SafeEncoder j.publish(SafeEncoder.encode("foo"),
.encode("exit")); SafeEncoder.encode("exit"));
j.disconnect(); j.disconnect();
} catch (Exception ex) { } catch (Exception ex) {
fail(ex.getMessage()); fail(ex.getMessage());
@@ -293,11 +294,11 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
try { try {
Jedis j = createJedis(); Jedis j = createJedis();
Thread.sleep(1000); Thread.sleep(1000);
j.publish(SafeEncoder.encode("foo"), SafeEncoder j.publish(SafeEncoder.encode("foo"),
.encode("exit")); SafeEncoder.encode("exit"));
Thread.sleep(1000); Thread.sleep(1000);
j.publish(SafeEncoder.encode("bar"), SafeEncoder j.publish(SafeEncoder.encode("bar"),
.encode("exit")); SafeEncoder.encode("exit"));
j.disconnect(); j.disconnect();
} catch (Exception ex) { } catch (Exception ex) {
fail(ex.getMessage()); fail(ex.getMessage());
@@ -337,8 +338,8 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
try { try {
Jedis j = createJedis(); Jedis j = createJedis();
Thread.sleep(1000); Thread.sleep(1000);
j.publish(SafeEncoder.encode("foo.bar"), SafeEncoder j.publish(SafeEncoder.encode("foo.bar"),
.encode("exit")); SafeEncoder.encode("exit"));
j.disconnect(); j.disconnect();
} catch (Exception ex) { } catch (Exception ex) {
fail(ex.getMessage()); fail(ex.getMessage());
@@ -386,11 +387,11 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
try { try {
Jedis j = createJedis(); Jedis j = createJedis();
Thread.sleep(1000); Thread.sleep(1000);
j.publish(SafeEncoder.encode("foo.123"), SafeEncoder j.publish(SafeEncoder.encode("foo.123"),
.encode("exit")); SafeEncoder.encode("exit"));
Thread.sleep(1000); Thread.sleep(1000);
j.publish(SafeEncoder.encode("bar.123"), SafeEncoder j.publish(SafeEncoder.encode("bar.123"),
.encode("exit")); SafeEncoder.encode("exit"));
j.disconnect(); j.disconnect();
} catch (Exception ex) { } catch (Exception ex) {
fail(ex.getMessage()); fail(ex.getMessage());
@@ -455,12 +456,12 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
Thread.sleep(1000); Thread.sleep(1000);
pubsub.subscribe(SafeEncoder.encode("bar")); pubsub.subscribe(SafeEncoder.encode("bar"));
pubsub.psubscribe(SafeEncoder.encode("bar.*")); pubsub.psubscribe(SafeEncoder.encode("bar.*"));
j.publish(SafeEncoder.encode("foo"), SafeEncoder j.publish(SafeEncoder.encode("foo"),
.encode("exit")); SafeEncoder.encode("exit"));
j.publish(SafeEncoder.encode("bar"), SafeEncoder j.publish(SafeEncoder.encode("bar"),
.encode("exit")); SafeEncoder.encode("exit"));
j.publish(SafeEncoder.encode("bar.123"), SafeEncoder j.publish(SafeEncoder.encode("bar.123"),
.encode("exit")); SafeEncoder.encode("exit"));
j.disconnect(); j.disconnect();
} catch (Exception ex) { } catch (Exception ex) {
fail(ex.getMessage()); fail(ex.getMessage());
@@ -472,7 +473,8 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
t.join(); t.join();
} }
@Test @Ignore @Test
@Ignore
public void subscribeWithoutConnecting() { public void subscribeWithoutConnecting() {
try { try {
Jedis jedis = new Jedis(hnp.getHost(), hnp.getPort()); Jedis jedis = new Jedis(hnp.getHost(), hnp.getPort());
@@ -530,37 +532,44 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
} }
@Test(expected = JedisConnectionException.class) @Test(expected = JedisConnectionException.class)
public void handleClientOutputBufferLimitForSubscribeTooSlow() throws InterruptedException { public void handleClientOutputBufferLimitForSubscribeTooSlow()
throws InterruptedException {
final Jedis j = createJedis();
final AtomicBoolean exit = new AtomicBoolean(false);
final Thread t = new Thread(new Runnable() { final Thread t = new Thread(new Runnable() {
public void run() { public void run() {
try { try {
Jedis j = createJedis();
Thread.sleep(1000); // we already set jedis1 config to
// client-output-buffer-limit pubsub 256k 128k 5
// we already set jedis1 config to client-output-buffer-limit pubsub 256k 128k 5 // it means if subscriber delayed to receive over 256k or
// it means if subscriber delayed to receive over 256k or 128k continuously 5 sec, // 128k continuously 5 sec,
// redis disconnects subscriber // redis disconnects subscriber
// we publish over 10M data for making situation for exceed client-output-buffer-limit // we publish over 100M data for making situation for exceed
String veryLargeString = makeLargeString(1024 * 10); // client-output-buffer-limit
String veryLargeString = makeLargeString(10485760);
// 10K * 1024 = 10M // 10M * 10 = 100M
for (int i = 0 ; i < 1024 ; i++) for (int i = 0; i < 10 && !exit.get(); i++) {
j.publish("foo", veryLargeString); // 1k j.publish("foo", veryLargeString);
}
j.disconnect(); j.disconnect();
} catch (Exception ex) { } catch (Exception ex) {
fail(ex.getMessage());
} }
} }
}); });
t.start(); t.start();
try {
jedis.subscribe(new JedisPubSub() { jedis.subscribe(new JedisPubSub() {
public void onMessage(String channel, String message) { public void onMessage(String channel, String message) {
try { try {
// wait 0.5 secs to slow down subscribe and client-output-buffer exceed // wait 0.5 secs to slow down subscribe and
//System.out.println("channel - " + channel + " / message - " + message); // client-output-buffer exceed
// System.out.println("channel - " + channel +
// " / message - " + message);
Thread.sleep(500); Thread.sleep(500);
} catch (Exception e) { } catch (Exception e) {
try { try {
@@ -581,7 +590,8 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
public void onPSubscribe(String pattern, int subscribedChannels) { public void onPSubscribe(String pattern, int subscribedChannels) {
} }
public void onPUnsubscribe(String pattern, int subscribedChannels) { public void onPUnsubscribe(String pattern,
int subscribedChannels) {
} }
public void onPMessage(String pattern, String channel, public void onPMessage(String pattern, String channel,
@@ -589,12 +599,20 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
} }
}, "foo"); }, "foo");
t.join(); t.join();
} finally {
// exit the publisher thread. if exception is thrown, thread might
// still keep publishing things.
exit.set(true);
if (t.isAlive()) {
t.wait();
}
}
} }
private String makeLargeString(int size) { private String makeLargeString(int size) {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
for (int i = 0 ; i < size ; i++) for (int i = 0; i < size; i++)
sb.append((char)('a' + i % 26)); sb.append((char) ('a' + i % 26));
return sb.toString(); return sb.toString();
} }