This is an automated email from the ASF dual-hosted git repository.

technoboy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere-elasticjob.git


The following commit(s) were added to refs/heads/master by this push:
     new fe7973a  There is a very low probability to reproduce 
SocketTimeoutException when executing testcase of HttpJobExecutor (#1405) 
(#1427)
fe7973a is described below

commit fe7973a67adee724d08eb392252822c0327aa19f
Author: Tboy <[email protected]>
AuthorDate: Sun Aug 30 19:25:47 2020 +0800

    There is a very low probability to reproduce SocketTimeoutException when 
executing testcase of HttpJobExecutor (#1405) (#1427)
---
 .../elasticjob-http-executor/pom.xml               |  7 ++
 .../http/executor/HttpJobExecutorTest.java         | 55 ++++++++++++---
 .../http/executor/fixture/InternalController.java  | 79 ++++++++++++++++++++++
 3 files changed, 133 insertions(+), 8 deletions(-)

diff --git 
a/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/pom.xml 
b/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/pom.xml
index d5020c0..3077e60 100644
--- 
a/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/pom.xml
+++ 
b/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/pom.xml
@@ -34,6 +34,13 @@
             <artifactId>elasticjob-executor-kernel</artifactId>
             <version>${project.parent.version}</version>
         </dependency>
+    
+        <dependency>
+            <groupId>org.apache.shardingsphere.elasticjob</groupId>
+            <artifactId>elasticjob-restful</artifactId>
+            <version>${project.parent.version}</version>
+            <scope>test</scope>
+        </dependency>
         
         <dependency>
             <groupId>org.projectlombok</groupId>
diff --git 
a/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/src/test/java/org/apache/shardingsphere/elasticjob/http/executor/HttpJobExecutorTest.java
 
b/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/src/test/java/org/apache/shardingsphere/elasticjob/http/executor/HttpJobExecutorTest.java
index da5982e..e4c6243 100644
--- 
a/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/src/test/java/org/apache/shardingsphere/elasticjob/http/executor/HttpJobExecutorTest.java
+++ 
b/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/src/test/java/org/apache/shardingsphere/elasticjob/http/executor/HttpJobExecutorTest.java
@@ -21,10 +21,16 @@ import org.apache.shardingsphere.elasticjob.api.ElasticJob;
 import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
 import org.apache.shardingsphere.elasticjob.api.ShardingContext;
 import org.apache.shardingsphere.elasticjob.executor.JobFacade;
+import 
org.apache.shardingsphere.elasticjob.http.executor.fixture.InternalController;
 import org.apache.shardingsphere.elasticjob.http.props.HttpJobProperties;
 import 
org.apache.shardingsphere.elasticjob.infra.exception.JobConfigurationException;
 import 
org.apache.shardingsphere.elasticjob.infra.exception.JobExecutionException;
+import org.apache.shardingsphere.elasticjob.restful.NettyRestfulService;
+import 
org.apache.shardingsphere.elasticjob.restful.NettyRestfulServiceConfiguration;
+import org.apache.shardingsphere.elasticjob.restful.RestfulService;
+import org.junit.AfterClass;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
@@ -39,6 +45,12 @@ import static org.mockito.Mockito.when;
 @RunWith(MockitoJUnitRunner.class)
 public final class HttpJobExecutorTest {
     
+    private static final int PORT = 9876;
+    
+    private static final String HOST = "localhost";
+    
+    private static RestfulService restfulService;
+    
     @Mock
     private ElasticJob elasticJob;
     
@@ -56,12 +68,28 @@ public final class HttpJobExecutorTest {
     
     private HttpJobExecutor jobExecutor;
     
+    @BeforeClass
+    public static void init() {
+        NettyRestfulServiceConfiguration configuration = new 
NettyRestfulServiceConfiguration(PORT);
+        configuration.setHost(HOST);
+        configuration.addControllerInstance(new InternalController());
+        restfulService = new NettyRestfulService(configuration);
+        restfulService.startup();
+    }
+    
     @Before
     public void setUp() {
         when(jobConfig.getProps()).thenReturn(properties);
         jobExecutor = new HttpJobExecutor();
     }
     
+    @AfterClass
+    public static void close() {
+        if (null != restfulService) {
+            restfulService.shutdown();
+        }
+    }
+    
     @Test(expected = JobConfigurationException.class)
     public void assertUrlEmpty() {
         
when(jobConfig.getProps().getProperty(HttpJobProperties.URI_KEY)).thenReturn("");
@@ -70,14 +98,14 @@ public final class HttpJobExecutorTest {
     
     @Test(expected = JobConfigurationException.class)
     public void assertMethodEmpty() {
-        
when(jobConfig.getProps().getProperty(HttpJobProperties.URI_KEY)).thenReturn("https://github.com";);
+        
when(jobConfig.getProps().getProperty(HttpJobProperties.URI_KEY)).thenReturn(getRequestUri("/getName"));
         
when(jobConfig.getProps().getProperty(HttpJobProperties.METHOD_KEY)).thenReturn("");
         jobExecutor.process(elasticJob, jobConfig, jobFacade, shardingContext);
     }
     
     @Test(expected = JobExecutionException.class)
     public void assertProcessWithoutSuccessCode() {
-        
when(jobConfig.getProps().getProperty(HttpJobProperties.URI_KEY)).thenReturn("https://github.com/apache/shardingsphere-elasticjob2";);
+        
when(jobConfig.getProps().getProperty(HttpJobProperties.URI_KEY)).thenReturn(getRequestUri("/unknownMethod"));
         
when(jobConfig.getProps().getProperty(HttpJobProperties.METHOD_KEY)).thenReturn("GET");
         
when(jobConfig.getProps().getProperty(HttpJobProperties.DATA_KEY)).thenReturn("");
         
when(jobConfig.getProps().getProperty(HttpJobProperties.CONNECT_TIMEOUT_KEY, 
"3000")).thenReturn("4000");
@@ -87,7 +115,7 @@ public final class HttpJobExecutorTest {
     
     @Test
     public void assertProcessWithGet() {
-        
when(jobConfig.getProps().getProperty(HttpJobProperties.URI_KEY)).thenReturn("https://github.com";);
+        
when(jobConfig.getProps().getProperty(HttpJobProperties.URI_KEY)).thenReturn(getRequestUri("/getName"));
         
when(jobConfig.getProps().getProperty(HttpJobProperties.METHOD_KEY)).thenReturn("GET");
         
when(jobConfig.getProps().getProperty(HttpJobProperties.DATA_KEY)).thenReturn("");
         
when(jobConfig.getProps().getProperty(HttpJobProperties.CONNECT_TIMEOUT_KEY, 
"3000")).thenReturn("4000");
@@ -96,10 +124,19 @@ public final class HttpJobExecutorTest {
     }
     
     @Test
+    public void assertProcessHeader() {
+        
when(jobConfig.getProps().getProperty(HttpJobProperties.URI_KEY)).thenReturn(getRequestUri("/getShardingContext"));
+        
when(jobConfig.getProps().getProperty(HttpJobProperties.METHOD_KEY)).thenReturn("GET");
+        
when(jobConfig.getProps().getProperty(HttpJobProperties.CONNECT_TIMEOUT_KEY, 
"3000")).thenReturn("4000");
+        
when(jobConfig.getProps().getProperty(HttpJobProperties.READ_TIMEOUT_KEY, 
"5000")).thenReturn("5000");
+        jobExecutor.process(elasticJob, jobConfig, jobFacade, shardingContext);
+    }
+    
+    @Test
     public void assertProcessWithPost() {
-        
when(jobConfig.getProps().getProperty(HttpJobProperties.URI_KEY)).thenReturn("http://www.baidu.com";);
+        
when(jobConfig.getProps().getProperty(HttpJobProperties.URI_KEY)).thenReturn(getRequestUri("/updateName"));
         
when(jobConfig.getProps().getProperty(HttpJobProperties.METHOD_KEY)).thenReturn("POST");
-        
when(jobConfig.getProps().getProperty(HttpJobProperties.DATA_KEY)).thenReturn("login=test&password=123");
+        
when(jobConfig.getProps().getProperty(HttpJobProperties.DATA_KEY)).thenReturn("name=elasticjob");
         
when(jobConfig.getProps().getProperty(HttpJobProperties.CONNECT_TIMEOUT_KEY, 
"3000")).thenReturn("4000");
         
when(jobConfig.getProps().getProperty(HttpJobProperties.READ_TIMEOUT_KEY, 
"5000")).thenReturn("5000");
         
when(jobConfig.getProps().getProperty(HttpJobProperties.CONTENT_TYPE_KEY)).thenReturn("application/x-www-form-urlencoded");
@@ -108,12 +145,11 @@ public final class HttpJobExecutorTest {
     
     @Test(expected = JobExecutionException.class)
     public void assertProcessWithIOException() {
-        
when(jobConfig.getProps().getProperty(HttpJobProperties.URI_KEY)).thenReturn("http://www.baidu.com";);
+        
when(jobConfig.getProps().getProperty(HttpJobProperties.URI_KEY)).thenReturn(getRequestUri("/postWithTimeout"));
         
when(jobConfig.getProps().getProperty(HttpJobProperties.METHOD_KEY)).thenReturn("POST");
-        
when(jobConfig.getProps().getProperty(HttpJobProperties.DATA_KEY)).thenReturn("login=test&password=123");
+        
when(jobConfig.getProps().getProperty(HttpJobProperties.DATA_KEY)).thenReturn("name=elasticjob");
         
when(jobConfig.getProps().getProperty(HttpJobProperties.CONNECT_TIMEOUT_KEY, 
"3000")).thenReturn("1");
         
when(jobConfig.getProps().getProperty(HttpJobProperties.READ_TIMEOUT_KEY, 
"5000")).thenReturn("1");
-        
when(jobConfig.getProps().getProperty(HttpJobProperties.CONTENT_TYPE_KEY)).thenReturn("application/x-www-form-urlencoded");
         jobExecutor.process(elasticJob, jobConfig, jobFacade, shardingContext);
     }
     
@@ -122,4 +158,7 @@ public final class HttpJobExecutorTest {
         assertThat(jobExecutor.getType(), is("HTTP"));
     }
     
+    private String getRequestUri(final String path) {
+        return "http://"; + HOST + ":" + PORT + path;
+    }
 }
diff --git 
a/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/src/test/java/org/apache/shardingsphere/elasticjob/http/executor/fixture/InternalController.java
 
b/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/src/test/java/org/apache/shardingsphere/elasticjob/http/executor/fixture/InternalController.java
new file mode 100644
index 0000000..9c3edee
--- /dev/null
+++ 
b/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/src/test/java/org/apache/shardingsphere/elasticjob/http/executor/fixture/InternalController.java
@@ -0,0 +1,79 @@
+/*
+ * 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.shardingsphere.elasticjob.http.executor.fixture;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.shardingsphere.elasticjob.restful.Http;
+import org.apache.shardingsphere.elasticjob.restful.RestfulController;
+import org.apache.shardingsphere.elasticjob.restful.annotation.Mapping;
+import org.apache.shardingsphere.elasticjob.restful.annotation.Param;
+import org.apache.shardingsphere.elasticjob.restful.annotation.ParamSource;
+
+import java.util.Objects;
+
+@Slf4j
+public final class InternalController implements RestfulController {
+    
+    /**
+     * Get name.
+     *
+     * @return "ejob"
+     */
+    @Mapping(method = Http.GET, path = "/getName")
+    public String getName() {
+        return "ejob";
+    }
+    
+    /**
+     * Get sharding context.
+     *
+     * @param shardingContext the shardingContext
+     * @return shardingContext
+     */
+    @Mapping(method = Http.GET, path = "/getShardingContext")
+    public String getShardingContext(@Param(name = "shardingContext", source = 
ParamSource.HEADER) final String shardingContext) {
+        Objects.nonNull(shardingContext);
+        return shardingContext;
+    }
+    
+    /**
+     * Update name.
+     *
+     * @param updateName the name
+     * @return the updated name
+     */
+    @Mapping(method = Http.POST, path = "/{updateName}")
+    public String postName(@Param(name = "updateName", source = 
ParamSource.PATH) final String updateName) {
+        Objects.nonNull(updateName);
+        return updateName;
+    }
+    
+    /**
+     * Post with 3 mills delay for request IO Exception.
+     *
+     * @return "ejob"
+     */
+    @Mapping(method = Http.POST, path = "/postWithTimeout")
+    public String postWithTimeout() {
+        try {
+            Thread.sleep(3);
+        } catch (InterruptedException ignore) {
+        }
+        return "ejob";
+    }
+}

Reply via email to