THRIFT-4949: Improve HTTP/1 server test case

Client: java

This closes #1886.
This commit is contained in:
pengzhouhu 2019-10-21 22:21:11 +08:00 committed by Duru Can Celasun
parent 3f3567a114
commit 6e4c581fdd
7 changed files with 146 additions and 6 deletions

View File

@ -28,7 +28,9 @@ maven-repository-id=apache.releases.https
httpclient.version=4.5.10
httpcore.version=4.4.12
slf4j.version=1.7.28
servlet.version=2.5
#servlet.version=2.5
#It contains servlet3
tomcat.embed.version=8.5.46
junit.version=4.12
mockito.version=1.10.19
javax.annotation.version=1.3.2

View File

@ -44,7 +44,8 @@ ext.mavenRepositoryUrl = property('maven-repository-url')
// Versions used in this project
ext.httpclientVersion = property('httpclient.version')
ext.httpcoreVersion = property('httpcore.version')
ext.servletVersion = property('servlet.version')
//ext.servletVersion = property('servlet.version')
ext.tomcatEmbedVersion = property('tomcat.embed.version')
ext.slf4jVersion = property('slf4j.version')
ext.junitVersion = property('junit.version')
ext.mockitoVersion = property('mockito.version')
@ -66,7 +67,8 @@ dependencies {
compile "org.slf4j:slf4j-api:${slf4jVersion}"
compile "org.apache.httpcomponents:httpclient:${httpclientVersion}"
compile "org.apache.httpcomponents:httpcore:${httpcoreVersion}"
compile "javax.servlet:servlet-api:${servletVersion}"
//compile "javax.servlet:servlet-api:${servletVersion}"
compile "org.apache.tomcat.embed:tomcat-embed-core:${tomcatEmbedVersion}"
compile "javax.annotation:javax.annotation-api:${javaxAnnotationVersion}"
testCompile "junit:junit:${junitVersion}"

View File

@ -34,6 +34,7 @@ sourceSets {
include '**/test/TestClient.java'
include '**/test/TestServer.java'
include '**/test/TestNonblockingServer.java'
include '**/test/TestTServletServer.java'
}
}
}
@ -55,7 +56,7 @@ dependencies {
shadowJar {
description = 'Assemble a test JAR file for cross-check execution'
// make sure the runners are created when this runs
dependsOn 'generateRunnerScriptForClient', 'generateRunnerScriptForServer', 'generateRunnerScriptForNonblockingServer'
dependsOn 'generateRunnerScriptForClient', 'generateRunnerScriptForServer', 'generateRunnerScriptForNonblockingServer', 'generateRunnerScriptForTServletServer'
baseName = 'functionalTest'
destinationDir = file("$buildDir/functionalTestJar")
@ -153,3 +154,24 @@ ${scriptHead}
serverFile.setExecutable(true, false)
}
}
task generateRunnerScriptForTServletServer(group: 'Build') {
description = 'Generate a runner script for cross-check tests with TestTServletServer'
def serverFile = file("$buildDir/runservletserver${scriptExt}")
def runServerText = """\
${scriptHead}
"${javaExe}" -cp "$jarPath" "-Djavax.net.ssl.keyStore=$keyStore" -Djavax.net.ssl.keyStorePassword=thrift org.apache.thrift.test.TestTServletServer $args
"""
inputs.property 'runServerText', runServerText
outputs.file serverFile
doLast {
serverFile.parentFile.mkdirs()
serverFile.text = runServerText
serverFile.setExecutable(true, false)
}
}

View File

@ -34,6 +34,7 @@ sourceSets {
exclude '**/test/TestClient.java'
exclude '**/test/TestServer.java'
exclude '**/test/TestNonblockingServer.java'
exclude '**/test/TestTServletServer.java'
}
resources {
srcDir 'test'

View File

@ -27,6 +27,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.http.impl.client.HttpClients;
import org.apache.thrift.TApplicationException;
import org.apache.thrift.TException;
import org.apache.thrift.TSerializer;
@ -76,6 +77,7 @@ public class TestClient {
String protocol_type = "binary";
String transport_type = "buffered";
boolean ssl = false;
boolean http_client = false;
int socketTimeout = 1000;
@ -99,6 +101,8 @@ public class TestClient {
transport_type.trim();
} else if (args[i].equals("--ssl")) {
ssl = true;
} else if (args[i].equals("--client")) {
http_client = true;
} else if (args[i].equals("--help")) {
System.out.println("Allowed options:");
System.out.println(" --help\t\t\tProduce help message");
@ -145,8 +149,13 @@ public class TestClient {
try {
if (transport_type.equals("http")) {
String url = "http://" + host + ":" + port + "/service";
transport = new THttpClient(url);
String url = "http://" + host + ":" + port + "/test/service";
if (http_client == true) {
transport = new THttpClient(url, HttpClients.createDefault());
} else {
transport = new THttpClient(url);
}
} else {
TSocket socket = null;
if (ssl == true) {

View File

@ -0,0 +1,52 @@
/*
* 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.thrift.test;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.server.ServerTestBase.TestHandler;
import org.apache.thrift.server.TExtensibleServlet;
import thrift.test.ThriftTest;
@SuppressWarnings("serial")
public class TestServlet extends TExtensibleServlet {
@Override
protected TProtocolFactory getInProtocolFactory(){
TProtocolFactory tProtocolFactory = new TCompactProtocol.Factory();
return tProtocolFactory;
}
@Override
protected TProtocolFactory getOutProtocolFactory(){
TProtocolFactory tProtocolFactory = new TCompactProtocol.Factory();
return tProtocolFactory;
}
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
protected TProcessor getProcessor(){
TestHandler testHandler = new TestHandler();
ThriftTest.Processor testProcessor = new ThriftTest.Processor(testHandler);
return testProcessor;
}
}

View File

@ -0,0 +1,52 @@
/*
* 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.thrift.test;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.Tomcat.FixContextListener;
/**
* run tomcat for test TServlet
*/
public class TestTServletServer {
static final int port = 9090;
public static void main(String [] args) throws Exception{
Tomcat tomcat = new Tomcat();
tomcat.setPort( port );
tomcat.setBaseDir(System.getProperty("user.dir")+"\\build");
tomcat.getHost().setAutoDeploy( false );
String contextPath = "/test";
StandardContext context = new StandardContext();
context.setPath( contextPath );
context.addLifecycleListener( new FixContextListener() );
tomcat.getHost().addChild( context );
tomcat.addServlet( contextPath, "testServlet", new TestServlet() );
context.addServletMappingDecoded( "/service", "testServlet");
tomcat.start();
tomcat.getServer().await();
}
}