RedisChannelWriterInterceptorTest.java 5.5 KB
Newer Older
于玉桔 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.lettuce.v5;

21
import io.lettuce.core.RedisURI;
于玉桔 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35
import io.lettuce.core.protocol.Command;
import io.lettuce.core.protocol.CommandType;
import io.lettuce.core.protocol.RedisCommand;
import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan;
import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer;
import org.apache.skywalking.apm.agent.core.context.trace.TraceSegment;
import org.apache.skywalking.apm.agent.core.context.util.TagValuePair;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.test.helper.SegmentHelper;
import org.apache.skywalking.apm.agent.test.helper.SpanHelper;
import org.apache.skywalking.apm.agent.test.tools.AgentServiceRule;
import org.apache.skywalking.apm.agent.test.tools.SegmentStorage;
import org.apache.skywalking.apm.agent.test.tools.SegmentStoragePoint;
import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner;
C
CharliePu 已提交
36
import org.apache.skywalking.apm.plugin.lettuce.v5.mock.MockClientOptions;
37
import org.apache.skywalking.apm.plugin.lettuce.v5.mock.MockRedisClusterClient;
于玉桔 已提交
38 39 40 41 42 43 44 45 46 47 48
import org.hamcrest.CoreMatchers;
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.Is;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;

49
import java.util.ArrayList;
于玉桔 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(TracingSegmentRunner.class)
public class RedisChannelWriterInterceptorTest {

    @SegmentStoragePoint
    private SegmentStorage segmentStorage;

    @Rule
    public AgentServiceRule serviceRule = new AgentServiceRule();

    @Mock
    private MockInstance mockClientOptionsInstance;
    @Mock
    private MockInstance mockRedisChannelWriterInstance;

    private RedisChannelWriterInterceptor interceptor;

    private class MockInstance implements EnhancedInstance {
        private Object object;

        @Override
        public Object getSkyWalkingDynamicField() {
            return object;
        }

        @Override
        public void setSkyWalkingDynamicField(Object value) {
            this.object = value;
        }
    }

86 87 88 89
    @SuppressWarnings({
        "rawtypes",
        "unchecked"
    })
于玉桔 已提交
90 91 92 93 94 95 96 97 98 99
    @Before
    public void setUp() throws Exception {
        mockRedisChannelWriterInstance = new MockInstance();
        mockClientOptionsInstance = new MockInstance();
        mockClientOptionsInstance.setSkyWalkingDynamicField("127.0.0.1:6379;127.0.0.1:6378;");
        interceptor = new RedisChannelWriterInterceptor();
    }

    @Test
    public void testInterceptor() throws Throwable {
100
        interceptor.onConstruct(mockRedisChannelWriterInstance, new Object[] {mockClientOptionsInstance});
于玉桔 已提交
101
        RedisCommand redisCommand = new Command(CommandType.SET, null);
102
        interceptor.beforeMethod(mockRedisChannelWriterInstance, null, new Object[] {redisCommand}, null, null);
于玉桔 已提交
103 104 105 106 107 108 109 110 111 112 113 114
        interceptor.afterMethod(mockRedisChannelWriterInstance, null, null, null, null);
        MatcherAssert.assertThat((String) mockRedisChannelWriterInstance.getSkyWalkingDynamicField(), Is.is("127.0.0.1:6379;127.0.0.1:6378;"));
        TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0);
        List<AbstractTracingSpan> spans = SegmentHelper.getSpans(traceSegment);
        assertThat(spans.size(), is(1));
        assertThat(spans.get(0).getOperationName(), is("Lettuce/SET"));
        assertThat(spans.get(0).isExit(), is(true));
        assertThat(SpanHelper.getComponentId(spans.get(0)), is(57));
        List<TagValuePair> tags = SpanHelper.getTags(spans.get(0));
        assertThat(tags.get(0).getValue(), is("Redis"));
        assertThat(SpanHelper.getLayer(spans.get(0)), CoreMatchers.is(SpanLayer.CACHE));
    }
115 116 117 118 119 120 121 122

    @Test
    public void testOnHugeClusterConsumerConfig() {
        List<RedisURI> redisURIs = new ArrayList<>(100);
        for (int i = 0; i < 100; i++) {
            redisURIs.add(RedisURI.create("localhost", i));
        }
        MockRedisClusterClient mockRedisClusterClient = new MockRedisClusterClient();
C
CharliePu 已提交
123 124 125
        MockClientOptions options = new MockClientOptions();
        mockRedisClusterClient.setOptions(options);
        RedisClusterClientConstructorInterceptor constructorInterceptor = new RedisClusterClientConstructorInterceptor();
126 127 128 129
        constructorInterceptor.onConstruct(mockRedisClusterClient, new Object[] {
            null,
            redisURIs
        });
C
CharliePu 已提交
130
        assertThat(options.getSkyWalkingDynamicField().toString().length(), Is.is(200));
131
    }
于玉桔 已提交
132
}