jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pmoua...@apache.org
Subject svn commit: r1200210 - in /jmeter/trunk: src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/ xdocs/
Date Thu, 10 Nov 2011 08:25:33 GMT
Author: pmouawad
Date: Thu Nov 10 08:25:32 2011
New Revision: 1200210

URL: http://svn.apache.org/viewvc?rev=1200210&view=rev
Log:
Bug 52104 - TCP Sampler handles badly errors
Bug 52087 - TCPClient interface does not allow for partial reads

Added:
    jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/ReadException.java
  (with props)
Modified:
    jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/BinaryTCPClientImpl.java
    jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/LengthPrefixedBinaryTCPClientImpl.java
    jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPClient.java
    jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPClientImpl.java
    jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPSampler.java
    jmeter/trunk/xdocs/changes.xml

Modified: jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/BinaryTCPClientImpl.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/BinaryTCPClientImpl.java?rev=1200210&r1=1200209&r2=1200210&view=diff
==============================================================================
--- jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/BinaryTCPClientImpl.java
(original)
+++ jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/BinaryTCPClientImpl.java
Thu Nov 10 08:25:32 2011
@@ -115,23 +115,27 @@ public class BinaryTCPClientImpl extends
      * @return hex-encoded binary string
      * @throws IOException 
      */
-    public String read(InputStream is) throws IOException {
-        byte[] buffer = new byte[4096];
-        ByteArrayOutputStream w = new ByteArrayOutputStream();
-        int x = 0;
-        while ((x = is.read(buffer)) > -1) {
-            w.write(buffer, 0, x);
-            if (useEolByte && (buffer[x - 1] == eolByte)) {
-                break;
-            }
-        }
+    public String read(InputStream is) throws ReadException {
+    	ByteArrayOutputStream w = new ByteArrayOutputStream();
+        try {
+			byte[] buffer = new byte[4096];
+			int x = 0;
+			while ((x = is.read(buffer)) > -1) {
+			    w.write(buffer, 0, x);
+			    if (useEolByte && (buffer[x - 1] == eolByte)) {
+			        break;
+			    }
+			}
 
-        IOUtils.closeQuietly(w); // For completeness
-        final String hexString = JOrphanUtils.baToHexString(w.toByteArray());
-        if(log.isDebugEnabled()) {
-            log.debug("Read: " + w.size() + "\n" + hexString);
-        }
-        return hexString;
+			IOUtils.closeQuietly(w); // For completeness
+			final String hexString = JOrphanUtils.baToHexString(w.toByteArray());
+			if(log.isDebugEnabled()) {
+			    log.debug("Read: " + w.size() + "\n" + hexString);
+			}
+			return hexString;
+		} catch (IOException e) {
+			throw new ReadException("", e, JOrphanUtils.baToHexString(w.toByteArray()));
+		}
     }
 
 }

Modified: jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/LengthPrefixedBinaryTCPClientImpl.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/LengthPrefixedBinaryTCPClientImpl.java?rev=1200210&r1=1200209&r2=1200210&view=diff
==============================================================================
--- jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/LengthPrefixedBinaryTCPClientImpl.java
(original)
+++ jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/LengthPrefixedBinaryTCPClientImpl.java
Thu Nov 10 08:25:32 2011
@@ -73,24 +73,29 @@ public class LengthPrefixedBinaryTCPClie
     /**
      * {@inheritDoc}
      */
