Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addresses supporting byte[] in direct methods and making TwinProperties able to be directly converted to json #3340

Merged
merged 38 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
28ac489
changed object to byte[]
patilsnr Jun 15, 2023
6d33014
methods for byte[]
patilsnr Jun 15, 2023
97886e7
doc comments
patilsnr Jun 15, 2023
5020031
doc comments p2
patilsnr Jun 15, 2023
175d2a0
twinProperties to json
patilsnr Jun 21, 2023
719d0ac
response payload bytes
patilsnr Jun 28, 2023
3bb20c8
fix build errors
patilsnr Jun 28, 2023
acb7163
unit tests
patilsnr Jun 28, 2023
a813e61
net analyzers
patilsnr Jun 28, 2023
32dbcde
setter for payload
patilsnr Jun 30, 2023
b38923d
version number is now serialized correctly
patilsnr Jul 5, 2023
1c1f4fd
misc fixes
patilsnr Jul 6, 2023
adadd2e
green payload convention tests
patilsnr Jul 13, 2023
8e74d47
Delete haproxy.pem
patilsnr Jul 13, 2023
f735b44
direct method payload tests green
patilsnr Jul 13, 2023
48e57a5
digital twins tests green
patilsnr Jul 13, 2023
017dea0
method fault injection tests green
patilsnr Jul 13, 2023
c4b2e02
method amqp pooling tests green
patilsnr Jul 13, 2023
bf099e2
more tests green
patilsnr Jul 13, 2023
2203156
method tests green
patilsnr Jul 13, 2023
fb8bf21
more method tests
patilsnr Jul 13, 2023
9a9e1a2
etc
patilsnr Jul 13, 2023
b98a0fc
object overload
patilsnr Jul 18, 2023
54a1a7d
misc
patilsnr Jul 19, 2023
3ed2ff3
payload as object e2e test
patilsnr Jul 19, 2023
9ff00f5
doc comments
patilsnr Jul 19, 2023
d4827fb
green object overload test
patilsnr Jul 19, 2023
9451955
misc
patilsnr Jul 19, 2023
59f4118
misc2
patilsnr Jul 19, 2023
0b21380
default payload in ctor
patilsnr Jul 19, 2023
d07b825
fixes
patilsnr Jul 20, 2023
969c9b0
more
patilsnr Jul 20, 2023
6b2cc8f
update unit tests
patilsnr Jul 21, 2023
d2d0081
remove serialization layer on outdated test
patilsnr Jul 21, 2023
5f53c64
misc
patilsnr Jul 21, 2023
bcaa2bf
comments
patilsnr Jul 21, 2023
0a4d88d
fix unit test
patilsnr Jul 21, 2023
78b03fc
transport protocol back to websocket in e2e test
patilsnr Jul 21, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions e2e/LongHaul/device/IotHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Azure.Storage.Blobs.Models;
using Azure.Storage.Blobs.Specialized;
using Mash.Logging;
using Newtonsoft.Json;
using Microsoft.Azure.Devices.Client;
using static Microsoft.Azure.Devices.LongHaul.Device.LoggingConstants;

Expand Down Expand Up @@ -472,20 +473,20 @@ private Task<DirectMethodResponse> DirectMethodCallback(DirectMethodRequest meth

// Log the current time again and send the response back to the service app.
methodPayload.SentOnUtc = DateTimeOffset.UtcNow;
return Task.FromResult(new DirectMethodResponse(200) { Payload = methodPayload });
return Task.FromResult(new DirectMethodResponse(200) { Payload = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(methodPayload)) });
}
}
catch (Exception ex)
{
_logger.Trace($"Failed to parse the payload for direct method {methodRequest.MethodName} due to {ex}.", TraceSeverity.Error);
return Task.FromResult(new DirectMethodResponse(400) { Payload = ex.Message });
return Task.FromResult(new DirectMethodResponse(400) { Payload = Encoding.UTF8.GetBytes(ex.Message) });
}
break;
}

