Skip to content

Commit

Permalink
fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
kkewwei committed Aug 7, 2024
1 parent 711332a commit cc0d143
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@
import org.apache.hc.core5.util.TimeValue;
import org.apache.hc.core5.util.Timeout;

import static org.apache.hc.core5.reactor.SingleCoreIOReactor.TCP_KEEPCOUNT;
import static org.apache.hc.core5.reactor.SingleCoreIOReactor.TCP_KEEPIDLE;
import static org.apache.hc.core5.reactor.SingleCoreIOReactor.TCP_KEEPINTERVAL;
import static org.apache.hc.core5.util.NetUtils.TCP_KEEPCOUNT;
import static org.apache.hc.core5.util.NetUtils.TCP_KEEPIDLE;
import static org.apache.hc.core5.util.NetUtils.TCP_KEEPINTERVAL;

/**
* HTTP/1.1 client side message exchange initiator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@
import org.apache.hc.core5.util.TimeValue;
import org.apache.hc.core5.util.Timeout;

import static org.apache.hc.core5.reactor.SingleCoreIOReactor.TCP_KEEPCOUNT;
import static org.apache.hc.core5.reactor.SingleCoreIOReactor.TCP_KEEPIDLE;
import static org.apache.hc.core5.reactor.SingleCoreIOReactor.TCP_KEEPINTERVAL;
import static org.apache.hc.core5.util.NetUtils.TCP_KEEPCOUNT;
import static org.apache.hc.core5.util.NetUtils.TCP_KEEPIDLE;
import static org.apache.hc.core5.util.NetUtils.TCP_KEEPINTERVAL;

/**
* HTTP/1.1 server side message exchange handler.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
import org.apache.hc.core5.io.Closer;
import org.apache.hc.core5.util.ReflectionUtils;

import static org.apache.hc.core5.reactor.SingleCoreIOReactor.TCP_KEEPCOUNT;
import static org.apache.hc.core5.reactor.SingleCoreIOReactor.TCP_KEEPIDLE;
import static org.apache.hc.core5.reactor.SingleCoreIOReactor.TCP_KEEPINTERVAL;
import static org.apache.hc.core5.util.NetUtils.TCP_KEEPCOUNT;
import static org.apache.hc.core5.util.NetUtils.TCP_KEEPIDLE;
import static org.apache.hc.core5.util.NetUtils.TCP_KEEPINTERVAL;

class RequestListener implements Runnable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,16 @@ public int getBacklogSize() {
* @see Builder#setTcpKeepIdle(int)
* @since 5.3
*/
public int getTcpKeepInterval() {
return this.tcpKeepInterval;
public int getTcpKeepIdle() {
return this.tcpKeepIdle;
}

/**
* @see Builder#setTcpKeepInterval(int)
* @since 5.3
*/
public int getTcpKeepIdle() {
return this.tcpKeepIdle;
public int getTcpKeepInterval() {
return this.tcpKeepInterval;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.hc.core5.annotation.Internal;
import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.core5.function.Callback;
import org.apache.hc.core5.function.Decorator;
Expand All @@ -53,14 +52,13 @@
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.Timeout;

import static org.apache.hc.core5.util.NetUtils.TCP_KEEPCOUNT;
import static org.apache.hc.core5.util.NetUtils.TCP_KEEPIDLE;
import static org.apache.hc.core5.util.NetUtils.TCP_KEEPINTERVAL;
import static org.apache.hc.core5.util.ReflectionUtils.getExtendedSocketOptionOrNull;

@Internal
public class SingleCoreIOReactor extends AbstractSingleCoreIOReactor implements ConnectionInitiator {
class SingleCoreIOReactor extends AbstractSingleCoreIOReactor implements ConnectionInitiator {

public static final String TCP_KEEPIDLE = "TCP_KEEPIDLE";
public static final String TCP_KEEPINTERVAL = "TCP_KEEPINTERVAL";
public static final String TCP_KEEPCOUNT = "TCP_KEEPCOUNT";
private static final int MAX_CHANNEL_REQUESTS = 10000;

private final IOEventHandlerFactory eventHandlerFactory;
Expand Down
12 changes: 12 additions & 0 deletions httpcore5/src/main/java/org/apache/hc/core5/util/NetUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.apache.hc.core5.util;

import org.apache.hc.core5.annotation.Internal;

@Internal
public class NetUtils {

public static final String TCP_KEEPIDLE = "TCP_KEEPIDLE";
public static final String TCP_KEEPINTERVAL = "TCP_KEEPINTERVAL";
public static final String TCP_KEEPCOUNT = "TCP_KEEPCOUNT";

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

package org.apache.hc.core5.util;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.SocketOption;
Expand All @@ -46,13 +47,25 @@ public static void callSetter(final Object object, final String setterName, fina
}
}

public static <T> T callGetter(final Object object, final String getterName, final Class<T> resultType) {
return callGetter(object, getterName, null, null, resultType);
}

public static <T> T callGetter(final Object object, final String getterName, final Object arg, final Class argType, final Class<T> resultType) {
try {
final Class<?> clazz = object.getClass();
final Method method;
method = clazz.getMethod("get" + getterName, argType);
method.setAccessible(true);
return resultType.cast(method.invoke(object, arg));
if (arg != null) {
assert argType != null;
method = clazz.getMethod("get" + getterName, argType);
method.setAccessible(true);
return resultType.cast(method.invoke(object, arg));
} else {
assert argType == null;
method = clazz.getMethod("get" + getterName);
method.setAccessible(true);
return resultType.cast(method.invoke(object));
}
} catch (final Exception ignore) {
return null;
}
Expand All @@ -78,6 +91,7 @@ public static int determineJRELevel() {
/**
* @since 5.3
*/
@SuppressWarnings("unchecked")
public static <T> SocketOption<T> getExtendedSocketOptionOrNull(final String fieldName) {
try {
final Class<?> extendedSocketOptionsClass = Class.forName("jdk.net.ExtendedSocketOptions");
Expand All @@ -93,17 +107,20 @@ public static <T> SocketOption<T> getExtendedSocketOptionOrNull(final String fie
*
* @since 5.3
*/
public static <T> void setOption(final T object, final String fieldName, final T value) {
public static <T> void setOption(final T object, final String fieldName, final T value) throws IOException {
try {
final Class<?> serverSocketClass = object.getClass();
final Method setOptionMethod = serverSocketClass.getMethod("setOption", SocketOption.class, Object.class);
final SocketOption<Integer> tcpKeepIdle = getExtendedSocketOptionOrNull(fieldName);
if (tcpKeepIdle == null) {
final SocketOption<Integer> socketOption = getExtendedSocketOptionOrNull(fieldName);
if (socketOption == null) {
throw new UnsupportedOperationException(fieldName + " is not supported in the current jdk");
}
setOptionMethod.invoke(object, tcpKeepIdle, value);
} catch (final Exception ignore) {
throw new UnsupportedOperationException(fieldName + " is not supported in the current jdk");
setOptionMethod.invoke(object, socketOption, value);
} catch (final UnsupportedOperationException e) {
throw e;
} catch (final Exception e) {
throw new IOException("failed to call setOption", e);
}
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
import java.net.Socket;
import java.net.SocketOption;

import static org.apache.hc.core5.reactor.SingleCoreIOReactor.TCP_KEEPCOUNT;
import static org.apache.hc.core5.reactor.SingleCoreIOReactor.TCP_KEEPIDLE;
import static org.apache.hc.core5.reactor.SingleCoreIOReactor.TCP_KEEPINTERVAL;
import static org.apache.hc.core5.util.NetUtils.TCP_KEEPCOUNT;
import static org.apache.hc.core5.util.NetUtils.TCP_KEEPIDLE;
import static org.apache.hc.core5.util.NetUtils.TCP_KEEPINTERVAL;
import static org.apache.hc.core5.util.ReflectionUtils.callGetter;
import static org.apache.hc.core5.util.ReflectionUtils.determineJRELevel;
import static org.apache.hc.core5.util.ReflectionUtils.setOption;
Expand All @@ -53,8 +53,8 @@ public void testGetExtendedSocketOptionOrNull() {
testGetExtendedSocketOption(TCP_KEEPCOUNT);
}

private void testGetExtendedSocketOption(final String option) {
final SocketOption socketOption = getExtendedSocketOptionOrNull(option);
private <T> void testGetExtendedSocketOption(final String option) {
final SocketOption<T> socketOption = getExtendedSocketOptionOrNull(option);
// 1.Partial versions of jdk1.8 contain TCP_KEEPIDLE, TCP_KEEPINTERVAL, TCP_KEEPCOUNT.
// 2. Windows may not support TCP_KEEPIDLE, TCP_KEEPINTERVAL, TCP_KEEPCOUNT.
if (determineJRELevel() > 8 && isWindows() == false) {
Expand Down Expand Up @@ -110,4 +110,5 @@ public void testSetOption() throws IOException {
public static boolean isWindows() {
return System.getProperty("os.name").contains("Windows");
}

}

0 comments on commit cc0d143

Please sign in to comment.