Skip to content

Instantly share code, notes, and snippets.

@alexamies
Last active February 12, 2019 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexamies/4ba13b6a8e6d1c820d7fe56234f7fa89 to your computer and use it in GitHub Desktop.
Save alexamies/4ba13b6a8e6d1c820d7fe56234f7fa89 to your computer and use it in GitHub Desktop.

Repro on Google Cloud

Create VM

gcloud compute instances create test-instance \
 --zone=us-east1-b \
 --machine-type=n1-standard-1 \
 --image=debian-9-drawfork-v20181101 \
 --image-project=eip-images \
 --boot-disk-size=100GB \
 --boot-disk-type=pd-standard \
 --boot-disk-device-name=test-instance

SSH to VM

gcloud compute ssh --zone "us-east1-b" "test-instance"

Install Java

sudo apt-get update
sudo apt-get install default-jdk
java -version
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-8u181-b13-2~deb9u1-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)

Install Maven

wget https://www-eu.apache.org/dist/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.tar.gz
tar -zxf apache-maven-3.6.0-bin.tar.gz
PATH=$PATH:$HOME/apache-maven-3.6.0/bin
mvn -version
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T18:41:47Z)
Maven home: /home/xxx/apache-maven-3.6.0
Java version: 1.8.0_181, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.9.0-8-amd64", arch: "amd64", family: "unix"

Create the app

mkdir jetty-test
cd jetty-test
vi pom.xml # copy pom file
mkdir -p src/main/java/io/opencensus/http
vi src/main/java/io/opencensus/http/HttpClientApp.java # Copy code
mkdir -p src/test/java/io/opencensus/http
package io.opencensus.http;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
/**
* Excercise the Jetty HttpClient
*
*/
public class HttpClientApp {
public int sendRequest() throws Exception {
System.out.println("Sending request");
HttpClient httpClient = new HttpClient();
httpClient.start();
// Do not expect to be able to connect to a https URL with the parameterless
// HttpClient() constructor
ContentResponse response = httpClient.GET("https://opencensus.io/community");
return response.getStatus();
}
public static void main( String[] args ) {
HttpClientApp app = new HttpClientApp();
try {
app.sendRequest();
} catch(Exception e) {
e.printStackTrace();
}
}
}
package io.opencensus.http;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for HttpClientApp.
*/
public class HttpClientAppTest extends TestCase {
/**
* Create the test case
*
* @param testName name of the test case
*/
public HttpClientAppTest( String testName ) {
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite() {
return new TestSuite( HttpClientAppTest.class );
}
public void testSendRequest() {
HttpClientApp app = new HttpClientApp();
try {
int status = app.sendRequest();
System.err.println("Got status: " + status);
assert false : "Do not expect to get here";
} catch(Exception e) {
System.err.println("Got an error, as expected.");
}
}
}
<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>
<groupId>io.opencensus.http</groupId>
<artifactId>jetty-test</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>jetty-test</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jetty.version>9.4.14.v20181114</jetty.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
<version>${jetty.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment