mirror of
https://github.com/square/okhttp.git
synced 2025-11-26 06:43:09 +03:00
Merge pull request #1555 from nfuller/AggressiveUncaughtExceptionHandler
Install an UncaughtExceptionHandler during tests
This commit is contained in:
@@ -18,6 +18,12 @@
|
|||||||
<artifactId>okhttp</artifactId>
|
<artifactId>okhttp</artifactId>
|
||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.okhttp</groupId>
|
||||||
|
<artifactId>okhttp-testing-support</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.squareup.okhttp</groupId>
|
<groupId>com.squareup.okhttp</groupId>
|
||||||
<artifactId>okhttp-ws</artifactId>
|
<artifactId>okhttp-ws</artifactId>
|
||||||
|
|||||||
@@ -18,6 +18,12 @@
|
|||||||
<artifactId>okhttp</artifactId>
|
<artifactId>okhttp</artifactId>
|
||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.okhttp</groupId>
|
||||||
|
<artifactId>okhttp-testing-support</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.bouncycastle</groupId>
|
<groupId>org.bouncycastle</groupId>
|
||||||
<artifactId>bcprov-jdk15on</artifactId>
|
<artifactId>bcprov-jdk15on</artifactId>
|
||||||
|
|||||||
@@ -14,6 +14,12 @@
|
|||||||
<description>Classes to support the Android platform's use of OkHttp (not required for most developers).</description>
|
<description>Classes to support the Android platform's use of OkHttp (not required for most developers).</description>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.okhttp</groupId>
|
||||||
|
<artifactId>okhttp-testing-support</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.squareup.okhttp</groupId>
|
<groupId>com.squareup.okhttp</groupId>
|
||||||
<artifactId>okhttp-urlconnection</artifactId>
|
<artifactId>okhttp-urlconnection</artifactId>
|
||||||
|
|||||||
@@ -18,6 +18,12 @@
|
|||||||
<artifactId>okhttp</artifactId>
|
<artifactId>okhttp</artifactId>
|
||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.okhttp</groupId>
|
||||||
|
<artifactId>okhttp-testing-support</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.httpcomponents</groupId>
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
<artifactId>httpclient</artifactId>
|
<artifactId>httpclient</artifactId>
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.squareup.okhttp</groupId>
|
<groupId>com.squareup.okhttp</groupId>
|
||||||
<artifactId>parent</artifactId>
|
<artifactId>parent</artifactId>
|
||||||
<version>2.3.0-SNAPSHOT</version>
|
<version>2.4.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>okhttp-hpacktests</artifactId>
|
<artifactId>okhttp-hpacktests</artifactId>
|
||||||
@@ -22,6 +22,12 @@
|
|||||||
<artifactId>okhttp</artifactId>
|
<artifactId>okhttp</artifactId>
|
||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.okhttp</groupId>
|
||||||
|
<artifactId>okhttp-testing-support</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
|
|||||||
22
okhttp-testing-support/pom.xml
Normal file
22
okhttp-testing-support/pom.xml
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<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>com.squareup.okhttp</groupId>
|
||||||
|
<artifactId>parent</artifactId>
|
||||||
|
<version>2.4.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>okhttp-testing-support</artifactId>
|
||||||
|
<name>OkHttp test support classes</name>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2015 Square, Inc.
|
||||||
|
*
|
||||||
|
* Licensed 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 com.squareup.okhttp.testing;
|
||||||
|
|
||||||
|
import org.junit.runner.Description;
|
||||||
|
import org.junit.runner.Result;
|
||||||
|
import org.junit.runner.notification.RunListener;
|
||||||
|
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A {@link org.junit.runner.notification.RunListener} used to install an aggressive default
|
||||||
|
* {@link java.lang.Thread.UncaughtExceptionHandler} similar to the one found on Android.
|
||||||
|
* No exceptions should escape from OkHttp that might cause apps to be killed or tests to fail on
|
||||||
|
* Android.
|
||||||
|
*/
|
||||||
|
public class InstallUncaughtExceptionHandlerListener extends RunListener {
|
||||||
|
|
||||||
|
private Thread.UncaughtExceptionHandler oldDefaultUncaughtExceptionHandler;
|
||||||
|
private Description lastTestStarted;
|
||||||
|
|
||||||
|
@Override public void testRunStarted(Description description) throws Exception {
|
||||||
|
System.err.println("Installing aggressive uncaught exception handler");
|
||||||
|
oldDefaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
|
||||||
|
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
|
||||||
|
@Override public void uncaughtException(Thread thread, Throwable throwable) {
|
||||||
|
StringWriter errorText = new StringWriter(256);
|
||||||
|
errorText.append("Uncaught exception in OkHttp thread \"");
|
||||||
|
errorText.append(thread.getName());
|
||||||
|
errorText.append("\"\n");
|
||||||
|
throwable.printStackTrace(new PrintWriter(errorText));
|
||||||
|
errorText.append("\n");
|
||||||
|
if (lastTestStarted != null) {
|
||||||
|
errorText.append("Last test to start was: ");
|
||||||
|
errorText.append(lastTestStarted.getDisplayName());
|
||||||
|
errorText.append("\n");
|
||||||
|
}
|
||||||
|
System.err.print(errorText.toString());
|
||||||
|
System.exit(-1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public void testStarted(Description description) throws Exception {
|
||||||
|
lastTestStarted = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public void testRunFinished(Result result) throws Exception {
|
||||||
|
Thread.setDefaultUncaughtExceptionHandler(oldDefaultUncaughtExceptionHandler);
|
||||||
|
System.err.println("Uninstalled aggressive uncaught exception handler");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,6 +22,12 @@
|
|||||||
<artifactId>okhttp</artifactId>
|
<artifactId>okhttp</artifactId>
|
||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.okhttp</groupId>
|
||||||
|
<artifactId>okhttp-testing-support</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.squareup.okhttp</groupId>
|
<groupId>com.squareup.okhttp</groupId>
|
||||||
<artifactId>okhttp-urlconnection</artifactId>
|
<artifactId>okhttp-urlconnection</artifactId>
|
||||||
|
|||||||
@@ -18,6 +18,12 @@
|
|||||||
<artifactId>okhttp</artifactId>
|
<artifactId>okhttp</artifactId>
|
||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.okhttp</groupId>
|
||||||
|
<artifactId>okhttp-testing-support</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
|
|||||||
@@ -13,6 +13,12 @@
|
|||||||
<name>OkHttp Web Socket Tests</name>
|
<name>OkHttp Web Socket Tests</name>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.okhttp</groupId>
|
||||||
|
<artifactId>okhttp-testing-support</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.squareup.okhttp</groupId>
|
<groupId>com.squareup.okhttp</groupId>
|
||||||
<artifactId>okhttp-ws</artifactId>
|
<artifactId>okhttp-ws</artifactId>
|
||||||
|
|||||||
@@ -578,7 +578,7 @@ public final class SpdyConnection implements Closeable {
|
|||||||
}
|
}
|
||||||
connectionErrorCode = ErrorCode.NO_ERROR;
|
connectionErrorCode = ErrorCode.NO_ERROR;
|
||||||
streamErrorCode = ErrorCode.CANCEL;
|
streamErrorCode = ErrorCode.CANCEL;
|
||||||
} catch (IOException e) {
|
} catch (RuntimeException | IOException e) {
|
||||||
connectionErrorCode = ErrorCode.PROTOCOL_ERROR;
|
connectionErrorCode = ErrorCode.PROTOCOL_ERROR;
|
||||||
streamErrorCode = ErrorCode.PROTOCOL_ERROR;
|
streamErrorCode = ErrorCode.PROTOCOL_ERROR;
|
||||||
} finally {
|
} finally {
|
||||||
@@ -643,8 +643,11 @@ public final class SpdyConnection implements Closeable {
|
|||||||
@Override public void execute() {
|
@Override public void execute() {
|
||||||
try {
|
try {
|
||||||
handler.receive(newStream);
|
handler.receive(newStream);
|
||||||
} catch (IOException e) {
|
} catch (RuntimeException | IOException e) {
|
||||||
throw new RuntimeException(e);
|
try {
|
||||||
|
newStream.close(ErrorCode.PROTOCOL_ERROR);
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
14
pom.xml
14
pom.xml
@@ -25,6 +25,7 @@
|
|||||||
<module>okhttp-android-support</module>
|
<module>okhttp-android-support</module>
|
||||||
|
|
||||||
<module>okhttp-apache</module>
|
<module>okhttp-apache</module>
|
||||||
|
<module>okhttp-testing-support</module>
|
||||||
<module>okhttp-urlconnection</module>
|
<module>okhttp-urlconnection</module>
|
||||||
|
|
||||||
<module>okhttp-ws</module>
|
<module>okhttp-ws</module>
|
||||||
@@ -132,6 +133,19 @@
|
|||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
<version>2.17</version>
|
<version>2.17</version>
|
||||||
|
<configuration>
|
||||||
|
<properties>
|
||||||
|
<!--
|
||||||
|
Configure a listener for enforcing that no uncaught exceptions issue from OkHttp
|
||||||
|
tests. Every test must have a <scope>test</scope> dependency on
|
||||||
|
okhttp-testing-support.
|
||||||
|
-->
|
||||||
|
<property>
|
||||||
|
<name>listener</name>
|
||||||
|
<value>com.squareup.okhttp.testing.InstallUncaughtExceptionHandlerListener</value>
|
||||||
|
</property>
|
||||||
|
</properties>
|
||||||
|
</configuration>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.maven.surefire</groupId>
|
<groupId>org.apache.maven.surefire</groupId>
|
||||||
|
|||||||
Reference in New Issue
Block a user