Skip to content
This repository has been archived by the owner on Jun 16, 2022. It is now read-only.

Commit

Permalink
Renamed tests to more reflect their actual contents.
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Removed logic for RPC method cancellation.
  • Loading branch information
DJGosnell committed Jun 21, 2018
1 parent 695022f commit 8772920
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@
<Compile Include="Mq\MqTestsBase.cs" />
<Compile Include="Mq\MqClientTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Rpc\RpcClientTests.cs" />
<Compile Include="Rpc\RpcTests.cs" />
<Compile Include="Rpc\RpcTestsBase.cs" />
<Compile Include="Rpc\Services\Server\CalculatorService.cs" />
<Compile Include="Rpc\Services\Server\SampleService.cs" />
<Compile Include="Mq\SimpleMqSession.cs" />
<Compile Include="Rpc\SimpleRpcSession.cs" />
<Compile Include="TestBase.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace DtronixMessageQueue.Tests.Rpc
{
public class RpcClientTests : RpcTestsBase
public class RpcTests : RpcTestsBase
{

public class Test
Expand All @@ -19,17 +19,17 @@ public class Test
[Test]
public void Client_calls_proxy_method()
{
Server.SessionSetup += (sender, args) => { args.Session.AddService(new CalculatorService()); };
Server.SessionSetup += (sender, args) => { args.Session.AddService(new SampleService()); };

Client.SessionSetup += (sender, args) =>
{
args.Session.AddProxy<ICalculatorService>("CalculatorService");
args.Session.AddProxy<ISampleService>("SampleService");
};


Client.Ready += (sender, args) =>
{
var service = Client.Session.GetProxy<ICalculatorService>();
var service = Client.Session.GetProxy<ISampleService>();
var result = service.Add(100, 200);

if (result != 300)
Expand All @@ -46,13 +46,13 @@ public void Client_calls_proxy_method()
[Test]
public void Client_calls_proxy_method_sequential()
{
Server.SessionSetup += (sender, args) => { args.Session.AddService(new CalculatorService()); };
Server.SessionSetup += (sender, args) => { args.Session.AddService(new SampleService()); };


Client.Ready += (sender, args) =>
{
args.Session.AddProxy<ICalculatorService>("CalculatorService");
var service = Client.Session.GetProxy<ICalculatorService>();
args.Session.AddProxy<ISampleService>("SampleService");
var service = Client.Session.GetProxy<ISampleService>();
Stopwatch stopwatch = Stopwatch.StartNew();

int addedInt = 0;
Expand All @@ -79,45 +79,6 @@ public void Client_calls_proxy_method_sequential()
StartAndWait();
}

[Test]
public void Client_calls_proxy_method_and_canceles()
{
Server.SessionSetup += (sender, args) =>
{
var service = new CalculatorService();
args.Session.AddService<ICalculatorService>(service);

service.LongRunningTaskCanceled += (o, eventArgs) => { TestComplete.Set(); };
};


Client.Ready += (sender, args) =>
{
args.Session.AddProxy<ICalculatorService>("CalculatorService");
var service = Client.Session.GetProxy<ICalculatorService>();
var tokenSource = new CancellationTokenSource();


bool threw = false;
try
{
tokenSource.CancelAfter(500);
service.LongRunningTask(1, 2, tokenSource.Token);
}
catch (OperationCanceledException)
{
threw = true;
}

if (threw != true)
{
LastException = new Exception("Operation did not cancel.");
}
};

StartAndWait();
}

[Test]
public void Server_requests_authentication()
{
Expand All @@ -134,15 +95,15 @@ public void Server_does_not_request_authentication()
Server.Config.RequireAuthentication = false;

Server.SessionSetup +=
(sender, args) => { args.Session.AddService<ICalculatorService>(new CalculatorService()); };
(sender, args) => { args.Session.AddService<ISampleService>(new SampleService()); };

Client.Authenticate += (sender, e) => { };


Client.Ready += (sender, e) =>
{
e.Session.AddProxy<ICalculatorService>("CalculatorService");
var service = Client.Session.GetProxy<ICalculatorService>();
e.Session.AddProxy<ISampleService>("SampleService");
var service = Client.Session.GetProxy<ISampleService>();

var result = service.Add(100, 200);

Expand All @@ -166,7 +127,7 @@ public void Server_verifies_authentication()
Server.Config.RequireAuthentication = true;

Server.SessionSetup +=
(sender, args) => { args.Session.AddService<ICalculatorService>(new CalculatorService()); };
(sender, args) => { args.Session.AddService<ISampleService>(new SampleService()); };

Server.Authenticate += (sender, e) =>
{
Expand Down Expand Up @@ -196,7 +157,7 @@ public void Server_disconnectes_from_failed_authentication()
Server.Config.RequireAuthentication = true;

Server.SessionSetup +=
(sender, args) => { args.Session.AddService<ICalculatorService>(new CalculatorService()); };
(sender, args) => { args.Session.AddService<ISampleService>(new SampleService()); };

Server.Authenticate += (sender, e) => { e.Authenticated = false; };

Expand All @@ -223,7 +184,7 @@ public void Client_disconnectes_from_failed_authentication()
Server.Config.RequireAuthentication = true;

Server.SessionSetup +=
(sender, args) => { args.Session.AddService<ICalculatorService>(new CalculatorService()); };
(sender, args) => { args.Session.AddService<ISampleService>(new SampleService()); };

Server.Authenticate += (sender, e) => { e.Authenticated = false; };

Expand All @@ -248,7 +209,7 @@ public void Client_notified_of_authentication_success()
ServerConfig.RequireAuthentication = true;

Server.SessionSetup +=
(sender, args) => { args.Session.AddService<ICalculatorService>(new CalculatorService()); };
(sender, args) => { args.Session.AddService<ISampleService>(new SampleService()); };

Server.Authenticate += (sender, e) => { e.Authenticated = true; };

Expand Down Expand Up @@ -316,5 +277,35 @@ public void Client_connects_disconnects_and_reconnects()

StartAndWait();
}

[Test]
public void Client_throws_on_invalid_argument_passed()
{
Client.Ready += (sender, args) =>
{
args.Session.AddProxy<ISampleService>("SampleService");
var service = Client.Session.GetProxy<ISampleService>();
var tokenSource = new CancellationTokenSource();


bool threw = false;
try
{
service.InvalidArguments(1, 2, tokenSource.Token);
}
catch (ArgumentException)
{
TestComplete.Set();
threw = true;
}

if (threw != true)
{
LastException = new Exception("Operation did not throw ArgumentException.");
}
};

StartAndWait();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using DtronixMessageQueue.Rpc;

namespace DtronixMessageQueue.Tests.Rpc.Services.Server
{
public class CalculatorService : MarshalByRefObject, ICalculatorService
public class SampleService : MarshalByRefObject, ISampleService
{
public string Name { get; } = "CalculatorService";
public string Name { get; } = "SampleService";
public SimpleRpcSession Session { get; set; }

public event EventHandler LongRunningTaskCanceled;
Expand All @@ -33,30 +34,17 @@ public int Divide(int number1, int number2)
return number1 / number2;
}

public int LongRunningTask(int number1, int number2, CancellationToken token)
public void InvalidArguments(int number1, int number2, CancellationToken token)
{
ManualResetEventSlim mre = new ManualResetEventSlim();

try
{
mre.Wait(token);
}
catch (Exception)
{
LongRunningTaskCanceled?.Invoke(this, EventArgs.Empty);
throw;
}

return number1 / number2;
}
}

public interface ICalculatorService : IRemoteService<SimpleRpcSession, RpcConfig>
public interface ISampleService : IRemoteService<SimpleRpcSession, RpcConfig>
{
int Add(int number1, int number2);
int Subtract(int number1, int number2);
int Multiply(int number1, int number2);
int Divide(int number1, int number2);
int LongRunningTask(int number1, int number2, CancellationToken token);
void InvalidArguments(int number1, int number2, CancellationToken token);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,5 @@ public enum RpcCallMessageAction : byte
/// Message is a Rpc response. Message contains information about the exception thrown.
/// </summary>
MethodException = 4,

/// <summary>
/// Message used to cancel a pending operation.
/// </summary>
MethodCancel = 5,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ public class RpcCallMessageHandler<TSession, TConfig> : MessageHandler<TSession,

public RpcCallMessageHandler(TSession session) : base(session)
{
Handlers.Add((byte) RpcCallMessageAction.MethodCancel, MethodCancelAction);

Handlers.Add((byte) RpcCallMessageAction.MethodCallNoReturn, ProcessRpcCallAction);
Handlers.Add((byte) RpcCallMessageAction.MethodCall, ProcessRpcCallAction);

Expand All @@ -58,17 +56,6 @@ public void AddService<T>(T instance) where T : IRemoteService<TSession, TConfig
_serviceInstances.Add(instance.Name, instance);
}

/// <summary>
/// Cancels the specified action.
/// </summary>
/// <param name="actionId">byte associated with the RpcCallMessageAction enum.></param>
/// <param name="message">Message containing the cancellation information.</param>
public void MethodCancelAction(byte actionId, MqMessage message)
{
var cancellationId = message[0].ReadUInt16(0);
_remoteWaitOperations.Cancel(cancellationId);
}


/// <summary>
/// Processes the incoming Rpc call from the recipient connection.
Expand Down Expand Up @@ -104,19 +91,8 @@ private void ProcessRpcCallAction(byte actionId, MqMessage message)
if (serviceMethod == null || !_serviceInstances.ContainsKey(recServiceName))
throw new Exception($"Service '{recServiceName}' does not exist.");

ResponseWaitHandle cancellationWait = null;

// If the past parameter is a cancellation token, setup a return wait for this call to allow for remote cancellation.
if (recMessageReturnId != 0 && serviceMethod.HasCancellation)
{
cancellationWait = _remoteWaitOperations.CreateWaitHandle(recMessageReturnId);

cancellationWait.TokenSource = new CancellationTokenSource();
cancellationWait.Token = cancellationWait.TokenSource.Token;
}

// Setup the parameters to pass to the invoked method.
var parameters = new object[recArgumentCount + (cancellationWait == null ? 0 : 1)];
var parameters = new object[recArgumentCount];

// Determine if we have any parameters to pass to the invoked method.
if (recArgumentCount > 0)
Expand All @@ -128,11 +104,6 @@ private void ProcessRpcCallAction(byte actionId, MqMessage message)
parameters[i] = serialization.DeserializeFromReader(serviceMethod.ParameterTypes[i], i);
}

// Add the cancellation token to the parameters.
if (cancellationWait != null)
parameters[parameters.Length - 1] = cancellationWait.Token;


object returnValue;
try
{
Expand All @@ -143,11 +114,9 @@ private void ProcessRpcCallAction(byte actionId, MqMessage message)
{
// Determine if this method was waited on. If it was and an exception was thrown,
// Let the recipient session know an exception was thrown.
if (recMessageReturnId != 0 &&
ex.InnerException?.GetType() != typeof(OperationCanceledException))
{
if (recMessageReturnId != 0)
SendRpcException(serialization, ex, recMessageReturnId);
}

return;
}
finally
Expand Down
20 changes: 1 addition & 19 deletions src/DtronixMessageQueue/Rpc/ResponseWait.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ public T CreateWaitHandle(ushort? handleId)
lock (_idLock)
{
if (++_id > ushort.MaxValue)
{
_id = 0;
}

returnWait.Id = (ushort) _id;
}
}
Expand All @@ -54,23 +53,6 @@ public T CreateWaitHandle(ushort? handleId)
return returnWait;
}

/// <summary>
/// Called to cancel a operation on this and the recipient connection if specified.
/// </summary>
/// <param name="handleId">Id of the waiting operation to cancel.</param>
public void Cancel(ushort handleId)
{
T callWaitHandle;

// Try to get the wait. If the Id does not exist, the wait operation has already been completed or removed.
if (!TryRemove(handleId, out callWaitHandle))
{
return;
}

callWaitHandle.TokenSource?.Cancel();
}

/// <summary>
/// Called to complete operation on this connection.
/// </summary>
Expand Down
10 changes: 0 additions & 10 deletions src/DtronixMessageQueue/Rpc/ResponseWaitHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,6 @@ public class ResponseWaitHandle
/// </summary>
public byte MessageActionId { get; set; }

/// <summary>
/// Cancellation token for the request.
/// </summary>
public CancellationToken Token { get; set; }

/// <summary>
/// Cancellation token source for the request.
/// </summary>
public CancellationTokenSource TokenSource { get; set; }

/// <summary>
/// Contains the time that this call wait was created to check for timeouts.
/// </summary>
Expand Down
Loading

0 comments on commit 8772920

Please sign in to comment.