Author: pmouawad
Date: Thu Oct 27 20:45:11 2011
New Revision: 1190014

URL: http://svn.apache.org/viewvc?rev=1190014&view=rev
Log:
Bug 52104 - TCP Sampler handles badly errors

Modified:
    
jakarta/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/BinaryTCPClientImpl.java
    
jakarta/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/LengthPrefixedBinaryTCPClientImpl.java
    
jakarta/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPClient.java
    
jakarta/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPClientImpl.java
    
jakarta/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPSampler.java
    jakarta/jmeter/trunk/xdocs/changes.xml

Modified: 
jakarta/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/BinaryTCPClientImpl.java
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/BinaryTCPClientImpl.java?rev=1190014&r1=1190013&r2=1190014&view=diff
==============================================================================
--- 
jakarta/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/BinaryTCPClientImpl.java
 (original)
+++ 
jakarta/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/BinaryTCPClientImpl.java
 Thu Oct 27 20:45:11 2011
@@ -27,9 +27,7 @@ package org.apache.jmeter.protocol.tcp.s
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.InterruptedIOException;
 import java.io.OutputStream;
-import java.net.SocketTimeoutException;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.jmeter.util.JMeterUtils;
@@ -93,15 +91,12 @@ public class BinaryTCPClientImpl extends
      * @param os output stream
      * @param hexEncodedBinary hex-encoded binary
      */
