Browse Source

[Fix] fix the HttpUtilsTest' test case , it is always time out. (#10294)

3.1.0-release
juzimao 3 years ago committed by GitHub
parent
commit
b2879c0e05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      dolphinscheduler-common/pom.xml
  2. 77
      dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/LocalJettyHttpServer.java
  3. 80
      dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/LocalServerHttpUtilsTest.java

7
dolphinscheduler-common/pom.xml

@ -293,5 +293,12 @@
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>
<dependency>
<artifactId>servlet-api</artifactId>
<groupId>javax.servlet</groupId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

77
dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/LocalJettyHttpServer.java

@ -0,0 +1,77 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dolphinscheduler.common.utils;
import java.io.IOException;
import java.io.OutputStream;
import java.net.BindException;
import junit.extensions.TestSetup;
import junit.framework.Test;
import org.mortbay.jetty.*;
import org.mortbay.jetty.handler.AbstractHandler;
import org.mortbay.jetty.handler.ContextHandler;
import org.mortbay.util.ByteArrayISO8859Writer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LocalJettyHttpServer extends TestSetup {
protected static Server server;
private static Logger logger = LoggerFactory.getLogger(LocalJettyHttpServer.class);
private Integer serverPort = 0;
public Integer getServerPort() {
return serverPort;
}
public LocalJettyHttpServer(Test suite) {
super(suite);
}
protected void setUp() throws Exception {
server = new Server(serverPort);
ContextHandler context = new ContextHandler("/test.json");
context.setHandler(new AbstractHandler() {
@Override
public void handle(String s, HttpServletRequest request, HttpServletResponse response, int i) throws IOException {
ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer();
writer.write("{\"name\":\"Github\"}");
writer.flush();
response.setContentLength(writer.size());
OutputStream out = response.getOutputStream();
writer.writeTo(out);
out.flush();
Request baseRequest = request instanceof Request ? (Request) request : HttpConnection.getCurrentConnection().getRequest();
baseRequest.setHandled(true);
}
});
server.setHandler(context);
logger.info("server for " + context.getBaseResource());
server.start();
serverPort = server.getConnectors()[0].getLocalPort();
logger.info("server is starting in port: " + serverPort);
}
protected void tearDown() throws Exception {
logger.info("server stopping...");
server.stop();
}
}

80
dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/LocalServerHttpUtilsTest.java

@ -0,0 +1,80 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dolphinscheduler.common.utils;
import com.fasterxml.jackson.databind.node.ObjectNode;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.dolphinscheduler.common.Constants;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.junit.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LocalServerHttpUtilsTest extends TestCase{
public static final Logger logger = LoggerFactory.getLogger(LocalServerHttpUtilsTest.class);
private static LocalJettyHttpServer server = null;
public static Test suite(){
TestSuite suite=new TestSuite();
suite.addTestSuite(LocalServerHttpUtilsTest.class);
server = new LocalJettyHttpServer(suite);
return server;
}
public void testGetTest() throws Exception {
// success
String result = null;
result = HttpUtils.get("http://localhost:" + server.getServerPort()+ "/test.json");
Assert.assertNotNull(result);
ObjectNode jsonObject = JSONUtils.parseObject(result);
Assert.assertEquals("Github",jsonObject.path("name").asText());
result = HttpUtils.get("http://123.333.111.33/ccc");
Assert.assertNull(result);
}
public void testGetResponseContentString() {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet("http://localhost:" +server.getServerPort()+"/test.json");
/** set timeout、request time、socket timeout */
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(Constants.HTTP_CONNECT_TIMEOUT)
.setConnectionRequestTimeout(Constants.HTTP_CONNECTION_REQUEST_TIMEOUT)
.setSocketTimeout(Constants.SOCKET_TIMEOUT).setRedirectsEnabled(true).build();
httpget.setConfig(requestConfig);
String responseContent = null;
responseContent = HttpUtils.getResponseContentString(httpget, httpclient);
Assert.assertNotNull(responseContent);
responseContent = HttpUtils.getResponseContentString(null, httpclient);
Assert.assertNull(responseContent);
responseContent = HttpUtils.getResponseContentString(httpget, null);
Assert.assertNull(responseContent);
}
public void testGetHttpClient() {
CloseableHttpClient httpClient1 = HttpUtils.getInstance();
CloseableHttpClient httpClient2 = HttpUtils.getInstance();
Assert.assertEquals(httpClient1, httpClient2);
}
}
Loading…
Cancel
Save