Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
润土的好友猹
SkyWalking
提交
1f876948
S
SkyWalking
项目概览
润土的好友猹
/
SkyWalking
与 Fork 源项目一致
Fork自
apache / SkyWalking
通知
9
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
SkyWalking
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
1f876948
编写于
3月 01, 2017
作者:
A
ascrutae
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add test case and fix some issue
上级
20193495
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
436 addition
and
3 deletion
+436
-3
skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/a/eye/skywalking/plugin/jedis/v2/JedisClusterConstructorWithHostAndPortArgInterceptor.java
...JedisClusterConstructorWithHostAndPortArgInterceptor.java
+1
-1
skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/a/eye/skywalking/plugin/jedis/v2/JedisClusterConstructorWithListHostAndPortArgInterceptor.java
...sClusterConstructorWithListHostAndPortArgInterceptor.java
+1
-1
skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/a/eye/skywalking/plugin/jedis/v2/JedisMethodInterceptor.java
...ye/skywalking/plugin/jedis/v2/JedisMethodInterceptor.java
+7
-1
skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisClusterConstructorWithHostAndPortArgInterceptorTest.java
...sClusterConstructorWithHostAndPortArgInterceptorTest.java
+56
-0
skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisClusterConstructorWithListHostAndPortArgInterceptorTest.java
...sterConstructorWithListHostAndPortArgInterceptorTest.java
+58
-0
skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisConstructorWithShardInfoArgInterceptorTest.java
...s/v2/JedisConstructorWithShardInfoArgInterceptorTest.java
+53
-0
skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisConstructorWithStringArgInterceptorTest.java
...edis/v2/JedisConstructorWithStringArgInterceptorTest.java
+57
-0
skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisConstructorWithUriArgInterceptorTest.java
...n/jedis/v2/JedisConstructorWithUriArgInterceptorTest.java
+51
-0
skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisMethodInterceptorTest.java
...kywalking/plugin/jedis/v2/JedisMethodInterceptorTest.java
+152
-0
未找到文件。
skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/a/eye/skywalking/plugin/jedis/v2/JedisClusterConstructorWithHostAndPortArgInterceptor.java
浏览文件 @
1f876948
...
...
@@ -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
());
...
...
skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/a/eye/skywalking/plugin/jedis/v2/JedisClusterConstructorWithListHostAndPortArgInterceptor.java
浏览文件 @
1f876948
...
...
@@ -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
()
);
}
}
skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/a/eye/skywalking/plugin/jedis/v2/JedisMethodInterceptor.java
浏览文件 @
1f876948
...
...
@@ -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
)
{
...
...
skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisClusterConstructorWithHostAndPortArgInterceptorTest.java
0 → 100644
浏览文件 @
1f876948
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
skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisClusterConstructorWithListHostAndPortArgInterceptorTest.java
0 → 100644
浏览文件 @
1f876948
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
<
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
(
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
skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisConstructorWithShardInfoArgInterceptorTest.java
0 → 100644
浏览文件 @
1f876948
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
skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisConstructorWithStringArgInterceptorTest.java
0 → 100644
浏览文件 @
1f876948
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
skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisConstructorWithUriArgInterceptorTest.java
0 → 100644
浏览文件 @
1f876948
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
skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/test/java/com/a/eye/skywalking/plugin/jedis/v2/JedisMethodInterceptorTest.java
0 → 100644
浏览文件 @
1f876948
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
.<
Object
>
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
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录