* Fixed randomly failing ChannelBuffers.compareTo test

* Used shutdownNow instead of shutdown to shorten the test running time
* Added a test case that checks shutdown time
This commit is contained in:
Trustin Lee 2008-09-05 00:01:33 +00:00
parent 500f972367
commit a056b9af63
12 changed files with 140 additions and 23 deletions

View File

@ -753,35 +753,35 @@ public class ChannelBuffers {
final int aLen = bufferA.readableBytes();
final int bLen = bufferB.readableBytes();
final int minLength = Math.min(aLen, bLen);
final int longCount = minLength >>> 3;
final int byteCount = minLength & 7;
final int uintCount = minLength >>> 2;
final int byteCount = minLength & 3;
int aIndex = bufferA.readerIndex();
int bIndex = bufferB.readerIndex();
if (bufferA.order() == bufferB.order()) {
for (int i = longCount; i > 0; i --) {
long va = bufferA.getLong(aIndex);
long vb = bufferB.getLong(bIndex);
for (int i = uintCount; i > 0; i --) {
long va = bufferA.getUnsignedInt(aIndex);
long vb = bufferB.getUnsignedInt(bIndex);
if (va > vb) {
return 1;
} else if (va < vb) {
return -1;
}
aIndex += 8;
bIndex += 8;
aIndex += 4;
bIndex += 4;
}
} else {
for (int i = longCount; i > 0; i --) {
long va = bufferA.getLong(aIndex);
long vb = swapLong(bufferB.getLong(bIndex));
for (int i = uintCount; i > 0; i --) {
long va = bufferA.getUnsignedInt(aIndex);
long vb = swapInt(bufferB.getInt(bIndex)) & 0xFFFFFFFFL;
if (va > vb) {
return 1;
} else if (va < vb) {
return -1;
}
aIndex += 8;
bIndex += 8;
aIndex += 4;
bIndex += 4;
}
}

View File

@ -337,7 +337,7 @@ class NioProviderMetadata {
loop.done = true;
loop.selector.wakeup();
try {
executor.shutdown();
executor.shutdownNow();
for (;;) {
try {
if (executor.awaitTermination(1, TimeUnit.SECONDS)) {

View File

@ -61,7 +61,7 @@ public abstract class AbstractSocketClientBootstrapTest {
@AfterClass
public static void destroy() {
executor.shutdown();
executor.shutdownNow();
for (;;) {
try {
if (executor.awaitTermination(1, TimeUnit.MILLISECONDS)) {

View File

@ -67,7 +67,7 @@ public abstract class AbstractSocketServerBootstrapTest {
@AfterClass
public static void destroy() {
executor.shutdown();
executor.shutdownNow();
for (;;) {
try {
if (executor.awaitTermination(1, TimeUnit.MILLISECONDS)) {

View File

@ -1470,11 +1470,20 @@ public abstract class AbstractChannelBufferTest {
// Expected
}
// Fill the random stuff
byte[] value = new byte[32];
buffer.setIndex(0, value.length);
random.nextBytes(value);
// Prevent overflow / underflow
if (value[0] == 0) {
value[0] ++;
} else if (value[0] == -1) {
value[0] --;
}
buffer.setIndex(0, value.length);
buffer.setBytes(0, value);
assertEquals(0, buffer.compareTo(wrappedBuffer(BIG_ENDIAN, value)));
assertEquals(0, buffer.compareTo(wrappedBuffer(LITTLE_ENDIAN, value)));

View File

@ -77,7 +77,7 @@ public abstract class AbstractSocketEchoTest {
@AfterClass
public static void destroy() {
executor.shutdown();
executor.shutdownNow();
for (;;) {
try {
if (executor.awaitTermination(1, TimeUnit.MILLISECONDS)) {

View File

@ -0,0 +1,109 @@
/*
* JBoss, Home of Professional Open Source
*
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.netty.channel.socket;
import static org.junit.Assert.*;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.junit.Test;
/**
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
*
* @version $Rev$, $Date$
*
*/
public class NioClientSocketShutdownTimeTest {
@Test
public void testShutdownTime() throws Throwable {
ServerSocketChannel serverSocket = ServerSocketChannel.open();
serverSocket.socket().bind(new InetSocketAddress(0));
long startTime = System.currentTimeMillis();
ExecutorService e1 = Executors.newCachedThreadPool();
ExecutorService e2 = Executors.newCachedThreadPool();
ClientBootstrap b = new ClientBootstrap(new NioClientSocketChannelFactory(e1, e2));
try {
serverSocket.configureBlocking(false);
ChannelFuture f = b.connect(new InetSocketAddress(
InetAddress.getLocalHost(),
serverSocket.socket().getLocalPort()));
serverSocket.accept();
f.awaitUninterruptibly();
if (f.getCause() != null) {
throw f.getCause();
}
assertTrue(f.isSuccess());
f.getChannel().close().awaitUninterruptibly();
} finally {
assertEquals(0, e1.shutdownNow().size());
assertEquals(0, e2.shutdownNow().size());
for (;;) {
try {
if (e1.awaitTermination(1, TimeUnit.SECONDS)) {
break;
}
} catch (InterruptedException ex) {
// Ignore
}
}
for (;;) {
try {
if (e2.awaitTermination(1, TimeUnit.SECONDS)) {
break;
}
} catch (InterruptedException ex) {
// Ignore
}
}
try {
serverSocket.close();
} catch (IOException ex) {
// Ignore.
}
}
long shutdownTime = System.currentTimeMillis() - startTime;
assertTrue("Shutdown takes too long: " + shutdownTime + " ms", shutdownTime < 500);
}
}

View File

@ -47,7 +47,6 @@ import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.handler.codec.frame.FixedLengthFrameDecoder;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
@ -78,7 +77,7 @@ public abstract class AbstractSocketFixedLengthEchoTest {
@AfterClass
public static void destroy() {
executor.shutdown();
executor.shutdownNow();
for (;;) {
try {
if (executor.awaitTermination(1, TimeUnit.MILLISECONDS)) {

View File

@ -83,7 +83,7 @@ public abstract class AbstractSocketCompatibleObjectStreamEchoTest {
@AfterClass
public static void destroy() {
executor.shutdown();
executor.shutdownNow();
for (;;) {
try {
if (executor.awaitTermination(1, TimeUnit.MILLISECONDS)) {

View File

@ -83,7 +83,7 @@ public abstract class AbstractSocketObjectStreamEchoTest {
@AfterClass
public static void destroy() {
executor.shutdown();
executor.shutdownNow();
for (;;) {
try {
if (executor.awaitTermination(1, TimeUnit.MILLISECONDS)) {

View File

@ -85,7 +85,7 @@ public abstract class AbstractSocketStringEchoTest {
@AfterClass
public static void destroy() {
executor.shutdown();
executor.shutdownNow();
for (;;) {
try {
if (executor.awaitTermination(1, TimeUnit.MILLISECONDS)) {

View File

@ -80,7 +80,7 @@ public abstract class AbstractSocketSslEchoTest {
@AfterClass
public static void destroy() {
executor.shutdown();
executor.shutdownNow();
for (;;) {
try {
if (executor.awaitTermination(1, TimeUnit.MILLISECONDS)) {