mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-21 04:03:05 +08:00
improved api surface of mqttserver and fixed unittests
This commit is contained in:
@@ -1,115 +1,20 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{A7FF0C91-25DE-4BA6-B39E-F54E8DADF1CC}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>MQTTnet.Core.Tests</RootNamespace>
|
||||
<AssemblyName>MQTTnet.Core.Tests</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Any CPU|AnyCPU'">
|
||||
<OutputPath>bin\Any CPU\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<Optimize>true</Optimize>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<OutputPath>bin\x64\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<DebugType>full</DebugType>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
|
||||
<OutputPath>bin\x64\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<Optimize>true</Optimize>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Any CPU|x64'">
|
||||
<OutputPath>bin\x64\Any CPU\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<Optimize>true</Optimize>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<OutputPath>bin\x86\Debug\</OutputPath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<OutputPath>bin\x86\Release\</OutputPath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Any CPU|x86'">
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<OutputPath>bin\x86\Any CPU\</OutputPath>
|
||||
<OutputType>Exe</OutputType>
|
||||
<DebugType>Full</DebugType>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="1.1.18" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="1.1.18" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="ByteReaderTests.cs" />
|
||||
<Compile Include="ByteWriterTests.cs" />
|
||||
<Compile Include="ExtensionTests.cs" />
|
||||
<Compile Include="MqttPacketSerializerTests.cs" />
|
||||
<Compile Include="MqttServerTests.cs" />
|
||||
<Compile Include="MqttSubscriptionsManagerTests.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="TestMqttCommunicationAdapter.cs" />
|
||||
<Compile Include="TestMqttServerAdapter.cs" />
|
||||
<Compile Include="TopicFilterComparerTests.cs" />
|
||||
<ProjectReference Include="..\..\Frameworks\MQTTnet.Netstandard\MQTTnet.NetStandard.csproj" />
|
||||
<ProjectReference Include="..\..\MQTTnet.Core\MQTTnet.Core.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\MQTTnet.Core\MQTTnet.Core.csproj">
|
||||
<Project>{2ecb99e4-72d0-4c23-99ba-93d511d3967d}</Project>
|
||||
<Name>MQTTnet.Core</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
|
||||
</Project>
|
||||
@@ -6,6 +6,7 @@ using MQTTnet.Core.Client;
|
||||
using MQTTnet.Core.Packets;
|
||||
using MQTTnet.Core.Protocol;
|
||||
using MQTTnet.Core.Server;
|
||||
using System;
|
||||
|
||||
namespace MQTTnet.Core.Tests
|
||||
{
|
||||
@@ -48,12 +49,13 @@ namespace MQTTnet.Core.Tests
|
||||
[TestMethod]
|
||||
public async Task MqttServer_WillMessage()
|
||||
{
|
||||
var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { new TestMqttServerAdapter() });
|
||||
s.StartAsync();
|
||||
var serverAdapter = new TestMqttServerAdapter();
|
||||
var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { serverAdapter });
|
||||
await s.StartAsync();
|
||||
|
||||
var willMessage = new MqttApplicationMessage("My/last/will", new byte[0], MqttQualityOfServiceLevel.AtMostOnce, false);
|
||||
var c1 = ConnectTestClient("c1", null, s);
|
||||
var c2 = ConnectTestClient("c2", willMessage, s);
|
||||
var c1 = await serverAdapter.ConnectTestClient(s, "c1");
|
||||
var c2 = await serverAdapter.ConnectTestClient(s, "c2", willMessage);
|
||||
|
||||
var receivedMessagesCount = 0;
|
||||
c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
|
||||
@@ -63,7 +65,7 @@ namespace MQTTnet.Core.Tests
|
||||
|
||||
await Task.Delay(1000);
|
||||
|
||||
s.StopAsync();
|
||||
await s.StopAsync();
|
||||
|
||||
Assert.AreEqual(1, receivedMessagesCount);
|
||||
}
|
||||
@@ -71,11 +73,12 @@ namespace MQTTnet.Core.Tests
|
||||
[TestMethod]
|
||||
public async Task MqttServer_Unsubscribe()
|
||||
{
|
||||
var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { new TestMqttServerAdapter() });
|
||||
s.StartAsync();
|
||||
var serverAdapter = new TestMqttServerAdapter();
|
||||
var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { serverAdapter });
|
||||
await s.StartAsync();
|
||||
|
||||
var c1 = ConnectTestClient("c1", null, s);
|
||||
var c2 = ConnectTestClient("c2", null, s);
|
||||
var c1 = await serverAdapter.ConnectTestClient(s, "c1");
|
||||
var c2 = await serverAdapter.ConnectTestClient(s, "c2");
|
||||
|
||||
var receivedMessagesCount = 0;
|
||||
c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
|
||||
@@ -97,7 +100,7 @@ namespace MQTTnet.Core.Tests
|
||||
await Task.Delay(500);
|
||||
Assert.AreEqual(1, receivedMessagesCount);
|
||||
|
||||
s.StopAsync();
|
||||
await s.StopAsync();
|
||||
await Task.Delay(500);
|
||||
|
||||
Assert.AreEqual(1, receivedMessagesCount);
|
||||
@@ -106,10 +109,11 @@ namespace MQTTnet.Core.Tests
|
||||
[TestMethod]
|
||||
public async Task MqttServer_Publish()
|
||||
{
|
||||
var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { new TestMqttServerAdapter() });
|
||||
s.StartAsync();
|
||||
var serverAdapter = new TestMqttServerAdapter();
|
||||
var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { serverAdapter });
|
||||
await s.StartAsync();
|
||||
|
||||
var c1 = ConnectTestClient("c1", null, s);
|
||||
var c1 = await serverAdapter.ConnectTestClient(s, "c1");
|
||||
|
||||
var receivedMessagesCount = 0;
|
||||
c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
|
||||
@@ -120,23 +124,10 @@ namespace MQTTnet.Core.Tests
|
||||
s.Publish(message);
|
||||
await Task.Delay(500);
|
||||
|
||||
s.StopAsync();
|
||||
await s.StopAsync();
|
||||
|
||||
Assert.AreEqual(1, receivedMessagesCount);
|
||||
}
|
||||
|
||||
private static MqttClient ConnectTestClient(string clientId, MqttApplicationMessage willMessage, MqttServer server)
|
||||
{
|
||||
var adapterA = new TestMqttCommunicationAdapter();
|
||||
var adapterB = new TestMqttCommunicationAdapter();
|
||||
adapterA.Partner = adapterB;
|
||||
adapterB.Partner = adapterA;
|
||||
|
||||
var client = new MqttClient(new MqttClientOptions(), adapterA);
|
||||
server.InjectClient(clientId, adapterB);
|
||||
client.ConnectAsync(willMessage).Wait();
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task TestPublishAsync(
|
||||
string topic,
|
||||
@@ -145,11 +136,12 @@ namespace MQTTnet.Core.Tests
|
||||
MqttQualityOfServiceLevel filterQualityOfServiceLevel,
|
||||
int expectedReceivedMessagesCount)
|
||||
{
|
||||
var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { new TestMqttServerAdapter() });
|
||||
s.StartAsync();
|
||||
var serverAdapter = new TestMqttServerAdapter();
|
||||
var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { serverAdapter });
|
||||
await s.StartAsync();
|
||||
|
||||
var c1 = ConnectTestClient("c1", null, s);
|
||||
var c2 = ConnectTestClient("c2", null, s);
|
||||
var c1 = await serverAdapter.ConnectTestClient(s, "c1");
|
||||
var c2 = await serverAdapter.ConnectTestClient(s, "c2");
|
||||
|
||||
var receivedMessagesCount = 0;
|
||||
c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
|
||||
@@ -162,7 +154,7 @@ namespace MQTTnet.Core.Tests
|
||||
|
||||
await Task.Delay(500);
|
||||
|
||||
s.StopAsync();
|
||||
await s.StopAsync();
|
||||
|
||||
Assert.AreEqual(expectedReceivedMessagesCount, receivedMessagesCount);
|
||||
}
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
[assembly: AssemblyTitle("MQTTnet.Core.Tests")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("Christian Kratky")]
|
||||
[assembly: AssemblyProduct("MQTTnet")]
|
||||
[assembly: AssemblyCopyright("Copyright © Christian Kratky 2016-2017")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
[assembly: ComVisible(false)]
|
||||
[assembly: Guid("a7ff0c91-25de-4ba6-b39e-f54e8dadf1cc")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
@@ -2,16 +2,52 @@
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Core.Adapter;
|
||||
using MQTTnet.Core.Server;
|
||||
using MQTTnet.Core.Client;
|
||||
|
||||
namespace MQTTnet.Core.Tests
|
||||
{
|
||||
public class TestMqttServerAdapter : IMqttServerAdapter
|
||||
{
|
||||
public event EventHandler<MqttClientConnectedEventArgs> ClientConnected;
|
||||
public event Action<IMqttCommunicationAdapter> ClientAccepted;
|
||||
|
||||
public void FireClientConnectedEvent(MqttClientConnectedEventArgs eventArgs)
|
||||
public async Task<MqttClient> ConnectTestClient(IMqttServer server, string clientId, MqttApplicationMessage willMessage = null)
|
||||
{
|
||||
ClientConnected?.Invoke(this, eventArgs);
|
||||
var adapterA = new TestMqttCommunicationAdapter();
|
||||
var adapterB = new TestMqttCommunicationAdapter();
|
||||
adapterA.Partner = adapterB;
|
||||
adapterB.Partner = adapterA;
|
||||
|
||||
var client = new MqttClient(new MqttClientOptions() { ClientId = clientId }, adapterA);
|
||||
var connected = WaitForClientToConnect(server, clientId);
|
||||
FireClientAcceptedEvent(adapterB);
|
||||
await client.ConnectAsync(willMessage);
|
||||
await connected;
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
private static Task WaitForClientToConnect(IMqttServer s, string clientId)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<object>();
|
||||
|
||||
EventHandler<MqttClientConnectedEventArgs> handler = null;
|
||||
handler = (sender, args) =>
|
||||
{
|
||||
if (args.Client.ClientId == clientId)
|
||||
{
|
||||
s.ClientConnected -= handler;
|
||||
tcs.SetResult(null);
|
||||
}
|
||||
};
|
||||
|
||||
s.ClientConnected += handler;
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
private void FireClientAcceptedEvent(IMqttCommunicationAdapter adapter)
|
||||
{
|
||||
ClientAccepted?.Invoke(adapter);
|
||||
}
|
||||
|
||||
public Task StartAsync(MqttServerOptions options)
|
||||
|
||||
Reference in New Issue
Block a user