Skip to content

Commit

Permalink
Merge pull request #171 from gigya/feature/ServiceArgumentsRefactor
Browse files Browse the repository at this point in the history
change onStopWaitTime And ServiceDrarinTime format
  • Loading branch information
eran authored Jun 12, 2018
2 parents 268aa01 + fb60480 commit 3496ed2
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 40 deletions.
4 changes: 3 additions & 1 deletion Gigya.Microdot.Hosting/Service/ServiceHostBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ public void Run(ServiceArguments argumentsOverride = null)
StopEvent.WaitOne();

Console.WriteLine(" *** Shutting down... *** ");
Task.Run(() => OnStop()).Wait(Arguments.OnStopWaitTime ?? TimeSpan.FromSeconds(10));


Task.Run(() => OnStop()).Wait(TimeSpan.FromSeconds(Arguments.OnStopWaitTimeSec + Arguments.ServiceDrainTimeSec ?? 0));

ServiceStartedEvent = new TaskCompletionSource<object>();
ServiceStoppedEvent.SetResult(null);
Expand Down
18 changes: 11 additions & 7 deletions Gigya.Microdot.SharedLogic/ServiceArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ public class ServiceArguments
/// Specifies drain time in this time the servcie status will be 521.
/// </summary>
public int? ServiceDrainTimeSec { get; }

/// <summary>
/// Defines wait time before service is stoping, default is 10 seconds.
/// Defines the time to wait for the service to stop, default is 10 seconds. Only after OnStopWaitTimeSec+ServiceDrainTimeSec the service will be forcibly closed.
/// </summary>
public TimeSpan? OnStopWaitTime { get; }
public int? OnStopWaitTimeSec { get; set; }

/// <summary>
/// An array of processor IDs the service should run on, otherwise null if none are is specified. This also affects the degree
Expand All @@ -98,7 +99,7 @@ public ServiceArguments(ServiceStartupMode serviceStartupMode = ServiceStartupMo
ConsoleOutputMode consoleOutputMode = ConsoleOutputMode.Unspecified,
SiloClusterMode siloClusterMode = SiloClusterMode.Unspecified,
int? basePortOverride = null, string instanceName = null,
int? shutdownWhenPidExits = null, int? slotNumber = null, TimeSpan? onStopWaitTimeInMs=null,int? serviceDrainTime=null)
int? shutdownWhenPidExits = null, int? slotNumber = null, int? shutdownWaitTimeSec=null,int? serviceDrainTimeSec=null)
{
ServiceStartupMode = serviceStartupMode;
ConsoleOutputMode = consoleOutputMode;
Expand All @@ -107,8 +108,8 @@ public ServiceArguments(ServiceStartupMode serviceStartupMode = ServiceStartupMo
InstanceName = instanceName;
ShutdownWhenPidExits = shutdownWhenPidExits;
SlotNumber = slotNumber;
OnStopWaitTime = onStopWaitTimeInMs;
ServiceDrainTimeSec = serviceDrainTime;
OnStopWaitTimeSec = shutdownWaitTimeSec;
ServiceDrainTimeSec = serviceDrainTimeSec;
ApplyDefaults();
}

Expand All @@ -126,13 +127,12 @@ public ServiceArguments(string[] args)
InstanceName = ParseStringArg(nameof(InstanceName), args);
ShutdownWhenPidExits = TryParseInt(ParseStringArg(nameof(ShutdownWhenPidExits), args));
SlotNumber = TryParseInt(ParseStringArg(nameof(SlotNumber), args));
OnStopWaitTime = TryParseTimeSpan(ParseStringArg(nameof(OnStopWaitTime), args));
OnStopWaitTimeSec = TryParseInt(ParseStringArg(nameof(OnStopWaitTimeSec), args));
ServiceDrainTimeSec = TryParseInt(ParseStringArg(nameof(ServiceDrainTimeSec), args));
ProcessorAffinity = ParseProcessorIds(ParseStringArg(nameof(ProcessorAffinity), args));
ApplyDefaults();
}

private static TimeSpan? TryParseTimeSpan(string str) { return TimeSpan.TryParse(str, out TimeSpan val) ? (TimeSpan?)val : null; }

private static int? TryParseInt(string str) { return int.TryParse(str, out int val) ? (int?)val : null; }

Expand Down Expand Up @@ -201,6 +201,10 @@ private void ApplyDefaults()
throw new ArgumentOutOfRangeException();
}
}

if (OnStopWaitTimeSec == null)
OnStopWaitTimeSec = 10;

// ReSharper restore SwitchStatementMissingSomeCases
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace Gigya.Microdot.Testing.Shared.Service

public NonOrleansServiceTester(int basePortOverride, IResolutionRoot resolutionRoot, TimeSpan? shutdownWaitTime = null)
{
var serviceArguments = GetServiceArguments(basePortOverride, false, shutdownWaitTime);
var serviceArguments = GetServiceArguments(basePortOverride, false, shutdownWaitTime.HasValue?(int?)shutdownWaitTime.Value.TotalSeconds:null);

BasePort = basePortOverride;
ResolutionRoot = resolutionRoot;
Expand Down
8 changes: 4 additions & 4 deletions Gigya.Microdot.Testing.Shared/Service/ServiceTesterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,23 @@ public virtual ServiceProxyProvider GetServiceProxyProvider(string serviceName,
return provider;
}

protected virtual ServiceArguments GetServiceArguments(int? basePortOverride, bool isSecondary, TimeSpan? shutdownWaitTime)
protected virtual ServiceArguments GetServiceArguments(int? basePortOverride, bool isSecondary, int? shutdownWaitTime)
{
if (isSecondary && basePortOverride == null)
throw new ArgumentException("You must specify a basePortOverride when running a secondary silo.");

var siloClusterMode = isSecondary ? SiloClusterMode.SecondaryNode : SiloClusterMode.PrimaryNode;
ServiceArguments arguments = new ServiceArguments(ServiceStartupMode.CommandLineNonInteractive, basePortOverride: basePortOverride, siloClusterMode: siloClusterMode, onStopWaitTimeInMs: shutdownWaitTime);
ServiceArguments arguments = new ServiceArguments(ServiceStartupMode.CommandLineNonInteractive, basePortOverride: basePortOverride, siloClusterMode: siloClusterMode, shutdownWaitTimeSec: shutdownWaitTime);

if (basePortOverride != null)
return arguments;

var serviceArguments = new ServiceArguments(ServiceStartupMode.CommandLineNonInteractive, siloClusterMode: siloClusterMode, onStopWaitTimeInMs: shutdownWaitTime);
var serviceArguments = new ServiceArguments(ServiceStartupMode.CommandLineNonInteractive, siloClusterMode: siloClusterMode, shutdownWaitTimeSec: shutdownWaitTime);
var commonConfig = new BaseCommonConfig(serviceArguments);
var mapper = new OrleansServiceInterfaceMapper(new AssemblyProvider(new ApplicationDirectoryProvider(commonConfig), commonConfig, Log));
var basePort = mapper.ServiceInterfaceTypes.First().GetCustomAttribute<HttpServiceAttribute>().BasePort;

return new ServiceArguments(ServiceStartupMode.CommandLineNonInteractive, basePortOverride: basePort, onStopWaitTimeInMs: shutdownWaitTime);
return new ServiceArguments(ServiceStartupMode.CommandLineNonInteractive, basePortOverride: basePort, shutdownWaitTimeSec: shutdownWaitTime);
}

public abstract void Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public static NonOrleansServiceTester<TServiceHost> GetServiceTesterForNonOrlean
{
NonOrleansServiceTester<TServiceHost> tester = kernel.Get<NonOrleansServiceTester<TServiceHost>>(
new ConstructorArgument(nameof(basePortOverride), basePortOverride),
new ConstructorArgument(nameof(shutdownWaitTime), shutdownWaitTime)
);
new ConstructorArgument(nameof(shutdownWaitTime),shutdownWaitTime));

return tester;
}
Expand Down
16 changes: 8 additions & 8 deletions Gigya.Microdot.Testing/Service/ServiceTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ namespace Gigya.Microdot.Testing.Service

private HttpListener LogListener { get; set; }

public ServiceTester(int? basePortOverride, bool isSecondary, ILog log, IResolutionRoot resolutionRoot, TimeSpan? shutdownWaitTime = null, bool writeLogToFile = false,int? serviceDrainTime=null)
public ServiceTester(int? basePortOverride, bool isSecondary, ILog log, IResolutionRoot resolutionRoot, TimeSpan? shutdownWaitTime = null, bool writeLogToFile = false,int? serviceDrainTimeSec=null)
{
Log = log;
ResolutionRoot = resolutionRoot;
// ReSharper disable VirtualMemberCallInContructor
InitializeInfrastructure();

var serviceArguments = GetServiceArguments(basePortOverride, isSecondary, shutdownWaitTime,serviceDrainTime);
var serviceArguments = GetServiceArguments(basePortOverride, isSecondary, shutdownWaitTime.HasValue?(int?)shutdownWaitTime.Value.TotalSeconds:null,serviceDrainTimeSec);

BasePort = serviceArguments.BasePortOverride.Value;
ServiceAppDomain = Common.CreateDomain(typeof(TServiceHost).Name + BasePort);
Expand Down Expand Up @@ -202,23 +202,23 @@ protected virtual void InitializeInfrastructure()
}


protected virtual ServiceArguments GetServiceArguments(int? basePortOverride, bool isSecondary, TimeSpan? shutdownWaitTime,int? serviceDrainTime)
protected virtual ServiceArguments GetServiceArguments(int? basePortOverride, bool isSecondary, int? shutdownWaitTime,int? serviceDrainTime)
{
if (isSecondary && basePortOverride == null)
throw new ArgumentException("You must specify a basePortOverride when running a secondary silo.");

var siloClusterMode = isSecondary ? SiloClusterMode.SecondaryNode : SiloClusterMode.PrimaryNode;
ServiceArguments arguments = new ServiceArguments(ServiceStartupMode.CommandLineNonInteractive, basePortOverride: basePortOverride, siloClusterMode: siloClusterMode, onStopWaitTimeInMs: shutdownWaitTime,serviceDrainTime:serviceDrainTime);
ServiceArguments arguments = new ServiceArguments(ServiceStartupMode.CommandLineNonInteractive, basePortOverride: basePortOverride, siloClusterMode: siloClusterMode, shutdownWaitTimeSec: shutdownWaitTime,serviceDrainTimeSec:serviceDrainTime);

if (basePortOverride != null)
return arguments;

var serviceArguments = new ServiceArguments(ServiceStartupMode.CommandLineNonInteractive, siloClusterMode: siloClusterMode, onStopWaitTimeInMs: shutdownWaitTime,serviceDrainTime:serviceDrainTime);
var serviceArguments = new ServiceArguments(ServiceStartupMode.CommandLineNonInteractive, siloClusterMode: siloClusterMode, shutdownWaitTimeSec: shutdownWaitTime,serviceDrainTimeSec:serviceDrainTime);
var commonConfig = new BaseCommonConfig(serviceArguments);
var mapper = new OrleansServiceInterfaceMapper(new AssemblyProvider(new ApplicationDirectoryProvider(commonConfig), commonConfig, Log));
var basePort = mapper.ServiceInterfaceTypes.First().GetCustomAttribute<HttpServiceAttribute>().BasePort;

return new ServiceArguments(ServiceStartupMode.CommandLineNonInteractive, basePortOverride: basePort, onStopWaitTimeInMs: shutdownWaitTime,serviceDrainTime:serviceDrainTime);
return new ServiceArguments(ServiceStartupMode.CommandLineNonInteractive, basePortOverride: basePort, shutdownWaitTimeSec: shutdownWaitTime,serviceDrainTimeSec:serviceDrainTime);
}


Expand Down Expand Up @@ -315,15 +315,15 @@ public class TestTraceConfiguration : ITraceConfiguration

