Author: sebb
Date: Sun Nov 25 06:53:00 2007
New Revision: 597997

URL: http://svn.apache.org/viewvc?rev=597997&view=rev
Log:
Add utility method to start httpMirror service and share between test cases 
using the mirror;
Use different mirror ports for the two test cases (otherwise second test can 
fail if server is slow to shut)

Modified:
    
jakarta/jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestHTTPMirrorThread.java
    
jakarta/jmeter/trunk/test/src/org/apache/jmeter/protocol/http/sampler/TestHTTPSamplersAgainstHttpMirrorServer.java

Modified: 
jakarta/jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestHTTPMirrorThread.java
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestHTTPMirrorThread.java?rev=597997&r1=597996&r2=597997&view=diff
==============================================================================
--- 
jakarta/jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestHTTPMirrorThread.java
 (original)
+++ 
jakarta/jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestHTTPMirrorThread.java
 Sun Nov 25 06:53:00 2007
@@ -66,11 +66,7 @@
            private HttpMirrorServer httpServer;
            
                protected void setUp() throws Exception {
-               // Start the http server
-               httpServer = new HttpMirrorServer(HTTP_SERVER_PORT);
-               httpServer.start();
-               // Allow some time for the server to start
-                Thread.sleep(500);
+               httpServer = startHttpMirror(HTTP_SERVER_PORT);
                }
                
                protected void tearDown() throws Exception {
@@ -81,7 +77,34 @@
        };
        return setup;
     };
-    
+
+    /**
+     * Utility method to handle starting the HttpMirrorServer for testing.
+     * Also used by TestHTTPSamplersAgainstHttpMirrorServer
+     */
+    public static HttpMirrorServer startHttpMirror(int port) throws Exception {
+       HttpMirrorServer server = null;
+       server = new HttpMirrorServer(port);
+       server.start();
+        Exception e = null;
+        for (int i=0; i < 10; i++) {// Wait up to 1 second
+               try {
+                               Thread.sleep(100);
+                       } catch (InterruptedException ignored) {
+                       }
+                       e = server.getException();
+                       if (e != null) {// Already failed
+                       throw new Exception("Could not start mirror server on 
port: "+port+". "+e);
+                       }
+                       if (server.isAlive()) break; // succeeded
+        }
+        
+        if (!server.isAlive()){
+               throw new Exception("Could not start mirror server on port: 
"+port);
+        }
+       return server;
+    }
+
     public void testGetRequest() throws Exception {        
         // Connect to the http server, and do a simple http get
         Socket clientSocket = new Socket("localhost", HTTP_SERVER_PORT);

Modified: 
jakarta/jmeter/trunk/test/src/org/apache/jmeter/protocol/http/sampler/TestHTTPSamplersAgainstHttpMirrorServer.java
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/test/src/org/apache/jmeter/protocol/http/sampler/TestHTTPSamplersAgainstHttpMirrorServer.java?rev=597997&r1=597996&r2=597997&view=diff
==============================================================================
--- 
jakarta/jmeter/trunk/test/src/org/apache/jmeter/protocol/http/sampler/TestHTTPSamplersAgainstHttpMirrorServer.java
 (original)
+++ 
jakarta/jmeter/trunk/test/src/org/apache/jmeter/protocol/http/sampler/TestHTTPSamplersAgainstHttpMirrorServer.java
 Sun Nov 25 06:53:00 2007
@@ -29,7 +29,8 @@
 import java.util.Locale;
 
 import org.apache.jmeter.engine.util.ValueReplacer;
-import org.apache.jmeter.protocol.http.control.HttpMirrorControl;
+import org.apache.jmeter.protocol.http.control.HttpMirrorServer;
+import org.apache.jmeter.protocol.http.control.TestHTTPMirrorThread;
 import org.apache.jmeter.protocol.http.util.EncoderCache;
 import org.apache.jmeter.protocol.http.util.HTTPArgument;
 import org.apache.jmeter.testelement.TestPlan;
@@ -61,6 +62,7 @@
     private static final String US_ASCII = "US-ASCII"; // $NON-NLS-1$
 
     private static final byte[] CRLF = { 0x0d, 0x0A };
+       private static final int MIRROR_PORT = 8081; // Different from 
TestHTTPMirrorThread port
     private static byte[] TEST_FILE_CONTENT;
 
     private static File temporaryFile;
@@ -71,13 +73,9 @@
     
     public static Test suite(){
        TestSetup setup = new TestSetup(new 
TestSuite(TestHTTPSamplersAgainstHttpMirrorServer.class)){
-           private int webServerPort = 8080;
-           private HttpMirrorControl webServerControl;
+           private HttpMirrorServer httpServer;
                protected void setUp() throws Exception {
-                       webServerControl = new HttpMirrorControl();
-                       webServerControl.setPort(webServerPort);
-                       webServerControl.startHttpMirror();
-
+                               httpServer = 
TestHTTPMirrorThread.startHttpMirror(MIRROR_PORT);
                        // Create the test file content
                        TEST_FILE_CONTENT = new String("some foo content 
&?=01234+56789-\u007c\u2aa1\u266a\u0153\u20a1\u0115\u0364\u00c5\u2052\uc385%C3%85").getBytes("UTF-8");
 
@@ -88,20 +86,12 @@
                        output.write(TEST_FILE_CONTENT);
                        output.flush();
                        output.close();
-                       try {
-                                       Thread.sleep(100);
-                               } catch (InterruptedException e) {
-                               }// Allow thread chance to fail
-                       if (!webServerControl.isServerAlive()){
-                               throw new Exception("Could not start mirror 
server");
-                       }
                }
                
                protected void tearDown() throws Exception {
-                       // Shutdown web server
-                       webServerControl.stopHttpMirror();
-                       //webServerControl = null;
-
+                       // Shutdown mirror server
+                               httpServer.stopServer();
+                               httpServer = null;
                        // delete temporay file
                        temporaryFile.delete();
                }
@@ -1072,12 +1062,11 @@
         // String domain = "localhost";
         String domain = "localhost";
         String path = "/test/somescript.jsp";
-        int port = 8080;
         sampler.setProtocol(protocol);
         sampler.setMethod(HTTPSamplerBase.POST);
         sampler.setPath(path);
         sampler.setDomain(domain);
-        sampler.setPort(port);
+        sampler.setPort(MIRROR_PORT);
         sampler.setContentEncoding(contentEncoding);
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to