string unsupportedMessage = $"The direct method [{methodRequest.MethodName}] is not supported.";
_logger.Trace(unsupportedMessage, TraceSeverity.Warning);
return Task.FromResult(new DirectMethodResponse(400) { Payload = unsupportedMessage });
return Task.FromResult(new DirectMethodResponse(400) { Payload = Encoding.UTF8.GetBytes(unsupportedMessage) });
}

private async Task DesiredPropertyUpdateCallbackAsync(DesiredProperties properties)
Expand Down
13 changes: 7 additions & 6 deletions e2e/LongHaul/module/IotHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Text;
using Mash.Logging;
using Newtonsoft.Json;
using Microsoft.Azure.Devices.Client;
using static Microsoft.Azure.Devices.LongHaul.Module.LoggingConstants;

Expand Down Expand Up @@ -280,7 +281,7 @@ public async Task InvokeDirectMethodOnLeafClientAsync(string deviceId, string mo
CurrentTimeUtc = DateTimeOffset.UtcNow,
};

var directMethodRequest = new EdgeModuleDirectMethodRequest(methodName, payload)
var directMethodRequest = new EdgeModuleDirectMethodRequest(methodName, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(payload)))
{
ResponseTimeout = TimeSpan.FromSeconds(30),
};
Expand Down Expand Up @@ -433,13 +434,13 @@ private Task<DirectMethodResponse> DirectMethodCallback(DirectMethodRequest meth

// Log the current time again and send the response back to the service app.
methodPayload.CurrentTimeUtc = DateTimeOffset.UtcNow;
return Task.FromResult(new DirectMethodResponse(200) { Payload = methodPayload });
return Task.FromResult(new DirectMethodResponse(200) { Payload = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(methodPayload)) });
}
}
catch (Exception ex)
{
_logger.Trace($"Failed to parse the payload for direct method {methodRequest.MethodName} due to {ex}.", TraceSeverity.Error);
return Task.FromResult(new DirectMethodResponse(400) { Payload = ex.Message });
return Task.FromResult(new DirectMethodResponse(400) { Payload = Encoding.UTF8.GetBytes(ex.Message) });
}
break;

Expand All @@ -456,20 +457,20 @@ private Task<DirectMethodResponse> DirectMethodCallback(DirectMethodRequest meth

// Log the current time again and send the response back to the service app.
methodPayload.CurrentTimeUtc = DateTimeOffset.UtcNow;
return Task.FromResult(new DirectMethodResponse(200) { Payload = methodPayload });
return Task.FromResult(new DirectMethodResponse(200) { Payload = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(methodPayload)) });
}
}
catch (Exception ex)
{
_logger.Trace($"Failed to parse the payload for direct method {methodRequest.MethodName} due to {ex}.", TraceSeverity.Error);
return Task.FromResult(new DirectMethodResponse(400) { Payload = ex.Message });
return Task.FromResult(new DirectMethodResponse(400) { Payload = Encoding.UTF8.GetBytes(ex.Message) });
}
break;
}

string unsupportedMessage = $"The direct method [{methodRequest.MethodName}] is not supported.";
_logger.Trace(unsupportedMessage, TraceSeverity.Warning);
return Task.FromResult(new DirectMethodResponse(400) { Payload = unsupportedMessage });
return Task.FromResult(new DirectMethodResponse(400) { Payload = Encoding.UTF8.GetBytes(unsupportedMessage) });
}

private async Task DesiredPropertyUpdateCallbackAsync(DesiredProperties properties)
Expand Down
3 changes: 2 additions & 1 deletion e2e/LongHaul/service/DeviceOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Mash.Logging;
Expand Down Expand Up @@ -49,7 +50,7 @@ public async Task InvokeDirectMethodAsync(Logger logger, CancellationToken ct)

var methodInvocation = new DirectMethodServiceRequest("EchoPayload")
{
Payload = payload,
Payload = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(payload)),
ResponseTimeout = TimeSpan.FromSeconds(30),
};

Expand Down
3 changes: 2 additions & 1 deletion e2e/LongHaul/service/ModuleOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Text;
using Mash.Logging;
using Newtonsoft.Json;
using static Microsoft.Azure.Devices.LongHaul.Service.LoggingConstants;
Expand Down Expand Up @@ -49,7 +50,7 @@ public async Task InvokeDirectMethodAsync(Logger logger, CancellationToken ct)

