Skip to content

Commit

Permalink
xXxHostRunner refactor, ConsoleHostRunnerTests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sholtee committed Jul 17, 2020
1 parent 16670dd commit 80608d6
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 59 deletions.
34 changes: 18 additions & 16 deletions SRC/RPC.Server/Private/Hosting/ConsoleHostRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected override void Dispose(bool disposeManaged)
public ConsoleHostRunner(IHost host) : base(host) { }

[SuppressMessage("Reliability", "CA2008:Do not create tasks without passing a TaskScheduler")]
protected override void Start()
public override void Start()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Expand All @@ -40,22 +40,11 @@ protected override void Start()
.ToArray();

if (missingServices.Any())
{
Console.Error.WriteLine(string.Format(Resources.Culture, Resources.DEPENDENCY_NOT_AVAILABLE, missingServices));
Environment.Exit(-1);
}
throw new Exception(string.Format(Resources.Culture, Resources.DEPENDENCY_NOT_AVAILABLE, string.Join(",", missingServices)));
}

Console.CancelKeyPress += (s, e) =>
{
Stop();
//
// E nelkul parhuzamosan ket modon probalnank leallitani az app-ot
//
e.Cancel = true;
};
Console.Title = Host.Name;
Console.CancelKeyPress += OnConsoleCancel;

try
{
Expand All @@ -75,8 +64,21 @@ protected override void Start()
}
}

internal void OnConsoleCancel(object sender, ConsoleCancelEventArgs e)
{
Stop();

//
// E nelkul parhuzamosan ket modon probalnank leallitani az app-ot
//
#if DEBUG
if (e != null)
#endif
e.Cancel = true;
}

public override bool ShouldUse => Environment.UserInteractive;

protected override void Stop() => FTerminate.Set();
public override void Stop() => FTerminate.Set();
}
}
8 changes: 2 additions & 6 deletions SRC/RPC.Server/Private/Hosting/DefaultHostRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@ internal class DefaultHostRunner: HostRunner

public override bool ShouldUse => true;

protected override void Start()
{
Console.Error.WriteLine(Resources.NO_HOSTING);
Environment.Exit(-1);
}
public override void Start() => throw new Exception(Resources.NO_HOSTING);

protected override void Stop()
public override void Stop()
{
}
}
Expand Down
4 changes: 2 additions & 2 deletions SRC/RPC.Server/Private/Hosting/InstallHostRunner_WinNT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public InstallHostRunner_WinNT(IHost host): base(host)
static bool ArgSet(string name) => Environment.GetCommandLineArgs().Any(arg => arg.ToLower(Resources.Culture) == name);
}

protected override void Start()
public override void Start()
{
if (Install)
{
Expand All @@ -74,7 +74,7 @@ protected override void Start()
string GetSafeServiceName() => Host.Name.Replace(' ', '_');
}

protected override void Stop()
public override void Stop()
{
}

Expand Down
4 changes: 2 additions & 2 deletions SRC/RPC.Server/Private/Hosting/ServiceHostRunner_WinNT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ public ServiceHostRunner_WinNT(IHost host) : base(host)
FServiceImpl = new ServiceImpl(host);
}

protected override void Start() =>
public override void Start() =>
//
// Blokkolodik
//

ServiceBase.Run(FServiceImpl ?? throw new PlatformNotSupportedException());