public static class ServiceTesterExtensions
{
public static ServiceTester<TServiceHost> GetServiceTester<TServiceHost>(this IResolutionRoot kernel, int? basePortOverride = null, bool isSecondary = false, TimeSpan? shutdownWaitTime = null, bool writeLogToFile = false,int? serviceDrainTime=null)
public static ServiceTester<TServiceHost> GetServiceTester<TServiceHost>(this IResolutionRoot kernel, int? basePortOverride = null, bool isSecondary = false, TimeSpan? shutdownWaitTime = null, bool writeLogToFile = false,int? serviceDrainTimeSec=null)
where TServiceHost : MicrodotOrleansServiceHost, new()
{
ServiceTester<TServiceHost> tester = kernel.Get<ServiceTester<TServiceHost>>(
new ConstructorArgument(nameof(basePortOverride), basePortOverride),
new ConstructorArgument(nameof(isSecondary), isSecondary),
new ConstructorArgument(nameof(shutdownWaitTime), shutdownWaitTime),
new ConstructorArgument(nameof(writeLogToFile), writeLogToFile),
new ConstructorArgument(nameof(serviceDrainTime), serviceDrainTime)
new ConstructorArgument(nameof(serviceDrainTimeSec), serviceDrainTimeSec)
);

return tester;
Expand Down
6 changes: 3 additions & 3 deletions SolutionVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
[assembly: AssemblyCopyright("© 2018 Gigya Inc.")]
[assembly: AssemblyDescription("Microdot Framework")]

[assembly: AssemblyVersion("1.10.3.0")]
[assembly: AssemblyFileVersion("1.10.3.0")]
[assembly: AssemblyInformationalVersion("1.10.3.0")]
[assembly: AssemblyVersion("1.10.4.0")]
[assembly: AssemblyFileVersion("1.10.4.0")]
[assembly: AssemblyInformationalVersion("1.10.4.0")]


// Setting ComVisible to false makes the types in this assembly not visible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,20 @@ public class MicroServiceTests
[Test]
public async Task ShouldCallSelfHostServcie()
{
NonOrleansServiceTester<CalculatorServiceHost> serviceTester = null;
var testingKernel = new TestingKernel<TraceLog>();
var serviceTester = testingKernel.GetServiceTesterForNonOrleansService<CalculatorServiceHost>(1111, TimeSpan.FromSeconds(10));
try
{
serviceTester = testingKernel.GetServiceTesterForNonOrleansService<CalculatorServiceHost>(1111, TimeSpan.FromSeconds(10));
(await serviceTester.GetServiceProxy<ICalculatorService>().Add(1, 2)).ShouldBe(3);
}
finally
{
serviceTester?.Dispose();
testingKernel.Dispose();
}


(await serviceTester.GetServiceProxy<ICalculatorService>().Add(1, 2)).ShouldBe(3);
serviceTester.Dispose();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public class HealthCheckTests
{
private ServiceTester<CalculatorServiceHost> tester;


private int mainPort = 9555;
[OneTimeSetUp]
public void SetUp()
{
tester = AssemblyInitialize.ResolutionRoot.GetServiceTester<CalculatorServiceHost>();
tester = AssemblyInitialize.ResolutionRoot.GetServiceTester<CalculatorServiceHost>(mainPort);
}

[OneTimeTearDown]
Expand All @@ -55,7 +55,7 @@ public void TearDown()
public async Task HealthCheck_ServcieDrain_StatueShouldBe521()
{
int port = 6755;//prevent prot collision, more then one silo is runing at the same time in this TestFixture.
var customServiceTester = AssemblyInitialize.ResolutionRoot.GetServiceTester<CalculatorServiceHost>(basePortOverride: port, serviceDrainTime: 10);
var customServiceTester = AssemblyInitialize.ResolutionRoot.GetServiceTester<CalculatorServiceHost>(basePortOverride: port, serviceDrainTimeSec: 10);

var dispose = Task.Run(() => customServiceTester.Dispose());
await Task.Delay(200);
Expand All @@ -69,22 +69,22 @@ public async Task HealthCheck_ServcieDrain_StatueShouldBe521()
public void HealthCheck_NotHealthy_ShouldReturn500()
{
tester.GetGrainClient<IProgrammableHealthGrain>(0).SetHealth(false);
var httpResponseMessage = new HttpClient().GetAsync(new Uri($"http://{CurrentApplicationInfo.HostName}:6555/{nameof(IProgrammableHealth).Substring(1)}.status")).Result;
var httpResponseMessage = new HttpClient().GetAsync(new Uri($"http://{CurrentApplicationInfo.HostName}:{mainPort}/{nameof(IProgrammableHealth).Substring(1)}.status")).Result;
httpResponseMessage.StatusCode.ShouldBe(HttpStatusCode.InternalServerError);
}

[Test]
public void HealthCheck_Healthy_ShouldReturn200()
{
tester.GetGrainClient<IProgrammableHealthGrain>(0).SetHealth(true);
var httpResponseMessage = new HttpClient().GetAsync(new Uri($"http://{CurrentApplicationInfo.HostName}:6555/{nameof(IProgrammableHealth).Substring(1)}.status")).Result;
var httpResponseMessage = new HttpClient().GetAsync(new Uri($"http://{CurrentApplicationInfo.HostName}:{mainPort}/{nameof(IProgrammableHealth).Substring(1)}.status")).Result;
httpResponseMessage.StatusCode.ShouldBe(HttpStatusCode.OK);
}

[Test]
public void HealthCheck_NotImplemented_ShouldReturn200()
{
var httpResponseMessage = new HttpClient().GetAsync(new Uri($"http://{CurrentApplicationInfo.HostName}:6555/{nameof(ICalculatorService).Substring(1)}.status")).Result;
var httpResponseMessage = new HttpClient().GetAsync(new Uri($"http://{CurrentApplicationInfo.HostName}:{mainPort}/{nameof(ICalculatorService).Substring(1)}.status")).Result;
httpResponseMessage.StatusCode.ShouldBe(HttpStatusCode.OK);
httpResponseMessage.Content.ShouldNotBeNull();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void SetUp()
{
try
{
_tester = AssemblyInitialize.ResolutionRoot.GetServiceTester<CalculatorServiceHost>();
_tester = AssemblyInitialize.ResolutionRoot.GetServiceTester<CalculatorServiceHost>(8555);
_serviceProxyProvider = _tester.GetServiceProxyProvider("CalculatorService");
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Threading.Tasks;
using Gigya.Microdot.Hosting.Service;
using Gigya.Microdot.SharedLogic;
using Ninject;
using NUnit.Framework;
using Shouldly;
Expand Down Expand Up @@ -37,7 +38,7 @@ public void SetUp()
try
{
Host = new SlowServiceHost(k => { Service = k.Get<ISlowService>(); });
StopTask = Host.RunAsync();
StopTask = Host.RunAsync(new ServiceArguments(ServiceStartupMode.CommandLineNonInteractive));
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Gigya.Common.Contracts.Exceptions;
using Gigya.Microdot.Fakes;
using Gigya.Microdot.Hosting.Service;
using Gigya.Microdot.SharedLogic;
using Gigya.Microdot.SharedLogic.Events;
using Gigya.Microdot.SharedLogic.Exceptions;
using Gigya.Microdot.Testing;
Expand Down Expand Up @@ -54,7 +55,7 @@ public virtual void SetUp()
TracingContext.SetRequestID("1");

_testinghost = new TestingHost<IDemoService>();
_stopTask = _testinghost.RunAsync();
_stopTask = _testinghost.RunAsync(new ServiceArguments(ServiceStartupMode.CommandLineNonInteractive));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void TearDown()
public void TestMetricsOnSuccess()
{
TestingHost<IDemoService> testinghost = new TestingHost<IDemoService>();
var task = testinghost.RunAsync();
var task = testinghost.RunAsync(new ServiceArguments(ServiceStartupMode.CommandLineNonInteractive));
testinghost.Instance.Increment(0).Returns((ulong)1);


Expand All @@ -73,7 +73,7 @@ public void TestMetricsOnSuccess()
public void TestMetricsOnFailure()
{
TestingHost<IDemoService> testinghost = new TestingHost<IDemoService>();
var task = testinghost.RunAsync();
var task = testinghost.RunAsync(new ServiceArguments(ServiceStartupMode.CommandLineNonInteractive));

testinghost.Instance.When(a => a.DoSomething()).Do(x => { throw new Exception(); });

Expand Down

0 comments on commit 3496ed2

Please sign in to comment.