-    public String read(InputStream is) throws IOException{
+    public String read(InputStream is) throws ReadException{
         byte[] msg = new byte[0];
         int msgLen = 0;
         byte[] lengthBuffer = new byte[lengthPrefixLen];
-        if (is.read(lengthBuffer, 0, lengthPrefixLen) == lengthPrefixLen) {
-            msgLen = byteArrayToInt(lengthBuffer);
-            msg = new byte[msgLen];
-            int bytes = JOrphanUtils.read(is, msg, 0, msgLen);
-            if (bytes < msgLen) {
-                log.warn("Incomplete message read, expected: "+msgLen+" got: "+bytes);
-            }
+        try {
+	        if (is.read(lengthBuffer, 0, lengthPrefixLen) == lengthPrefixLen) {
+	            msgLen = byteArrayToInt(lengthBuffer);
+	            msg = new byte[msgLen];
+	            int bytes = JOrphanUtils.read(is, msg, 0, msgLen);
+	            if (bytes < msgLen) {
+	                log.warn("Incomplete message read, expected: "+msgLen+" got: "+bytes);
+	            }
+	        }
+	
+	        String buffer = JOrphanUtils.baToHexString(msg);
+	        if(log.isDebugEnabled()) {
+	            log.debug("Read: " + msgLen + "\n" + buffer);
+	        }
+	        return buffer;
+        } 
+        catch(IOException e) {
+        	throw new ReadException("", e, JOrphanUtils.baToHexString(msg));
         }
-
-        String buffer = JOrphanUtils.baToHexString(msg);
-        if(log.isDebugEnabled()) {
-            log.debug("Read: " + msgLen + "\n" + buffer);
-        }
-        return buffer;
     }
 
     /**

Added: jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/ReadException.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/ReadException.java?rev=1200210&view=auto
==============================================================================
--- jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/ReadException.java
(added)
+++ jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/ReadException.java
Thu Nov 10 08:25:32 2011
@@ -0,0 +1,46 @@
+/*
+ * 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.jmeter.protocol.tcp.sampler;
+
+/**
+ * Exception that contains partial response (Text read until exception occured)
+ */
+public class ReadException extends Exception {
+
+	private static final long serialVersionUID = -2770054697780959330L;
+	private String partialResponse;
+
+
+	/**
+	 * Constructor
+	 * @param message Message
+	 * @param cause Source cause
+	 * @param partialResponse  Text read until error occured
+	 */
+	public ReadException(String message, Throwable cause, String partialResponse) {
+		super(message, cause);
+		this.partialResponse = partialResponse;
+	}
+
+	/**
+	 * @return the partialResponse Text read until error occured
+	 */
+	public String getPartialResponse() {
+		return partialResponse;
+	}
+}
\ No newline at end of file

Propchange: jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/ReadException.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPClient.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPClient.java?rev=1200210&r1=1200209&r2=1200210&view=diff
==============================================================================
--- jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPClient.java (original)
+++ jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPClient.java Thu
Nov 10 08:25:32 2011
@@ -66,8 +66,9 @@ public interface TCPClient {
      * @param is -
      *            InputStream for socket
      * @return String read from socket
+     * @throws ReadException exception that can contain partial response (Response until
error occured)
      */
-    String read(InputStream is) throws IOException;
+    String read(InputStream is) throws ReadException;
 
     /**
      * Get the end-of-line/end-of-message byte.
@@ -83,5 +84,4 @@ public interface TCPClient {
      *            The value to set
      */
     public void setEolByte(int eolInt);
-
 }
\ No newline at end of file

Modified: jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPClientImpl.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPClientImpl.java?rev=1200210&r1=1200209&r2=1200210&view=diff
==============================================================================
--- jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPClientImpl.java
(original)
+++ jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPClientImpl.java
Thu Nov 10 08:25:32 2011
@@ -83,21 +83,25 @@ public class TCPClientImpl extends Abstr
      * If there is no EOL byte defined, then reads until
      * the end of the stream is reached.
      */
-    public String read(InputStream is) throws IOException{
-        byte[] buffer = new byte[4096];
-        ByteArrayOutputStream w = new ByteArrayOutputStream();
-        int x = 0;
-        while ((x = is.read(buffer)) > -1) {
-            w.write(buffer, 0, x);
-            if (useEolByte && (buffer[x - 1] == eolByte)) {
-                break;
-            }
-        }
+    public String read(InputStream is) throws ReadException{
+    	ByteArrayOutputStream w = new ByteArrayOutputStream();
+        try {
+			byte[] buffer = new byte[4096];
+			int x = 0;
+			while ((x = is.read(buffer)) > -1) {
+			    w.write(buffer, 0, x);
+			    if (useEolByte && (buffer[x - 1] == eolByte)) {
+			        break;
+			    }
+			}
 
-        // do we need to close byte array (or flush it?)
-        if(log.isDebugEnabled()) {
-            log.debug("Read: " + w.size() + "\n" + w.toString());
-        }
-        return w.toString();
+			// do we need to close byte array (or flush it?)
+			if(log.isDebugEnabled()) {
+			    log.debug("Read: " + w.size() + "\n" + w.toString());
+			}
+			return w.toString();
+		} catch (IOException e) {
+			throw new ReadException("", e, w.toString());
+		}
     }
 }

Modified: jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPSampler.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPSampler.java?rev=1200210&r1=1200209&r2=1200210&view=diff
==============================================================================
--- jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPSampler.java (original)
+++ jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPSampler.java Thu
Nov 10 08:25:32 2011
@@ -33,12 +33,13 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.jmeter.config.ConfigTestElement;
-import org.apache.jmeter.util.JMeterUtils;
 import org.apache.jmeter.samplers.AbstractSampler;
 import org.apache.jmeter.samplers.Entry;
 import org.apache.jmeter.samplers.SampleResult;
 import org.apache.jmeter.testelement.ThreadListener;
+import org.apache.jmeter.util.JMeterUtils;
 import org.apache.jorphan.logging.LoggingManager;
 import org.apache.jorphan.util.JOrphanUtils;
 import org.apache.log.Logger;
@@ -339,42 +340,15 @@ public class TCPSampler extends Abstract
                 res.setSamplerData(req);
                 protocolHandler.write(os, req);
                 String in = protocolHandler.read(is);
-                res.setResponseData(in, null);
-                res.setDataType(SampleResult.TEXT);
-                res.setResponseCodeOK();
-                res.setResponseMessage("OK"); //$NON-NLS-1$
-                isSuccessful = true;
-                // Reset the status code if the message contains one
-                if (STATUS_PREFIX.length() > 0) {
-                    int i = in.indexOf(STATUS_PREFIX);
-                    int j = in.indexOf(STATUS_SUFFIX, i + STATUS_PREFIX.length());
-                    if (i != -1 && j > i) {
-                        String rc = in.substring(i + STATUS_PREFIX.length(), j);
-                        res.setResponseCode(rc);
-                        isSuccessful = checkResponseCode(rc);
-                        if (haveStatusProps) {
-                            res.setResponseMessage(statusProps.getProperty(rc, "Status code
not found in properties")); //$NON-NLS-1$
-                        } else {
-                            res.setResponseMessage("No status property file");
-                        }
-                    } else {
-                        res.setResponseCode("999"); //$NON-NLS-1$
-                        res.setResponseMessage("Status value not found");
-                        isSuccessful = false;
-                    }
-                }
+                isSuccessful = setupSampleResult(res, in, null);
             }
-        } catch (IOException ex) {
-            log.debug("", ex);
-            isSuccessful=false;
-            res.setResponseCode("500"); //$NON-NLS-1$
-            res.setResponseMessage(ex.toString());
+        } catch (ReadException ex) {
+            log.error("", ex);
+            isSuccessful=setupSampleResult(res, ex.getPartialResponse(), ex);
             closeSocket();
         } catch (Exception ex) {
             log.error("", ex);
-            isSuccessful=false;
-            res.setResponseCode("500");
-            res.setResponseMessage(ex.toString());
+            isSuccessful=setupSampleResult(res, "", ex);
             closeSocket();
         } finally {
             // Calculate response time
@@ -387,10 +361,51 @@ public class TCPSampler extends Abstract
                 closeSocket();
             }
         }
-
         return res;
     }
 