-    public void write(OutputStream os, String hexEncodedBinary) {
-        try {
-            os.write(hexStringToByteArray(hexEncodedBinary));
-            os.flush();
-        } catch (IOException e) {
-            log.warn("Write error", e);
+    public void write(OutputStream os, String hexEncodedBinary) throws 
IOException{
+        os.write(hexStringToByteArray(hexEncodedBinary));
+        os.flush();
+        if(log.isDebugEnabled()) {
+            log.debug("Wrote: " + hexEncodedBinary);
         }
-        log.debug("Wrote: " + hexEncodedBinary);
-        return;
     }
 
     /**
@@ -118,30 +113,24 @@ public class BinaryTCPClientImpl extends
      * the end of the stream is reached.
      * Response data is converted to hex-encoded binary
      * @return hex-encoded binary string
+     * @throws IOException 
      */
-    public String read(InputStream is) {
+    public String read(InputStream is) throws IOException {
         byte[] buffer = new byte[4096];
         ByteArrayOutputStream w = new ByteArrayOutputStream();
         int x = 0;
-        try {
-            while ((x = is.read(buffer)) > -1) {
-                w.write(buffer, 0, x);
-                if (useEolByte && (buffer[x - 1] == eolByte)) {
-                    break;
-                }
+        while ((x = is.read(buffer)) > -1) {
+            w.write(buffer, 0, x);
+            if (useEolByte && (buffer[x - 1] == eolByte)) {
+                break;
             }
-        } catch (SocketTimeoutException e) {
-            // drop out to handle buffer
-        } catch (InterruptedIOException e) {
-            // drop out to handle buffer
-        } catch (IOException e) {
-            log.warn("Read error:" + e);
-            return "";
         }
 
         IOUtils.closeQuietly(w); // For completeness
         final String hexString = JOrphanUtils.baToHexString(w.toByteArray());
-        log.debug("Read: " + w.size() + "\n" + hexString);
+        if(log.isDebugEnabled()) {
+            log.debug("Read: " + w.size() + "\n" + hexString);
+        }
         return hexString;
     }
 

Modified: 
jakarta/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/LengthPrefixedBinaryTCPClientImpl.java
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/LengthPrefixedBinaryTCPClientImpl.java?rev=1190014&r1=1190013&r2=1190014&view=diff
==============================================================================
--- 
jakarta/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/LengthPrefixedBinaryTCPClientImpl.java
 (original)
+++ 
jakarta/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/LengthPrefixedBinaryTCPClientImpl.java
 Thu Oct 27 20:45:11 2011
@@ -30,9 +30,7 @@ package org.apache.jmeter.protocol.tcp.s
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.InterruptedIOException;
 import java.io.OutputStream;
-import java.net.SocketTimeoutException;
 
 import org.apache.jmeter.util.JMeterUtils;
 import org.apache.jorphan.logging.LoggingManager;
@@ -57,50 +55,41 @@ public class LengthPrefixedBinaryTCPClie
     /**
      * {@inheritDoc}
      */
-    public void write(OutputStream os, String s) {
-        try {
-            os.write(intToByteArray(s.length()/2,lengthPrefixLen));
+    public void write(OutputStream os, String s)  throws IOException{
+        os.write(intToByteArray(s.length()/2,lengthPrefixLen));
+        if(log.isDebugEnabled()) {
             log.debug("Wrote: " + s.length()/2 + " bytes");
-            this.tcpClient.write(os, s);
-        } catch (IOException e) {
-            log.warn("Write error", e);
         }
-        return;
+        this.tcpClient.write(os, s);        
     }
 
     /**
      * {@inheritDoc}
      */
-    public void write(OutputStream os, InputStream is) {
+    public void write(OutputStream os, InputStream is) throws IOException {
         this.tcpClient.write(os, is);
     }
 
     /**
      * {@inheritDoc}
      */
-    public String read(InputStream is) {
+    public String read(InputStream is) throws IOException{
         byte[] msg = new byte[0];
         int msgLen = 0;
-        try {
-            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);
-                }
+        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);
             }
-        } catch (SocketTimeoutException e) {
-            // drop out to handle buffer
-        } catch (InterruptedIOException e) {
-            // drop out to handle buffer
-        } catch (IOException e) {
-            log.warn("Read error:" + e);
         }
 
         String buffer = JOrphanUtils.baToHexString(msg);
-        log.debug("Read: " + msgLen + "\n" + buffer);
+        if(log.isDebugEnabled()) {
+            log.debug("Read: " + msgLen + "\n" + buffer);
+        }
         return buffer;
     }
 

Modified: 
jakarta/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPClient.java
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPClient.java?rev=1190014&r1=1190013&r2=1190014&view=diff
==============================================================================
--- 
jakarta/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPClient.java
 (original)
+++ 
jakarta/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPClient.java
 Thu Oct 27 20:45:11 2011
@@ -24,6 +24,7 @@
  */
 package org.apache.jmeter.protocol.tcp.sampler;
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 
@@ -49,7 +50,7 @@ public interface TCPClient {
      * @param is -
      *            InputStream to be written to Socket
      */
-    void write(OutputStream os, InputStream is);
+    void write(OutputStream os, InputStream is) throws IOException;
 
     /**
      *
@@ -58,7 +59,7 @@ public interface TCPClient {
      * @param s -
      *            String to write
      */
-    void write(OutputStream os, String s);
+    void write(OutputStream os, String s) throws IOException;
 
     /**
      *
@@ -66,7 +67,7 @@ public interface TCPClient {
      *            InputStream for socket
      * @return String read from socket
      */
-    String read(InputStream is);
+    String read(InputStream is) throws IOException;
 
     /**
      * Get the end-of-line/end-of-message byte.

Modified: 
jakarta/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPClientImpl.java
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPClientImpl.java?rev=1190014&r1=1190013&r2=1190014&view=diff
==============================================================================
--- 
jakarta/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPClientImpl.java
 (original)
+++ 
jakarta/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPClientImpl.java
 Thu Oct 27 20:45:11 2011
@@ -29,9 +29,7 @@ package org.apache.jmeter.protocol.tcp.s
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.InterruptedIOException;
 import java.io.OutputStream;
-import java.net.SocketTimeoutException;
 
 import org.apache.jmeter.util.JMeterUtils;
 import org.apache.jorphan.logging.LoggingManager;
@@ -61,29 +59,22 @@ public class TCPClientImpl extends Abstr
     /**
      * {@inheritDoc}
      */
-    public void write(OutputStream os, String s) {
-        try {
-            os.write(s.getBytes()); // TODO - charset?
-            os.flush();
-        } catch (IOException e) {
-            log.warn("Write error", e);
+    public void write(OutputStream os, String s)  throws IOException{
+        os.write(s.getBytes()); // TODO - charset?
+        os.flush();
+        if(log.isDebugEnabled()) {
+            log.debug("Wrote: " + s);
         }
-        log.debug("Wrote: " + s);
-        return;
     }
 
     /**
      * {@inheritDoc}
      */
-    public void write(OutputStream os, InputStream is) {
+    public void write(OutputStream os, InputStream is) throws IOException{
         byte buff[]=new byte[512];
-        try {
-            while(is.read(buff) > 0){
-                os.write(buff);
-                os.flush();
-            }
-        } catch (IOException e) {
-            log.warn("Write error", e);
+        while(is.read(buff) > 0){
+            os.write(buff);
+            os.flush();
         }
     }
 
@@ -92,28 +83,21 @@ 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) {
+    public String read(InputStream is) throws IOException{
         byte[] buffer = new byte[4096];
         ByteArrayOutputStream w = new ByteArrayOutputStream();
         int x = 0;
-        try {
-            while ((x = is.read(buffer)) > -1) {
-                w.write(buffer, 0, x);
-                if (useEolByte && (buffer[x - 1] == eolByte)) {
-                    break;
-                }
+        while ((x = is.read(buffer)) > -1) {
+            w.write(buffer, 0, x);
+            if (useEolByte && (buffer[x - 1] == eolByte)) {
+                break;
             }
-        } catch (SocketTimeoutException e) {
-            // drop out to handle buffer
-        } catch (InterruptedIOException e) {
-            // drop out to handle buffer
-        } catch (IOException e) {
-            log.warn("Read error:" + e);
-            return "";
         }
 
         // do we need to close byte array (or flush it?)
-        log.debug("Read: " + w.size() + "\n" + w.toString());
+        if(log.isDebugEnabled()) {
+            log.debug("Read: " + w.size() + "\n" + w.toString());
+        }
         return w.toString();
     }
 }

Modified: 
jakarta/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPSampler.java
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPSampler.java?rev=1190014&r1=1190013&r2=1190014&view=diff
==============================================================================
--- 
jakarta/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPSampler.java
 (original)
+++ 
jakarta/jmeter/trunk/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPSampler.java
 Thu Oct 27 20:45:11 2011
@@ -155,21 +155,27 @@ public class TCPSampler extends Abstract
                 SocketAddress sockaddr = new InetSocketAddress(getServer(), 
getPort());
                 con = new Socket();
                 con.connect(sockaddr, getConnectTimeout());
-                log.debug("Created new connection " + con); //$NON-NLS-1$
+                if(log.isDebugEnabled()) {
+                    log.debug("Created new connection " + con); //$NON-NLS-1$
+                }
                 cp.put(TCPKEY, con);
             } catch (UnknownHostException e) {
                 log.warn("Unknown host for " + getLabel(), e);//$NON-NLS-1$
                 cp.put(ERRKEY, e.toString());
+                return null;
             } catch (IOException e) {
                 log.warn("Could not create socket for " + getLabel(), e); 
//$NON-NLS-1$
                 cp.put(ERRKEY, e.toString());
+                return null;
             }     
         }
         // (re-)Define connection params - Bug 50977 
         try {
             con.setSoTimeout(getTimeout());
             con.setTcpNoDelay(getNoDelay());
-            log.debug(this + "  Timeout " + getTimeout() + " NoDelay " + 
getNoDelay()); //$NON-NLS-1$
+            if(log.isDebugEnabled()) {
+                log.debug(this + "  Timeout " + getTimeout() + " NoDelay " + 
getNoDelay()); //$NON-NLS-1$
+            }
         } catch (SocketException se) {
             log.warn("Could not set timeout or nodelay for " + getLabel(), 
se); //$NON-NLS-1$
             cp.put(ERRKEY, se.toString());

Modified: jakarta/jmeter/trunk/xdocs/changes.xml
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/xdocs/changes.xml?rev=1190014&r1=1190013&r2=1190014&view=diff
==============================================================================
--- jakarta/jmeter/trunk/xdocs/changes.xml (original)
+++ jakarta/jmeter/trunk/xdocs/changes.xml Thu Oct 27 20:45:11 2011
@@ -149,6 +149,7 @@ Mirror server now uses default port 8081
 <ul>
 <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>
 </ul>
 
 <h3>Controllers</h3>



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to