Skip to content

Commit

Permalink
Merge pull request #310 from gabikliot/SerializationTests_JObject
Browse files Browse the repository at this point in the history
Serialization test for JObject
  • Loading branch information
sergeybykov committed Apr 8, 2015
2 parents 43a7ffd + cc4b59a commit 7760bba
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Orleans/Configuration/MessagingConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public class MessagingConfiguration : IMessagingConfiguration
private static readonly int DEFAULT_GATEWAY_SENDER_QUEUES = Environment.ProcessorCount;
private static readonly int DEFAULT_CLIENT_SENDER_BUCKETS = (int)Math.Pow(2, 13);

private const int DEFAULT_BUFFER_POOL_BUFFER_SIZE = 4*1024;
private const int DEFAULT_BUFFER_POOL_BUFFER_SIZE = 4 * 1024;
private const int DEFAULT_BUFFER_POOL_MAX_SIZE = 10000;
private const int DEFAULT_BUFFER_POOL_PREALLOCATION_SIZE = 250;
private const bool DEFAULT_DROP_EXPIRED_MESSAGES = true;
Expand Down
23 changes: 18 additions & 5 deletions src/Orleans/Serialization/SerializationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ The above copyright notice and this permission notice shall be included in all c
using Orleans.Runtime;
using Orleans.Concurrency;
using Orleans.CodeGeneration;
using Orleans.Runtime.Configuration;

namespace Orleans.Serialization
{
Expand Down Expand Up @@ -133,6 +134,16 @@ internal static bool UseStandardSerializer

#region Static initialization

public static void InitializeForTesting()
{
BufferPool.InitGlobalBufferPool(new MessagingConfiguration(false));
// Load serialization info for currently-loaded assemblies
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
FindSerializationInfo(assembly);
}
}

internal static void Initialize(bool useStandardSerializer)
{
UseStandardSerializer = useStandardSerializer;
Expand Down Expand Up @@ -841,7 +852,8 @@ private static object DeepCopierHelper(Type t, object original)
if (t.IsSerializable)
return FallbackSerializationDeepCopy(original);

throw new OrleansException("No copier found for object of type " + t.OrleansTypeName() + ". Perhaps you need to mark it [Serializable]?");
throw new OrleansException("No copier found for object of type " + t.OrleansTypeName() +
". Perhaps you need to mark it [Serializable] or define a custom serializer for it?");
}

#endregion
Expand Down Expand Up @@ -990,7 +1002,7 @@ public static void SerializeInner(object obj, BinaryTokenStreamWriter stream, Ty
}

throw new ArgumentException("No serializer found for object of type " + t.OrleansTypeName()
+ ". Perhaps you need to mark it [Serializable]?");
+ ". Perhaps you need to mark it [Serializable] or define a custom serializer for it?");
}

// We assume that all lower bounds are 0, since creating an array with lower bound !=0 is hard in .NET 4.0+
Expand Down Expand Up @@ -1294,7 +1306,8 @@ public static object DeserializeInner(Type expected, BinaryTokenStreamReader str
return result;
}

throw new SerializationException("Unsupported type '" + resultType.OrleansTypeName() + "' encountered. Perhaps you need to mark it [Serializable]?");
throw new SerializationException("Unsupported type '" + resultType.OrleansTypeName() +
"' encountered. Perhaps you need to mark it [Serializable] or define a custom serializer for it?");
}

private static object DeserializeArray(Type resultType, BinaryTokenStreamReader stream)
Expand Down Expand Up @@ -1838,10 +1851,10 @@ internal static Type ResolveTypeName(string typeName)
/// <summary>
/// Internal test method to do a round-trip Serialize+Deserialize loop
/// </summary>
internal static object RoundTripSerializationForTesting(object source)
public static T RoundTripSerializationForTesting<T>(T source)
{
byte[] data = SerializeToByteArray(source);
return DeserializeFromByteArray<object>(data);
return DeserializeFromByteArray<T>(data);
}

private static void InstallAssemblyLoadEventHandler()
Expand Down
93 changes: 93 additions & 0 deletions src/Tester/SerializationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
Project Orleans Cloud Service SDK ver. 1.0
Copyright (c) Microsoft Corporation
All rights reserved.
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the ""Software""), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/


using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
using Orleans.Serialization;

namespace UnitTests.General
{
/// <summary>
/// Summary description for SerializationTests
/// </summary>
[TestClass]
public class SerializationTests
{
[TestInitialize]
public void InitializeForTesting()
{
SerializationManager.InitializeForTesting();
}

[TestMethod, TestCategory("BVT"), TestCategory("Nightly"), TestCategory("Serialization")]
public void SerializationTests_JObject()
{
const string json = @"{
CPU: 'Intel',
Drives: [
'DVD read/writer',
'500 gigabyte hard drive'
]
}";

JObject input = JObject.Parse(json);
JObject output = SerializationManager.RoundTripSerializationForTesting(input);
Assert.AreEqual(input.ToString(), output.ToString());
}

[Orleans.CodeGeneration.RegisterSerializerAttribute()]
internal class JObjectSerialization
{
static JObjectSerialization()
{
Register();
}

public static object DeepCopier(object original)
{
// I assume JObject is immutable, so no need to deep copy.
return original;
}

public static void Serializer(object untypedInput, BinaryTokenStreamWriter stream, Type expected)
{
var input = (JObject)(untypedInput);
string str = input.ToString();
SerializationManager.SerializeInner(str, stream, typeof(string));
}

public static object Deserializer(Type expected, BinaryTokenStreamReader stream)
{
var str = (string)(SerializationManager.DeserializeInner(typeof(string), stream));
return JObject.Parse(str);
}

public static void Register()
{
SerializationManager.Register(typeof(JObject), DeepCopier, Serializer, Deserializer);
}
}
}
}
1 change: 1 addition & 0 deletions src/Tester/Tester.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="ResultHandle.cs" />
<Compile Include="SerializationTests.cs" />
<Compile Include="StreamingTests\AQSubscriptionMultiplicityTests.cs" />
<Compile Include="StreamingTests\SampleStreamingTests.cs" />
<Compile Include="SimpleGrainTests.cs" />
Expand Down

0 comments on commit 7760bba

Please sign in to comment.