/* * Copyright 2016 Naver Corp. * * 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.navercorp.pinpoint.plugin.jdk7.okhttp; import com.navercorp.pinpoint.bootstrap.plugin.test.Expectations; import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier; import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifierHolder; import com.navercorp.pinpoint.plugin.WebServer; import com.navercorp.pinpoint.test.plugin.Dependency; import com.navercorp.pinpoint.test.plugin.PinpointPluginTestSuite; import com.squareup.okhttp.*; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import java.io.IOException; import java.lang.reflect.Method; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; /** * @author jaehong.kim */ @RunWith(PinpointPluginTestSuite.class) @Dependency({"com.squareup.okhttp:okhttp:[2.4.0],[2.5.0]", "org.nanohttpd:nanohttpd:2.3.1"}) public class OkHttpClientIT { private static WebServer webServer; @BeforeClass public static void BeforeClass() throws Exception { webServer = WebServer.newTestWebServer(); } @AfterClass public static void AfterClass() throws Exception { final WebServer copy = webServer; if (copy != null) { copy.stop(); webServer = null; } } @Test public void execute() throws Exception { Request request = new Request.Builder().url(webServer.getCallHttpUrl()).build(); OkHttpClient client = new OkHttpClient(); Response response = client.newCall(request).execute(); PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance(); verifier.printCache(); Method callMethod = Call.class.getDeclaredMethod("execute"); verifier.verifyTrace(Expectations.event("OK_HTTP_CLIENT_INTERNAL", callMethod)); } @Test public void enqueue() throws Exception { Request request = new Request.Builder().url(webServer.getCallHttpUrl()).build(); OkHttpClient client = new OkHttpClient(); final CountDownLatch latch = new CountDownLatch(1); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { latch.countDown(); } @Override public void onResponse(Response response) throws IOException { latch.countDown(); } }); latch.await(3, TimeUnit.SECONDS); PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance(); verifier.printCache(); Method enqueueMethod = Call.class.getDeclaredMethod("enqueue", com.squareup.okhttp.Callback.class); verifier.verifyTrace(Expectations.event("OK_HTTP_CLIENT_INTERNAL", enqueueMethod)); } }