Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-enable exception serialization tests #22305

Merged
1 commit merged into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions test/EFCore.Design.Tests/DesignExceptionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.EntityFrameworkCore.Design;
using Xunit;

Expand Down Expand Up @@ -31,5 +33,32 @@ public void OperationException_exposes_public_string_and_inner_exception_constru
Assert.Equal("Foo", ex.Message);
Assert.Same(inner, ex.InnerException);
}

[ConditionalFact]
public void Deserialized_OperationException_can_be_serialized_and_deserialized_again()
{
var transportedException = SerializeAndDeserialize(
SerializeAndDeserialize(
new OperationException(
"But somehow the vital connection is made",
new Exception("Bang!"))));

Assert.Equal("But somehow the vital connection is made", transportedException.Message);
Assert.Equal("Bang!", transportedException.InnerException.Message);
}

private TException SerializeAndDeserialize<TException>(TException exception)
where TException : Exception
{
var stream = new MemoryStream();
var formatter = new BinaryFormatter();

#pragma warning disable MSLIB0003 // Issue https://github.com/dotnet/runtime/issues/39289 tracks finding an alternative to BinaryFormatter
formatter.Serialize(stream, exception);
stream.Seek(0, SeekOrigin.Begin);

return (TException)formatter.Deserialize(stream);
#pragma warning restore MSLIB0003
}
}
}
81 changes: 81 additions & 0 deletions test/EFCore.Tests/ExceptionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
Expand Down Expand Up @@ -40,6 +42,19 @@ public void RetryLimitExceededException_exposes_public_string_and_inner_exceptio
Assert.Same(inner, ex.InnerException);
}

[ConditionalFact]
public void Deserialized_RetryLimitExceededException_can_be_serialized_and_deserialized_again()
{
var transportedException = SerializeAndDeserialize(
SerializeAndDeserialize(
new RetryLimitExceededException(
"But somehow the vital connection is made",
new Exception("Bang!"))));

Assert.Equal("But somehow the vital connection is made", transportedException.Message);
Assert.Equal("Bang!", transportedException.InnerException.Message);
}

[ConditionalFact]
public void DbUpdateException_exposes_public_empty_constructor()
{
Expand All @@ -63,6 +78,31 @@ public void DbUpdateException_exposes_public_string_and_inner_exception_construc
Assert.Same(inner, ex.InnerException);
}

[ConditionalFact]
public void Deserialized_DbUpdateException_can_be_serialized_and_deserialized_again()
{
var transportedException = SerializeAndDeserialize(
SerializeAndDeserialize(
new DbUpdateException("But somehow the vital connection is made")));

Assert.Equal("But somehow the vital connection is made", transportedException.Message);
}

[ConditionalFact]
public void Deserialized_DbUpdateException_can_be_serialized_and_deserialized_again_with_entries()
{
var transportedException = SerializeAndDeserialize(
SerializeAndDeserialize(
new DbUpdateException(
"But somehow the vital connection is made",
new Exception("Bang!"),
new IUpdateEntry[] { new FakeUpdateEntry() })));

Assert.Equal("But somehow the vital connection is made", transportedException.Message);
Assert.Equal("Bang!", transportedException.InnerException.Message);
Assert.Empty(transportedException.Entries); // Because the entries cannot be serialized
}

[ConditionalFact]
public void DbUpdateConcurrencyException_exposes_public_empty_constructor()
{
Expand All @@ -86,6 +126,33 @@ public void DbUpdateConcurrencyException_exposes_public_string_and_inner_excepti
Assert.Same(inner, ex.InnerException);
}

[ConditionalFact]
public void Deserialized_DbUpdateConcurrencyException_can_be_serialized_and_deserialized_again()
{
var transportedException = SerializeAndDeserialize(
SerializeAndDeserialize(
new DbUpdateConcurrencyException("But somehow the vital connection is made")));

Assert.Equal(
"But somehow the vital connection is made",
transportedException.Message);
}

[ConditionalFact]
public void Deserialized_DbUpdateConcurrencyException_can_be_serialized_and_deserialized_again_with_entries()
{
var transportedException = SerializeAndDeserialize(
SerializeAndDeserialize(
new DbUpdateConcurrencyException(
"But somehow the vital connection is made",
new Exception("Bang!"),
new IUpdateEntry[] { new FakeUpdateEntry() })));

Assert.Equal("But somehow the vital connection is made", transportedException.Message);
Assert.Equal("Bang!", transportedException.InnerException.Message);
Assert.Empty(transportedException.Entries); // Because the entries cannot be serialized
}

private class FakeUpdateEntry : IUpdateEntry
{
public void SetOriginalValue(IProperty property, object value)
Expand Down Expand Up @@ -152,5 +219,19 @@ private static IEntityType CreateEntityType()
model.FinalizeModel();
return entityType;
}

private TException SerializeAndDeserialize<TException>(TException exception)
where TException : Exception
{
var stream = new MemoryStream();
var formatter = new BinaryFormatter();

#pragma warning disable MSLIB0003 // Issue https://github.com/dotnet/runtime/issues/39289 tracks finding an alternative to BinaryFormatter
formatter.Serialize(stream, exception);
stream.Seek(0, SeekOrigin.Begin);

return (TException)formatter.Deserialize(stream);
#pragma warning restore MSLIB0003
}
}
}