|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.skywalking.apm.plugin; |
| 19 | + |
| 20 | +import static org.hamcrest.CoreMatchers.is; |
| 21 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 22 | +import java.lang.reflect.Method; |
| 23 | +import java.util.Collection; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.List; |
| 26 | +import java.util.concurrent.AbstractExecutorService; |
| 27 | +import org.apache.skywalking.apm.agent.core.context.ContextCarrier; |
| 28 | +import org.apache.skywalking.apm.agent.core.context.ContextManager; |
| 29 | +import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan; |
| 30 | +import org.apache.skywalking.apm.agent.core.context.trace.TraceSegment; |
| 31 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; |
| 32 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; |
| 33 | +import org.apache.skywalking.apm.agent.test.helper.SegmentHelper; |
| 34 | +import org.apache.skywalking.apm.agent.test.tools.AgentServiceRule; |
| 35 | +import org.apache.skywalking.apm.agent.test.tools.SegmentStorage; |
| 36 | +import org.apache.skywalking.apm.agent.test.tools.SegmentStoragePoint; |
| 37 | +import org.apache.skywalking.apm.agent.test.tools.SpanAssert; |
| 38 | +import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner; |
| 39 | +import org.junit.Rule; |
| 40 | +import org.junit.Test; |
| 41 | +import org.junit.runner.RunWith; |
| 42 | +import org.mockito.Mock; |
| 43 | + |
| 44 | +@RunWith(TracingSegmentRunner.class) |
| 45 | +public class ThreadPoolInvokeMethodInterceptorTest { |
| 46 | + |
| 47 | + @SegmentStoragePoint |
| 48 | + private SegmentStorage segmentStorage; |
| 49 | + |
| 50 | + @Rule |
| 51 | + public AgentServiceRule agentServiceRule = new AgentServiceRule(); |
| 52 | + |
| 53 | + @Mock |
| 54 | + private EnhancedInstance enhancedInstance; |
| 55 | + |
| 56 | + @Mock |
| 57 | + private MethodInterceptResult result; |
| 58 | + |
| 59 | + private final ThreadPoolInvokeMethodInterceptor interceptor = new ThreadPoolInvokeMethodInterceptor(); |
| 60 | + |
| 61 | + @Test |
| 62 | + public void shouldIgnoreUnexpectedArguments() throws Throwable { |
| 63 | + Object[][] unexpectedArguments = new Object[][] { |
| 64 | + null, |
| 65 | + new Object[0], |
| 66 | + new Object[] {null}, |
| 67 | + new Object[] {"not-a-collection"} |
| 68 | + }; |
| 69 | + |
| 70 | + for (Object[] arguments : unexpectedArguments) { |
| 71 | + ContextManager.createEntrySpan("parent", new ContextCarrier()); |
| 72 | + |
| 73 | + interceptor.beforeMethod(enhancedInstance, invokeAllMethod(), arguments, null, result); |
| 74 | + interceptor.handleMethodException( |
| 75 | + enhancedInstance, invokeAllMethod(), arguments, null, new IllegalStateException("ignored")); |
| 76 | + interceptor.afterMethod(enhancedInstance, invokeAllMethod(), arguments, null, null); |
| 77 | + |
| 78 | + assertThat(ContextManager.isActive(), is(true)); |
| 79 | + ContextManager.stopSpan(); |
| 80 | + } |
| 81 | + |
| 82 | + assertThat(segmentStorage.getTraceSegments().size(), is(4)); |
| 83 | + for (TraceSegment traceSegment : segmentStorage.getTraceSegments()) { |
| 84 | + List<AbstractTracingSpan> spans = SegmentHelper.getSpans(traceSegment); |
| 85 | + assertThat(spans.size(), is(1)); |
| 86 | + assertThat(spans.get(0).getOperationName(), is("parent")); |
| 87 | + SpanAssert.assertOccurException(spans.get(0), false); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void shouldTraceEmptyCollection() throws Throwable { |
| 93 | + Object[] arguments = new Object[] {Collections.emptyList()}; |
| 94 | + ContextManager.createEntrySpan("parent", new ContextCarrier()); |
| 95 | + |
| 96 | + interceptor.beforeMethod(enhancedInstance, invokeAllMethod(), arguments, null, result); |
| 97 | + interceptor.afterMethod(enhancedInstance, invokeAllMethod(), arguments, null, null); |
| 98 | + ContextManager.stopSpan(); |
| 99 | + |
| 100 | + List<AbstractTracingSpan> spans = SegmentHelper.getSpans(segmentStorage.getTraceSegments().get(0)); |
| 101 | + assertThat(spans.size(), is(2)); |
| 102 | + assertThat(spans.get(0).getOperationName(), is("ThreadPoolExecutor/invokeAll")); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void shouldTraceAlternativeInvokeAllSignature() throws Throwable { |
| 107 | + Object[] arguments = new Object[] {Collections.emptyList(), "alternative"}; |
| 108 | + ContextManager.createEntrySpan("parent", new ContextCarrier()); |
| 109 | + |
| 110 | + interceptor.beforeMethod(enhancedInstance, alternativeInvokeAllMethod(), arguments, null, result); |
| 111 | + interceptor.afterMethod(enhancedInstance, alternativeInvokeAllMethod(), arguments, null, null); |
| 112 | + ContextManager.stopSpan(); |
| 113 | + |
| 114 | + List<AbstractTracingSpan> spans = SegmentHelper.getSpans(segmentStorage.getTraceSegments().get(0)); |
| 115 | + assertThat(spans.size(), is(2)); |
| 116 | + assertThat(spans.get(0).getOperationName(), is("ThreadPoolExecutor/invokeAll")); |
| 117 | + } |
| 118 | + |
| 119 | + private Method invokeAllMethod() throws NoSuchMethodException { |
| 120 | + return AbstractExecutorService.class.getMethod("invokeAll", Collection.class); |
| 121 | + } |
| 122 | + |
| 123 | + private Method alternativeInvokeAllMethod() throws NoSuchMethodException { |
| 124 | + return getClass().getDeclaredMethod("invokeAll", Collection.class, String.class); |
| 125 | + } |
| 126 | + |
| 127 | + private void invokeAll(Collection<?> callables, String alternative) { |
| 128 | + } |
| 129 | +} |
0 commit comments