var methodInvocation = new DirectMethodServiceRequest("EchoPayload")
{
Payload = payload,
Payload = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(payload)),
ResponseTimeout = TimeSpan.FromSeconds(30),
};

Expand Down
4 changes: 2 additions & 2 deletions e2e/Tests/helpers/TestDeviceCallbackHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void Dispose()
}

// Set a direct method callback that expects a request with payload of type T.
internal async Task SetDeviceReceiveMethodAndRespondAsync<T>(object deviceResponsePayload, CancellationToken ct = default)
internal async Task SetDeviceReceiveMethodAndRespondAsync<T>(byte[] deviceResponsePayload, CancellationToken ct = default)
{
await _deviceClient.SetDirectMethodCallbackAsync(
(request) =>
Expand All @@ -78,7 +78,7 @@ await _deviceClient.SetDirectMethodCallbackAsync(
{
request.MethodName.Should().Be(ExpectedDirectMethodRequest.MethodName, "The expected method name should match what was sent from service");

var expectedRequestPayload = (T)ExpectedDirectMethodRequest.Payload;
byte[] expectedRequestPayload = ExpectedDirectMethodRequest.Payload;
request.TryGetPayload(out T actualRequestPayload).Should().BeTrue();
actualRequestPayload.Should().BeEquivalentTo(expectedRequestPayload, "The expected method data should match what was sent from service");

Expand Down
4 changes: 2 additions & 2 deletions e2e/Tests/helpers/TestModuleCallbackHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void Dispose()
_methodCallbackSemaphore?.Dispose();
}

internal async Task SetModuleReceiveMethodAndRespondAsync<T>(object moduleResponsePayload, CancellationToken ct)
internal async Task SetModuleReceiveMethodAndRespondAsync<T>(byte[] moduleResponsePayload, CancellationToken ct)
{
await _moduleClient.SetDirectMethodCallbackAsync(
(request) =>
Expand All @@ -54,7 +54,7 @@ await _moduleClient.SetDirectMethodCallbackAsync(
{
request.MethodName.Should().Be(ExpectedDirectMethodRequest.MethodName, "The expected method name should match what was sent from service");

var expectedRequestPayload = (T)ExpectedDirectMethodRequest.Payload;
byte[] expectedRequestPayload = ExpectedDirectMethodRequest.Payload;
request.TryGetPayload(out T actualRequestPayload).Should().BeTrue();
actualRequestPayload.Should().BeEquivalentTo(expectedRequestPayload, "The expected method data should match what was sent from service");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Text;
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.E2ETests.Helpers;
using Microsoft.Azure.Devices.E2ETests.Helpers.Templates;
using Microsoft.Azure.Devices.E2ETests.Methods;
using Microsoft.Azure.Devices.E2ETests.Twins;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;

namespace Microsoft.Azure.Devices.E2ETests
{
Expand Down Expand Up @@ -64,7 +66,8 @@ async Task InitOperationAsync(TestDevice testDevice, TestDeviceCallbackHandler t

// Set method handler
VerboseTestLogger.WriteLine($"{nameof(CombinedClientOperationsPoolAmqpTests)}: Set direct method {MethodName} for device={testDevice.Id}");
Task methodCallbackSet = testDeviceCallbackHandler.SetDeviceReceiveMethodAndRespondAsync<DirectMethodRequestPayload>(s_deviceResponsePayload, ct);
Task methodCallbackSet = testDeviceCallbackHandler.SetDeviceReceiveMethodAndRespondAsync<DirectMethodRequestPayload>(
Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(s_deviceResponsePayload)), ct);
initOperations.Add(methodCallbackSet);

// Set the twin desired properties callback
Expand Down Expand Up @@ -103,7 +106,7 @@ async Task TestOperationAsync(TestDevice testDevice, TestDeviceCallbackHandler t
VerboseTestLogger.WriteLine($"{nameof(CombinedClientOperationsPoolAmqpTests)}: Operation 3: Direct methods test for device={testDevice.Id}");
var directMethodRequest = new DirectMethodServiceRequest(MethodName)
{
Payload = s_serviceRequestPayload,
Payload = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(s_serviceRequestPayload)),
ResponseTimeout = s_defaultMethodResponseTimeout,
};
testDeviceCallbackHandler.ExpectedDirectMethodRequest = directMethodRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Text;
using FluentAssertions;
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.E2ETests.Helpers;
Expand Down Expand Up @@ -146,12 +147,13 @@ private async Task SendMethodAndRespondAsync<T>(
await testDevice.OpenWithRetryAsync(ct).ConfigureAwait(false);

using var testDeviceCallbackHandler = new TestDeviceCallbackHandler(deviceClient, testDevice.Id);
await testDeviceCallbackHandler.SetDeviceReceiveMethodAndRespondAsync<T>(directMethodResponseFromClient, ct);
await testDeviceCallbackHandler.SetDeviceReceiveMethodAndRespondAsync<T>(
Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(directMethodResponseFromClient)), ct);

var directMethodRequest = new DirectMethodServiceRequest(MethodName)
{
ResponseTimeout = s_defaultMethodResponseTimeout,
Payload = directMethodRequestFromService,
Payload = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(directMethodRequestFromService)),
};
testDeviceCallbackHandler.ExpectedDirectMethodRequest = directMethodRequest;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Text;
using FluentAssertions;
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.E2ETests.Helpers;
Expand Down Expand Up @@ -103,7 +104,8 @@ private async Task SendMethodAndRespondRecoveryPoolOverAmqpAsync(
async Task InitOperationAsync(TestDevice _, TestDeviceCallbackHandler testDeviceCallbackHandler, CancellationToken ct)
{
await testDeviceCallbackHandler
.SetDeviceReceiveMethodAndRespondAsync<DirectMethodRequestPayload>(s_deviceResponsePayload, ct)
.SetDeviceReceiveMethodAndRespondAsync<DirectMethodRequestPayload>(
Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(s_deviceResponsePayload)), ct)
.ConfigureAwait(false);
}

Expand All @@ -114,7 +116,7 @@ async Task TestOperationAsync(TestDevice testDevice, TestDeviceCallbackHandler t

var directMethodRequest = new DirectMethodServiceRequest(MethodName)
{
Payload = s_serviceRequestPayload,
Payload = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(s_serviceRequestPayload)),
ResponseTimeout = s_defaultMethodResponseTimeout,
};
testDeviceCallbackHandler.ExpectedDirectMethodRequest = directMethodRequest;
Expand Down
5 changes: 3 additions & 2 deletions e2e/Tests/iothub/device/MethodE2EPoolAmqpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Text;
using FluentAssertions;
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.E2ETests.Helpers;
Expand Down Expand Up @@ -57,15 +58,15 @@ private async Task SendMethodAndRespondPoolOverAmqp(
async Task InitOperationAsync(TestDevice testDevice, TestDeviceCallbackHandler testDeviceCallbackHandler, CancellationToken ct)
{
VerboseTestLogger.WriteLine($"{nameof(MethodE2EPoolAmqpTests)}: Setting method for device {testDevice.Id}");
await testDeviceCallbackHandler.SetDeviceReceiveMethodAndRespondAsync<DirectMethodRequestPayload>(s_deviceResponsePayload, ct).ConfigureAwait(false);
await testDeviceCallbackHandler.SetDeviceReceiveMethodAndRespondAsync<DirectMethodRequestPayload>(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(s_deviceResponsePayload)), ct).ConfigureAwait(false);
}

async Task TestOperationAsync(TestDevice testDevice, TestDeviceCallbackHandler testDeviceCallbackHandler, CancellationToken ct)
{
VerboseTestLogger.WriteLine($"{nameof(MethodE2EPoolAmqpTests)}: Preparing to receive method for device {testDevice.Id}");
var directMethodRequest = new DirectMethodServiceRequest(MethodName)
{
Payload = s_serviceRequestPayload,
Payload = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(s_serviceRequestPayload)),
ResponseTimeout = s_defaultMethodResponseTimeout,
};
testDeviceCallbackHandler.ExpectedDirectMethodRequest = directMethodRequest;
Expand Down
Loading