-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
672435c
commit c4db2e9
Showing
4 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using MQTTnet.AspNetCore; | ||
using System.Net.Sockets; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using MQTTnet.Server; | ||
using Microsoft.AspNetCore.Hosting; | ||
|
||
namespace MQTTnet.Tests.ASP | ||
{ | ||
[TestClass] | ||
public class MqttHostedServerStartup | ||
{ | ||
private async Task TestStartup(bool useOccupiedPort) | ||
{ | ||
using TcpListener l = new TcpListener(IPAddress.Any, 0); | ||
l.Start(); | ||
int port = ((IPEndPoint)l.LocalEndpoint).Port; | ||
|
||
if(!useOccupiedPort) | ||
l.Stop(); | ||
|
||
|
||
var builder = WebApplication.CreateBuilder(); | ||
builder.WebHost.UseUrls("http://127.0.0.1:0"); | ||
|
||
builder.Services.AddMqttTcpServerAdapter(); | ||
builder.Services.AddHostedMqttServer(cfg => | ||
{ | ||
cfg | ||
.WithDefaultEndpoint() | ||
.WithDefaultEndpointPort(port); | ||
}); | ||
|
||
|
||
var app = builder.Build(); | ||
|
||
if(!useOccupiedPort) | ||
{ | ||
await app.StartAsync(); | ||
var server = app.Services.GetRequiredService<MqttServer>(); | ||
Assert.IsTrue(server.IsStarted); | ||
await app.StopAsync(); | ||
} | ||
else | ||
{ | ||
await Assert.ThrowsExceptionAsync<SocketException>(() => | ||
app.StartAsync() | ||
); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
[DoNotParallelize] | ||
public Task TestSuccessfullyStartup() | ||
=> TestStartup(useOccupiedPort: false); | ||
|
||
[TestMethod] | ||
[DoNotParallelize] | ||
public Task TestFailedStartup() | ||
=> TestStartup(useOccupiedPort: true); | ||
|
||
} | ||
} |