Skip to content

Commit

Permalink
Fix bug where module client send batch fails if output name already s…
Browse files Browse the repository at this point in the history
…et (#3441)

#3434

The method ```Add``` on a dictionary throws if the key is already present. This change will overwrite any previously set output name without throwing.
  • Loading branch information
timtay-microsoft authored Mar 13, 2024
1 parent 9e445ce commit df0634b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion iothub/device/src/InternalClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1570,7 +1570,7 @@ public Task SendEventBatchAsync(string outputName, IEnumerable<Message> messages
throw new ArgumentNullException(nameof(messages));
}

messagesList.ForEach(m => m.SystemProperties.Add(MessageSystemPropertyNames.OutputName, outputName));
messagesList.ForEach(m => m.SystemProperties[MessageSystemPropertyNames.OutputName] = outputName);

return InnerHandler.SendEventAsync(messagesList, cancellationToken);
}
Expand Down
35 changes: 34 additions & 1 deletion iothub/device/tests/ModuleClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using System;
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NSubstitute;

Expand Down Expand Up @@ -352,5 +357,33 @@ public void ModuleClient_InvokeMethodAsyncWithoutBodyShouldNotThrow()
// act
_ = new MethodInvokeRequest(request.Name, request.DataAsJson, request.ResponseTimeout, request.ConnectionTimeout);
}

[TestMethod]
public async Task SendEventBatchToOutputSetOutputNameIfNotSet()
{
// arrange
var moduleClient = ModuleClient.CreateFromConnectionString(FakeConnectionString, TransportType.Mqtt_Tcp_Only);

var innerHandler = Substitute.For<IDelegatingHandler>();
innerHandler.SendEventAsync(Arg.Any<Message>(), Arg.Any<CancellationToken>()).Returns(Task.FromResult(0));
moduleClient.InnerHandler = innerHandler;

// act
var messageWithoutOutputName = new Message();
var messageWithOutputName = new Message();

messageWithOutputName.SystemProperties.Remove(MessageSystemPropertyNames.OutputName);
string initialOutputName = "someInitialOutputName";
messageWithOutputName.SystemProperties.Add(MessageSystemPropertyNames.OutputName, initialOutputName);

string newOutputName = "someNewOutputName";
await moduleClient.SendEventBatchAsync(newOutputName, new List<Message> { messageWithoutOutputName, messageWithOutputName }).ConfigureAwait(false);

// assert
messageWithoutOutputName.SystemProperties.Keys.Should().Contain(MessageSystemPropertyNames.OutputName);
messageWithoutOutputName.SystemProperties[MessageSystemPropertyNames.OutputName].Should().Equals(newOutputName);
messageWithOutputName.SystemProperties.Keys.Should().Contain(MessageSystemPropertyNames.OutputName);
messageWithOutputName.SystemProperties[MessageSystemPropertyNames.OutputName].Should().Equals(newOutputName);
}
}
}

0 comments on commit df0634b

Please sign in to comment.