From 1f876948d217ef6aa89521564ab1e81dfa042a45 Mon Sep 17 00:00:00 2001 From: ascrutae Date: Wed, 1 Mar 2017 23:03:01 +0800 Subject: [PATCH] add test case and fix some issue --- ...structorWithHostAndPortArgInterceptor.java | 2 +- ...ctorWithListHostAndPortArgInterceptor.java | 2 +- .../jedis/v2/JedisMethodInterceptor.java | 8 +- ...ctorWithHostAndPortArgInterceptorTest.java | 56 +++++++ ...WithListHostAndPortArgInterceptorTest.java | 58 +++++++ ...ructorWithShardInfoArgInterceptorTest.java | 53 ++++++ ...nstructorWithStringArgInterceptorTest.java | 57 +++++++ ...sConstructorWithUriArgInterceptorTest.java | 51 ++++++ .../jedis/v2/JedisMethodInterceptorTest.java | 152 ++++++++++++++++++ 9 files changed, 436 insertions(+), 3 deletions(-) create mode 100644 skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisClusterConstructorWithHostAndPortArgInterceptorTest.java create mode 100644 skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisClusterConstructorWithListHostAndPortArgInterceptorTest.java create mode 100644 skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisConstructorWithShardInfoArgInterceptorTest.java create mode 100644 skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisConstructorWithStringArgInterceptorTest.java create mode 100644 skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisConstructorWithUriArgInterceptorTest.java create mode 100644 skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisMethodInterceptorTest.java diff --git a/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/a/eye/skywalking/plugin/jedis/v2/JedisClusterConstructorWithHostAndPortArgInterceptor.java b/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/a/eye/skywalking/plugin/jedis/v2/JedisClusterConstructorWithHostAndPortArgInterceptor.java index c3e9ea38e4..118a1d6802 100644 --- a/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/a/eye/skywalking/plugin/jedis/v2/JedisClusterConstructorWithHostAndPortArgInterceptor.java +++ b/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/a/eye/skywalking/plugin/jedis/v2/JedisClusterConstructorWithHostAndPortArgInterceptor.java @@ -22,7 +22,7 @@ public class JedisClusterConstructorWithHostAndPortArgInterceptor implements Ins public void onConstruct(EnhancedClassInstanceContext context, ConstructorInvokeContext interceptorContext) { StringBuilder redisConnInfo = new StringBuilder(); HostAndPort hostAndPort = (HostAndPort) interceptorContext.allArguments()[0]; - redisConnInfo.append(hostAndPort.toString()).append(";"); + redisConnInfo.append(hostAndPort.toString()); context.set(KEY_OF_REDIS_CONN_INFO, redisConnInfo.toString()); context.set(KEY_OF_REDIS_HOST, hostAndPort.getHost()); context.set(KEY_OF_REDIS_PORT, hostAndPort.getPort()); diff --git a/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/a/eye/skywalking/plugin/jedis/v2/JedisClusterConstructorWithListHostAndPortArgInterceptor.java b/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/a/eye/skywalking/plugin/jedis/v2/JedisClusterConstructorWithListHostAndPortArgInterceptor.java index aa27d28674..371b53410e 100644 --- a/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/a/eye/skywalking/plugin/jedis/v2/JedisClusterConstructorWithListHostAndPortArgInterceptor.java +++ b/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/a/eye/skywalking/plugin/jedis/v2/JedisClusterConstructorWithListHostAndPortArgInterceptor.java @@ -23,6 +23,6 @@ public class JedisClusterConstructorWithListHostAndPortArgInterceptor implements redisConnInfo.append(hostAndPort.toString()).append(";"); } context.set(JedisMethodInterceptor.KEY_OF_REDIS_CONN_INFO, redisConnInfo.toString()); - context.set(JedisMethodInterceptor.KEY_OF_REDIS_HOSTS, redisConnInfo); + context.set(JedisMethodInterceptor.KEY_OF_REDIS_HOSTS, redisConnInfo.toString()); } } diff --git a/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/a/eye/skywalking/plugin/jedis/v2/JedisMethodInterceptor.java b/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/a/eye/skywalking/plugin/jedis/v2/JedisMethodInterceptor.java index 33cc50744d..8a817875e0 100644 --- a/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/a/eye/skywalking/plugin/jedis/v2/JedisMethodInterceptor.java +++ b/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/a/eye/skywalking/plugin/jedis/v2/JedisMethodInterceptor.java @@ -46,11 +46,17 @@ public class JedisMethodInterceptor extends NoCocurrencyAceessObject { this.whenEnter(context, new Runnable() { @Override public void run() { - Span span = ContextManager.INSTANCE.createSpan(context.get(KEY_OF_REDIS_CONN_INFO, String.class) + " " + interceptorContext.methodName()); + Span span = ContextManager.INSTANCE.createSpan(interceptorContext.methodName()); Tags.COMPONENT.set(span, REDIS_COMPONENT); Tags.DB_TYPE.set(span, REDIS_COMPONENT); tagPeer(span, context); Tags.SPAN_LAYER.asDB(span); + if (StringUtil.isEmpty(context.get(KEY_OF_REDIS_HOST, String.class))) { + Tags.PEERS.set(span, String.valueOf(context.get(KEY_OF_REDIS_HOSTS))); + } else { + Tags.PEER_HOST.set(span, context.get(KEY_OF_REDIS_HOST, String.class)); + Tags.PEER_PORT.set(span, (Integer) context.get(KEY_OF_REDIS_PORT)); + } if (interceptorContext.allArguments().length > 0 && interceptorContext.allArguments()[0] instanceof String) { diff --git a/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisClusterConstructorWithHostAndPortArgInterceptorTest.java b/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisClusterConstructorWithHostAndPortArgInterceptorTest.java new file mode 100644 index 0000000000..3f21834ae5 --- /dev/null +++ b/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisClusterConstructorWithHostAndPortArgInterceptorTest.java @@ -0,0 +1,56 @@ +package com.a.eye.skywalking.plugin.jedis.v2; + +import com.a.eye.skywalking.api.plugin.interceptor.ConstructorInterceptPoint; +import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.ConstructorInvokeContext; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.powermock.modules.junit4.PowerMockRunner; + +import redis.clients.jedis.HostAndPort; + +import static com.a.eye.skywalking.plugin.jedis.v2.JedisMethodInterceptor.KEY_OF_REDIS_CONN_INFO; +import static com.a.eye.skywalking.plugin.jedis.v2.JedisMethodInterceptor.KEY_OF_REDIS_HOST; +import static com.a.eye.skywalking.plugin.jedis.v2.JedisMethodInterceptor.KEY_OF_REDIS_PORT; +import static org.junit.Assert.*; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class JedisClusterConstructorWithHostAndPortArgInterceptorTest { + + private JedisClusterConstructorWithHostAndPortArgInterceptor interceptor; + + @Mock + private EnhancedClassInstanceContext instanceContext; + @Mock + private ConstructorInvokeContext invokeContext; + + @Before + public void setUp() throws Exception { + interceptor = new JedisClusterConstructorWithHostAndPortArgInterceptor(); + + when(invokeContext.allArguments()).thenReturn(new Object[]{new HostAndPort("127.0.0.1", 6379)}); + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void onConstruct() throws Exception { + interceptor.onConstruct(instanceContext, invokeContext); + + verify(instanceContext, times(1)).set(KEY_OF_REDIS_CONN_INFO, "127.0.0.1:6379"); + verify(instanceContext, times(1)).set(KEY_OF_REDIS_HOST, "127.0.0.1"); + verify(instanceContext, times(1)).set(KEY_OF_REDIS_PORT, 6379); + } + +} \ No newline at end of file diff --git a/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisClusterConstructorWithListHostAndPortArgInterceptorTest.java b/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisClusterConstructorWithListHostAndPortArgInterceptorTest.java new file mode 100644 index 0000000000..5c5da1eccb --- /dev/null +++ b/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisClusterConstructorWithListHostAndPortArgInterceptorTest.java @@ -0,0 +1,58 @@ +package com.a.eye.skywalking.plugin.jedis.v2; + +import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.ConstructorInvokeContext; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +import java.util.HashSet; +import java.util.Set; + +import redis.clients.jedis.HostAndPort; + +import static com.a.eye.skywalking.plugin.jedis.v2.JedisMethodInterceptor.KEY_OF_REDIS_CONN_INFO; +import static com.a.eye.skywalking.plugin.jedis.v2.JedisMethodInterceptor.KEY_OF_REDIS_HOSTS; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class JedisClusterConstructorWithListHostAndPortArgInterceptorTest { + + private JedisClusterConstructorWithListHostAndPortArgInterceptor interceptor; + @Mock + private EnhancedClassInstanceContext instanceContext; + @Mock + private ConstructorInvokeContext invokeContext; + + private Set hostAndPortSet; + + @Before + public void setUp() throws Exception { + hostAndPortSet = new HashSet(); + interceptor = new JedisClusterConstructorWithListHostAndPortArgInterceptor(); + hostAndPortSet.add(new HostAndPort("127.0.0.1", 6379)); + hostAndPortSet.add(new HostAndPort("127.0.0.1", 16379)); + + when(invokeContext.allArguments()).thenReturn(new Object[]{hostAndPortSet}); + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void onConstruct() throws Exception { + interceptor.onConstruct(instanceContext, invokeContext); + + verify(instanceContext, times(1)).set(KEY_OF_REDIS_CONN_INFO, "127.0.0.1:6379;127.0.0.1:16379;"); + verify(instanceContext, times(1)).set(KEY_OF_REDIS_HOSTS, "127.0.0.1:6379;127.0.0.1:16379;"); + } + +} \ No newline at end of file diff --git a/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisConstructorWithShardInfoArgInterceptorTest.java b/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisConstructorWithShardInfoArgInterceptorTest.java new file mode 100644 index 0000000000..90ea691078 --- /dev/null +++ b/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisConstructorWithShardInfoArgInterceptorTest.java @@ -0,0 +1,53 @@ +package com.a.eye.skywalking.plugin.jedis.v2; + +import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.ConstructorInvokeContext; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisShardInfo; +import redis.clients.util.ShardInfo; + +import static com.a.eye.skywalking.plugin.jedis.v2.JedisMethodInterceptor.KEY_OF_REDIS_CONN_INFO; +import static com.a.eye.skywalking.plugin.jedis.v2.JedisMethodInterceptor.KEY_OF_REDIS_HOST; +import static com.a.eye.skywalking.plugin.jedis.v2.JedisMethodInterceptor.KEY_OF_REDIS_PORT; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class JedisConstructorWithShardInfoArgInterceptorTest { + private JedisConstructorWithShardInfoArgInterceptor interceptor; + @Mock + private EnhancedClassInstanceContext instanceContext; + @Mock + private ConstructorInvokeContext invokeContext; + + @Before + public void setUp() throws Exception { + interceptor = new JedisConstructorWithShardInfoArgInterceptor(); + + when(invokeContext.allArguments()).thenReturn(new Object[]{new JedisShardInfo("127.0.0.1", 6379)}); + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void onConstruct() throws Exception { + + interceptor.onConstruct(instanceContext, invokeContext); + verify(instanceContext, times(1)).set(KEY_OF_REDIS_CONN_INFO, "127.0.0.1:6379"); + verify(instanceContext, times(1)).set(KEY_OF_REDIS_HOST, "127.0.0.1"); + verify(instanceContext, times(1)).set(KEY_OF_REDIS_PORT, 6379); + } + +} \ No newline at end of file diff --git a/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisConstructorWithStringArgInterceptorTest.java b/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisConstructorWithStringArgInterceptorTest.java new file mode 100644 index 0000000000..a208a94f74 --- /dev/null +++ b/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisConstructorWithStringArgInterceptorTest.java @@ -0,0 +1,57 @@ +package com.a.eye.skywalking.plugin.jedis.v2; + +import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.ConstructorInvokeContext; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +import static com.a.eye.skywalking.plugin.jedis.v2.JedisMethodInterceptor.KEY_OF_REDIS_CONN_INFO; +import static com.a.eye.skywalking.plugin.jedis.v2.JedisMethodInterceptor.KEY_OF_REDIS_HOST; +import static com.a.eye.skywalking.plugin.jedis.v2.JedisMethodInterceptor.KEY_OF_REDIS_PORT; +import static org.junit.Assert.*; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class JedisConstructorWithStringArgInterceptorTest { + + private JedisConstructorWithStringArgInterceptor interceptor; + + @Mock + private EnhancedClassInstanceContext instanceContext; + @Mock + private ConstructorInvokeContext invokeContext; + + @Before + public void setUp() throws Exception { + interceptor = new JedisConstructorWithStringArgInterceptor(); + when(invokeContext.allArguments()).thenReturn(new Object[]{"127.0.0.1"}); + } + + @Test + public void onConstruct() throws Exception { + interceptor.onConstruct(instanceContext, invokeContext); + + verify(instanceContext, times(1)).set(KEY_OF_REDIS_CONN_INFO, "127.0.0.1:6379"); + verify(instanceContext, times(1)).set(KEY_OF_REDIS_HOST, "127.0.0.1"); + verify(instanceContext, times(1)).set(KEY_OF_REDIS_PORT, 6379); + } + + + @Test + public void onConstructWithPort(){ + when(invokeContext.allArguments()).thenReturn(new Object[]{"127.0.0.1", 16379}); + interceptor.onConstruct(instanceContext, invokeContext); + + verify(instanceContext, times(1)).set(KEY_OF_REDIS_CONN_INFO, "127.0.0.1:16379"); + verify(instanceContext, times(1)).set(KEY_OF_REDIS_HOST, "127.0.0.1"); + verify(instanceContext, times(1)).set(KEY_OF_REDIS_PORT, 16379); + } + +} \ No newline at end of file diff --git a/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisConstructorWithUriArgInterceptorTest.java b/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisConstructorWithUriArgInterceptorTest.java new file mode 100644 index 0000000000..293397fbe3 --- /dev/null +++ b/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisConstructorWithUriArgInterceptorTest.java @@ -0,0 +1,51 @@ +package com.a.eye.skywalking.plugin.jedis.v2; + +import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.ConstructorInvokeContext; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.net.URI; + +import static com.a.eye.skywalking.plugin.jedis.v2.JedisMethodInterceptor.KEY_OF_REDIS_CONN_INFO; +import static com.a.eye.skywalking.plugin.jedis.v2.JedisMethodInterceptor.KEY_OF_REDIS_HOST; +import static com.a.eye.skywalking.plugin.jedis.v2.JedisMethodInterceptor.KEY_OF_REDIS_PORT; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(PowerMockRunner.class) +@PrepareForTest(URI.class) +public class JedisConstructorWithUriArgInterceptorTest { + + private JedisConstructorWithUriArgInterceptor interceptor; + + @Mock + private EnhancedClassInstanceContext instanceContext; + @Mock + private ConstructorInvokeContext invokeContext; + private URI uri = URI.create("http://127.0.0.1:6379"); + + @Before + public void setUp() throws Exception { + interceptor = new JedisConstructorWithUriArgInterceptor(); + + when(invokeContext.allArguments()).thenReturn(new Object[]{uri}); + } + + + @Test + public void onConstruct() throws Exception { + interceptor.onConstruct(instanceContext, invokeContext); + + verify(instanceContext, times(1)).set(KEY_OF_REDIS_CONN_INFO, "127.0.0.1:6379"); + verify(instanceContext, times(1)).set(KEY_OF_REDIS_HOST, "127.0.0.1"); + verify(instanceContext, times(1)).set(KEY_OF_REDIS_PORT, 6379); + } +} \ No newline at end of file diff --git a/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisMethodInterceptorTest.java b/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisMethodInterceptorTest.java new file mode 100644 index 0000000000..db6df17a7b --- /dev/null +++ b/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisMethodInterceptorTest.java @@ -0,0 +1,152 @@ +package com.a.eye.skywalking.plugin.jedis.v2; + +import com.a.eye.skywalking.api.context.TracerContext; +import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodInvokeContext; +import com.a.eye.skywalking.sniffer.mock.context.MockTracerContextListener; +import com.a.eye.skywalking.sniffer.mock.context.SegmentAssert; +import com.a.eye.skywalking.trace.LogData; +import com.a.eye.skywalking.trace.Span; +import com.a.eye.skywalking.trace.TraceSegment; +import com.a.eye.skywalking.trace.tag.Tags; + +import org.hamcrest.CoreMatchers; +import org.hamcrest.MatcherAssert; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +import java.sql.SQLException; + +import static com.a.eye.skywalking.plugin.jedis.v2.JedisMethodInterceptor.KEY_OF_REDIS_HOST; +import static com.a.eye.skywalking.plugin.jedis.v2.JedisMethodInterceptor.KEY_OF_REDIS_HOSTS; +import static com.a.eye.skywalking.plugin.jedis.v2.JedisMethodInterceptor.KEY_OF_REDIS_PORT; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.anyInt; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class JedisMethodInterceptorTest { + + private JedisMethodInterceptor interceptor; + + private MockTracerContextListener mockTracerContextListener; + + @Mock + private EnhancedClassInstanceContext classInstanceContext; + @Mock + private InstanceMethodInvokeContext methodInvokeContext; + + @Before + public void setUp() throws Exception { + interceptor = new JedisMethodInterceptor(); + mockTracerContextListener = new MockTracerContextListener(); + + TracerContext.ListenerManager.add(mockTracerContextListener); + + when(classInstanceContext.get(KEY_OF_REDIS_HOST, String.class)).thenReturn("127.0.0.1"); + when(classInstanceContext.get(KEY_OF_REDIS_PORT)).thenReturn(6379); + when(methodInvokeContext.methodName()).thenReturn("set"); + when(methodInvokeContext.allArguments()).thenReturn(new Object[]{"OperationKey"}); + when(classInstanceContext.isContain("__$invokeCounterKey")).thenReturn(true); + } + + @Test + public void testIntercept() { + when(classInstanceContext.get("__$invokeCounterKey", Integer.class)).thenReturn(0); + interceptor.beforeMethod(classInstanceContext, methodInvokeContext, null); + when(classInstanceContext.get("__$invokeCounterKey", Integer.class)).thenReturn(1); + interceptor.afterMethod(classInstanceContext, methodInvokeContext, null); + + mockTracerContextListener.assertSize(1); + mockTracerContextListener.assertTraceSegment(0, new SegmentAssert() { + @Override + public void call(TraceSegment traceSegment) { + assertThat(traceSegment.getSpans().size(), is(1)); + Span span = traceSegment.getSpans().get(0); + assertRedisSpan(span); + } + }); + } + + @Test + public void testInterceptWithMultiHost() { + when(classInstanceContext.get("__$invokeCounterKey", Integer.class)).thenReturn(0); + when(classInstanceContext.get(KEY_OF_REDIS_HOST, String.class)).thenReturn(null); + when(classInstanceContext.get(KEY_OF_REDIS_HOSTS)).thenReturn("127.0.0.1:6379;127.0.0.1:16379;"); + + interceptor.beforeMethod(classInstanceContext, methodInvokeContext, null); + when(classInstanceContext.get("__$invokeCounterKey", Integer.class)).thenReturn(1); + interceptor.afterMethod(classInstanceContext, methodInvokeContext, null); + + mockTracerContextListener.assertSize(1); + mockTracerContextListener.assertTraceSegment(0, new SegmentAssert() { + @Override + public void call(TraceSegment traceSegment) { + assertThat(traceSegment.getSpans().size(), is(1)); + Span span = traceSegment.getSpans().get(0); + assertRedisSpan(span, "127.0.0.1:6379;127.0.0.1:16379;"); + } + }); + } + + @Test + public void testInterceptWithException() { + when(classInstanceContext.get("__$invokeCounterKey", Integer.class)).thenReturn(0); + interceptor.beforeMethod(classInstanceContext, methodInvokeContext, null); + interceptor.handleMethodException(new RuntimeException(), classInstanceContext, methodInvokeContext); + when(classInstanceContext.get("__$invokeCounterKey", Integer.class)).thenReturn(1); + interceptor.afterMethod(classInstanceContext, methodInvokeContext, null); + + mockTracerContextListener.assertSize(1); + mockTracerContextListener.assertTraceSegment(0, new SegmentAssert() { + @Override + public void call(TraceSegment traceSegment) { + assertThat(traceSegment.getSpans().size(), is(1)); + Span span = traceSegment.getSpans().get(0); + assertRedisSpan(span); + assertThat(span.getLogs().size(),is(1)); + assertLogData(span.getLogs().get(0)); + } + }); + } + + private void assertLogData(LogData logData) { + MatcherAssert.assertThat(logData.getFields().size(), is(4)); + MatcherAssert.assertThat(logData.getFields().get("event"), CoreMatchers.is("error")); + assertEquals(logData.getFields().get("error.kind"), RuntimeException.class.getName()); + assertNull(logData.getFields().get("message")); + } + + private void assertRedisSpan(Span span) { + assertThat(span.getOperationName(), is("set")); + assertThat(Tags.PEER_HOST.get(span), is("127.0.0.1")); + assertThat(Tags.PEER_PORT.get(span), is(6379)); + assertThat(Tags.COMPONENT.get(span), is("Redis")); + assertThat(Tags.DB_STATEMENT.get(span), is("set OperationKey")); + assertThat(Tags.DB_TYPE.get(span), is("Redis")); + assertTrue(Tags.SPAN_LAYER.isDB(span)); + } + + private void assertRedisSpan(Span span, String exceptedPeerHosts){ + assertThat(span.getOperationName(), is("set")); + assertThat(Tags.PEERS.get(span), is(exceptedPeerHosts)); + assertThat(Tags.COMPONENT.get(span), is("Redis")); + assertThat(Tags.DB_STATEMENT.get(span), is("set OperationKey")); + assertThat(Tags.DB_TYPE.get(span), is("Redis")); + assertTrue(Tags.SPAN_LAYER.isDB(span)); + } + + @After + public void tearDown() throws Exception { + TracerContext.ListenerManager.remove(mockTracerContextListener); + } + +} \ No newline at end of file -- GitLab