From c0669b09c0ca8ea5bfac01238f866c66adf2f8a1 Mon Sep 17 00:00:00 2001 From: Smit Patel Date: Fri, 28 Aug 2020 13:47:32 -0700 Subject: [PATCH] Re-enable exception serialization tests Resolves #22232 --- .../DesignExceptionTest.cs | 29 +++++++ test/EFCore.Tests/ExceptionTest.cs | 81 +++++++++++++++++++ 2 files changed, 110 insertions(+) diff --git a/test/EFCore.Design.Tests/DesignExceptionTest.cs b/test/EFCore.Design.Tests/DesignExceptionTest.cs index eca71ec076b..080d8ab7a0a 100644 --- a/test/EFCore.Design.Tests/DesignExceptionTest.cs +++ b/test/EFCore.Design.Tests/DesignExceptionTest.cs @@ -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; @@ -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 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 + } } } diff --git a/test/EFCore.Tests/ExceptionTest.cs b/test/EFCore.Tests/ExceptionTest.cs index 340741cc877..d8fa4849cf2 100644 --- a/test/EFCore.Tests/ExceptionTest.cs +++ b/test/EFCore.Tests/ExceptionTest.cs @@ -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; @@ -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() { @@ -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() { @@ -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) @@ -152,5 +219,19 @@ private static IEntityType CreateEntityType() model.FinalizeModel(); return entityType; } + + private TException SerializeAndDeserialize(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 + } } }