Commit 24de5e75 authored by 吴晟's avatar 吴晟 Committed by GitHub
Browse files

Merge pull request #95 from ascrutae/feature/3.0

add test case and fix some issue
parents dbcb868d 6ee093b1
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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());
+1 −1
Original line number Diff line number Diff line
@@ -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());
    }
}
+7 −1
Original line number Diff line number Diff line
@@ -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("Jedis/" + 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) {
+56 −0
Original line number Diff line number Diff line
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
+62 −0
Original line number Diff line number Diff line
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.hamcrest.core.Is.is;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.contains;
import static org.mockito.Matchers.eq;
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<HostAndPort> hostAndPortSet;

    @Before
    public void setUp() throws Exception {
        hostAndPortSet = new HashSet<HostAndPort>();
        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(eq(KEY_OF_REDIS_CONN_INFO), contains("127.0.0.1:6379;"));
        verify(instanceContext, times(1)).set(eq(KEY_OF_REDIS_HOSTS), contains("127.0.0.1:16379;"));
    }

}
 No newline at end of file
Loading