-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load Assembly referenced from within a lambda closure (#135)
- Loading branch information
Showing
6 changed files
with
405 additions
and
31 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
// 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.IO; | ||
using System.Reflection; | ||
using System.Runtime.Serialization.Formatters.Binary; | ||
using Microsoft.Spark.Utils; | ||
using Xunit; | ||
|
||
namespace Microsoft.Spark.UnitTest | ||
{ | ||
public class UdfSerDeTests | ||
{ | ||
[Serializable] | ||
private class TestClass | ||
{ | ||
private string _str; | ||
|
||
public TestClass(string s) | ||
{ | ||
_str = s; | ||
} | ||
|
||
public string Concat(string s) | ||
{ | ||
if (_str == null) | ||
{ | ||
return s + s; | ||
} | ||
|
||
return _str + s; | ||
} | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
var that = obj as TestClass; | ||
|
||
if (that == null) | ||
{ | ||
return false; | ||
} | ||
|
||
return _str == that._str; | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
return base.GetHashCode(); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void TestUdfSerDe() | ||
{ | ||
{ | ||
// Without closure. | ||
Func<int, int> expectedUdf = i => 10 * i; | ||
Delegate actualUdf = SerDe(expectedUdf); | ||
|
||
VerifyUdfSerDe(expectedUdf, actualUdf, false); | ||
Assert.Equal(100, ((Func<int, int>)actualUdf)(10)); | ||
} | ||
|
||
{ | ||
// With closure where the delegate target is an anonymous class. | ||
// The target will contain fields ["tc1", "tc2"], where "tc1" is | ||
// non null and "tc2" is null. | ||
TestClass tc1 = new TestClass("Test"); | ||
TestClass tc2 = null; | ||
Func<string, string> expectedUdf = | ||
(s) => | ||
{ | ||
if (tc2 == null) | ||
{ | ||
return tc1.Concat(s); | ||
} | ||
return s; | ||
}; | ||
Delegate actualUdf = SerDe(expectedUdf); | ||
|
||
VerifyUdfSerDe(expectedUdf, actualUdf, true); | ||
Assert.Equal("TestHelloWorld", ((Func<string, string>)actualUdf)("HelloWorld")); | ||
} | ||
|
||
{ | ||
// With closure where the delegate target is TestClass | ||
// and target's field "_str" is set to "Test". | ||
TestClass tc = new TestClass("Test"); | ||
Func<string, string> expectedUdf = tc.Concat; | ||
Delegate actualUdf = SerDe(expectedUdf); | ||
|
||
VerifyUdfSerDe(expectedUdf, actualUdf, true); | ||
Assert.Equal("TestHelloWorld", ((Func<string, string>)actualUdf)("HelloWorld")); | ||
} | ||
|
||
{ | ||
// With closure where the delegate target is TestClass, | ||
// and target's field "_str" is set to null. | ||
TestClass tc = new TestClass(null); | ||
Func<string, string> expectedUdf = tc.Concat; | ||
Delegate actualUdf = SerDe(expectedUdf); | ||
|
||
VerifyUdfSerDe(expectedUdf, actualUdf, true); | ||
Assert.Equal( | ||
"HelloWorldHelloWorld", | ||
((Func<string, string>)actualUdf)("HelloWorld")); | ||
} | ||
} | ||
|
||
private void VerifyUdfSerDe(Delegate expectedUdf, Delegate actualUdf, bool hasClosure) | ||
{ | ||
VerifyUdfData( | ||
UdfSerDe.Serialize(expectedUdf), | ||
UdfSerDe.Serialize(actualUdf), | ||
hasClosure); | ||
VerifyDelegate(expectedUdf, actualUdf); | ||
} | ||
|
||
private void VerifyUdfData( | ||
UdfSerDe.UdfData expectedUdfData, | ||
UdfSerDe.UdfData actualUdfData, | ||
bool hasClosure) | ||
{ | ||
Assert.Equal(expectedUdfData, actualUdfData); | ||
|
||
if (!hasClosure) | ||
{ | ||
Assert.Null(expectedUdfData.TargetData.Fields); | ||
Assert.Null(actualUdfData.TargetData.Fields); | ||
} | ||
} | ||
|
||
private void VerifyDelegate(Delegate expectedDelegate, Delegate actualDelegate) | ||
{ | ||
Assert.Equal(expectedDelegate.GetType(), actualDelegate.GetType()); | ||
Assert.Equal(expectedDelegate.Method, actualDelegate.Method); | ||
Assert.Equal(expectedDelegate.Target.GetType(), actualDelegate.Target.GetType()); | ||
|
||
FieldInfo[] expectedFields = expectedDelegate.Target.GetType().GetFields(); | ||
FieldInfo[] actualFields = actualDelegate.Target.GetType().GetFields(); | ||
Assert.Equal(expectedFields, actualFields); | ||
} | ||
|
||
private Delegate SerDe(Delegate udf) | ||
{ | ||
return Deserialize(Serialize(udf)); | ||
} | ||
|
||
private byte[] Serialize(Delegate udf) | ||
{ | ||
UdfSerDe.UdfData udfData = UdfSerDe.Serialize(udf); | ||
|
||
using (var ms = new MemoryStream()) | ||
{ | ||
var bf = new BinaryFormatter(); | ||
bf.Serialize(ms, udfData); | ||
return ms.ToArray(); | ||
} | ||
} | ||
|
||
private Delegate Deserialize(byte[] serializedUdf) | ||
{ | ||
using (var ms = new MemoryStream(serializedUdf, false)) | ||
{ | ||
var bf = new BinaryFormatter(); | ||
UdfSerDe.UdfData udfData = (UdfSerDe.UdfData)bf.Deserialize(ms); | ||
return UdfSerDe.Deserialize(udfData); | ||
} | ||
} | ||
} | ||
} |
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.