Skip to content

Commit

Permalink
Backend.Tests: add ex marshalling new formats
Browse files Browse the repository at this point in the history
Somehow, in master branch (where we use runs-on: windows-latest
instead of windows-2019), the exception marshalling unit tests
started failing; maybe because of some update from Microsoft
of the v4.x .NET framework.

This commit makes these tests compatible with the new format
(master branch), while keeping the old format intact (stable
branch), and hoping that the new .NET6 migration will match one.

Co-authored-by: Andres G. Aragoneses <[email protected]>
  • Loading branch information
aarani and knocte committed Dec 26, 2022
1 parent f5a69fe commit 08d7795
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 19 deletions.
41 changes: 35 additions & 6 deletions src/GWallet.Backend.Tests/ExceptionMarshalling.fs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ type ExceptionMarshalling () =
cex
Marshalling.Serialize ex

let msg = "Exceptions didn't match. Full binary form was "
let legacyMsg = "(Legacy)Exceptions didn't match. Full binary form was "

[<Test>]
member __.``can serialize basic exceptions``() =
let json = SerializeBasicException ()
Assert.That(json, Is.Not.Null)
Assert.That(json, Is.Not.Empty)
Assert.That(MarshallingData.SerializedExceptionsAreSame json MarshallingData.BasicExceptionExampleInJson)
Assert.That(MarshallingData.SerializedExceptionsAreSame json MarshallingData.BasicExceptionExampleInJson msg)

[<Test>]
member __.``can deserialize basic exceptions``() =
Expand All @@ -98,7 +100,16 @@ type ExceptionMarshalling () =
let json = SerializeRealException ()
Assert.That(json, Is.Not.Null)
Assert.That(json, Is.Not.Empty)
Assert.That(MarshallingData.SerializedExceptionsAreSame json MarshallingData.RealExceptionExampleInJson)
if Config.IsWindowsPlatform () then
let serializedExceptionsAreSame =
try
MarshallingData.SerializedExceptionsAreSame json MarshallingData.RealExceptionExampleInJson msg
with
| :? AssertionException ->
MarshallingData.SerializedExceptionsAreSame json MarshallingData.RealExceptionWindowsLegacyExampleInJson legacyMsg
Assert.That serializedExceptionsAreSame
else
Assert.That(MarshallingData.SerializedExceptionsAreSame json MarshallingData.RealExceptionUnixLegacyExampleInJson legacyMsg)

[<Test>]
member __.``can deserialize real exceptions``() =
Expand All @@ -123,7 +134,7 @@ type ExceptionMarshalling () =
let json = SerializeInnerException ()
Assert.That(json, Is.Not.Null)
Assert.That(json, Is.Not.Empty)
Assert.That(MarshallingData.SerializedExceptionsAreSame json MarshallingData.InnerExceptionExampleInJson)
Assert.That(MarshallingData.SerializedExceptionsAreSame json MarshallingData.InnerExceptionExampleInJson msg)

[<Test>]
member __.``can deserialize inner exceptions``() =
Expand Down Expand Up @@ -151,7 +162,7 @@ type ExceptionMarshalling () =
let json = SerializeCustomException ()
Assert.That(json, Is.Not.Null)
Assert.That(json, Is.Not.Empty)
Assert.That(MarshallingData.SerializedExceptionsAreSame json MarshallingData.CustomExceptionExampleInJson)
Assert.That(MarshallingData.SerializedExceptionsAreSame json MarshallingData.CustomExceptionExampleInJson msg)

[<Test>]
member __.``serializing custom exception not prepared for binary serialization, throws``() =
Expand Down Expand Up @@ -183,7 +194,16 @@ type ExceptionMarshalling () =
let json = SerializeCustomFSharpException ()
Assert.That(json, Is.Not.Null)
Assert.That(json, Is.Not.Empty)
Assert.That(MarshallingData.SerializedExceptionsAreSame json MarshallingData.CustomFSharpExceptionExampleInJson)
if Config.IsWindowsPlatform () then
let serializedExceptionsAreSame =
try
MarshallingData.SerializedExceptionsAreSame json MarshallingData.CustomFSharpExceptionExampleInJson msg
with
| :? AssertionException ->
MarshallingData.SerializedExceptionsAreSame json MarshallingData.CustomFSharpExceptionLegacyExampleInJson legacyMsg
Assert.That serializedExceptionsAreSame
else
Assert.That(MarshallingData.SerializedExceptionsAreSame json MarshallingData.CustomFSharpExceptionLegacyExampleInJson legacyMsg)

[<Test>]
member __.``can deserialize F# custom exceptions``() =
Expand Down Expand Up @@ -212,7 +232,16 @@ type ExceptionMarshalling () =
Assert.That(json, Is.Not.Null)
Assert.That(json, Is.Not.Empty)

Assert.That(MarshallingData.SerializedExceptionsAreSame json MarshallingData.FullExceptionExampleInJson)
if Config.IsWindowsPlatform () then
let serializedExceptionsAreSame =
try
MarshallingData.SerializedExceptionsAreSame json MarshallingData.FullExceptionExampleInJson msg
with
| :? AssertionException ->
MarshallingData.SerializedExceptionsAreSame json MarshallingData.FullExceptionWindowsLegacyExampleInJson legacyMsg
Assert.That serializedExceptionsAreSame
else
Assert.That(MarshallingData.SerializedExceptionsAreSame json MarshallingData.FullExceptionUnixLegacyExampleInJson legacyMsg)

[<Test>]
member __.``can deserialize full exceptions (all previous features combined)``() =
Expand Down
5 changes: 5 additions & 0 deletions src/GWallet.Backend.Tests/GWallet.Backend.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,15 @@
<EmbeddedResource Include="data\unsignedAndFormattedEtherTransaction.json" />
<EmbeddedResource Include="data\basicException.json" />
<EmbeddedResource Include="data\realException.json" />
<EmbeddedResource Include="data\realException_unixLegacy.json" />
<EmbeddedResource Include="data\realException_windowsLegacy.json" />
<EmbeddedResource Include="data\innerException.json" />
<EmbeddedResource Include="data\customException.json" />
<EmbeddedResource Include="data\customFSharpException.json" />
<EmbeddedResource Include="data\customFSharpException_legacy.json" />
<EmbeddedResource Include="data\fullException.json" />
<EmbeddedResource Include="data\fullException_unixLegacy.json" />
<EmbeddedResource Include="data\fullException_windowsLegacy.json" />
<Compile Include="ElectrumIntegrationTests.fs" />
<Compile Include="WarpWallet.fs" />
<Compile Include="CompoundBalanceCaching.fs" />
Expand Down
41 changes: 31 additions & 10 deletions src/GWallet.Backend.Tests/MarshallingData.fs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ module MarshallingData =
let RealExceptionExampleInJson =
ReadEmbeddedResource "realException.json"

let RealExceptionUnixLegacyExampleInJson =
ReadEmbeddedResource "realException_unixLegacy.json"

let RealExceptionWindowsLegacyExampleInJson =
ReadEmbeddedResource "realException_windowsLegacy.json"

let InnerExceptionExampleInJson =
ReadEmbeddedResource "innerException.json"

Expand All @@ -65,11 +71,26 @@ module MarshallingData =
let CustomFSharpExceptionExampleInJson =
ReadEmbeddedResource "customFSharpException.json"

let CustomFSharpExceptionLegacyExampleInJson =
ReadEmbeddedResource "customFSharpException_legacy.json"

let FullExceptionExampleInJson =
ReadEmbeddedResource "fullException.json"

let FullExceptionUnixLegacyExampleInJson =
ReadEmbeddedResource "fullException_unixLegacy.json"

let FullExceptionWindowsLegacyExampleInJson =
ReadEmbeddedResource "fullException_windowsLegacy.json"

let rec TrimOutsideAndInside(str: string) =
let trimmed = str.Replace(" ", " ").Trim()
if trimmed = str then
trimmed
else
TrimOutsideAndInside trimmed

let SerializedExceptionsAreSame actualJsonString expectedJsonString =
let SerializedExceptionsAreSame actualJsonString expectedJsonString msg =

let actualJsonException = JObject.Parse actualJsonString
let expectedJsonException = JObject.Parse expectedJsonString
Expand All @@ -85,17 +106,14 @@ module MarshallingData =
let stackTraceJToken = jsonEx.SelectToken stackTracePath
Assert.That(stackTraceJToken, Is.Not.Null, sprintf "Path %s not found in %s" stackTracePath (jsonEx.ToString()))
let initialStackTraceJToken = stackTraceJToken.ToString()
if initialStackTraceJToken.Length > 0 then
if initialStackTraceJToken.Length > 0 && isUnix then
Assert.That(initialStackTraceJToken, Is.StringContaining stackTraceFragment,
sprintf "comparing actual '%s' with expected '%s'" actualJsonString expectedJsonString)
let endOfStackTrace = initialStackTraceJToken.Substring(initialStackTraceJToken.IndexOf stackTraceFragment)
let tweakedEndOfStackTrace =
if isUnix then
endOfStackTrace
.Replace(":line 42", ":41 ")
.Replace(":line 65", ":64 ")
else
endOfStackTrace
endOfStackTrace
.Replace(":line 42", ":41 ")
.Replace(":line 65", ":64 ")
stackTraceJToken.Replace (tweakedEndOfStackTrace |> JToken.op_Implicit)

let binaryFormToken = jsonEx.SelectToken fullBinaryFormPath
Expand All @@ -111,8 +129,11 @@ module MarshallingData =
tweakStackTraces()

let actualBinaryForm = (actualJsonException.SelectToken fullBinaryFormPath).ToString()
Assert.That(actualJsonException.ToString(), Is.EqualTo (expectedJsonException.ToString()),
sprintf "Exceptions didn't match. Full binary form was %s" actualBinaryForm)
Assert.That(
TrimOutsideAndInside(actualJsonException.ToString()),
Is.EqualTo (TrimOutsideAndInside(expectedJsonException.ToString())),
msg + actualBinaryForm
)

true

