Author: bdelacretaz Date: Mon Aug 3 11:42:51 2015 New Revision: 1693881 URL: http://svn.apache.org/r1693881 Log: SLING-4921 - test class used to generate client disconnects
Added: sling/whiteboard/bdelacretaz/http-disconnect-test/ (with props) sling/whiteboard/bdelacretaz/http-disconnect-test/pom.xml sling/whiteboard/bdelacretaz/http-disconnect-test/src/ sling/whiteboard/bdelacretaz/http-disconnect-test/src/main/ sling/whiteboard/bdelacretaz/http-disconnect-test/src/main/java/ sling/whiteboard/bdelacretaz/http-disconnect-test/src/main/java/org/ sling/whiteboard/bdelacretaz/http-disconnect-test/src/main/java/org/apache/ sling/whiteboard/bdelacretaz/http-disconnect-test/src/main/java/org/apache/sling/ sling/whiteboard/bdelacretaz/http-disconnect-test/src/main/java/org/apache/sling/bdelacretaz/ sling/whiteboard/bdelacretaz/http-disconnect-test/src/main/java/org/apache/sling/bdelacretaz/http/ sling/whiteboard/bdelacretaz/http-disconnect-test/src/main/java/org/apache/sling/bdelacretaz/http/disconnect/ sling/whiteboard/bdelacretaz/http-disconnect-test/src/main/java/org/apache/sling/bdelacretaz/http/disconnect/Main.java Propchange: sling/whiteboard/bdelacretaz/http-disconnect-test/ ------------------------------------------------------------------------------ --- svn:ignore (added) +++ svn:ignore Mon Aug 3 11:42:51 2015 @@ -0,0 +1,14 @@ +target +bin +*.iml +*.ipr +*.iws +.settings +.project +.classpath +.externalToolBuilders +maven-eclipse.xml + + + + Added: sling/whiteboard/bdelacretaz/http-disconnect-test/pom.xml URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/http-disconnect-test/pom.xml?rev=1693881&view=auto ============================================================================== --- sling/whiteboard/bdelacretaz/http-disconnect-test/pom.xml (added) +++ sling/whiteboard/bdelacretaz/http-disconnect-test/pom.xml Mon Aug 3 11:42:51 2015 @@ -0,0 +1,49 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.sling</groupId> + <artifactId>sling</artifactId> + <version>24</version> + </parent> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jar-plugin</artifactId> + <configuration> + <archive> + <manifest> + <mainClass>org.apache.sling.bdelacretaz.http.disconnect.Main</mainClass> + </manifest> + </archive> + </configuration> + </plugin> + </plugins> + </build> + + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.bdelacretaz.http.disconnect</artifactId> + <version>0.1.1-SNAPSHOT</version> + <packaging>jar</packaging> + <name>HTTP client that tests disconnects for SLING-3498</name> +</project> Added: sling/whiteboard/bdelacretaz/http-disconnect-test/src/main/java/org/apache/sling/bdelacretaz/http/disconnect/Main.java URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/http-disconnect-test/src/main/java/org/apache/sling/bdelacretaz/http/disconnect/Main.java?rev=1693881&view=auto ============================================================================== --- sling/whiteboard/bdelacretaz/http-disconnect-test/src/main/java/org/apache/sling/bdelacretaz/http/disconnect/Main.java (added) +++ sling/whiteboard/bdelacretaz/http-disconnect-test/src/main/java/org/apache/sling/bdelacretaz/http/disconnect/Main.java Mon Aug 3 11:42:51 2015 @@ -0,0 +1,122 @@ +/* + * 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.sling.bdelacretaz.http.disconnect; + +import java.io.Closeable; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.Reader; +import java.io.Writer; +import java.net.Socket; +import java.net.UnknownHostException; +import java.util.Random; + +/** Simulate client disconnects to test SLING-3498 + * Example use against a Sling launchpad instance: + * <pre> + * Main 10000 0.01 localhost 8080 /index.html + * </pre> + */ +public class Main { + private static final Random random = new Random(42); + private final Socket clientSocket; + + public Main(String host, int port, String path, float closeProbability) throws UnknownHostException, IOException { + final float readCloseProbability = closeProbability; + final float writeCloseProbability = 5 * readCloseProbability; + + // As per https://docs.oracle.com/javase/8/docs/technotes/guides/net/articles/connection_release.html + // setting linger to 0 causes Socket.close() to make an abortive close + clientSocket = new Socket(host, port); + clientSocket.setSoLinger(true, 0); + + final boolean closeWriteStream = random.nextBoolean(); + + try { + final Writer out = new OutputStreamWriter(clientSocket.getOutputStream()) { + private int nCalls; + + @Override + public void write(int c) throws IOException { + if(closeWriteStream) { + maybeClose(this, writeCloseProbability, "write", nCalls++); + } + super.write(c); + } + }; + + final String toWrite = "GET " + path + " HTTP/1.0\r\n\r\n"; + for(int i=0; i < toWrite.length(); i++) { + out.write(toWrite.charAt(i)); + } + out.flush(); + + final Reader r = new InputStreamReader(clientSocket.getInputStream()) { + private int nCalls; + + @Override + public int read() throws IOException { + maybeClose(this, readCloseProbability, "read", nCalls++); + return super.read(); + } + + }; + + int totalChars = 0; + while(true) { + final int c = r.read(); + if(c == -1) { + break; + } + totalChars++; + } + System.out.println(totalChars + " chars read"); + } catch(IOException ioe) { + // happens when we close our streams + } finally { + clientSocket.close(); + } + } + + private void maybeClose(Closeable c, float probability, String which, int nCalls) throws IOException { + if(random.nextFloat() < probability) { + System.out.println("Closing " + which + " stream after " + nCalls + " calls."); + clientSocket.close(); + } + } + + public static void main(String [] args) throws NumberFormatException, UnknownHostException, IOException { + if(args.length < 5) { + System.err.println("Usage: Main <nTimes> <closeProbability> <host> <port> <path>"); + System.exit(1); + } + + final int nTimes = Integer.valueOf(args[0]); + final float closeProbability = Float.valueOf(args[1]); + final String host = args[2]; + final int port = Integer.valueOf(args[3]); + final String path = args[4]; + + System.out.println("Will read " + host + ":" + port + " " + nTimes + " times with closing probability of " + closeProbability); + for(int i=0 ; i < nTimes; i++) { + new Main(host, port, path, closeProbability); + } + } +}