Skip to content

Commit

Permalink
Add support to LeafDevice for testing devices behind an HTTP proxy (#465
Browse files Browse the repository at this point in the history
)

Similar to 1217b83 which was for Quickstart.

This change adds a new CLI parameter `--use-web-sockets` that sets the
transport used by IoT SDK's `DeviceClient` and `ServiceClient` and
EventHub SDK's `EventHubClient` to use websockets.
  • Loading branch information
arsing committed Oct 19, 2018
1 parent de36515 commit 96bc2de
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
5 changes: 3 additions & 2 deletions smoke/LeafDevice/LeafDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ public class LeafDevice : Details.Details
string eventhubCompatibleEndpointWithEntityPath,
string deviceId,
string certificateFileName,
string edgeHostName) :
base(iothubConnectionString, eventhubCompatibleEndpointWithEntityPath, deviceId, certificateFileName, edgeHostName)
string edgeHostName,
bool useWebSockets) :
base(iothubConnectionString, eventhubCompatibleEndpointWithEntityPath, deviceId, certificateFileName, edgeHostName, useWebSockets)
{
}

Expand Down
2 changes: 1 addition & 1 deletion smoke/LeafDevice/LeafDevice.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.2.0-*" />
<PackageReference Include="Microsoft.Azure.Devices" Version="1.17.1" />
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.18.1" />
<PackageReference Include="Microsoft.Azure.EventHubs" Version="1.1.0" />
<PackageReference Include="Microsoft.Azure.EventHubs" Version="2.2.0" />
<PackageReference Include="Microsoft.CodeCoverage" Version="1.0.3" />
<PackageReference Include="RunProcessAsTask" Version="1.2.3" />
<PackageReference Include="YamlDotNet" Version="5.2.1" />
Expand Down
8 changes: 6 additions & 2 deletions smoke/LeafDevice/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace LeafDevice
Option Environment variable
--connection-string iothubConnectionString
--eventhub-endpoint eventhubCompatibleEndpointWithEntityPath
Defaults:
All options to this command have defaults. If an option is not specified and
its corresponding environment variable is not defined, then the default will
Expand Down Expand Up @@ -54,6 +54,9 @@ class Program
[Option("-ed|--edge-hostname", Description = "Leaf device identifier to be registered with IoT Hub")]
public string EdgeHostName { get; } = "";

[Option("--use-web-sockets", CommandOptionType.NoValue, Description = "Use websockets for IoT Hub connections.")]
public bool UseWebSockets { get; } = false;

// ReSharper disable once UnusedMember.Local
async Task<int> OnExecuteAsync()
{
Expand All @@ -70,7 +73,8 @@ async Task<int> OnExecuteAsync()
endpoint,
this.DeviceId,
this.CertificateFileName,
this.EdgeHostName);
this.EdgeHostName,
this.UseWebSockets);
await test.RunAsync();
}
catch (Exception ex)
Expand Down
31 changes: 26 additions & 5 deletions smoke/LeafDevice/details/Details.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ namespace LeafDevice.Details
using Microsoft.Azure.EventHubs;
using System.Net;
using Microsoft.Azure.Devices.Edge.Util;
using DeviceClientTransportType = Microsoft.Azure.Devices.Client.TransportType;
using EventHubClientTransportType = Microsoft.Azure.EventHubs.TransportType;
using ServiceClientTransportType = Microsoft.Azure.Devices.TransportType;

public class Details
{
Expand All @@ -22,6 +25,9 @@ public class Details
readonly string deviceId;
readonly string certificateFileName;
readonly string edgeHostName;
readonly ServiceClientTransportType serviceClientTransportType;
readonly EventHubClientTransportType eventHubClientTransportType;
readonly DeviceClientTransportType deviceClientTransportType;

DeviceContext context;

Expand All @@ -30,14 +36,28 @@ public class Details
string eventhubCompatibleEndpointWithEntityPath,
string deviceId,
string certificateFileName,
string edgeHostName
string edgeHostName,
bool useWebSockets
)
{
this.iothubConnectionString = iothubConnectionString;
this.eventhubCompatibleEndpointWithEntityPath = eventhubCompatibleEndpointWithEntityPath;
this.deviceId = deviceId;
this.certificateFileName = certificateFileName;
this.edgeHostName = edgeHostName;

if (useWebSockets)
{
this.serviceClientTransportType = ServiceClientTransportType.Amqp_WebSocket_Only;
this.eventHubClientTransportType = EventHubClientTransportType.AmqpWebSockets;
this.deviceClientTransportType = DeviceClientTransportType.Mqtt_WebSocket_Only;
}
else
{
this.serviceClientTransportType = ServiceClientTransportType.Amqp;
this.eventHubClientTransportType = EventHubClientTransportType.Amqp;
this.deviceClientTransportType = DeviceClientTransportType.Mqtt;
}
}

