Skip to content

Commit

Permalink
Fix serialization roundtrip testing
Browse files Browse the repository at this point in the history
  • Loading branch information
yevhen committed Jul 24, 2017
1 parent d554e9f commit 929bacd
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 22 deletions.
6 changes: 1 addition & 5 deletions Source/Demo.App.Tests/ActorFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

using Orleankka.TestKit;

using Orleans.Runtime.Configuration;
using Orleans.Serialization;

namespace Demo
{
public abstract class ActorFixture
Expand All @@ -21,8 +18,7 @@ public abstract class ActorFixture
[SetUp]
public virtual void SetUp()
{
var config = new ClusterConfiguration();
Runtime = new ActorRuntimeMock(new SerializationManager(null, config.Globals, config.Defaults));
Runtime = new ActorRuntimeMock(new SerializationOptions(roundtrip: true));
}

protected ActorSystemMock System => Runtime.System;
Expand Down
7 changes: 3 additions & 4 deletions Source/Orleankka.TestKit/ActorRefMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@
using System.Linq.Expressions;
using System.Threading.Tasks;

using Orleans.Serialization;

namespace Orleankka.TestKit
{
[Serializable]
public class ActorRefMock : ActorRef
{
[NonSerialized] readonly SerializationManager serialization;
[NonSerialized] readonly SerializationOptions serialization;
[NonSerialized] readonly List<IExpectation> expectations = new List<IExpectation>();
[NonSerialized] readonly List<RecordedMessage> messages = new List<RecordedMessage>();

internal ActorRefMock(ActorPath path, SerializationManager serialization = null)
internal ActorRefMock(ActorPath path, SerializationOptions serialization = null)
: base(path)
{
this.serialization = serialization;
this.serialization = serialization ?? SerializationOptions.Default;
}

public TellExpectation<TMessage> ExpectTell<TMessage>(Expression<Func<TMessage, bool>> match = null)
Expand Down
4 changes: 1 addition & 3 deletions Source/Orleankka.TestKit/ActorRuntimeMock.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using Orleankka.Services;

using Orleans.Serialization;

namespace Orleankka.TestKit
{
public class ActorRuntimeMock : IActorRuntime
{
public ActorRuntimeMock(SerializationManager serialization = null)
public ActorRuntimeMock(SerializationOptions serialization = null)
{
System = new ActorSystemMock(serialization);
Timers = new TimerServiceMock();
Expand Down
8 changes: 3 additions & 5 deletions Source/Orleankka.TestKit/ActorSystemMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
using System.Collections.Generic;
using System.Threading.Tasks;

using Orleans.Serialization;

namespace Orleankka.TestKit
{
using Client;

public class ActorSystemMock : IClientActorSystem
{
readonly SerializationManager serialization;
readonly SerializationOptions serialization;

readonly Dictionary<ActorPath, ActorRefMock> actors =
new Dictionary<ActorPath, ActorRefMock>();
Expand All @@ -21,9 +19,9 @@ public class ActorSystemMock : IClientActorSystem
readonly Queue<ClientObservableMock> observables =
new Queue<ClientObservableMock>();

public ActorSystemMock(SerializationManager serialization = null)
public ActorSystemMock(SerializationOptions serialization = null)
{
this.serialization = serialization;
this.serialization = serialization ?? SerializationOptions.Default;
}

public ActorRefMock MockActorOf<TActor>(string id)
Expand Down
2 changes: 1 addition & 1 deletion Source/Orleankka.TestKit/Orleankka.TestKit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<Link>Properties\Product.cs</Link>
</Compile>
<Compile Include="ActorBehaviorExtensions.cs" />
<Compile Include="SerializationExtensions.cs" />
<Compile Include="SerializationOptions.cs" />
<Compile Include="StorageServiceMock.cs" />
<Compile Include="ActivationServiceMock.cs" />
<Compile Include="StreamSubscriptionMock.cs" />
Expand Down
52 changes: 52 additions & 0 deletions Source/Orleankka.TestKit/SerializationOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Linq;
using System.Net;
using System.Reflection;

using Microsoft.Extensions.DependencyInjection;

using Orleans;
using Orleans.Runtime.Configuration;
using Orleans.Serialization;
using Orleans.Runtime;

namespace Orleankka.TestKit
{
using Utility;

public class SerializationOptions
{
internal static readonly SerializationOptions Default = new SerializationOptions(false);

readonly SerializationManager manager;

public SerializationOptions(bool roundtrip, params Type[] serializers)
{
Requires.NotNull(serializers, nameof(serializers));

if (!roundtrip)
return;

var configuration = new ClientConfiguration
{
GatewayProvider = ClientConfiguration.GatewayProviderType.Config,
Gateways = { new IPEndPoint(0, 0) },
TraceToConsole = false,
DefaultTraceLevel = Severity.Off
};

if (serializers.Length > 0)
configuration.SerializationProviders.AddRange(serializers.Select(x => x.GetTypeInfo()));

var client = new ClientBuilder()
.UseConfiguration(configuration)
.Build();

manager = client.ServiceProvider.GetRequiredService<SerializationManager>();
}

internal object Roundtrip(object obj) => manager != null
? manager.RoundTripSerializationForTesting(obj)
: obj;
}
}
7 changes: 3 additions & 4 deletions Source/Orleankka.TestKit/StreamRefMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
using System.Linq.Expressions;
using System.Threading.Tasks;

using Orleans.Serialization;

namespace Orleankka.TestKit
{
[Serializable]
public class StreamRefMock : StreamRef
{
[NonSerialized] readonly SerializationManager serialization;
[NonSerialized] readonly SerializationOptions serialization;
[NonSerialized] readonly List<IExpectation> expectations = new List<IExpectation>();

[NonSerialized] readonly List<RecordedItem> items = new List<RecordedItem>();
Expand All @@ -24,10 +23,10 @@ public class StreamRefMock : StreamRef
public Actor Subscribed { get; private set; }
public Actor Resumed { get; private set; }

internal StreamRefMock(StreamPath path, SerializationManager serialization = null)
internal StreamRefMock(StreamPath path, SerializationOptions serialization = null)
: base(path, null)
{
this.serialization = serialization;
this.serialization = serialization ?? SerializationOptions.Default;
}

public PushExpectation<TItem> Expect<TItem>(Expression<Func<TItem, bool>> match = null)
Expand Down

0 comments on commit 929bacd

Please sign in to comment.