NUC-4: Fix unhandled InvalidJson exception in Firestore publisher.

This commit is contained in:
Denis-Cosmin Nutiu 2020-10-25 15:24:34 +02:00
parent 6cae46c0cd
commit fb689b2440
5 changed files with 42 additions and 3 deletions

View file

@ -1,12 +1,14 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net; using System.Net;
using System.Net.Http;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NucuCar.Domain.Http; using NucuCar.Domain.Http;
using NucuCar.Domain.Utilities; using NucuCar.Domain.Utilities;
using NucuCar.Telemetry.Abstractions; using NucuCar.Telemetry.Abstractions;
using HttpClient = NucuCar.Domain.Http.HttpClient;
namespace NucuCar.Telemetry.Publishers namespace NucuCar.Telemetry.Publishers
{ {
@ -103,7 +105,18 @@ namespace NucuCar.Telemetry.Publishers
} }
var data = FirebaseRestTranslator.Translator.Translate(null, GetTelemetry()); var data = FirebaseRestTranslator.Translator.Translate(null, GetTelemetry());
var responseMessage = await HttpClient.PostAsync("", data);
HttpResponseMessage responseMessage = null;
try
{
responseMessage = await HttpClient.PostAsync("", data);
}
// ArgumentException occurs during json serialization errors.
catch (ArgumentException e)
{
Logger?.LogWarning(e.Message);
}
switch (responseMessage?.StatusCode) switch (responseMessage?.StatusCode)
{ {

View file

@ -80,7 +80,6 @@ namespace NucuCar.Domain.Http
#endregion #endregion
#region Public Methods #region Public Methods
public void Authorization(string scheme, string token) public void Authorization(string scheme, string token)

View file

@ -93,6 +93,27 @@ namespace NucuCar.UnitTests.NucuCar.Domain.Telemetry.Tests
Assert.Equal("{\"fields\":{\"testData\":{\"integerValue\":1}}}", Assert.Equal("{\"fields\":{\"testData\":{\"integerValue\":1}}}",
request.Content.ReadAsStringAsync().GetAwaiter().GetResult()); request.Content.ReadAsStringAsync().GetAwaiter().GetResult());
} }
[Fact]
private async Task Test_PublishAsync_InvalidJson()
{
// Setup
var opts = new TelemetryPublisherOptions()
{
ConnectionString = "ProjectId=test;CollectionName=test"
};
var publisher = new MockTelemetryPublisherFirestore(opts);
var mockHttpClient = new MockHttpClient("http://testing.com");
mockHttpClient.SendAsyncResponses.Add(new HttpResponseMessage(HttpStatusCode.OK));
publisher.SetHttpClient(mockHttpClient);
publisher.SetMockData(new Dictionary<string, object> {["testData"] = double.PositiveInfinity});
// Run
await publisher.PublishAsync(CancellationToken.None);
// Assert no request made.
Assert.Empty(mockHttpClient.SendAsyncArgCalls);
}
[Fact] [Fact]
private async Task Test_PublishAsync_Cancel() private async Task Test_PublishAsync_Cancel()

View file

@ -7,6 +7,11 @@
&lt;TestAncestor&gt;&#xD; &lt;TestAncestor&gt;&#xD;
&lt;TestId&gt;xUnit::C6F07921-1052-4945-911E-F328A622F229::.NETCoreApp,Version=v3.1::NucuCar.UnitTests.NucuCar.Telemetry.Tests.TelemetryPublisherFirestoreTest.Test_PublishAsync_Cancel&lt;/TestId&gt;&#xD; &lt;TestId&gt;xUnit::C6F07921-1052-4945-911E-F328A622F229::.NETCoreApp,Version=v3.1::NucuCar.UnitTests.NucuCar.Telemetry.Tests.TelemetryPublisherFirestoreTest.Test_PublishAsync_Cancel&lt;/TestId&gt;&#xD;
&lt;/TestAncestor&gt;&#xD; &lt;/TestAncestor&gt;&#xD;
&lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=4ab13c7d_002D1d5f_002D41c4_002D8b17_002D1e5c39c322e7/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="TelemetryPublisherFirestoreTest" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;TestAncestor&gt;&#xD;
&lt;TestId&gt;xUnit::C6F07921-1052-4945-911E-F328A622F229::.NETCoreApp,Version=v3.1::NucuCar.UnitTests.NucuCar.Domain.Telemetry.Tests.TelemetryPublisherFirestoreTest&lt;/TestId&gt;&#xD;
&lt;/TestAncestor&gt;&#xD;
&lt;/SessionState&gt;</s:String> &lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=59acec6f_002Dda48_002D4e34_002Dbf2a_002D5b52d718278a/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="Test_PublishAsync_Authorization_OK" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD; <s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=59acec6f_002Dda48_002D4e34_002Dbf2a_002D5b52d718278a/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="Test_PublishAsync_Authorization_OK" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;TestAncestor&gt;&#xD; &lt;TestAncestor&gt;&#xD;
@ -24,5 +29,6 @@
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=e8af539e_002D0b50_002D476d_002Daa84_002D80fbff5a748b/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="Test_PublishAsync_OK" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD; <s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=e8af539e_002D0b50_002D476d_002Daa84_002D80fbff5a748b/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="Test_PublishAsync_OK" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;TestAncestor&gt;&#xD; &lt;TestAncestor&gt;&#xD;
&lt;TestId&gt;xUnit::C6F07921-1052-4945-911E-F328A622F229::.NETCoreApp,Version=v3.1::NucuCar.UnitTests.NucuCar.Domain.Tests.GuardTest&lt;/TestId&gt;&#xD; &lt;TestId&gt;xUnit::C6F07921-1052-4945-911E-F328A622F229::.NETCoreApp,Version=v3.1::NucuCar.UnitTests.NucuCar.Domain.Tests.GuardTest&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::C6F07921-1052-4945-911E-F328A622F229::.NETCoreApp,Version=v3.1::NucuCar.UnitTests.NucuCar.Domain.Telemetry.Tests.TelemetryPublisherFirestoreTest&lt;/TestId&gt;&#xD;
&lt;/TestAncestor&gt;&#xD; &lt;/TestAncestor&gt;&#xD;
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary> &lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>

View file

@ -40,7 +40,7 @@ Command line utility to play around with the car functionality. You can use it t
To build the project and target the Raspberry Pi you can use the following command: To build the project and target the Raspberry Pi you can use the following command:
```$xslt ```$xslt
dotnet build --runtime linux-arm -p:PublishSingleFile=true dotnet build --runtime linux-arm -p:PublishSingleFile=true --configuration Release
``` ```
--- ---