protected Task InstallCaCertificate()
Expand All @@ -54,7 +74,7 @@ protected async Task ConnectToEdgeAndSendData()
Microsoft.Azure.Devices.IotHubConnectionStringBuilder builder = Microsoft.Azure.Devices.IotHubConnectionStringBuilder.Create(this.iothubConnectionString);
string leafDeviceConnectionString = $"HostName={builder.HostName};DeviceId={this.deviceId};SharedAccessKey={this.context.Device.Authentication.SymmetricKey.PrimaryKey};GatewayHostName={this.edgeHostName}";

this.context.DeviceClientInstance = Option.Some(DeviceClient.CreateFromConnectionString(leafDeviceConnectionString, Microsoft.Azure.Devices.Client.TransportType.Mqtt));
this.context.DeviceClientInstance = Option.Some(DeviceClient.CreateFromConnectionString(leafDeviceConnectionString, this.deviceClientTransportType));
Console.WriteLine("Leaf Device client created.");

var message = new Microsoft.Azure.Devices.Client.Message(Encoding.ASCII.GetBytes($"Message from Leaf Device. MsgGUID: {this.context.MessageGuid}"));
Expand Down Expand Up @@ -122,6 +142,7 @@ async Task CreateDeviceIdentity(RegistryManager rm)
protected async Task VerifyDataOnIoTHub()
{
var builder = new EventHubsConnectionStringBuilder(this.eventhubCompatibleEndpointWithEntityPath);
builder.TransportType = this.eventHubClientTransportType;

Console.WriteLine($"Receiving events from device '{this.context.Device.Id}' on Event Hub '{builder.EntityPath}'");

Expand All @@ -133,7 +154,7 @@ protected async Task VerifyDataOnIoTHub()
EventHubPartitionKeyResolver.ResolveToPartition(
this.context.Device.Id,
(await eventHubClient.GetRuntimeInformationAsync()).PartitionCount),
DateTime.Now.AddMinutes(-5));
EventPosition.FromEnqueuedTime(DateTime.Now.AddMinutes(-5)));

var result = new TaskCompletionSource<bool>();
using (var cts = new CancellationTokenSource(TimeSpan.FromMinutes(3)))
Expand All @@ -144,7 +165,7 @@ protected async Task VerifyDataOnIoTHub()
new PartitionReceiveHandler(
eventData =>
{
eventData.Properties.TryGetValue("iothub-connection-device-id", out object devId);
eventData.SystemProperties.TryGetValue("iothub-connection-device-id", out object devId);
if (devId != null && devId.ToString().Equals(this.context.Device.Id)
&& Encoding.UTF8.GetString(eventData.Body).Contains(this.context.MessageGuid))
Expand Down Expand Up @@ -174,7 +195,7 @@ protected async Task VerifyDirectMethod()
{
//User Service SDK to invoke Direct Method on the device.
ServiceClient serviceClient =
ServiceClient.CreateFromConnectionString(this.context.IotHubConnectionString);
ServiceClient.CreateFromConnectionString(this.context.IotHubConnectionString, this.serviceClientTransportType);

//Call a direct method
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(300)))
Expand Down
2 changes: 1 addition & 1 deletion smoke/LeafDevice/details/PartitionReceiveHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public Task ProcessEventsAsync(IEnumerable<EventData> events)
return Task.CompletedTask;
}
public Task ProcessErrorAsync(Exception error) => throw error;
public int MaxBatchSize { get; } = 10;
public int MaxBatchSize { get; set; }
}
}

0 comments on commit 96bc2de

Please sign in to comment.