Files
speckle-sharp-sdk/tests/Speckle.Sdk.Serialization.Tests/ExceptionTests.cs
T
Adam Hathcock c3d716f6fd fix: an exception in the underlying code results in UnobservedTaskExceptions and not ending of the send process. (#237)
* Cancel all channels when first exception happens then throw that exception

* Use single exception to end things.

* fix verifications

* fmt

* Fix tests as stacktrace was removed

* Handle one exception and cancel on receive

* moved ThrowIfFailed and made throw speckle exceptions

* Fixed tests

* fmt
2025-02-27 14:04:02 +00:00

148 lines
4.6 KiB
C#

using FluentAssertions;
using Microsoft.Extensions.Logging.Abstractions;
using Speckle.Objects.Geometry;
using Speckle.Sdk.Host;
using Speckle.Sdk.Models;
using Speckle.Sdk.Serialisation.V2.Receive;
using Speckle.Sdk.Serialisation.V2.Send;
using Speckle.Sdk.Serialization.Tests.Framework;
using Speckle.Sdk.Testing.Framework;
namespace Speckle.Sdk.Serialization.Tests;
public class ExceptionTests
{
public ExceptionTests()
{
TypeLoader.Reset();
TypeLoader.Initialize(typeof(Base).Assembly, typeof(DetachedTests).Assembly, typeof(Polyline).Assembly);
}
[Fact]
public async Task Test_Exceptions_Upload()
{
var testClass = new TestClass() { RegularProperty = "Hello" };
var objects = new Dictionary<string, string>();
await using var process2 = new SerializeProcess(
null,
new DummySendCacheManager(objects),
new ExceptionServerObjectManager(),
new BaseChildFinder(new BasePropertyGatherer()),
new BaseSerializer(new DummySendCacheManager(objects), new ObjectSerializerFactory(new BasePropertyGatherer())),
new NullLoggerFactory(),
default,
new SerializeProcessOptions(false, false, false, true)
);
//4 exceptions are fine because we use 4 threads for saving cache
var ex = await Assert.ThrowsAsync<SpeckleException>(async () => await process2.Serialize(testClass));
await Verify(ex);
}
[Fact]
public async Task Test_Exceptions_Cache()
{
var testClass = new TestClass() { RegularProperty = "Hello" };
await using var process2 = new SerializeProcess(
null,
new ExceptionSendCacheManager(),
new DummyServerObjectManager(),
new BaseChildFinder(new BasePropertyGatherer()),
new BaseSerializer(new ExceptionSendCacheManager(), new ObjectSerializerFactory(new BasePropertyGatherer())),
new NullLoggerFactory(),
default,
new SerializeProcessOptions(false, false, false, true)
);
var ex = await Assert.ThrowsAsync<SpeckleException>(async () => await process2.Serialize(testClass));
await Verify(ex);
}
[Fact]
public async Task Test_Exceptions_Receive_Server_Skip_Both()
{
var o = new ObjectLoader(
new DummySqLiteReceiveManager(new Dictionary<string, string>()),
new ExceptionServerObjectManager(),
null,
default
);
await using var process = new DeserializeProcess(
o,
null,
new BaseDeserializer(new ObjectDeserializerFactory()),
new NullLoggerFactory(),
default,
new(SkipCache: true, MaxParallelism: 1, SkipServer: true)
);
var ex = await Assert.ThrowsAsync<SpeckleException>(async () =>
{
var root = await process.Deserialize(Guid.NewGuid().ToString());
});
await Verify(ex);
}
[Theory]
[InlineData("RevitObject.json.gz", "3416d3fe01c9196115514c4a2f41617b", 7818)]
public async Task Test_Exceptions_Receive_Server(string fileName, string rootId, int oldCount)
{
var closures = await TestFileManager.GetFileAsClosures(fileName);
closures.Count.Should().Be(oldCount);
await using var process = new DeserializeProcess(
new DummySqLiteReceiveManager(closures),
new ExceptionServerObjectManager(),
null,
new BaseDeserializer(new ObjectDeserializerFactory()),
new NullLoggerFactory(),
default,
new(true, MaxParallelism: 1)
);
var ex = await Assert.ThrowsAsync<NotImplementedException>(async () =>
{
var root = await process.Deserialize(rootId);
});
await Verify(ex);
}
[Theory]
[InlineData("RevitObject.json.gz", "3416d3fe01c9196115514c4a2f41617b", 7818, false)]
[InlineData("RevitObject.json.gz", "3416d3fe01c9196115514c4a2f41617b", 7818, true)]
public async Task Test_Exceptions_Receive_Cache(string fileName, string rootId, int oldCount, bool? hasObject)
{
var closures = await TestFileManager.GetFileAsClosures(fileName);
closures.Count.Should().Be(oldCount);
await using var process = new DeserializeProcess(
new ExceptionSendCacheManager(hasObject),
new DummyReceiveServerObjectManager(closures),
null,
new BaseDeserializer(new ObjectDeserializerFactory()),
new NullLoggerFactory(),
default,
new(MaxParallelism: 1)
);
Exception ex;
if (hasObject == true)
{
ex = await Assert.ThrowsAsync<NotImplementedException>(async () =>
{
var root = await process.Deserialize(rootId);
});
}
else
{
ex = await Assert.ThrowsAsync<SpeckleException>(async () =>
{
var root = await process.Deserialize(rootId);
});
}
await Verify(ex).UseParameters(hasObject);
}
}