Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
abhipsaMisra committed Jun 3, 2021
1 parent 2f2494d commit ed3a15a
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 116 deletions.
46 changes: 46 additions & 0 deletions e2e/test/Helpers/TestDeviceCallbackHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public class TestDeviceCallbackHandler : IDisposable
private ExceptionDispatchInfo _receiveMessageExceptionDispatch;
private Message _expectedMessageSentByService = null;

private readonly SemaphoreSlim _clientPropertyCallbackSemaphore = new SemaphoreSlim(0, 1);
private ExceptionDispatchInfo _clientPropertyExceptionDispatch;
private object _expectedClientPropertyValue = null;

public TestDeviceCallbackHandler(DeviceClient deviceClient, TestDevice testDevice, MsTestLogger logger)
{
_deviceClient = deviceClient;
Expand All @@ -48,6 +52,12 @@ public Message ExpectedMessageSentByService
set => Volatile.Write(ref _expectedMessageSentByService, value);
}

public object ExpectedClientPropertyValue
{
get => Volatile.Read(ref _expectedClientPropertyValue);
set => Volatile.Write(ref _expectedClientPropertyValue, value);
}

public async Task SetDeviceReceiveMethodAsync(string methodName, string deviceResponseJson, string expectedServiceRequestJson)
{
await _deviceClient.SetMethodHandlerAsync(methodName,
Expand Down Expand Up @@ -158,6 +168,42 @@ public async Task WaitForReceiveMessageCallbackAsync(CancellationToken ct)
_receiveMessageExceptionDispatch?.Throw();
}

public async Task SetClientPropertyUpdateCallbackHandlerAsync<T>(string expectedPropName)
{
string userContext = "myContext";

await _deviceClient.SubscribeToWritablePropertiesEventAsync(
(patch, context) =>
{
_logger.Trace($"{nameof(SetClientPropertyUpdateCallbackHandlerAsync)}: DeviceClient {_testDevice.Id} callback property: WritableProperty: {patch}, {context}");

try
{
bool isPropertyPresent = patch.TryGetValue(expectedPropName, out T propertyFromCollection);
isPropertyPresent.Should().BeTrue();
propertyFromCollection.Should().BeEquivalentTo((T)ExpectedClientPropertyValue);
context.Should().Be(userContext);
}
catch (Exception ex)
{
_clientPropertyExceptionDispatch = ExceptionDispatchInfo.Capture(ex);
}
finally
{
// Always notify that we got the callback.
_clientPropertyCallbackSemaphore.Release();
}

return Task.FromResult(true);
}, userContext).ConfigureAwait(false);
}

public async Task WaitForClientPropertyUpdateCallbcakAsync(CancellationToken ct)
{
await _clientPropertyCallbackSemaphore.WaitAsync(ct).ConfigureAwait(false);
_clientPropertyExceptionDispatch?.Throw();
}

public void Dispose()
{
_methodCallbackSemaphore?.Dispose();
Expand Down
Loading

0 comments on commit ed3a15a

Please sign in to comment.