+	/**
+	 * Fills SampleResult object
+	 * @param sampleResult {@link SampleResult}
+	 * @param readResponse Response read until error occured
+	 * @param exception Source exception
+	 * @return boolean if sample is considered as successful
+	 */
+	private boolean setupSampleResult(SampleResult sampleResult,
+			String readResponse, 
+			Exception exception) {
+		sampleResult.setResponseData(readResponse, null);
+		sampleResult.setDataType(SampleResult.TEXT);
+		if(exception==null) {
+			sampleResult.setResponseCodeOK();
+			sampleResult.setResponseMessage("OK"); //$NON-NLS-1$
+		} else {
+			sampleResult.setResponseCode("500"); //$NON-NLS-1$
+			sampleResult.setResponseMessage(exception.toString()); //$NON-NLS-1$
+		}
+		boolean isSuccessful = exception == null;
+		// Reset the status code if the message contains one
+		if (!StringUtils.isEmpty(readResponse) && STATUS_PREFIX.length() > 0) {
+		    int i = readResponse.indexOf(STATUS_PREFIX);
+		    int j = readResponse.indexOf(STATUS_SUFFIX, i + STATUS_PREFIX.length());
+		    if (i != -1 && j > i) {
+		        String rc = readResponse.substring(i + STATUS_PREFIX.length(), j);
+		        sampleResult.setResponseCode(rc);
+		        isSuccessful = isSuccessful && checkResponseCode(rc);
+		        if (haveStatusProps) {
+		            sampleResult.setResponseMessage(statusProps.getProperty(rc, "Status code not
found in properties")); //$NON-NLS-1$
+		        } else {
+		            sampleResult.setResponseMessage("No status property file");
+		        }
+		    } else {
+		        sampleResult.setResponseCode("999"); //$NON-NLS-1$
+		        sampleResult.setResponseMessage("Status value not found");
+		        isSuccessful = false;
+		    }
+		}
+		return isSuccessful;
+	}
+
     /**
      * @param rc response code
      * @return whether this represents success or not

Modified: jmeter/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/jmeter/trunk/xdocs/changes.xml?rev=1200210&r1=1200209&r2=1200210&view=diff
==============================================================================
--- jmeter/trunk/xdocs/changes.xml (original)
+++ jmeter/trunk/xdocs/changes.xml Thu Nov 10 08:25:32 2011
@@ -161,6 +161,7 @@ these occurs, Sampler is marked as faile
 <li>Bug 51419 - JMS Subscriber: ability to use Selectors</li>
 <li>Bug 52088 - JMS Sampler : Add a selector when REQUEST / RESPONSE is chosen</li>
 <li>Bug 52104 - TCP Sampler handles badly errors</li>
+<li>Bug 52087 - TCPClient interface does not allow for partial reads</li>
 <li>Bug 52115 - SOAP/XML-RPC should not send a POST request when file to send is not
found</li>
 </ul>
 



Mime
View raw message