protected override void Stop()
public override void Stop()
{
if (FServiceImpl == null) throw new PlatformNotSupportedException();
FServiceImpl.Stop();
Expand Down
2 changes: 1 addition & 1 deletion SRC/RPC.Server/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion SRC/RPC.Server/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
<value>The module must be an interface.</value>
</data>
<data name="NO_HOSTING" xml:space="preserve">
<value>Compatible hosting not found.</value>
<value>No compatible hosting found.</value>
</data>
<data name="NO_METHOD" xml:space="preserve">
<value>Request must specify a method.</value>
Expand Down
4 changes: 2 additions & 2 deletions SRC/RPC.Server/Public/Hosting/AppHostBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public abstract class AppHostBase: Disposable, IHost
/// Creates a new instance.
/// </summary>
protected AppHostBase()
{
Runner = HostRunner.GetFor(this);
{
FContainer = new ServiceContainer();
FRpcService = new RpcService(FContainer);
Runner = HostRunner.GetFor(this);
}

/// <summary>
Expand Down
23 changes: 2 additions & 21 deletions SRC/RPC.Server/Public/Hosting/HostRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,16 @@ public abstract class HostRunner: Disposable, IHostRunner
/// </summary>
public abstract bool ShouldUse { get; }

/// <summary>
/// See <see cref="IHostRunner.IsStarted"/>.
/// </summary>
public bool IsStarted { get; private set; }

void IHostRunner.Start()
{
if (IsStarted) throw new InvalidOperationException();
Start();
IsStarted = true;
}

void IHostRunner.Stop()
{
if (!IsStarted) throw new InvalidOperationException();
Stop();
IsStarted = false;
}

/// <summary>
/// Starts the host.
/// </summary>
protected abstract void Start();
public abstract void Start();

/// <summary>
/// Stops the host.
/// </summary>
[SuppressMessage("Naming", "CA1716:Identifiers should not match keywords")]
protected abstract void Stop();
public abstract void Stop();

//
// A legutoljara regisztralt futtatot vizsgaljuk eloszor kompatibilitasi szempontbol.
Expand Down
5 changes: 2 additions & 3 deletions SRC/RPC.Server/Public/Hosting/IHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
* *
* Author: Denes Solti *
********************************************************************************/
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading;

namespace Solti.Utils.Rpc.Hosting
{
/// <summary>
/// Represents an abstract service host.
/// </summary>
public interface IHost
public interface IHost: IDisposable
{
/// <summary>
/// The name of the host.
Expand Down
5 changes: 0 additions & 5 deletions SRC/RPC.Server/Public/Hosting/IHostRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ public interface IHostRunner: IDisposable
/// </summary>
IHost Host { get; }

/// <summary>
/// Indicates whether the runner was started.
/// </summary>
bool IsStarted { get; }

/// <summary>
/// Starts the host.
/// </summary>
Expand Down
76 changes: 76 additions & 0 deletions TEST/HostRunners/ConsoleHostRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/********************************************************************************
* ConsoleHostRunner.cs *
* *
* Author: Denes Solti *
********************************************************************************/
using System;
using System.Threading;
using System.Threading.Tasks;

using NUnit.Framework;

namespace Solti.Utils.Rpc.Hosting.Tests
{
using Internals;

[TestFixture]
public class ConsoleHostRunnerTests
{
private class BadDependencyAppHost : AppHostBase
{
public override string Name => throw new System.NotImplementedException();

public override string Url => throw new System.NotImplementedException();

public BadDependencyAppHost() : base()
{
Dependencies.Add("invalid");
}
}

[Test]
public void Start_ShouldValidateTheDependencies() => Assert.Throws<Exception>(() =>
{
using IHost appHost = new BadDependencyAppHost();
using IHostRunner hostRunner = new ConsoleHostRunner(appHost);
hostRunner.Start();
});

private class ConsoleAppHost : AppHostBase
{
public override string Name => nameof(ConsoleAppHost);

public override string Url => "http://127.0.0.1:1986/api/";

public ManualResetEventSlim Started { get; } = new ManualResetEventSlim();

public override void OnStart()
{
base.OnStart();
Started.Set();
}

protected override void Dispose(bool disposeManaged)
{
if (disposeManaged)
Started.Dispose();

base.Dispose(disposeManaged);
}
}

[Test]
public void Runner_ShouldTerminateOnCtrlC()
{
using ConsoleAppHost appHost = new ConsoleAppHost();
using ConsoleHostRunner hostRunner = new ConsoleHostRunner(appHost);

Task t = Task.Factory.StartNew(((IHostRunner) hostRunner).Start);
Assert.That(appHost.Started.Wait(TimeSpan.FromSeconds(1)));

hostRunner.OnConsoleCancel(null, null);

Assert.That(t.Wait(TimeSpan.FromSeconds(1)));
}
}
}

0 comments on commit 80608d6

Please sign in to comment.