A unit test can be better. (#6620)
This commit is contained in:
parent
a396888de8
commit
35d7a520c5
|
|
@ -33,8 +33,8 @@ 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;
|
||||
import org.apache.skywalking.apm.plugin.lettuce.v5.mock.MockClientOptions;
|
||||
import org.apache.skywalking.apm.plugin.lettuce.v5.mock.MockRedisClusterClient;
|
||||
import org.apache.skywalking.apm.plugin.lettuce.v5.mock.MockRedisClusterClientConstructorInterceptor;
|
||||
import org.hamcrest.CoreMatchers;
|
||||
import org.hamcrest.MatcherAssert;
|
||||
import org.hamcrest.core.Is;
|
||||
|
|
@ -120,11 +120,13 @@ public class RedisChannelWriterInterceptorTest {
|
|||
redisURIs.add(RedisURI.create("localhost", i));
|
||||
}
|
||||
MockRedisClusterClient mockRedisClusterClient = new MockRedisClusterClient();
|
||||
MockRedisClusterClientConstructorInterceptor constructorInterceptor = new MockRedisClusterClientConstructorInterceptor();
|
||||
MockClientOptions options = new MockClientOptions();
|
||||
mockRedisClusterClient.setOptions(options);
|
||||
RedisClusterClientConstructorInterceptor constructorInterceptor = new RedisClusterClientConstructorInterceptor();
|
||||
constructorInterceptor.onConstruct(mockRedisClusterClient, new Object[] {
|
||||
null,
|
||||
redisURIs
|
||||
});
|
||||
assertThat(mockRedisClusterClient.getOptions().getSkyWalkingDynamicField().toString().length(), Is.is(200));
|
||||
assertThat(options.getSkyWalkingDynamicField().toString().length(), Is.is(200));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,22 +18,28 @@
|
|||
|
||||
package org.apache.skywalking.apm.plugin.lettuce.v5.mock;
|
||||
|
||||
import io.lettuce.core.RedisURI;
|
||||
import io.lettuce.core.cluster.ClusterClientOptions;
|
||||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
|
||||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
|
||||
import org.apache.skywalking.apm.agent.core.context.util.PeerFormat;
|
||||
|
||||
public class MockRedisClusterClientConstructorInterceptor implements InstanceConstructorInterceptor {
|
||||
public class MockClientOptions extends ClusterClientOptions implements EnhancedInstance {
|
||||
|
||||
private Object object;
|
||||
|
||||
public MockClientOptions() {
|
||||
this(ClusterClientOptions.builder());
|
||||
}
|
||||
|
||||
protected MockClientOptions(Builder builder) {
|
||||
super(builder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
|
||||
@SuppressWarnings("unchecked") Iterable<RedisURI> redisURIs = (Iterable<RedisURI>) allArguments[1];
|
||||
MockRedisClusterClient redisClusterClient = (MockRedisClusterClient) objInst;
|
||||
StringBuilder peer = new StringBuilder();
|
||||
for (RedisURI redisURI : redisURIs) {
|
||||
peer.append(redisURI.getHost()).append(":").append(redisURI.getPort()).append(";");
|
||||
}
|
||||
EnhancedInstance optionsInst = redisClusterClient.getOptions();
|
||||
optionsInst.setSkyWalkingDynamicField(PeerFormat.shorten(peer.toString()));
|
||||
public Object getSkyWalkingDynamicField() {
|
||||
return object;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSkyWalkingDynamicField(Object value) {
|
||||
this.object = value;
|
||||
}
|
||||
}
|
||||
|
|
@ -18,41 +18,20 @@
|
|||
|
||||
package org.apache.skywalking.apm.plugin.lettuce.v5.mock;
|
||||
|
||||
import io.lettuce.core.cluster.RedisClusterClient;
|
||||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
|
||||
|
||||
public class MockRedisClusterClient implements EnhancedInstance {
|
||||
public class MockRedisClusterClient extends RedisClusterClient implements EnhancedInstance {
|
||||
|
||||
private Object ms;
|
||||
|
||||
private EnhancedInstance options = new EnhancedInstance() {
|
||||
private Object os;
|
||||
|
||||
@Override
|
||||
public Object getSkyWalkingDynamicField() {
|
||||
return os;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSkyWalkingDynamicField(Object value) {
|
||||
this.os = value;
|
||||
}
|
||||
};
|
||||
|
||||
public EnhancedInstance getOptions() {
|
||||
return options;
|
||||
}
|
||||
|
||||
public void setOptions(EnhancedInstance options) {
|
||||
this.options = options;
|
||||
}
|
||||
private Object object;
|
||||
|
||||
@Override
|
||||
public Object getSkyWalkingDynamicField() {
|
||||
return ms;
|
||||
return object;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSkyWalkingDynamicField(Object value) {
|
||||
this.ms = value;
|
||||
this.object = value;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue