-
Notifications
You must be signed in to change notification settings - Fork 321
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
Migrated to MessagePack instead of BinaryFormatter #1202
Merged
wudanzy
merged 2 commits into
dotnet:main
from
grazy27:feature/message-pack-serialization
Feb 19, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
src/csharp/Microsoft.Spark.E2ETest/Microsoft.Spark.E2ETest.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
src/csharp/Microsoft.Spark.UnitTest/BinarySerDeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using MessagePack; | ||
using Microsoft.Spark.Utils; | ||
using Xunit; | ||
|
||
namespace Microsoft.Spark.UnitTest; | ||
|
||
[Collection("Spark Unit Tests")] | ||
public class BinarySerDeTests | ||
{ | ||
[Theory] | ||
[InlineData(42)] | ||
[InlineData("Test")] | ||
[InlineData(99.99)] | ||
public void Serialize_ShouldWriteObjectToStream(object input) | ||
{ | ||
using var memoryStream = new MemoryStream(); | ||
BinarySerDe.Serialize(memoryStream, input); | ||
memoryStream.Position = 0; | ||
|
||
var deserializedObject = MessagePackSerializer.Typeless.Deserialize(memoryStream); | ||
|
||
Assert.Equal(input, deserializedObject); | ||
} | ||
|
||
[Fact] | ||
public void Deserialize_ShouldReturnExpectedObject_WhenTypeMatches() | ||
{ | ||
var employee = new Employee { Id = 101, Name = "John Doe" }; | ||
using var memoryStream = new MemoryStream(); | ||
MessagePackSerializer.Typeless.Serialize(memoryStream, employee); | ||
memoryStream.Position = 0; | ||
|
||
var result = BinarySerDe.Deserialize<Employee>(memoryStream); | ||
|
||
Assert.Equal(employee.Id, result.Id); | ||
Assert.Equal(employee.Name, result.Name); | ||
} | ||
|
||
[Fact] | ||
public void Deserialize_ShouldThrowInvalidCastEx_WhenTypeDoesNotMatch() | ||
{ | ||
var employee = new Employee { Id = 101, Name = "John Doe" }; | ||
using var memoryStream = new MemoryStream(); | ||
MessagePackSerializer.Typeless.Serialize(memoryStream, employee); | ||
memoryStream.Position = 0; | ||
|
||
var action = () => BinarySerDe.Deserialize<Department>(memoryStream); | ||
|
||
Assert.Throws<InvalidCastException>(action); | ||
} | ||
|
||
[Fact] | ||
public void Serialize_CustomFunctionAndObject_ShouldBeSerializable() | ||
{ | ||
var department = new Department { Name = "HR", EmployeeCount = 27 }; | ||
var employeeStub = new Employee | ||
{ | ||
EmbeddedObject = department, | ||
Id = 11, | ||
Name = "Derek", | ||
}; | ||
using var memoryStream = new MemoryStream(); | ||
MessagePackSerializer.Typeless.Serialize(memoryStream, employeeStub); | ||
memoryStream.Position = 0; | ||
|
||
var deserializedCalculation = BinarySerDe.Deserialize<Employee>(memoryStream); | ||
|
||
Assert.IsType<Department>(deserializedCalculation.EmbeddedObject); | ||
Assert.Equal(27, ((Department)deserializedCalculation.EmbeddedObject).EmployeeCount); | ||
Assert.Equal("HR", ((Department)deserializedCalculation.EmbeddedObject).Name); | ||
} | ||
|
||
[Fact] | ||
public void Serialize_ClassWithoutSerializableAttribute_ShouldThrowException() | ||
{ | ||
var nonSerializableClass = new NonSerializableClass { Value = 123 }; | ||
using var memoryStream = new MemoryStream(); | ||
BinarySerDe.Serialize(memoryStream, nonSerializableClass); | ||
memoryStream.Position = 0; | ||
|
||
Assert.Throws<MessagePackSerializationException>(() => BinarySerDe.Deserialize<NonSerializableClass>(memoryStream)); | ||
} | ||
|
||
[Fact] | ||
public void Serialize_CollectionAndDictionary_ShouldBeSerializable() | ||
{ | ||
var list = new List<int> { 1, 2, 3 }; | ||
var dictionary = new Dictionary<string, int> { { "one", 1 }, { "two", 2 } }; | ||
|
||
using var memoryStream = new MemoryStream(); | ||
BinarySerDe.Serialize(memoryStream, list); | ||
memoryStream.Position = 0; | ||
var deserializedList = MessagePackSerializer.Typeless.Deserialize(memoryStream) as List<int>; | ||
|
||
Assert.Equal(list, deserializedList); | ||
|
||
memoryStream.SetLength(0); | ||
BinarySerDe.Serialize(memoryStream, dictionary); | ||
memoryStream.Position = 0; | ||
var deserializedDictionary = MessagePackSerializer.Typeless.Deserialize(memoryStream) as Dictionary<string, int>; | ||
|
||
Assert.Equal(dictionary, deserializedDictionary); | ||
} | ||
|
||
[Fact] | ||
public void Serialize_PolymorphicObject_ShouldBeSerializable() | ||
{ | ||
Employee manager = new Manager { Id = 1, Name = "Alice", Role = "Account manager" }; | ||
using var memoryStream = new MemoryStream(); | ||
BinarySerDe.Serialize(memoryStream, manager); | ||
memoryStream.Position = 0; | ||
|
||
var deserializedEmployee = BinarySerDe.Deserialize<Employee>(memoryStream); | ||
|
||
Assert.IsType<Manager>(deserializedEmployee); | ||
Assert.Equal("Alice", deserializedEmployee.Name); | ||
Assert.Equal("Account manager", ((Manager)deserializedEmployee).Role); | ||
} | ||
|
||
[Serializable] | ||
private class Employee | ||
{ | ||
public int Id { get; set; } | ||
|
||
public string Name { get; set; } | ||
|
||
public object EmbeddedObject { get; set; } | ||
} | ||
|
||
[Serializable] | ||
private class Department | ||
{ | ||
public string Name { get; set; } | ||
public int EmployeeCount { get; set; } | ||
} | ||
|
||
[Serializable] | ||
private class Manager : Employee | ||
{ | ||
public string Role { get; set; } | ||
} | ||
|
||
private class NonSerializableClass | ||
{ | ||
public int Value { get; init; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
src/csharp/Microsoft.Spark.Worker.UnitTest/Microsoft.Spark.Worker.UnitTest.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise messagePack cannot instantiate an ExternalClass instance, as it doesn't know what to use for argument.
We can either create a default parameter-less constructor, or rename field to match ctor argument name.