Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
dotNET Platform
MQTTnet
提交
cf2974cd
MQTTnet
项目概览
dotNET Platform
/
MQTTnet
12 个月 前同步成功
通知
0
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
MQTTnet
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
cf2974cd
编写于
3月 20, 2022
作者:
C
Christian
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update Subscribe_Tests.cs
上级
7d95c238
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
125 addition
and
116 deletion
+125
-116
Source/MQTTnet.Tests/Server/Subscribe_Tests.cs
Source/MQTTnet.Tests/Server/Subscribe_Tests.cs
+125
-116
未找到文件。
Source/MQTTnet.Tests/Server/Subscribe_Tests.cs
浏览文件 @
cf2974cd
...
...
@@ -18,6 +18,33 @@ namespace MQTTnet.Tests.Server
[
TestClass
]
public
sealed
class
Subscribe_Tests
:
BaseTestClass
{
[
TestMethod
]
public
async
Task
Deny_Invalid_Topic
()
{
using
(
var
testEnvironment
=
CreateTestEnvironment
(
MqttProtocolVersion
.
V500
))
{
var
server
=
await
testEnvironment
.
StartServer
();
server
.
InterceptingSubscriptionAsync
+=
e
=>
{
if
(
e
.
TopicFilter
.
Topic
==
"not_allowed_topic"
)
{
e
.
Response
.
ReasonCode
=
MqttSubscribeReasonCode
.
TopicFilterInvalid
;
}
return
PlatformAbstractionLayer
.
CompletedTask
;
};
var
client
=
await
testEnvironment
.
ConnectClient
();
var
subscribeResult
=
await
client
.
SubscribeAsync
(
"allowed_topic"
);
Assert
.
AreEqual
(
MqttClientSubscribeResultCode
.
GrantedQoS0
,
subscribeResult
.
Items
.
First
().
ResultCode
);
subscribeResult
=
await
client
.
SubscribeAsync
(
"not_allowed_topic"
);
Assert
.
AreEqual
(
MqttClientSubscribeResultCode
.
TopicFilterInvalid
,
subscribeResult
.
Items
.
First
().
ResultCode
);
}
}
[
TestMethod
]
public
async
Task
Intercept_Subscription
()
{
...
...
@@ -31,7 +58,7 @@ namespace MQTTnet.Tests.Server
e
.
TopicFilter
.
Topic
=
"a"
;
return
PlatformAbstractionLayer
.
CompletedTask
;
};
var
topicAReceived
=
false
;
var
topicBReceived
=
false
;
...
...
@@ -46,7 +73,7 @@ namespace MQTTnet.Tests.Server
{
topicBReceived
=
true
;
}
return
PlatformAbstractionLayer
.
CompletedTask
;
};
...
...
@@ -62,67 +89,27 @@ namespace MQTTnet.Tests.Server
}
[
TestMethod
]
public
async
Task
Subscribe_Unsubscribe
()
public
async
Task
Response_Contains_Equal_Reason_Codes
()
{
using
(
var
testEnvironment
=
CreateTestEnvironment
())
{
var
receivedMessagesCount
=
0
;
var
server
=
await
testEnvironment
.
StartServer
();
var
c1
=
await
testEnvironment
.
ConnectClient
(
new
MqttClientOptionsBuilder
().
WithClientId
(
"c1"
));
c1
.
ApplicationMessageReceivedAsync
+=
e
=>
{
Interlocked
.
Increment
(
ref
receivedMessagesCount
);
return
PlatformAbstractionLayer
.
CompletedTask
;
};
var
c2
=
await
testEnvironment
.
ConnectClient
(
new
MqttClientOptionsBuilder
().
WithClientId
(
"c2"
));
var
message
=
new
MqttApplicationMessageBuilder
().
WithTopic
(
"a"
).
WithQualityOfServiceLevel
(
MqttQualityOfServiceLevel
.
ExactlyOnce
).
Build
();
await
c2
.
PublishAsync
(
message
);
await
Task
.
Delay
(
500
);
Assert
.
AreEqual
(
0
,
receivedMessagesCount
);
var
subscribeEventCalled
=
false
;
server
.
ClientSubscribedTopicAsync
+=
e
=>
{
subscribeEventCalled
=
e
.
TopicFilter
.
Topic
==
"a"
&&
e
.
ClientId
==
c1
.
Options
.
ClientId
;
return
PlatformAbstractionLayer
.
CompletedTask
;
};
await
c1
.
SubscribeAsync
(
new
MqttTopicFilter
{
Topic
=
"a"
,
QualityOfServiceLevel
=
MqttQualityOfServiceLevel
.
AtLeastOnce
});
await
Task
.
Delay
(
250
);
Assert
.
IsTrue
(
subscribeEventCalled
,
"Subscribe event not called."
);
await
c2
.
PublishAsync
(
message
);
await
Task
.
Delay
(
250
);
Assert
.
AreEqual
(
1
,
receivedMessagesCount
);
var
unsubscribeEventCalled
=
false
;
server
.
ClientUnsubscribedTopicAsync
+=
e
=>
{
unsubscribeEventCalled
=
e
.
TopicFilter
==
"a"
&&
e
.
ClientId
==
c1
.
Options
.
ClientId
;
return
PlatformAbstractionLayer
.
CompletedTask
;
};
await
c1
.
UnsubscribeAsync
(
"a"
);
await
Task
.
Delay
(
250
);
Assert
.
IsTrue
(
unsubscribeEventCalled
,
"Unsubscribe event not called."
);
await
c2
.
PublishAsync
(
message
);
await
Task
.
Delay
(
500
);
Assert
.
AreEqual
(
1
,
receivedMessagesCount
);
await
testEnvironment
.
StartServer
();
var
client
=
await
testEnvironment
.
ConnectClient
();
await
Task
.
Delay
(
500
);
var
subscribeOptions
=
new
MqttClientSubscribeOptionsBuilder
().
WithTopicFilter
(
"a"
)
.
WithTopicFilter
(
"b"
,
MqttQualityOfServiceLevel
.
AtLeastOnce
)
.
WithTopicFilter
(
"c"
,
MqttQualityOfServiceLevel
.
ExactlyOnce
)
.
WithTopicFilter
(
"d"
)
.
Build
();
var
response
=
await
client
.
SubscribeAsync
(
subscribeOptions
);
Assert
.
AreEqual
(
1
,
receivedMessages
Count
);
Assert
.
AreEqual
(
subscribeOptions
.
TopicFilters
.
Count
,
response
.
Items
.
Count
);
}
}
[
TestMethod
]
public
async
Task
Subscribe_
Multiple_In_Single_Request
()
public
async
Task
Subscribe_
Lots_In_Multiple_Requests
()
{
using
(
var
testEnvironment
=
CreateTestEnvironment
())
{
...
...
@@ -136,26 +123,31 @@ namespace MQTTnet.Tests.Server
Interlocked
.
Increment
(
ref
receivedMessagesCount
);
return
PlatformAbstractionLayer
.
CompletedTask
;
};
await
c1
.
SubscribeAsync
(
new
MqttClientSubscribeOptionsBuilder
()
.
WithTopicFilter
(
"a"
)
.
WithTopicFilter
(
"b"
)
.
WithTopicFilter
(
"c"
)
.
Build
());
for
(
var
i
=
0
;
i
<
500
;
i
++)
{
var
so
=
new
MqttClientSubscribeOptionsBuilder
().
WithTopicFilter
(
i
.
ToString
()).
Build
();
await
c1
.
SubscribeAsync
(
so
).
ConfigureAwait
(
false
);
await
Task
.
Delay
(
10
);
}
var
c2
=
await
testEnvironment
.
ConnectClient
();
await
c2
.
PublishStringAsync
(
"a"
);
await
Task
.
Delay
(
100
);
Assert
.
AreEqual
(
receivedMessagesCount
,
1
);
var
messageBuilder
=
new
MqttApplicationMessageBuilder
();
for
(
var
i
=
0
;
i
<
500
;
i
++)
{
messageBuilder
.
WithTopic
(
i
.
ToString
());
await
c2
.
PublishStringAsync
(
"b"
);
await
Task
.
Delay
(
100
);
Assert
.
AreEqual
(
receivedMessagesCount
,
2
);
await
c2
.
PublishAsync
(
messageBuilder
.
Build
()).
ConfigureAwait
(
false
);
await
c2
.
PublishStringAsync
(
"c"
);
await
Task
.
Delay
(
100
);
Assert
.
AreEqual
(
receivedMessagesCount
,
3
);
await
Task
.
Delay
(
10
);
}
SpinWait
.
SpinUntil
(()
=>
receivedMessagesCount
==
500
,
5000
);
Assert
.
AreEqual
(
500
,
receivedMessagesCount
);
}
}
...
...
@@ -200,7 +192,7 @@ namespace MQTTnet.Tests.Server
}
[
TestMethod
]
public
async
Task
Subscribe_
Lots_In_Multiple_Requests
()
public
async
Task
Subscribe_
Multiple_In_Multiple_Request
()
{
using
(
var
testEnvironment
=
CreateTestEnvironment
())
{
...
...
@@ -215,36 +207,30 @@ namespace MQTTnet.Tests.Server
return
PlatformAbstractionLayer
.
CompletedTask
;
};
for
(
var
i
=
0
;
i
<
500
;
i
++)
{
var
so
=
new
MqttClientSubscribeOptionsBuilder
()
.
WithTopicFilter
(
i
.
ToString
()).
Build
();
await
c1
.
SubscribeAsync
(
new
MqttClientSubscribeOptionsBuilder
().
WithTopicFilter
(
"a"
).
Build
());
await
c1
.
SubscribeAsync
(
so
).
ConfigureAwait
(
false
);
await
c1
.
SubscribeAsync
(
new
MqttClientSubscribeOptionsBuilder
().
WithTopicFilter
(
"b"
).
Build
()
);
await
Task
.
Delay
(
10
);
}
await
c1
.
SubscribeAsync
(
new
MqttClientSubscribeOptionsBuilder
().
WithTopicFilter
(
"c"
).
Build
());
var
c2
=
await
testEnvironment
.
ConnectClient
();
var
messageBuilder
=
new
MqttApplicationMessageBuilder
();
for
(
var
i
=
0
;
i
<
500
;
i
++)
{
messageBuilder
.
WithTopic
(
i
.
ToString
());
await
c2
.
PublishAsync
(
messageBuilder
.
Build
()).
ConfigureAwait
(
false
);
await
Task
.
Delay
(
10
);
}
await
c2
.
PublishStringAsync
(
"a"
);
await
Task
.
Delay
(
100
);
Assert
.
AreEqual
(
receivedMessagesCount
,
1
);
SpinWait
.
SpinUntil
(()
=>
receivedMessagesCount
==
500
,
5000
);
await
c2
.
PublishStringAsync
(
"b"
);
await
Task
.
Delay
(
100
);
Assert
.
AreEqual
(
receivedMessagesCount
,
2
);
Assert
.
AreEqual
(
500
,
receivedMessagesCount
);
await
c2
.
PublishStringAsync
(
"c"
);
await
Task
.
Delay
(
100
);
Assert
.
AreEqual
(
receivedMessagesCount
,
3
);
}
}
[
TestMethod
]
public
async
Task
Subscribe_Multiple_In_
Multip
le_Request
()
public
async
Task
Subscribe_Multiple_In_
Sing
le_Request
()
{
using
(
var
testEnvironment
=
CreateTestEnvironment
())
{
...
...
@@ -258,18 +244,8 @@ namespace MQTTnet.Tests.Server
Interlocked
.
Increment
(
ref
receivedMessagesCount
);
return
PlatformAbstractionLayer
.
CompletedTask
;
};
await
c1
.
SubscribeAsync
(
new
MqttClientSubscribeOptionsBuilder
()
.
WithTopicFilter
(
"a"
)
.
Build
());
await
c1
.
SubscribeAsync
(
new
MqttClientSubscribeOptionsBuilder
()
.
WithTopicFilter
(
"b"
)
.
Build
());
await
c1
.
SubscribeAsync
(
new
MqttClientSubscribeOptionsBuilder
()
.
WithTopicFilter
(
"c"
)
.
Build
());
await
c1
.
SubscribeAsync
(
new
MqttClientSubscribeOptionsBuilder
().
WithTopicFilter
(
"a"
).
WithTopicFilter
(
"b"
).
WithTopicFilter
(
"c"
).
Build
());
var
c2
=
await
testEnvironment
.
ConnectClient
();
...
...
@@ -286,31 +262,64 @@ namespace MQTTnet.Tests.Server
Assert
.
AreEqual
(
receivedMessagesCount
,
3
);
}
}
[
TestMethod
]
public
async
Task
Deny_Invalid_Topic
()
public
async
Task
Subscribe_Unsubscribe
()
{
using
(
var
testEnvironment
=
CreateTestEnvironment
(
MqttProtocolVersion
.
V500
))
using
(
var
testEnvironment
=
CreateTestEnvironment
())
{
var
receivedMessagesCount
=
0
;
var
server
=
await
testEnvironment
.
StartServer
();
server
.
InterceptingSubscriptionAsync
+=
e
=>
var
c1
=
await
testEnvironment
.
ConnectClient
(
new
MqttClientOptionsBuilder
().
WithClientId
(
"c1"
));
c1
.
ApplicationMessageReceivedAsync
+=
e
=>
{
if
(
e
.
TopicFilter
.
Topic
==
"not_allowed_topic"
)
{
e
.
Response
.
ReasonCode
=
MqttSubscribeReasonCode
.
TopicFilterInvalid
;
}
Interlocked
.
Increment
(
ref
receivedMessagesCount
);
return
PlatformAbstractionLayer
.
CompletedTask
;
};
var
client
=
await
testEnvironment
.
ConnectClient
();
var
subscribeResult
=
await
client
.
SubscribeAsync
(
"allowed_topic"
);
Assert
.
AreEqual
(
MqttClientSubscribeResultCode
.
GrantedQoS0
,
subscribeResult
.
Items
.
First
().
ResultCode
);
subscribeResult
=
await
client
.
SubscribeAsync
(
"not_allowed_topic"
);
Assert
.
AreEqual
(
MqttClientSubscribeResultCode
.
TopicFilterInvalid
,
subscribeResult
.
Items
.
First
().
ResultCode
);
var
c2
=
await
testEnvironment
.
ConnectClient
(
new
MqttClientOptionsBuilder
().
WithClientId
(
"c2"
));
var
message
=
new
MqttApplicationMessageBuilder
().
WithTopic
(
"a"
).
WithQualityOfServiceLevel
(
MqttQualityOfServiceLevel
.
ExactlyOnce
).
Build
();
await
c2
.
PublishAsync
(
message
);
await
Task
.
Delay
(
500
);
Assert
.
AreEqual
(
0
,
receivedMessagesCount
);
var
subscribeEventCalled
=
false
;
server
.
ClientSubscribedTopicAsync
+=
e
=>
{
subscribeEventCalled
=
e
.
TopicFilter
.
Topic
==
"a"
&&
e
.
ClientId
==
c1
.
Options
.
ClientId
;
return
PlatformAbstractionLayer
.
CompletedTask
;
};
await
c1
.
SubscribeAsync
(
new
MqttTopicFilter
{
Topic
=
"a"
,
QualityOfServiceLevel
=
MqttQualityOfServiceLevel
.
AtLeastOnce
});
await
Task
.
Delay
(
250
);
Assert
.
IsTrue
(
subscribeEventCalled
,
"Subscribe event not called."
);
await
c2
.
PublishAsync
(
message
);
await
Task
.
Delay
(
250
);
Assert
.
AreEqual
(
1
,
receivedMessagesCount
);
var
unsubscribeEventCalled
=
false
;
server
.
ClientUnsubscribedTopicAsync
+=
e
=>
{
unsubscribeEventCalled
=
e
.
TopicFilter
==
"a"
&&
e
.
ClientId
==
c1
.
Options
.
ClientId
;
return
PlatformAbstractionLayer
.
CompletedTask
;
};
await
c1
.
UnsubscribeAsync
(
"a"
);
await
Task
.
Delay
(
250
);
Assert
.
IsTrue
(
unsubscribeEventCalled
,
"Unsubscribe event not called."
);
await
c2
.
PublishAsync
(
message
);
await
Task
.
Delay
(
500
);
Assert
.
AreEqual
(
1
,
receivedMessagesCount
);
await
Task
.
Delay
(
500
);
Assert
.
AreEqual
(
1
,
receivedMessagesCount
);
}
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录