Skip to content

Commit df0634b

Browse files
Fix bug where module client send batch fails if output name already set (#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.
1 parent 9e445ce commit df0634b

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

iothub/device/src/InternalClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1570,7 +1570,7 @@ public Task SendEventBatchAsync(string outputName, IEnumerable<Message> messages
15701570
throw new ArgumentNullException(nameof(messages));
15711571
}
15721572

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

15751575
return InnerHandler.SendEventAsync(messagesList, cancellationToken);
15761576
}

iothub/device/tests/ModuleClientTests.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
using System;
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Collections.Generic;
26
using System.Threading;
37
using System.Threading.Tasks;
8+
using FluentAssertions;
49
using Microsoft.VisualStudio.TestTools.UnitTesting;
510
using NSubstitute;
611

@@ -352,5 +357,33 @@ public void ModuleClient_InvokeMethodAsyncWithoutBodyShouldNotThrow()
352357
// act
353358
_ = new MethodInvokeRequest(request.Name, request.DataAsJson, request.ResponseTimeout, request.ConnectionTimeout);
354359
}
360+
361+
[TestMethod]
362+
public async Task SendEventBatchToOutputSetOutputNameIfNotSet()
363+
{
364+
// arrange
365+
var moduleClient = ModuleClient.CreateFromConnectionString(FakeConnectionString, TransportType.Mqtt_Tcp_Only);
366+
367+
var innerHandler = Substitute.For<IDelegatingHandler>();
368+
innerHandler.SendEventAsync(Arg.Any<Message>(), Arg.Any<CancellationToken>()).Returns(Task.FromResult(0));
369+
moduleClient.InnerHandler = innerHandler;
370+
371+
// act
372+
var messageWithoutOutputName = new Message();
373+
var messageWithOutputName = new Message();
374+
375+
messageWithOutputName.SystemProperties.Remove(MessageSystemPropertyNames.OutputName);
376+
string initialOutputName = "someInitialOutputName";
377+
messageWithOutputName.SystemProperties.Add(MessageSystemPropertyNames.OutputName, initialOutputName);
378+
379+
string newOutputName = "someNewOutputName";
380+
await moduleClient.SendEventBatchAsync(newOutputName, new List<Message> { messageWithoutOutputName, messageWithOutputName }).ConfigureAwait(false);
381+
382+
// assert
383+
messageWithoutOutputName.SystemProperties.Keys.Should().Contain(MessageSystemPropertyNames.OutputName);
384+
messageWithoutOutputName.SystemProperties[MessageSystemPropertyNames.OutputName].Should().Equals(newOutputName);
385+
messageWithOutputName.SystemProperties.Keys.Should().Contain(MessageSystemPropertyNames.OutputName);
386+
messageWithOutputName.SystemProperties[MessageSystemPropertyNames.OutputName].Should().Equals(newOutputName);
387+
}
355388
}
356389
}

0 commit comments

Comments
 (0)