Expand Down
2 changes: 1 addition & 1 deletion src/GWallet.Backend.Tests/data/customFSharpException.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"Value": {
"HumanReadableSummary": {
"ExceptionType": "GWallet.Backend.Tests.CustomFSharpException",
"Message": "Exception of type 'GWallet.Backend.Tests.CustomFSharpException' was thrown.",
"Message": "CustomFSharpException",
"StackTrace": "",
"InnerException": null
},
Expand Down
13 changes: 13 additions & 0 deletions src/GWallet.Backend.Tests/data/customFSharpException_legacy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"Version": "{version}",
"TypeName": "GWallet.Backend.MarshalledException",
"Value": {
"HumanReadableSummary": {
"ExceptionType": "GWallet.Backend.Tests.CustomFSharpException",
"Message": "Exception of type 'GWallet.Backend.Tests.CustomFSharpException' was thrown.",
"StackTrace": "",
"InnerException": null
},
"FullBinaryForm": "{binaryMarshalledException}"
}
}
2 changes: 1 addition & 1 deletion src/GWallet.Backend.Tests/data/fullException.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"HumanReadableSummary": {
"ExceptionType": "GWallet.Backend.Tests.CustomException",
"Message": "msg",
"StackTrace": " at GWallet.Backend.Tests.ExceptionMarshalling.SerializeFullException () [0x00016] in {prjDirAbsolutePath}/ExceptionMarshalling.fs:line 65",
"StackTrace": " at GWallet.Backend.Tests.ExceptionMarshalling.SerializeFullException()",
"InnerException": {
"Case": "Some",
"Fields": [
Expand Down
23 changes: 23 additions & 0 deletions src/GWallet.Backend.Tests/data/fullException_unixLegacy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"Version": "{version}",
"TypeName": "GWallet.Backend.MarshalledException",
"Value": {
"HumanReadableSummary": {
"ExceptionType": "GWallet.Backend.Tests.CustomException",
"Message": "msg",
"StackTrace": " at GWallet.Backend.Tests.ExceptionMarshalling.SerializeFullException () [0x00016] in {prjDirAbsolutePath}/ExceptionMarshalling.fs:line 65",
"InnerException": {
"Case": "Some",
"Fields": [
{
"ExceptionType": "GWallet.Backend.Tests.CustomException",
"Message": "innerMsg",
"StackTrace": "",
"InnerException": null
}
]
}
},
"FullBinaryForm": "{binaryMarshalledException}"
}
}
23 changes: 23 additions & 0 deletions src/GWallet.Backend.Tests/data/fullException_windowsLegacy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"Version": "{version}",
"TypeName": "GWallet.Backend.MarshalledException",
"Value": {
"HumanReadableSummary": {
"ExceptionType": "GWallet.Backend.Tests.CustomException",
"Message": "msg",
"StackTrace": " at GWallet.Backend.Tests.ExceptionMarshalling.SerializeFullException() in D:\\a\\geewallet\\geewallet\\src\\GWallet.Backend.Tests\\ExceptionMarshalling.fs:line 65",
"InnerException": {
"Case": "Some",
"Fields": [
{
"ExceptionType": "GWallet.Backend.Tests.CustomException",
"Message": "innerMsg",
"StackTrace": "",
"InnerException": null
}
]
}
},
"FullBinaryForm": "{binaryMarshalledException}"
}
}
2 changes: 1 addition & 1 deletion src/GWallet.Backend.Tests/data/realException.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"HumanReadableSummary": {
"ExceptionType": "System.Exception",
"Message": "msg",
"StackTrace": " at GWallet.Backend.Tests.ExceptionMarshalling.can serialize real exceptions () [0x0000c] in {prjDirAbsolutePath}/ExceptionMarshalling.fs:line 42",
"StackTrace": " at GWallet.Backend.Tests.ExceptionMarshalling.SerializeRealException()",
"InnerException": null
},
"FullBinaryForm": "{binaryMarshalledException}"
Expand Down
13 changes: 13 additions & 0 deletions src/GWallet.Backend.Tests/data/realException_unixLegacy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"Version": "{version}",
"TypeName": "GWallet.Backend.MarshalledException",
"Value": {
"HumanReadableSummary": {
"ExceptionType": "System.Exception",
"Message": "msg",
"StackTrace": " at GWallet.Backend.Tests.ExceptionMarshalling.can serialize real exceptions () [0x0000c] in {prjDirAbsolutePath}/ExceptionMarshalling.fs:line 42",
"InnerException": null
},
"FullBinaryForm": "{binaryMarshalledException}"
}
}
13 changes: 13 additions & 0 deletions src/GWallet.Backend.Tests/data/realException_windowsLegacy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"Version": "{version}",
"TypeName": "GWallet.Backend.MarshalledException",
"Value": {
"HumanReadableSummary": {
"ExceptionType": "System.Exception",
"Message": "msg",
"StackTrace": " at GWallet.Backend.Tests.ExceptionMarshalling.SerializeRealException() in D:\\a\\geewallet\\geewallet\\src\\GWallet.Backend.Tests\\ExceptionMarshalling.fs:line 42",
"InnerException": null
},
"FullBinaryForm": "{binaryMarshalledException}"
}
}

0 comments on commit 08d7795

Please sign in to comment.