NUC-4: Fix unhandled InvalidJson exception in Firestore publisher.
This commit is contained in:
parent
6cae46c0cd
commit
fb689b2440
5 changed files with 42 additions and 3 deletions
|
@ -1,12 +1,14 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NucuCar.Domain.Http;
|
||||
using NucuCar.Domain.Utilities;
|
||||
using NucuCar.Telemetry.Abstractions;
|
||||
using HttpClient = NucuCar.Domain.Http.HttpClient;
|
||||
|
||||
namespace NucuCar.Telemetry.Publishers
|
||||
{
|
||||
|
@ -103,7 +105,18 @@ namespace NucuCar.Telemetry.Publishers
|
|||
}
|
||||
|
||||
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)
|
||||
{
|
||||
|
|
|
@ -80,7 +80,6 @@ namespace NucuCar.Domain.Http
|
|||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public void Authorization(string scheme, string token)
|
||||
|
|
|
@ -93,6 +93,27 @@ namespace NucuCar.UnitTests.NucuCar.Domain.Telemetry.Tests
|
|||
Assert.Equal("{\"fields\":{\"testData\":{\"integerValue\":1}}}",
|
||||
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]
|
||||
private async Task Test_PublishAsync_Cancel()
|
||||
|
|
|
@ -7,6 +7,11 @@
|
|||
<TestAncestor>
|
||||
<TestId>xUnit::C6F07921-1052-4945-911E-F328A622F229::.NETCoreApp,Version=v3.1::NucuCar.UnitTests.NucuCar.Telemetry.Tests.TelemetryPublisherFirestoreTest.Test_PublishAsync_Cancel</TestId>
|
||||
</TestAncestor>
|
||||
</SessionState></s:String>
|
||||
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=4ab13c7d_002D1d5f_002D41c4_002D8b17_002D1e5c39c322e7/@EntryIndexedValue"><SessionState ContinuousTestingMode="0" Name="TelemetryPublisherFirestoreTest" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session">
|
||||
<TestAncestor>
|
||||
<TestId>xUnit::C6F07921-1052-4945-911E-F328A622F229::.NETCoreApp,Version=v3.1::NucuCar.UnitTests.NucuCar.Domain.Telemetry.Tests.TelemetryPublisherFirestoreTest</TestId>
|
||||
</TestAncestor>
|
||||
</SessionState></s:String>
|
||||
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=59acec6f_002Dda48_002D4e34_002Dbf2a_002D5b52d718278a/@EntryIndexedValue"><SessionState ContinuousTestingMode="0" Name="Test_PublishAsync_Authorization_OK" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session">
|
||||
<TestAncestor>
|
||||
|
@ -24,5 +29,6 @@
|
|||
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=e8af539e_002D0b50_002D476d_002Daa84_002D80fbff5a748b/@EntryIndexedValue"><SessionState ContinuousTestingMode="0" Name="Test_PublishAsync_OK" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session">
|
||||
<TestAncestor>
|
||||
<TestId>xUnit::C6F07921-1052-4945-911E-F328A622F229::.NETCoreApp,Version=v3.1::NucuCar.UnitTests.NucuCar.Domain.Tests.GuardTest</TestId>
|
||||
<TestId>xUnit::C6F07921-1052-4945-911E-F328A622F229::.NETCoreApp,Version=v3.1::NucuCar.UnitTests.NucuCar.Domain.Telemetry.Tests.TelemetryPublisherFirestoreTest</TestId>
|
||||
</TestAncestor>
|
||||
</SessionState></s:String></wpf:ResourceDictionary>
|
|
@ -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:
|
||||
|
||||
```$xslt
|
||||
dotnet build --runtime linux-arm -p:PublishSingleFile=true
|
||||
dotnet build --runtime linux-arm -p:PublishSingleFile=true --configuration Release
|
||||
```
|
||||
---
|
||||
|
||||
|
|
Loading…
Reference in a new issue