public async Task DeviceMethodTestAsync() { await this.EnsureServerInitializedAsync(); int protocolGatewayPort = this.ProtocolGatewayPort; string iotHubConnectionString = ConfigurationManager.AppSettings["IotHubClient.ConnectionString"]; IotHubConnectionStringBuilder hubConnectionStringBuilder = IotHubConnectionStringBuilder.Create(iotHubConnectionString); var device = await this.SetupDeviceAsync(iotHubConnectionString); await this.CleanupDeviceQueueAsync(hubConnectionStringBuilder.HostName, device); var clientScenarios = new ClientScenarios(hubConnectionStringBuilder.HostName, this.deviceId, this.deviceSas); var group = new MultithreadEventLoopGroup(); string targetHost = this.tlsCertificate.GetNameInfo(X509NameType.DnsName, false); var readHandler1 = new ReadListeningHandler(CommunicationTimeout); Bootstrap bootstrap = new Bootstrap() .Group(group) .Channel <TcpSocketChannel>() .Option(ChannelOption.TcpNodelay, true) .Handler(this.ComposeClientChannelInitializer(targetHost, readHandler1)); IChannel clientChannel = await bootstrap.ConnectAsync(this.ServerAddress, protocolGatewayPort); this.ScheduleCleanup(() => clientChannel.CloseAsync()); // Connect + Subscribe await clientScenarios.ConnectAsync(clientChannel, readHandler1).WithTimeout(TestTimeout); await clientScenarios.SubscribeAsync(clientChannel, readHandler1).WithTimeout(TestTimeout); // Invoke method await this.InvokeMethodAsync(iotHubConnectionString, device); // Receive and complete await this.ReceiveAndCompleteCommandAsync(clientChannel, readHandler1, hubConnectionStringBuilder, device); }
public async Task BasicFunctionalityTest() { await this.EnsureServerInitializedAsync(); int protocolGatewayPort = this.ProtocolGatewayPort; string iotHubConnectionString = ConfigurationManager.AppSettings["IotHubClient.ConnectionString"]; IotHubConnectionStringBuilder hubConnectionStringBuilder = IotHubConnectionStringBuilder.Create(iotHubConnectionString); bool deviceNameProvided; this.deviceId = ConfigurationManager.AppSettings["End2End.DeviceName"]; if (!string.IsNullOrEmpty(this.deviceId)) { deviceNameProvided = true; } else { deviceNameProvided = false; this.deviceId = Guid.NewGuid().ToString("N"); } RegistryManager registryManager = RegistryManager.CreateFromConnectionString(iotHubConnectionString); if (!deviceNameProvided) { await registryManager.AddDeviceAsync(new Device(this.deviceId)); this.ScheduleCleanup(async () => { await registryManager.RemoveDeviceAsync(this.deviceId); await registryManager.CloseAsync(); }); } Device device = await registryManager.GetDeviceAsync(this.deviceId); this.deviceSas = new SharedAccessSignatureBuilder { Key = device.Authentication.SymmetricKey.PrimaryKey, Target = $"{hubConnectionStringBuilder.HostName}/devices/{this.deviceId}", KeyName = null, TimeToLive = TimeSpan.FromMinutes(20) } .ToSignature(); EventHubClient eventHubClient = EventHubClient.CreateFromConnectionString(iotHubConnectionString, "messages/events"); EventHubConsumerGroup ehGroup = eventHubClient.GetDefaultConsumerGroup(); string[] partitionIds = (await eventHubClient.GetRuntimeInformationAsync()).PartitionIds; EventHubReceiver[] receivers = await Task.WhenAll(partitionIds.Select(pid => ehGroup.CreateReceiverAsync(pid.Trim(), DateTime.UtcNow))); ServiceClient serviceClient = ServiceClient.CreateFromConnectionString(iotHubConnectionString); Stopwatch sw = Stopwatch.StartNew(); await this.CleanupDeviceQueueAsync(hubConnectionStringBuilder.HostName, device); var clientScenarios = new ClientScenarios(hubConnectionStringBuilder.HostName, this.deviceId, this.deviceSas); var group = new MultithreadEventLoopGroup(); string targetHost = this.tlsCertificate.GetNameInfo(X509NameType.DnsName, false); var readHandler1 = new ReadListeningHandler(CommunicationTimeout); Bootstrap bootstrap = new Bootstrap() .Group(group) .Channel<TcpSocketChannel>() .Option(ChannelOption.TcpNodelay, true) .Handler(this.ComposeClientChannelInitializer(targetHost, readHandler1)); IChannel clientChannel = await bootstrap.ConnectAsync(this.ServerAddress, protocolGatewayPort); this.ScheduleCleanup(() => clientChannel.CloseAsync()); Task testWorkTask = Task.Run(async () => { Tuple<EventData, string>[] ehMessages = await CollectEventHubMessagesAsync(receivers, 2); Tuple<EventData, string> qos0Event = Assert.Single(ehMessages.Where(x => TelemetryQoS0Content.Equals(x.Item2, StringComparison.Ordinal))); Tuple<EventData, string> qos1Event = Assert.Single(ehMessages.Where(x => TelemetryQoS1Content.Equals(x.Item2, StringComparison.Ordinal))); string qosPropertyName = ConfigurationManager.AppSettings["QoSPropertyName"]; var qos0Notification = new Message(Encoding.UTF8.GetBytes(NotificationQoS0Content)); qos0Notification.Properties[qosPropertyName] = "0"; qos0Notification.Properties["subTopic"] = "tips"; await serviceClient.SendAsync(this.deviceId, qos0Notification); var qos1Notification = new Message(Encoding.UTF8.GetBytes(NotificationQoS1Content)); qos1Notification.Properties["subTopic"] = "firmware-update"; await serviceClient.SendAsync(this.deviceId, qos1Notification); var qos2Notification = new Message(Encoding.UTF8.GetBytes(NotificationQoS2Content)); qos2Notification.Properties[qosPropertyName] = "2"; qos2Notification.Properties["subTopic"] = "critical-alert"; await serviceClient.SendAsync(this.deviceId, qos2Notification); var qos2Notification2 = new Message(Encoding.UTF8.GetBytes(NotificationQoS2Content2)); qos2Notification2.Properties[qosPropertyName] = "2"; await serviceClient.SendAsync(this.deviceId, qos2Notification2); }); await clientScenarios.RunPart1StepsAsync(clientChannel, readHandler1).WithTimeout(TestTimeout); await testWorkTask.WithTimeout(TestTimeout); this.output.WriteLine($"part 1 completed in {sw.Elapsed}"); await this.EnsureDeviceQueueLengthAsync(hubConnectionStringBuilder.HostName, device, 1); this.output.WriteLine($"part 1 clean completed in {sw.Elapsed}"); string part2Payload = Guid.NewGuid().ToString("N"); await serviceClient.SendAsync(this.deviceId, new Message(Encoding.ASCII.GetBytes(part2Payload))); var readHandler2 = new ReadListeningHandler(CommunicationTimeout); IChannel clientChannelPart2 = await bootstrap .Handler(this.ComposeClientChannelInitializer(targetHost, readHandler2)) .ConnectAsync(this.ServerAddress, protocolGatewayPort); this.ScheduleCleanup(async () => { await clientChannelPart2.CloseAsync(); await group.ShutdownGracefullyAsync(); }); await clientScenarios.RunPart2StepsAsync(clientChannelPart2, readHandler2).WithTimeout(TestTimeout); this.output.WriteLine($"Core test completed in {sw.Elapsed}"); await this.EnsureDeviceQueueLengthAsync(hubConnectionStringBuilder.HostName, device, 0); }