Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
带水的鱼儿
spring-framework
提交
e94811f1
S
spring-framework
项目概览
带水的鱼儿
/
spring-framework
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
e94811f1
编写于
7月 24, 2021
作者:
S
Sam Brannen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Polish WebSocketMessageBrokerConfigurationSupportTests
上级
d034a1f2
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
50 addition
and
60 deletion
+50
-60
spring-websocket/src/test/java/org/springframework/web/socket/config/annotation/WebSocketMessageBrokerConfigurationSupportTests.java
...tion/WebSocketMessageBrokerConfigurationSupportTests.java
+50
-60
未找到文件。
spring-websocket/src/test/java/org/springframework/web/socket/config/annotation/WebSocketMessageBrokerConfigurationSupportTests.java
浏览文件 @
e94811f1
/*
* Copyright 2002-202
0
the original author or authors.
* Copyright 2002-202
1
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -47,14 +47,12 @@ import org.springframework.messaging.support.ImmutableMessageChannelInterceptor;
import
org.springframework.scheduling.TaskScheduler
;
import
org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.servlet.HandlerMapping
;
import
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping
;
import
org.springframework.web.socket.WebSocketHandler
;
import
org.springframework.web.socket.WebSocketSession
;
import
org.springframework.web.socket.config.WebSocketMessageBrokerStats
;
import
org.springframework.web.socket.handler.TestWebSocketSession
;
import
org.springframework.web.socket.handler.WebSocketHandlerDecorator
;
import
org.springframework.web.socket.handler.WebSocketHandlerDecoratorFactory
;
import
org.springframework.web.socket.messaging.StompSubProtocolHandler
;
import
org.springframework.web.socket.messaging.StompTextMessageBuilder
;
import
org.springframework.web.socket.messaging.SubProtocolHandler
;
...
...
@@ -65,29 +63,29 @@ import static org.assertj.core.api.Assertions.assertThat;
import
static
org
.
mockito
.
Mockito
.
mock
;
/**
* Test
fixture
for {@link WebSocketMessageBrokerConfigurationSupport}.
* Test
s
for {@link WebSocketMessageBrokerConfigurationSupport}.
*
* @author Rossen Stoyanchev
* @author Sebastien Deleuze
*/
public
class
WebSocketMessageBrokerConfigurationSupportTests
{
class
WebSocketMessageBrokerConfigurationSupportTests
{
@Test
public
void
handlerMapping
()
{
ApplicationContext
con
fig
=
createConfig
(
TestChannelConfig
.
class
,
TestConfigurer
.
class
);
SimpleUrlHandlerMapping
hm
=
(
SimpleUrlHandlerMapping
)
config
.
getBean
(
HandlerMapping
.
class
);
void
handlerMapping
()
{
ApplicationContext
con
text
=
createContext
(
TestChannelConfig
.
class
,
TestConfigurer
.
class
);
SimpleUrlHandlerMapping
hm
=
context
.
getBean
(
SimpleUrl
HandlerMapping
.
class
);
assertThat
(
hm
.
getOrder
()).
isEqualTo
(
1
);
Map
<
String
,
Object
>
handlerMap
=
hm
.
getHandlerMap
();
assertThat
(
handlerMap
.
size
()).
isEqualTo
(
1
);
assertThat
(
handlerMap
).
hasSize
(
1
);
assertThat
(
handlerMap
.
get
(
"/simpleBroker"
)).
isNotNull
();
}
@Test
public
void
clientInboundChannelSendMessage
()
throws
Exception
{
ApplicationContext
con
fig
=
createConfig
(
TestChannelConfig
.
class
,
TestConfigurer
.
class
);
TestChannel
channel
=
con
fig
.
getBean
(
"clientInboundChannel"
,
TestChannel
.
class
);
SubProtocolWebSocketHandler
webSocketHandler
=
con
fig
.
getBean
(
SubProtocolWebSocketHandler
.
class
);
void
clientInboundChannelSendMessage
()
throws
Exception
{
ApplicationContext
con
text
=
createContext
(
TestChannelConfig
.
class
,
TestConfigurer
.
class
);
TestChannel
channel
=
con
text
.
getBean
(
"clientInboundChannel"
,
TestChannel
.
class
);
SubProtocolWebSocketHandler
webSocketHandler
=
con
text
.
getBean
(
SubProtocolWebSocketHandler
.
class
);
List
<
ChannelInterceptor
>
interceptors
=
channel
.
getInterceptors
();
assertThat
(
interceptors
.
get
(
interceptors
.
size
()
-
1
).
getClass
()).
isEqualTo
(
ImmutableMessageChannelInterceptor
.
class
);
...
...
@@ -108,66 +106,66 @@ public class WebSocketMessageBrokerConfigurationSupportTests {
}
@Test
public
void
clientOutboundChannel
()
{
ApplicationContext
con
fig
=
createConfig
(
TestChannelConfig
.
class
,
TestConfigurer
.
class
);
TestChannel
channel
=
con
fig
.
getBean
(
"clientOutboundChannel"
,
TestChannel
.
class
);
void
clientOutboundChannel
()
{
ApplicationContext
con
text
=
createContext
(
TestChannelConfig
.
class
,
TestConfigurer
.
class
);
TestChannel
channel
=
con
text
.
getBean
(
"clientOutboundChannel"
,
TestChannel
.
class
);
Set
<
MessageHandler
>
handlers
=
channel
.
getSubscribers
();
List
<
ChannelInterceptor
>
interceptors
=
channel
.
getInterceptors
();
assertThat
(
interceptors
.
get
(
interceptors
.
size
()
-
1
).
getClass
()).
isEqualTo
(
ImmutableMessageChannelInterceptor
.
class
);
assertThat
(
handlers
.
size
()).
isEqualTo
(
1
);
assertThat
(
handlers
.
contains
(
config
.
getBean
(
SubProtocolWebSocketHandler
.
class
))).
isTrue
(
);
assertThat
(
handlers
).
hasSize
(
1
);
assertThat
(
handlers
).
contains
(
context
.
getBean
(
SubProtocolWebSocketHandler
.
class
)
);
}
@Test
public
void
brokerChannel
()
{
ApplicationContext
con
fig
=
createConfig
(
TestChannelConfig
.
class
,
TestConfigurer
.
class
);
TestChannel
channel
=
con
fig
.
getBean
(
"brokerChannel"
,
TestChannel
.
class
);
void
brokerChannel
()
{
ApplicationContext
con
text
=
createContext
(
TestChannelConfig
.
class
,
TestConfigurer
.
class
);
TestChannel
channel
=
con
text
.
getBean
(
"brokerChannel"
,
TestChannel
.
class
);
Set
<
MessageHandler
>
handlers
=
channel
.
getSubscribers
();
List
<
ChannelInterceptor
>
interceptors
=
channel
.
getInterceptors
();
assertThat
(
interceptors
.
get
(
interceptors
.
size
()
-
1
).
getClass
()).
isEqualTo
(
ImmutableMessageChannelInterceptor
.
class
);
assertThat
(
handlers
.
size
()).
isEqualTo
(
2
);
assertThat
(
handlers
.
contains
(
config
.
getBean
(
SimpleBrokerMessageHandler
.
class
))).
isTrue
(
);
assertThat
(
handlers
.
contains
(
config
.
getBean
(
UserDestinationMessageHandler
.
class
))).
isTrue
(
);
assertThat
(
handlers
).
hasSize
(
2
);
assertThat
(
handlers
).
contains
(
context
.
getBean
(
SimpleBrokerMessageHandler
.
class
)
);
assertThat
(
handlers
).
contains
(
context
.
getBean
(
UserDestinationMessageHandler
.
class
)
);
}
@Test
public
void
webSocketHandler
()
{
ApplicationContext
con
fig
=
createConfig
(
TestChannelConfig
.
class
,
TestConfigurer
.
class
);
SubProtocolWebSocketHandler
subWsHandler
=
con
fig
.
getBean
(
SubProtocolWebSocketHandler
.
class
);
void
webSocketHandler
()
{
ApplicationContext
con
text
=
createContext
(
TestChannelConfig
.
class
,
TestConfigurer
.
class
);
SubProtocolWebSocketHandler
subWsHandler
=
con
text
.
getBean
(
SubProtocolWebSocketHandler
.
class
);
assertThat
(
subWsHandler
.
getSendBufferSizeLimit
()).
isEqualTo
(
(
1024
*
1024
)
);
assertThat
(
subWsHandler
.
getSendTimeLimit
()).
isEqualTo
(
(
25
*
1000
)
);
assertThat
(
subWsHandler
.
getTimeToFirstMessage
()).
isEqualTo
(
(
30
*
1000
)
);
assertThat
(
subWsHandler
.
getSendBufferSizeLimit
()).
isEqualTo
(
1024
*
1024
);
assertThat
(
subWsHandler
.
getSendTimeLimit
()).
isEqualTo
(
25
*
1000
);
assertThat
(
subWsHandler
.
getTimeToFirstMessage
()).
isEqualTo
(
30
*
1000
);
Map
<
String
,
SubProtocolHandler
>
handlerMap
=
subWsHandler
.
getProtocolHandlerMap
();
StompSubProtocolHandler
protocolHandler
=
(
StompSubProtocolHandler
)
handlerMap
.
get
(
"v12.stomp"
);
assertThat
(
protocolHandler
.
getMessageSizeLimit
()).
isEqualTo
(
(
128
*
1024
)
);
assertThat
(
protocolHandler
.
getMessageSizeLimit
()).
isEqualTo
(
128
*
1024
);
}
@Test
public
void
taskScheduler
()
{
ApplicationContext
con
fig
=
createConfig
(
TestChannelConfig
.
class
,
TestConfigurer
.
class
);
void
taskScheduler
()
{
ApplicationContext
con
text
=
createContext
(
TestChannelConfig
.
class
,
TestConfigurer
.
class
);
String
name
=
"messageBrokerSockJsTaskScheduler"
;
ThreadPoolTaskScheduler
taskScheduler
=
con
fig
.
getBean
(
name
,
ThreadPoolTaskScheduler
.
class
);
ThreadPoolTaskScheduler
taskScheduler
=
con
text
.
getBean
(
name
,
ThreadPoolTaskScheduler
.
class
);
ScheduledThreadPoolExecutor
executor
=
taskScheduler
.
getScheduledThreadPoolExecutor
();
assertThat
(
executor
.
getCorePoolSize
()).
isEqualTo
(
Runtime
.
getRuntime
().
availableProcessors
());
assertThat
(
executor
.
getRemoveOnCancelPolicy
()).
isTrue
();
SimpleBrokerMessageHandler
handler
=
con
fig
.
getBean
(
SimpleBrokerMessageHandler
.
class
);
SimpleBrokerMessageHandler
handler
=
con
text
.
getBean
(
SimpleBrokerMessageHandler
.
class
);
assertThat
(
handler
.
getTaskScheduler
()).
isNotNull
();
assertThat
(
handler
.
getHeartbeatValue
()).
isEqualTo
(
new
long
[]
{
15000
,
15000
}
);
assertThat
(
handler
.
getHeartbeatValue
()).
containsExactly
(
15000
,
15000
);
}
@Test
public
void
webSocketMessageBrokerStats
()
{
ApplicationContext
con
fig
=
createConfig
(
TestChannelConfig
.
class
,
TestConfigurer
.
class
);
void
webSocketMessageBrokerStats
()
{
ApplicationContext
con
text
=
createContext
(
TestChannelConfig
.
class
,
TestConfigurer
.
class
);
String
name
=
"webSocketMessageBrokerStats"
;
WebSocketMessageBrokerStats
stats
=
con
fig
.
getBean
(
name
,
WebSocketMessageBrokerStats
.
class
);
WebSocketMessageBrokerStats
stats
=
con
text
.
getBean
(
name
,
WebSocketMessageBrokerStats
.
class
);
String
actual
=
stats
.
toString
();
String
expected
=
"WebSocketSession\\[0 current WS\\(0\\)-HttpStream\\(0\\)-HttpPoll\\(0\\), "
+
"0 total, 0 closed abnormally \\(0 connect failure, 0 send limit, 0 transport error\\)], "
+
...
...
@@ -177,16 +175,16 @@ public class WebSocketMessageBrokerConfigurationSupportTests {
"outboundChannel\\[pool size = \\d, active threads = \\d, queued tasks = \\d, completed tasks = \\d], "
+
"sockJsScheduler\\[pool size = \\d, active threads = \\d, queued tasks = \\d, completed tasks = \\d]"
;
assertThat
(
actual
.
matches
(
expected
)).
as
(
"\nExpected: "
+
expected
.
replace
(
"\\"
,
""
)
+
"\n Actual: "
+
actual
).
isTrue
(
);
assertThat
(
actual
).
matches
(
expected
);
}
@Test
public
void
webSocketHandlerDecorator
()
throws
Exception
{
ApplicationContext
con
fig
=
createConfig
(
WebSocketHandlerDecoratorConfig
.
class
);
WebSocketHandler
handler
=
con
fig
.
getBean
(
SubProtocolWebSocketHandler
.
class
);
void
webSocketHandlerDecorator
()
throws
Exception
{
ApplicationContext
con
text
=
createContext
(
WebSocketHandlerDecoratorConfig
.
class
);
WebSocketHandler
handler
=
con
text
.
getBean
(
SubProtocolWebSocketHandler
.
class
);
assertThat
(
handler
).
isNotNull
();
SimpleUrlHandlerMapping
mapping
=
(
SimpleUrlHandlerMapping
)
config
.
getBean
(
"stompWebSocketHandlerMapping"
);
SimpleUrlHandlerMapping
mapping
=
context
.
getBean
(
"stompWebSocketHandlerMapping"
,
SimpleUrlHandlerMapping
.
class
);
WebSocketHttpRequestHandler
httpHandler
=
(
WebSocketHttpRequestHandler
)
mapping
.
getHandlerMap
().
get
(
"/test"
);
handler
=
httpHandler
.
getWebSocketHandler
();
...
...
@@ -196,11 +194,8 @@ public class WebSocketMessageBrokerConfigurationSupportTests {
}
private
ApplicationContext
createConfig
(
Class
<?>...
configClasses
)
{
AnnotationConfigApplicationContext
config
=
new
AnnotationConfigApplicationContext
();
config
.
register
(
configClasses
);
config
.
refresh
();
return
config
;
private
ApplicationContext
createContext
(
Class
<?>...
configClasses
)
{
return
new
AnnotationConfigApplicationContext
(
configClasses
);
}
...
...
@@ -286,17 +281,12 @@ public class WebSocketMessageBrokerConfigurationSupportTests {
@Override
protected
void
configureWebSocketTransport
(
WebSocketTransportRegistration
registry
)
{
registry
.
addDecoratorFactory
(
new
WebSocketHandlerDecoratorFactory
()
{
@Override
public
WebSocketHandlerDecorator
decorate
(
WebSocketHandler
handler
)
{
return
new
WebSocketHandlerDecorator
(
handler
)
{
@Override
public
void
afterConnectionEstablished
(
WebSocketSession
session
)
throws
Exception
{
session
.
getAttributes
().
put
(
"decorated"
,
true
);
super
.
afterConnectionEstablished
(
session
);
}
};
}
registry
.
addDecoratorFactory
(
handler
->
new
WebSocketHandlerDecorator
(
handler
)
{
@Override
public
void
afterConnectionEstablished
(
WebSocketSession
session
)
throws
Exception
{
session
.
getAttributes
().
put
(
"decorated"
,
true
);
super
.
afterConnectionEstablished
(
session
);
}
});
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录