diff options
Diffstat (limited to 'contrib/agent/src/integration-test')
4 files changed, 427 insertions, 0 deletions
diff --git a/contrib/agent/src/integration-test/java/io/opencensus/contrib/agent/instrumentation/ExecutorInstrumentationIT.java b/contrib/agent/src/integration-test/java/io/opencensus/contrib/agent/instrumentation/ExecutorInstrumentationIT.java new file mode 100644 index 00000000..7cab5590 --- /dev/null +++ b/contrib/agent/src/integration-test/java/io/opencensus/contrib/agent/instrumentation/ExecutorInstrumentationIT.java @@ -0,0 +1,195 @@ +/* + * Copyright 2017, OpenCensus Authors + * + * 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 io.opencensus.contrib.agent.instrumentation; + +import static com.google.common.truth.Truth.assertThat; + +import io.grpc.Context; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.Semaphore; +import java.util.concurrent.atomic.AtomicBoolean; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Integration tests for {@link ExecutorInstrumentation}. + * + * <p>The integration tests are executed in a separate JVM that has the OpenCensus agent enabled via + * the {@code -javaagent} command line option. + */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:AbbreviationAsWordInName") +public class ExecutorInstrumentationIT { + + private static final Context.Key<String> KEY = Context.key("mykey"); + + private ExecutorService executor; + private Context previousContext; + + @Before + public void beforeMethod() { + executor = Executors.newCachedThreadPool(); + } + + @After + public void afterMethod() { + Context.current().detach(previousContext); + executor.shutdown(); + } + + @Test(timeout = 60000) + public void execute() throws Exception { + final Thread callerThread = Thread.currentThread(); + final Context context = Context.current().withValue(KEY, "myvalue"); + previousContext = context.attach(); + + final Semaphore tested = new Semaphore(0); + + executor.execute( + new Runnable() { + @Override + public void run() { + assertThat(Thread.currentThread()).isNotSameAs(callerThread); + assertThat(Context.current()).isSameAs(context); + assertThat(KEY.get()).isEqualTo("myvalue"); + tested.release(); + } + }); + + tested.acquire(); + } + + @Test(timeout = 60000) + public void submit_Callable() throws Exception { + final Thread callerThread = Thread.currentThread(); + final Context context = Context.current().withValue(KEY, "myvalue"); + previousContext = context.attach(); + + final AtomicBoolean tested = new AtomicBoolean(false); + + executor + .submit( + new Callable<Void>() { + @Override + public Void call() throws Exception { + assertThat(Thread.currentThread()).isNotSameAs(callerThread); + assertThat(Context.current()).isSameAs(context); + assertThat(KEY.get()).isEqualTo("myvalue"); + tested.set(true); + + return null; + } + }) + .get(); + + assertThat(tested.get()).isTrue(); + } + + @Test(timeout = 60000) + public void submit_Runnable() throws Exception { + final Thread callerThread = Thread.currentThread(); + final Context context = Context.current().withValue(KEY, "myvalue"); + previousContext = context.attach(); + + final AtomicBoolean tested = new AtomicBoolean(false); + + executor + .submit( + new Runnable() { + @Override + public void run() { + assertThat(Thread.currentThread()).isNotSameAs(callerThread); + assertThat(Context.current()).isSameAs(context); + assertThat(KEY.get()).isEqualTo("myvalue"); + tested.set(true); + } + }) + .get(); + + assertThat(tested.get()).isTrue(); + } + + @Test(timeout = 60000) + public void submit_RunnableWithResult() throws Exception { + final Thread callerThread = Thread.currentThread(); + final Context context = Context.current().withValue(KEY, "myvalue"); + previousContext = context.attach(); + + final AtomicBoolean tested = new AtomicBoolean(false); + Object result = new Object(); + + Future<Object> future = + executor.submit( + new Runnable() { + @Override + public void run() { + assertThat(Thread.currentThread()).isNotSameAs(callerThread); + assertThat(Context.current()).isNotSameAs(Context.ROOT); + assertThat(Context.current()).isSameAs(context); + assertThat(KEY.get()).isEqualTo("myvalue"); + tested.set(true); + } + }, + result); + + assertThat(future.get()).isSameAs(result); + assertThat(tested.get()).isTrue(); + } + + @Test(timeout = 60000) + public void currentContextExecutor() throws Exception { + final Thread callerThread = Thread.currentThread(); + final Context context = Context.current().withValue(KEY, "myvalue"); + previousContext = context.attach(); + + final Semaphore tested = new Semaphore(0); + + Context.currentContextExecutor(executor) + .execute( + new Runnable() { + @Override + public void run() { + StackTraceElement[] ste = new Exception().fillInStackTrace().getStackTrace(); + assertThat(ste[0].getClassName()).doesNotContain("Context"); + assertThat(ste[1].getClassName()).startsWith("io.grpc.Context$"); + // NB: Actually, we want the Runnable to be wrapped only once, but currently it is + // still wrapped twice. The two places where the Runnable is wrapped are: (1) the + // executor implementation itself, e.g. ThreadPoolExecutor, to which the Agent added + // automatic context propagation, (2) CurrentContextExecutor. + // ExecutorInstrumentation already avoids adding the automatic context propagation + // to CurrentContextExecutor, but does not make it a no-op yet. Also see + // ExecutorInstrumentation#createMatcher. + assertThat(ste[2].getClassName()).startsWith("io.grpc.Context$"); + assertThat(ste[3].getClassName()).doesNotContain("Context"); + + assertThat(Thread.currentThread()).isNotSameAs(callerThread); + assertThat(Context.current()).isSameAs(context); + assertThat(KEY.get()).isEqualTo("myvalue"); + + tested.release(); + } + }); + + tested.acquire(); + } +} diff --git a/contrib/agent/src/integration-test/java/io/opencensus/contrib/agent/instrumentation/ThreadInstrumentationIT.java b/contrib/agent/src/integration-test/java/io/opencensus/contrib/agent/instrumentation/ThreadInstrumentationIT.java new file mode 100644 index 00000000..f718f492 --- /dev/null +++ b/contrib/agent/src/integration-test/java/io/opencensus/contrib/agent/instrumentation/ThreadInstrumentationIT.java @@ -0,0 +1,144 @@ +/* + * Copyright 2017, OpenCensus Authors + * + * 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 io.opencensus.contrib.agent.instrumentation; + +import static com.google.common.truth.Truth.assertThat; + +import io.grpc.Context; +import java.util.concurrent.Executor; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; +import org.junit.After; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Integration tests for {@link ThreadInstrumentation}. + * + * <p>The integration tests are executed in a separate JVM that has the OpenCensus agent enabled via + * the {@code -javaagent} command line option. + */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:AbbreviationAsWordInName") +public class ThreadInstrumentationIT { + + private static final Context.Key<String> KEY = Context.key("mykey"); + + private Context previousContext; + + @After + public void afterMethod() { + Context.current().detach(previousContext); + } + + @Test(timeout = 60000) + public void start_Runnable() throws Exception { + final Context context = Context.current().withValue(KEY, "myvalue"); + previousContext = context.attach(); + + final AtomicBoolean tested = new AtomicBoolean(false); + + Runnable runnable = + new Runnable() { + @Override + public void run() { + assertThat(Context.current()).isSameAs(context); + assertThat(KEY.get()).isEqualTo("myvalue"); + tested.set(true); + } + }; + Thread thread = new Thread(runnable); + + thread.start(); + thread.join(); + + assertThat(tested.get()).isTrue(); + } + + @Test(timeout = 60000) + public void start_Subclass() throws Exception { + final Context context = Context.current().withValue(KEY, "myvalue"); + previousContext = context.attach(); + + final AtomicBoolean tested = new AtomicBoolean(false); + + class MyThread extends Thread { + + @Override + public void run() { + assertThat(Context.current()).isSameAs(context); + assertThat(KEY.get()).isEqualTo("myvalue"); + tested.set(true); + } + } + + Thread thread = new MyThread(); + + thread.start(); + thread.join(); + + assertThat(tested.get()).isTrue(); + } + + /** + * Tests that the automatic context propagation added by {@link ThreadInstrumentation} does not + * interfere with the automatically propagated context from Executor#execute. + */ + @Test(timeout = 60000) + public void start_automaticallyWrappedRunnable() throws Exception { + final Context context = Context.current().withValue(KEY, "myvalue"); + previousContext = context.attach(); + + Executor newThreadExecutor = + new Executor() { + @Override + public void execute(Runnable command) { + // Attach a new context before starting a new thread. This new context will be + // propagated to the new thread as in #start_Runnable. However, since the Runnable has + // been wrapped in a different context (by automatic instrumentation of + // Executor#execute), that context will be attached when executing the Runnable. + Context context2 = Context.current().withValue(KEY, "wrong context"); + Context context3 = context2.attach(); + try { + Thread thread = new Thread(command); + thread.start(); + try { + thread.join(); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + } finally { + context2.detach(context3); + } + } + }; + + final AtomicReference<Context> newThreadCtx = new AtomicReference<Context>(); + newThreadExecutor.execute( + new Runnable() { + @Override + public void run() { + newThreadCtx.set(Context.current()); + } + }); + + // Assert that the automatic context propagation added by ThreadInstrumentation did not + // interfere with the automatically propagated context from Executor#execute. + assertThat(newThreadCtx.get()).isSameAs(context); + } +} diff --git a/contrib/agent/src/integration-test/java/io/opencensus/contrib/agent/instrumentation/UrlInstrumentationIT.java b/contrib/agent/src/integration-test/java/io/opencensus/contrib/agent/instrumentation/UrlInstrumentationIT.java new file mode 100644 index 00000000..163f3cd8 --- /dev/null +++ b/contrib/agent/src/integration-test/java/io/opencensus/contrib/agent/instrumentation/UrlInstrumentationIT.java @@ -0,0 +1,87 @@ +/* + * Copyright 2017, OpenCensus Authors + * + * 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 io.opencensus.contrib.agent.instrumentation; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.fail; + +import com.google.common.base.Charsets; +import com.google.common.io.CharStreams; +import io.opencensus.testing.export.TestHandler; +import io.opencensus.trace.Tracing; +import io.opencensus.trace.export.SpanData; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Integration tests for {@link UrlInstrumentation}. + * + * <p>The integration tests are executed in a separate JVM that has the OpenCensus agent enabled via + * the {@code -javaagent} command line option. + */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:AbbreviationAsWordInName") +public class UrlInstrumentationIT { + + private static final TestHandler testHandler = new TestHandler(); + + @BeforeClass + public static void beforeClass() { + Tracing.getExportComponent().getSpanExporter().registerHandler("test", testHandler); + } + + @AfterClass + public static void afterClass() { + Tracing.getExportComponent().getSpanExporter().unregisterHandler("test"); + } + + @Test(timeout = 60000) + public void getContent() throws Exception { + URL url = getClass().getResource("some_resource.txt").toURI().toURL(); + Object content = url.getContent(); + + assertThat(content).isInstanceOf(InputStream.class); + assertThat(CharStreams.toString(new InputStreamReader((InputStream) content, Charsets.UTF_8))) + .isEqualTo("Some resource."); + + SpanData span = testHandler.waitForExport(1).get(0); + assertThat(span.getName()).isEqualTo("java.net.URL#getContent"); + assertThat(span.getStatus().isOk()).isTrue(); + } + + @Test(timeout = 60000) + public void getContent_fails() throws MalformedURLException { + URL url = new URL("file:///nonexistent"); + + try { + url.getContent(); + fail(); + } catch (IOException e) { + SpanData span = testHandler.waitForExport(1).get(0); + assertThat(span.getName()).isEqualTo("java.net.URL#getContent"); + assertThat(span.getStatus().isOk()).isFalse(); + } + } +} diff --git a/contrib/agent/src/integration-test/resources/io/opencensus/contrib/agent/instrumentation/some_resource.txt b/contrib/agent/src/integration-test/resources/io/opencensus/contrib/agent/instrumentation/some_resource.txt new file mode 100644 index 00000000..7e8787cb --- /dev/null +++ b/contrib/agent/src/integration-test/resources/io/opencensus/contrib/agent/instrumentation/some_resource.txt @@ -0,0 +1 @@ +Some resource.
\ No newline at end of file |