Do not use SLF4J/Logback API directly

Motivation:

TrafficShapingHandlerTest uses Logback API directly, which is
discouraged.  Also, it overrides the global default log level, which
silences the DEBUG messages from other tests.

Modifications:

Remove the direct use of Logback API

Result:

The tests executed after TrafficShapingHandlerTest logs their DEBUG
messages correctly.
This commit is contained in:
Trustin Lee 2014-12-17 10:15:52 +09:00
parent 5ff0b1253c
commit 28553c9ee0

View File

@ -29,15 +29,9 @@ import io.netty.util.concurrent.EventExecutorGroup;
import io.netty.util.concurrent.Promise; import io.netty.util.concurrent.Promise;
import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory; import io.netty.util.internal.logging.InternalLoggerFactory;
import io.netty.util.internal.logging.Slf4JLoggerFactory;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
@ -57,7 +51,7 @@ public class TrafficShapingHandlerTest extends AbstractSocketTest {
static final int messageSize = 1024; static final int messageSize = 1024;
static final int bandwidthFactor = 12; static final int bandwidthFactor = 12;
static final int minfactor = 3; static final int minfactor = 3;
static final int maxfactor = bandwidthFactor + (bandwidthFactor / 2); static final int maxfactor = bandwidthFactor + bandwidthFactor / 2;
static final long stepms = (1000 / bandwidthFactor - 10) / 10 * 10; static final long stepms = (1000 / bandwidthFactor - 10) / 10 * 10;
static final long minimalms = Math.max(stepms / 2, 20) / 10 * 10; static final long minimalms = Math.max(stepms / 2, 20) / 10 * 10;
static final long check = 10; static final long check = 10;
@ -65,12 +59,12 @@ public class TrafficShapingHandlerTest extends AbstractSocketTest {
static final byte[] data = new byte[messageSize]; static final byte[] data = new byte[messageSize];
private static final String TRAFFIC = "traffic"; private static final String TRAFFIC = "traffic";
private static String TESTNAME; private static String currentTestName;
private static int TESTRUN; private static int currentTestRun;
private static EventExecutorGroup group; private static EventExecutorGroup group;
private static EventExecutorGroup groupForGlobal; private static EventExecutorGroup groupForGlobal;
private static ScheduledExecutorService executor = Executors.newScheduledThreadPool(10); private static final ScheduledExecutorService executor = Executors.newScheduledThreadPool(10);
static { static {
random.nextBytes(data); random.nextBytes(data);
} }
@ -79,9 +73,6 @@ public class TrafficShapingHandlerTest extends AbstractSocketTest {
public static void createGroup() { public static void createGroup() {
logger.info("Bandwidth: " + minfactor + " <= " + bandwidthFactor + " <= " + maxfactor + logger.info("Bandwidth: " + minfactor + " <= " + bandwidthFactor + " <= " + maxfactor +
" StepMs: " + stepms + " MinMs: " + minimalms + " CheckMs: " + check); " StepMs: " + stepms + " MinMs: " + minimalms + " CheckMs: " + check);
InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory());
Logger logger = (Logger) LoggerFactory.getLogger("ROOT");
logger.setLevel(Level.INFO);
group = new DefaultEventExecutorGroup(8); group = new DefaultEventExecutorGroup(8);
groupForGlobal = new DefaultEventExecutorGroup(8); groupForGlobal = new DefaultEventExecutorGroup(8);
} }
@ -137,8 +128,8 @@ public class TrafficShapingHandlerTest extends AbstractSocketTest {
@Test(timeout = 10000) @Test(timeout = 10000)
public void testNoTrafficShapping() throws Throwable { public void testNoTrafficShapping() throws Throwable {
TESTNAME = "TEST NO TRAFFIC"; currentTestName = "TEST NO TRAFFIC";
TESTRUN = 0; currentTestRun = 0;
run(); run();
} }
@ -151,8 +142,8 @@ public class TrafficShapingHandlerTest extends AbstractSocketTest {
@Test(timeout = 10000) @Test(timeout = 10000)
public void testWriteTrafficShapping() throws Throwable { public void testWriteTrafficShapping() throws Throwable {
TESTNAME = "TEST WRITE"; currentTestName = "TEST WRITE";
TESTRUN = 0; currentTestRun = 0;
run(); run();
} }
@ -165,8 +156,8 @@ public class TrafficShapingHandlerTest extends AbstractSocketTest {
@Test(timeout = 10000) @Test(timeout = 10000)
public void testReadTrafficShapping() throws Throwable { public void testReadTrafficShapping() throws Throwable {
TESTNAME = "TEST READ"; currentTestName = "TEST READ";
TESTRUN = 0; currentTestRun = 0;
run(); run();
} }
@ -179,8 +170,8 @@ public class TrafficShapingHandlerTest extends AbstractSocketTest {
@Test(timeout = 10000) @Test(timeout = 10000)
public void testWrite1TrafficShapping() throws Throwable { public void testWrite1TrafficShapping() throws Throwable {
TESTNAME = "TEST WRITE"; currentTestName = "TEST WRITE";
TESTRUN = 0; currentTestRun = 0;
run(); run();
} }
@ -193,8 +184,8 @@ public class TrafficShapingHandlerTest extends AbstractSocketTest {
@Test(timeout = 10000) @Test(timeout = 10000)
public void testRead1TrafficShapping() throws Throwable { public void testRead1TrafficShapping() throws Throwable {
TESTNAME = "TEST READ"; currentTestName = "TEST READ";
TESTRUN = 0; currentTestRun = 0;
run(); run();
} }
@ -207,8 +198,8 @@ public class TrafficShapingHandlerTest extends AbstractSocketTest {
@Test(timeout = 10000) @Test(timeout = 10000)
public void testWriteGlobalTrafficShapping() throws Throwable { public void testWriteGlobalTrafficShapping() throws Throwable {
TESTNAME = "TEST GLOBAL WRITE"; currentTestName = "TEST GLOBAL WRITE";
TESTRUN = 0; currentTestRun = 0;
run(); run();
} }
@ -221,8 +212,8 @@ public class TrafficShapingHandlerTest extends AbstractSocketTest {
@Test(timeout = 10000) @Test(timeout = 10000)
public void testReadGlobalTrafficShapping() throws Throwable { public void testReadGlobalTrafficShapping() throws Throwable {
TESTNAME = "TEST GLOBAL READ"; currentTestName = "TEST GLOBAL READ";
TESTRUN = 0; currentTestRun = 0;
run(); run();
} }
@ -235,8 +226,8 @@ public class TrafficShapingHandlerTest extends AbstractSocketTest {
@Test(timeout = 10000) @Test(timeout = 10000)
public void testAutoReadTrafficShapping() throws Throwable { public void testAutoReadTrafficShapping() throws Throwable {
TESTNAME = "TEST AUTO READ"; currentTestName = "TEST AUTO READ";
TESTRUN = 0; currentTestRun = 0;
run(); run();
} }
@ -247,10 +238,11 @@ public class TrafficShapingHandlerTest extends AbstractSocketTest {
long[] minimalWaitBetween = computeWaitAutoRead(autoRead); long[] minimalWaitBetween = computeWaitAutoRead(autoRead);
testTrafficShapping0(sb, cb, false, true, false, false, autoRead, minimalWaitBetween, multipleMessage); testTrafficShapping0(sb, cb, false, true, false, false, autoRead, minimalWaitBetween, multipleMessage);
} }
@Test(timeout = 10000) @Test(timeout = 10000)
public void testAutoReadGlobalTrafficShapping() throws Throwable { public void testAutoReadGlobalTrafficShapping() throws Throwable {
TESTNAME = "TEST AUTO READ GLOBAL"; currentTestName = "TEST AUTO READ GLOBAL";
TESTRUN = 0; currentTestRun = 0;
run(); run();
} }
@ -264,8 +256,6 @@ public class TrafficShapingHandlerTest extends AbstractSocketTest {
/** /**
* *
* @param sb
* @param cb
* @param additionalExecutor * @param additionalExecutor
* shall the pipeline add the handler using an additionnal executor * shall the pipeline add the handler using an additionnal executor
* @param limitRead * @param limitRead
@ -274,7 +264,6 @@ public class TrafficShapingHandlerTest extends AbstractSocketTest {
* True to set Write Limit on Client side * True to set Write Limit on Client side
* @param globalLimit * @param globalLimit
* True to change Channel to Global TrafficShapping * True to change Channel to Global TrafficShapping
* @param autoRead
* @param minimalWaitBetween * @param minimalWaitBetween
* time in ms that should be waited before getting the final result (note: for READ the values are * time in ms that should be waited before getting the final result (note: for READ the values are
* right shifted once, the first value being 0) * right shifted once, the first value being 0)
@ -286,8 +275,9 @@ public class TrafficShapingHandlerTest extends AbstractSocketTest {
private static void testTrafficShapping0(ServerBootstrap sb, Bootstrap cb, final boolean additionalExecutor, private static void testTrafficShapping0(ServerBootstrap sb, Bootstrap cb, final boolean additionalExecutor,
final boolean limitRead, final boolean limitWrite, final boolean globalLimit, int[] autoRead, final boolean limitRead, final boolean limitWrite, final boolean globalLimit, int[] autoRead,
long[] minimalWaitBetween, int[] multipleMessage) throws Throwable { long[] minimalWaitBetween, int[] multipleMessage) throws Throwable {
TESTRUN ++;
logger.info("TEST: " + TESTNAME + " RUN: " + TESTRUN + currentTestRun++;
logger.info("TEST: " + currentTestName + " RUN: " + currentTestRun +
" Exec: " + additionalExecutor + " Read: " + limitRead + " Write: " + limitWrite + " Global: " " Exec: " + additionalExecutor + " Read: " + limitRead + " Write: " + limitWrite + " Global: "
+ globalLimit); + globalLimit);
final ServerHandler sh = new ServerHandler(autoRead, multipleMessage); final ServerHandler sh = new ServerHandler(autoRead, multipleMessage);
@ -350,7 +340,7 @@ public class TrafficShapingHandlerTest extends AbstractSocketTest {
assertTrue("Error during exceution of TrafficShapping: " + promise.cause(), promise.isSuccess()); assertTrue("Error during exceution of TrafficShapping: " + promise.cause(), promise.isSuccess());
float average = (totalNb * messageSize) / (float) (stop - start); float average = (totalNb * messageSize) / (float) (stop - start);
logger.info("TEST: " + TESTNAME + " RUN: " + TESTRUN + logger.info("TEST: " + currentTestName + " RUN: " + currentTestRun +
" Average of traffic: " + average + " compare to " + bandwidthFactor); " Average of traffic: " + average + " compare to " + bandwidthFactor);
sh.channel.close().sync(); sh.channel.close().sync();
ch.channel.close().sync(); ch.channel.close().sync();
@ -416,7 +406,7 @@ public class TrafficShapingHandlerTest extends AbstractSocketTest {
@Override @Override
public void channelRead0(ChannelHandlerContext ctx, ByteBuf in) throws Exception { public void channelRead0(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
long lastTimestamp = 0; long lastTimestamp = 0;
loggerClient.debug("Step: " + step + " Read: " + (in.readableBytes() / 8) + " blocks"); loggerClient.debug("Step: " + step + " Read: " + in.readableBytes() / 8 + " blocks");
while (in.isReadable()) { while (in.isReadable()) {
lastTimestamp = in.readLong(); lastTimestamp = in.readLong();
multipleMessage[step]--; multipleMessage[step]--;
@ -425,7 +415,7 @@ public class TrafficShapingHandlerTest extends AbstractSocketTest {
// still some message to get // still some message to get
return; return;
} }
long minimalWait = (minimalWaitBetween != null) ? minimalWaitBetween[step] : 0; long minimalWait = minimalWaitBetween != null? minimalWaitBetween[step] : 0;
int ar = 0; int ar = 0;
if (autoRead != null) { if (autoRead != null) {
if (step > 0 && autoRead[step - 1] != 0) { if (step > 0 && autoRead[step - 1] != 0) {
@ -433,7 +423,7 @@ public class TrafficShapingHandlerTest extends AbstractSocketTest {
} }
} }
loggerClient.info("Step: " + step + " Interval: " + (lastTimestamp - currentLastTime) + " compareTo " loggerClient.info("Step: " + step + " Interval: " + (lastTimestamp - currentLastTime) + " compareTo "
+ minimalWait + " (" + ar + ")"); + minimalWait + " (" + ar + ')');
assertTrue("The interval of time is incorrect:" + (lastTimestamp - currentLastTime) + " not> " assertTrue("The interval of time is incorrect:" + (lastTimestamp - currentLastTime) + " not> "
+ minimalWait, lastTimestamp - currentLastTime >= minimalWait); + minimalWait, lastTimestamp - currentLastTime >= minimalWait);
currentLastTime = lastTimestamp; currentLastTime = lastTimestamp;
@ -517,11 +507,12 @@ public class TrafficShapingHandlerTest extends AbstractSocketTest {
if (isAutoRead != 0) { if (isAutoRead != 0) {
if (isAutoRead < 0) { if (isAutoRead < 0) {
final int exactStep = step; final int exactStep = step;
long wait = (isAutoRead == -1) ? minimalms : stepms + minimalms; long wait = isAutoRead == -1? minimalms : stepms + minimalms;
if (isAutoRead == -3) { if (isAutoRead == -3) {
wait = stepms * 3; wait = stepms * 3;
} }
executor.schedule(new Runnable() { executor.schedule(new Runnable() {
@Override
public void run() { public void run() {
loggerServer.info("Step: " + exactStep + " Reset AutoRead"); loggerServer.info("Step: " + exactStep + " Reset AutoRead");
channel.config().setAutoRead(true); channel.config().setAutoRead(true);
@ -532,6 +523,7 @@ public class TrafficShapingHandlerTest extends AbstractSocketTest {
loggerServer.debug("Step: " + step + " Will Set AutoRead: True"); loggerServer.debug("Step: " + step + " Will Set AutoRead: True");
final int exactStep = step; final int exactStep = step;
executor.schedule(new Runnable() { executor.schedule(new Runnable() {
@Override
public void run() { public void run() {
loggerServer.info("Step: " + exactStep + " Set AutoRead: True"); loggerServer.info("Step: " + exactStep + " Set AutoRead: True");
channel.config().setAutoRead(true); channel.config().setAutoRead(true);