From d64ad7b798ef5f209a05c1813d7788b633426c01 Mon Sep 17 00:00:00 2001 From: Yevgeny Kaplan Date: Thu, 13 Dec 2018 16:59:50 +0200 Subject: [PATCH 1/9] Don't rebind already nimded configs --- Gigya.Microdot.Ninject/NinjectExtensions.cs | 8 +++++ .../SystemInitializer/SystemInitializer.cs | 33 ++++++++++++++----- .../CalculatorService/AgeLimitServiceHost.cs | 23 +++---------- .../ConsulDiscoveryMasterFallBackTest.cs | 14 ++++++-- .../Discovery/ConsulDiscoverySourceTest.cs | 3 +- 5 files changed, 51 insertions(+), 30 deletions(-) diff --git a/Gigya.Microdot.Ninject/NinjectExtensions.cs b/Gigya.Microdot.Ninject/NinjectExtensions.cs index 855495f5..fdc4dc69 100644 --- a/Gigya.Microdot.Ninject/NinjectExtensions.cs +++ b/Gigya.Microdot.Ninject/NinjectExtensions.cs @@ -22,8 +22,10 @@ using System; using System.Collections.Concurrent; +using System.Linq; using Ninject; using Ninject.Activation; +using Ninject.Planning.Bindings; namespace Gigya.Microdot.Ninject { @@ -167,6 +169,12 @@ public static void BindPerString(this IKernel kernel) kernel.BindPerString(); } + public static bool IsBinded(this IKernel kernel, Type serviceType) + { + IBinding binding = kernel.GetBindings(serviceType).FirstOrDefault(); + + return binding != null && binding.Target != BindingTarget.Provider; + } } public class DisposableConcurrentDictionary : ConcurrentDictionary, IDisposable diff --git a/Gigya.Microdot.Ninject/SystemInitializer/SystemInitializer.cs b/Gigya.Microdot.Ninject/SystemInitializer/SystemInitializer.cs index 528d7b42..15cddd30 100644 --- a/Gigya.Microdot.Ninject/SystemInitializer/SystemInitializer.cs +++ b/Gigya.Microdot.Ninject/SystemInitializer/SystemInitializer.cs @@ -18,18 +18,21 @@ // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. -#endregion +#endregion using System; using System.Linq; +using System.Linq.Expressions; using System.Net; using System.Reflection; using System.Threading.Tasks.Dataflow; using Gigya.Microdot.Configuration; using Gigya.Microdot.Configuration.Objects; +using Gigya.Microdot.Hosting.Metrics; using Gigya.Microdot.Hosting.Validators; using Gigya.Microdot.Interfaces; using Ninject; +using Ninject.Planning.Bindings; namespace Gigya.Microdot.Ninject.SystemInitializer { @@ -64,17 +67,29 @@ private void SearchAssembliesAndRebindIConfig() { IConfigObjectCreator configObjectCreator = _kernel.Get>()(configType); - dynamic getLatestLambda = configObjectCreator.GetLambdaOfGetLatest(configType); - _kernel.Rebind(typeof(Func<>).MakeGenericType(configType)).ToMethod(t => getLatestLambda()); + if (!_kernel.IsBinded(typeof(Func<>).MakeGenericType(configType))) + { + dynamic getLatestLambda = configObjectCreator.GetLambdaOfGetLatest(configType); + _kernel.Rebind(typeof(Func<>).MakeGenericType(configType)).ToMethod(t => getLatestLambda()); + } - Type sourceBlockType = typeof(ISourceBlock<>).MakeGenericType(configType); - _kernel.Rebind(sourceBlockType).ToMethod(t => configObjectCreator.ChangeNotifications); + if (!_kernel.IsBinded(typeof(ISourceBlock<>).MakeGenericType(configType))) + { + Type sourceBlockType = typeof(ISourceBlock<>).MakeGenericType(configType); + _kernel.Rebind(sourceBlockType).ToMethod(t => configObjectCreator.ChangeNotifications); - dynamic changeNotificationsLambda = configObjectCreator.GetLambdaOfChangeNotifications(sourceBlockType); - _kernel.Rebind(typeof(Func<>).MakeGenericType(sourceBlockType)).ToMethod(t => changeNotificationsLambda()); - - _kernel.Rebind(configType).ToMethod(t => configObjectCreator.GetLatest()); + if (!_kernel.IsBinded(typeof(Func<>).MakeGenericType(sourceBlockType))) + { + dynamic changeNotificationsLambda = configObjectCreator.GetLambdaOfChangeNotifications(sourceBlockType); + _kernel.Rebind(typeof(Func<>).MakeGenericType(sourceBlockType)).ToMethod(t => changeNotificationsLambda()); + } + } + if (!_kernel.IsBinded(configType)) + { + _kernel.Rebind(configType).ToMethod(t => configObjectCreator.GetLatest()); + } + _configObjectsCache.RegisterConfigObjectCreator(configObjectCreator); } } diff --git a/tests/Gigya.Microdot.Orleans.Hosting.UnitTests/Microservice/CalculatorService/AgeLimitServiceHost.cs b/tests/Gigya.Microdot.Orleans.Hosting.UnitTests/Microservice/CalculatorService/AgeLimitServiceHost.cs index 68029b4a..e3cad9ae 100644 --- a/tests/Gigya.Microdot.Orleans.Hosting.UnitTests/Microservice/CalculatorService/AgeLimitServiceHost.cs +++ b/tests/Gigya.Microdot.Orleans.Hosting.UnitTests/Microservice/CalculatorService/AgeLimitServiceHost.cs @@ -26,6 +26,7 @@ using Gigya.Microdot.Configuration; using Gigya.Microdot.Fakes; using Ninject; +using Ninject.Syntax; using Shouldly; namespace Gigya.Microdot.Orleans.Hosting.UnitTests.Microservice.CalculatorService @@ -70,9 +71,11 @@ protected override void Configure(IKernel kernel, OrleansCodeConfig commonConfig public class WithInvalidAgeLimitServiceHost : CalculatorServiceHost { - protected override void Configure(IKernel kernel, OrleansCodeConfig commonConfig) + protected override void OnInitilize(IResolutionRoot resolutionRoot) { - var originConfig = kernel.Get(); + base.OnInitilize(resolutionRoot); + + var originConfig = resolutionRoot.Get(); originConfig.GrainAgeLimits = new Dictionary { [ServiceName] = new GrainAgeLimitConfig @@ -81,22 +84,6 @@ protected override void Configure(IKernel kernel, OrleansCodeConfig commonConfig GrainAgeLimitInMins = 10 } }; - - - kernel. Rebind() - .To() - .InSingletonScope(); - - kernel.Rebind() - .To() - .InSingletonScope() - .WithConstructorArgument("data", new Dictionary()); - - OverridableConfigItems configItems = kernel.Get() as OverridableConfigItems; - configItems.SetValue("OrleansConfig.GrainAgeLimits.SiteService.grainType", "Fake - Should throw an exception."); - configItems.SetValue("OrleansConfig.GrainAgeLimits.SiteService.grainAgeLimitInMins", "10"); - - base.Configure(kernel, commonConfig); } } diff --git a/tests/Gigya.Microdot.UnitTests/Discovery/ConsulDiscoveryMasterFallBackTest.cs b/tests/Gigya.Microdot.UnitTests/Discovery/ConsulDiscoveryMasterFallBackTest.cs index 621067f5..530d62fd 100644 --- a/tests/Gigya.Microdot.UnitTests/Discovery/ConsulDiscoveryMasterFallBackTest.cs +++ b/tests/Gigya.Microdot.UnitTests/Discovery/ConsulDiscoveryMasterFallBackTest.cs @@ -42,7 +42,7 @@ public class ConsulDiscoveryMasterFallBackTest [SetUp] public void SetUp() { - _unitTestingKernel?.Dispose(); + _serviceName = $"ServiceName{++id}"; _environment = Substitute.For(); @@ -68,6 +68,16 @@ public void SetUp() Assert.AreEqual(_environment, environment); } + [TearDown] + public void Teardown() + { + _unitTestingKernel?.Dispose(); + _configDic?.Clear(); + _configDic = null; + _configRefresh = null; + _consulClient?.Clear(); + } + private void SetupConsulClientMocks() { _consulClient = new Dictionary(); @@ -147,7 +157,7 @@ public async Task CreateServiceDiscoveyWithoutGetNextHostNoServiceHealthShouldAp public async Task ScopeZoneShouldUseServiceNameAsConsoleQuery() { _configDic[$"Discovery.Services.{_serviceName}.Scope"] = "Zone"; - _unitTestingKernel.Get().Init(); + _configRefresh.RaiseChangeEvent(); SetMockToReturnHost(_serviceName); var nextHost = GetServiceDiscovey().GetNextHost(); (await nextHost).HostName.ShouldBe(_serviceName); diff --git a/tests/Gigya.Microdot.UnitTests/Discovery/ConsulDiscoverySourceTest.cs b/tests/Gigya.Microdot.UnitTests/Discovery/ConsulDiscoverySourceTest.cs index e53e7a85..cced03cf 100644 --- a/tests/Gigya.Microdot.UnitTests/Discovery/ConsulDiscoverySourceTest.cs +++ b/tests/Gigya.Microdot.UnitTests/Discovery/ConsulDiscoverySourceTest.cs @@ -123,7 +123,8 @@ public async Task ServiceInEnvironmentScope() public async Task ServiceInZoneScope() { _configDic[$"Discovery.Services.{SERVICE_NAME}.Scope"] = "Zone"; - _unitTestingKernel.Get().Init(); + await _unitTestingKernel.Get().ApplyChanges(); + await GetFirstResult().ConfigureAwait(false); Assert.AreEqual($"{SERVICE_NAME}", _requestedConsulServiceName); } From 72e34af54e331f84955f4fb11aa4c42a6e3a0353 Mon Sep 17 00:00:00 2001 From: Yevgeny Kaplan Date: Thu, 13 Dec 2018 17:49:19 +0200 Subject: [PATCH 2/9] test fix --- .../Discovery/ConsulDiscoveryMasterFallBackTest.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/Gigya.Microdot.UnitTests/Discovery/ConsulDiscoveryMasterFallBackTest.cs b/tests/Gigya.Microdot.UnitTests/Discovery/ConsulDiscoveryMasterFallBackTest.cs index 530d62fd..33ead3c9 100644 --- a/tests/Gigya.Microdot.UnitTests/Discovery/ConsulDiscoveryMasterFallBackTest.cs +++ b/tests/Gigya.Microdot.UnitTests/Discovery/ConsulDiscoveryMasterFallBackTest.cs @@ -10,6 +10,7 @@ using Gigya.Microdot.Interfaces.SystemWrappers; using Gigya.Microdot.Ninject.SystemInitializer; using Gigya.Microdot.ServiceDiscovery; +using Gigya.Microdot.ServiceDiscovery.Config; using Gigya.Microdot.Testing; using Gigya.Microdot.Testing.Shared; using Gigya.Microdot.Testing.Shared.Utils; @@ -76,6 +77,7 @@ public void Teardown() _configDic = null; _configRefresh = null; _consulClient?.Clear(); + _consulClient = null; } private void SetupConsulClientMocks() @@ -156,8 +158,7 @@ public async Task CreateServiceDiscoveyWithoutGetNextHostNoServiceHealthShouldAp [Repeat(Repeat)] public async Task ScopeZoneShouldUseServiceNameAsConsoleQuery() { - _configDic[$"Discovery.Services.{_serviceName}.Scope"] = "Zone"; - _configRefresh.RaiseChangeEvent(); + _unitTestingKernel.Get>()().Services[_serviceName].Scope = ServiceScope.Zone; SetMockToReturnHost(_serviceName); var nextHost = GetServiceDiscovey().GetNextHost(); (await nextHost).HostName.ShouldBe(_serviceName); @@ -290,7 +291,7 @@ public async Task EndPointsChangedShouldFireConfigChange() _configDic[$"Discovery.Services.{_serviceName}.Source"] = "Config"; Task waitForChangeEvent = waitForEvents.WhenNextEventReceived(); - _configRefresh.RaiseChangeEvent(); + await _configRefresh.ApplyChanges(); await waitForChangeEvent; var host = await discovey.GetNextHost(); host.HostName.ShouldBe("localhost"); From fb60c654e734c8f57284edbf7f2009aac347ec9e Mon Sep 17 00:00:00 2001 From: Yevgeny Kaplan Date: Sun, 16 Dec 2018 11:56:56 +0200 Subject: [PATCH 3/9] minor fix --- .../SystemInitializer/SystemInitializer.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Gigya.Microdot.Ninject/SystemInitializer/SystemInitializer.cs b/Gigya.Microdot.Ninject/SystemInitializer/SystemInitializer.cs index 15cddd30..1f3a8582 100644 --- a/Gigya.Microdot.Ninject/SystemInitializer/SystemInitializer.cs +++ b/Gigya.Microdot.Ninject/SystemInitializer/SystemInitializer.cs @@ -73,16 +73,16 @@ private void SearchAssembliesAndRebindIConfig() _kernel.Rebind(typeof(Func<>).MakeGenericType(configType)).ToMethod(t => getLatestLambda()); } + Type sourceBlockType = typeof(ISourceBlock<>).MakeGenericType(configType); if (!_kernel.IsBinded(typeof(ISourceBlock<>).MakeGenericType(configType))) { - Type sourceBlockType = typeof(ISourceBlock<>).MakeGenericType(configType); _kernel.Rebind(sourceBlockType).ToMethod(t => configObjectCreator.ChangeNotifications); + } - if (!_kernel.IsBinded(typeof(Func<>).MakeGenericType(sourceBlockType))) - { - dynamic changeNotificationsLambda = configObjectCreator.GetLambdaOfChangeNotifications(sourceBlockType); - _kernel.Rebind(typeof(Func<>).MakeGenericType(sourceBlockType)).ToMethod(t => changeNotificationsLambda()); - } + if (!_kernel.IsBinded(typeof(Func<>).MakeGenericType(sourceBlockType))) + { + dynamic changeNotificationsLambda = configObjectCreator.GetLambdaOfChangeNotifications(sourceBlockType); + _kernel.Rebind(typeof(Func<>).MakeGenericType(sourceBlockType)).ToMethod(t => changeNotificationsLambda()); } if (!_kernel.IsBinded(configType)) From da93b737935581efa38cf6bd4f9e615e97da4a3d Mon Sep 17 00:00:00 2001 From: Yevgeny Kaplan Date: Sun, 16 Dec 2018 12:34:13 +0200 Subject: [PATCH 4/9] minor fix --- Gigya.Microdot.Ninject/SystemInitializer/SystemInitializer.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Gigya.Microdot.Ninject/SystemInitializer/SystemInitializer.cs b/Gigya.Microdot.Ninject/SystemInitializer/SystemInitializer.cs index 1f3a8582..07a1e9e7 100644 --- a/Gigya.Microdot.Ninject/SystemInitializer/SystemInitializer.cs +++ b/Gigya.Microdot.Ninject/SystemInitializer/SystemInitializer.cs @@ -112,6 +112,10 @@ private void SetServicePointManagerDefaultValues(ServicePointManagerDefaultConfi public virtual void Dispose() { + if (_configSource == null) + { + return; + } _configSource.Complete(); } } From 7d804a2e5d1f64bc2bd208c4f33c6fd3574a9b88 Mon Sep 17 00:00:00 2001 From: "eran.of" Date: Mon, 17 Dec 2018 16:32:17 +0200 Subject: [PATCH 5/9] host override backward compatibility --- .../HostManagement/RemoteHostPool.cs | 4 +- .../MultiEnvironmentServiceDiscovery.cs | 2 +- .../Rewrite/ServiceProxyProvider.cs | 2 +- .../Events/TracingContext.cs | 2 +- .../HttpService/RequestOverrides.cs | 2 +- .../AdditionalDataSerializationTests.cs | 6 +- .../ServiceProxyTests/BehaviorTests.cs | 107 ++++++++++++++---- 7 files changed, 95 insertions(+), 30 deletions(-) diff --git a/Gigya.Microdot.ServiceDiscovery/HostManagement/RemoteHostPool.cs b/Gigya.Microdot.ServiceDiscovery/HostManagement/RemoteHostPool.cs index eb9374db..87414f67 100644 --- a/Gigya.Microdot.ServiceDiscovery/HostManagement/RemoteHostPool.cs +++ b/Gigya.Microdot.ServiceDiscovery/HostManagement/RemoteHostPool.cs @@ -261,7 +261,7 @@ public IEndPointHandle GetNextHost(string affinityToken = null) var hostOverride = TracingContext.GetHostOverride(DeploymentIdentifier.ServiceName); if (hostOverride != null) - return new OverriddenRemoteHost(DeploymentIdentifier.ServiceName, hostOverride.Hostname, hostOverride.Port?? GetConfig().DefaultPort); + return new OverriddenRemoteHost(DeploymentIdentifier.ServiceName, hostOverride.Host, hostOverride.Port?? GetConfig().DefaultPort); lock (_lock) { @@ -288,7 +288,7 @@ public async Task GetOrWaitForNextHost(CancellationToken cancel var hostOverride = TracingContext.GetHostOverride(DeploymentIdentifier.ServiceName); if (hostOverride != null) - return new OverriddenRemoteHost(DeploymentIdentifier.ServiceName, hostOverride.Hostname, hostOverride.Port ?? GetConfig().DefaultPort); + return new OverriddenRemoteHost(DeploymentIdentifier.ServiceName, hostOverride.Host, hostOverride.Port ?? GetConfig().DefaultPort); if (ReachableHosts.Count > 0) return GetNextHost(); diff --git a/Gigya.Microdot.ServiceDiscovery/Rewrite/MultiEnvironmentServiceDiscovery.cs b/Gigya.Microdot.ServiceDiscovery/Rewrite/MultiEnvironmentServiceDiscovery.cs index f9d73e33..a953974e 100644 --- a/Gigya.Microdot.ServiceDiscovery/Rewrite/MultiEnvironmentServiceDiscovery.cs +++ b/Gigya.Microdot.ServiceDiscovery/Rewrite/MultiEnvironmentServiceDiscovery.cs @@ -87,7 +87,7 @@ public async Task GetNode() var hostOverride = TracingContext.GetHostOverride(ServiceName); if (hostOverride != null) return new NodeAndLoadBalancer { - Node = new Node(hostOverride.Hostname, hostOverride.Port), + Node = new Node(hostOverride.Host, hostOverride.Port), LoadBalancer = null, PreferredEnvironment = preferredEnvironment ?? Environment.DeploymentEnvironment, }; diff --git a/Gigya.Microdot.ServiceProxy/Rewrite/ServiceProxyProvider.cs b/Gigya.Microdot.ServiceProxy/Rewrite/ServiceProxyProvider.cs index 378f62fc..8e751cfa 100644 --- a/Gigya.Microdot.ServiceProxy/Rewrite/ServiceProxyProvider.cs +++ b/Gigya.Microdot.ServiceProxy/Rewrite/ServiceProxyProvider.cs @@ -73,7 +73,7 @@ public async Task Invoke(HttpServiceRequest request, Type resultReturnTy private HttpRequestMessage CreateHttpRequest(HttpServiceRequest request, JsonSerializerSettings jsonSettings, HostOverride node) { - string uri = $"{(HttpSettings.UseHttps ? "https" : "http")}://{node.Hostname}:{node.Port ?? HttpSettings.BasePort}/{ServiceName}.{request.Target.MethodName}"; + string uri = $"{(HttpSettings.UseHttps ? "https" : "http")}://{node.Host}:{node.Port ?? HttpSettings.BasePort}/{ServiceName}.{request.Target.MethodName}"; return new HttpRequestMessage(HttpMethod.Post, uri) { diff --git a/Gigya.Microdot.SharedLogic/Events/TracingContext.cs b/Gigya.Microdot.SharedLogic/Events/TracingContext.cs index 6933413d..fcdb3a61 100644 --- a/Gigya.Microdot.SharedLogic/Events/TracingContext.cs +++ b/Gigya.Microdot.SharedLogic/Events/TracingContext.cs @@ -105,7 +105,7 @@ public static void SetHostOverride(string serviceName, string host, int? port=nu overrides.Hosts.Add(hostOverride); } - hostOverride.Hostname = host; + hostOverride.Host = host; hostOverride.Port = port; } diff --git a/Gigya.Microdot.SharedLogic/HttpService/RequestOverrides.cs b/Gigya.Microdot.SharedLogic/HttpService/RequestOverrides.cs index 291d62d2..7a2f12f1 100644 --- a/Gigya.Microdot.SharedLogic/HttpService/RequestOverrides.cs +++ b/Gigya.Microdot.SharedLogic/HttpService/RequestOverrides.cs @@ -52,7 +52,7 @@ public class HostOverride : ExtendableJson [JsonRequired] [JsonProperty] - public string Hostname { get; set; } + public string Host { get; set; } [JsonProperty] public int? Port { get; set; } diff --git a/tests/Gigya.Microdot.UnitTests/AdditionalDataSerializationTests.cs b/tests/Gigya.Microdot.UnitTests/AdditionalDataSerializationTests.cs index 0540e302..4541ed5e 100644 --- a/tests/Gigya.Microdot.UnitTests/AdditionalDataSerializationTests.cs +++ b/tests/Gigya.Microdot.UnitTests/AdditionalDataSerializationTests.cs @@ -70,7 +70,7 @@ public void HttpServiceRequestShouldContainAdditionaPropertiesRecursively() }; RequestOverrides requestOverrides = new RequestOverrides { - Hosts = new[] { new HostOverride { ServiceName = "Service1", Hostname = "HostNameOverride" } }.ToList() + Hosts = new[] { new HostOverride { ServiceName = "Service1", Host = "HostNameOverride" } }.ToList() }; InvocationTarget invocationTarget = new InvocationTarget{MethodName = "MethodName1" }; @@ -102,7 +102,7 @@ private void AssertShouldBeNull(HttpServiceRequest serviceRequestResult) serviceRequestResult.TracingData.SpanID.ShouldBe("SpanID1"); serviceRequestResult.TracingData.AdditionalProperties.ShouldBeNull(); - serviceRequestResult.Overrides.Hosts[0].Hostname.ShouldBe("HostNameOverride"); + serviceRequestResult.Overrides.Hosts[0].Host.ShouldBe("HostNameOverride"); serviceRequestResult.Overrides.Hosts[0].AdditionalProperties.ShouldBeNull(); serviceRequestResult.Overrides.AdditionalProperties.ShouldBeNull(); @@ -119,7 +119,7 @@ private void AssertShouldNotBeNull(HttpServiceRequest serviceRequestResult) serviceRequestResult.TracingData.AdditionalProperties.Count.ShouldBe(1); serviceRequestResult.TracingData.AdditionalProperties.ShouldContainKeyAndValue("TracingData", "TracingData1"); - serviceRequestResult.Overrides.Hosts[0].Hostname.ShouldBe("HostNameOverride"); + serviceRequestResult.Overrides.Hosts[0].Host.ShouldBe("HostNameOverride"); serviceRequestResult.Overrides.Hosts[0].AdditionalProperties.Count.ShouldBe(1); serviceRequestResult.Overrides.Hosts[0].AdditionalProperties.ShouldContainKeyAndValue("HostOverrideData", "HostOverrideData1"); serviceRequestResult.Overrides.AdditionalProperties.Count.ShouldBe(1); diff --git a/tests/Gigya.Microdot.UnitTests/ServiceProxyTests/BehaviorTests.cs b/tests/Gigya.Microdot.UnitTests/ServiceProxyTests/BehaviorTests.cs index e956673c..e2918a52 100644 --- a/tests/Gigya.Microdot.UnitTests/ServiceProxyTests/BehaviorTests.cs +++ b/tests/Gigya.Microdot.UnitTests/ServiceProxyTests/BehaviorTests.cs @@ -88,39 +88,104 @@ public async Task RequestContextShouldOverridePortAndHost() {$"Discovery.Services.{serviceName}.DefaultPort", defaultPort.ToString()} }; - using (var kernel = - new TestingKernel( - k => k.Rebind().To().InSingletonScope(), dict)) + using (var kernel = new TestingKernel(k => k.Rebind().To().InSingletonScope(), dict)) { - var providerFactory = kernel.Get>(); var serviceProxy = providerFactory(serviceName); - - - + Uri uri = null; + string requestMessage = null; var messageHandler = new MockHttpMessageHandler(); messageHandler - .When("*") - .Respond(req => HttpResponseFactory.GetResponse( - content: $"'{req.RequestUri.Host}:{req.RequestUri.Port}'")); + .When("*").Respond(async req => + { + requestMessage = await req.Content.ReadAsStringAsync(); + uri = req.RequestUri; + return HttpResponseFactory.GetResponse(HttpStatusCode.Accepted); + }); serviceProxy.HttpMessageHandler = messageHandler; - string overrideHost = "override-host"; - int overridePort = 5318; + string expectedHost = "override-host"; + int expectedPort = 5318; - TracingContext.SetHostOverride(serviceName, overrideHost, overridePort); + TracingContext.SetHostOverride(serviceName, expectedHost, expectedPort); var request = new HttpServiceRequest("testMethod", null, new Dictionary()); - for (int i = 0; i < 50; i++) - { - var host = (string)await serviceProxy.Invoke(request, typeof(string)); - host.ShouldBe($"{overrideHost}:{overridePort}"); - } - + await serviceProxy.Invoke(request, typeof(string)); + var body = requestMessage; + JsonConvert.DeserializeObject(body, new JsonSerializerSettings() { MissingMemberHandling = MissingMemberHandling.Error }); + uri.Host.ShouldBe(expectedHost); + uri.Port.ShouldBe(expectedPort); } + + + } + + public class Arguments + { + } + + public class TracingData + { + [JsonRequired] + public string RequestID { get; set; } + + [JsonRequired] + public string HostName { get; set; } + + [JsonRequired] + public string ServiceName { get; set; } + + [JsonRequired] + public string SpanID { get; set; } + + [JsonRequired] + public DateTime SpanStartTime { get; set; } + } + + public class Host1 + { + [JsonRequired] + public string ServiceName { get; set; } + + [JsonRequired] + public string Host { get; set; } + + [JsonRequired] + public int Port { get; set; } + } + + public class Overrides + { + [JsonRequired] + public List Hosts { get; set; } + + [JsonRequired] + public string PreferredEnvironment { get; set; } } + public class Target + { + [JsonRequired] + public string MethodName { get; set; } + } + + public class GigyaRequestProtocol + { + [JsonRequired] + public Arguments Arguments { get; set; } + + [JsonRequired] + public TracingData TracingData { get; set; } + + [JsonRequired] + public Overrides Overrides { get; set; } + + [JsonRequired] + public Target Target { get; set; } + } + + [Test] public async Task RequestContextShouldOverrideHostOnly() { @@ -255,7 +320,7 @@ public async Task OneHostHasNetworkErrorShouldMoveToNextHost() var server = await serviceProxy.Invoke(request, typeof(string)); server.ShouldBe("host2"); } - + counter.ShouldBe(4); } } @@ -297,7 +362,7 @@ public async Task RequestContextOverrideShouldFailOnFirstAttempt() string overrideHost = "override-host"; - int overridePort = 5318; + int overridePort = 5318; TracingContext.SetHostOverride("DemoService", overrideHost, overridePort); serviceProxy.HttpMessageHandler = messageHandler; From 38b2ca12a86a26db2c1dc901340607e581de85f3 Mon Sep 17 00:00:00 2001 From: Yevgeny Kaplan Date: Tue, 18 Dec 2018 12:30:37 +0200 Subject: [PATCH 6/9] test --- .../SystemInitializerTests.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/Gigya.Microdot.UnitTests/SystemInitializer/SystemInitializerTests.cs b/tests/Gigya.Microdot.UnitTests/SystemInitializer/SystemInitializerTests.cs index d6bbf895..d017bc5a 100644 --- a/tests/Gigya.Microdot.UnitTests/SystemInitializer/SystemInitializerTests.cs +++ b/tests/Gigya.Microdot.UnitTests/SystemInitializer/SystemInitializerTests.cs @@ -1,10 +1,12 @@ using System; using System.Linq; using System.Net; +using System.Threading.Tasks.Dataflow; using Gigya.Microdot.Configuration; using Gigya.Microdot.Configuration.Objects; using Gigya.Microdot.Fakes; using Gigya.Microdot.Interfaces; +using Gigya.Microdot.SharedLogic.SystemWrappers; using Gigya.Microdot.Testing.Shared; using Ninject; using NSubstitute; @@ -37,5 +39,27 @@ public void ShouldCallConfigObjectCacheWhileScanningAssembliesForIConfigObjects( configCacheMock.Received(typesCount).RegisterConfigObjectCreator(Arg.Any()); } + + [Test] + public void ShouldNotRebindIConfigObjectDuringInitializingIfAlreadyBindedBefore() + { + DataCentersConfig config = new DataCentersConfig(); + config.Current = "Test"; + + BroadcastBlock block = new BroadcastBlock(null); + + TestingKernel kernel = new TestingKernel(k => + { + k.Rebind().ToConstant(config); + k.Rebind>().ToMethod(c => () => config); + k.Rebind>().ToConstant(block); + k.Rebind>>().ToMethod(c => () => block); + }); + + Assert.AreSame(config, kernel.Get()); + Assert.AreSame(config, kernel.Get>()()); + Assert.AreSame(block, kernel.Get>()); + Assert.AreSame(block, kernel.Get>>()()); + } } } From 99e25b49a51f93e84266ded251322b84601db014 Mon Sep 17 00:00:00 2001 From: Yevgeny Kaplan Date: Tue, 18 Dec 2018 13:20:14 +0200 Subject: [PATCH 7/9] test fix --- Gigya.Microdot.Testing.Shared/Service/NonOrleansServiceTester.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Gigya.Microdot.Testing.Shared/Service/NonOrleansServiceTester.cs b/Gigya.Microdot.Testing.Shared/Service/NonOrleansServiceTester.cs index 5475e901..b0fbc4de 100644 --- a/Gigya.Microdot.Testing.Shared/Service/NonOrleansServiceTester.cs +++ b/Gigya.Microdot.Testing.Shared/Service/NonOrleansServiceTester.cs @@ -46,7 +46,6 @@ public NonOrleansServiceTester(int basePortOverride, IResolutionRoot resolutionR public override void Dispose() { _host.Stop(); - _host.Dispose(); var completed = _stopTask.Wait(60000); if (!completed) From 22ba4bca9a1209cd2eadfd2fcdcf2602aaecfbad Mon Sep 17 00:00:00 2001 From: Yevgeny Kaplan Date: Tue, 18 Dec 2018 14:34:00 +0200 Subject: [PATCH 8/9] new 1.13.1.0 version --- SolutionVersion.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/SolutionVersion.cs b/SolutionVersion.cs index a2be5699..a4e3452f 100644 --- a/SolutionVersion.cs +++ b/SolutionVersion.cs @@ -28,9 +28,9 @@ [assembly: AssemblyCopyright("© 2018 Gigya Inc.")] [assembly: AssemblyDescription("Microdot Framework")] -[assembly: AssemblyVersion("1.13.0.0")] -[assembly: AssemblyFileVersion("1.13.0.0")] -[assembly: AssemblyInformationalVersion("1.13.0.0")] +[assembly: AssemblyVersion("1.13.1.0")] +[assembly: AssemblyFileVersion("1.13.1.0")] +[assembly: AssemblyInformationalVersion("1.13.1.0")] // Setting ComVisible to false makes the types in this assembly not visible // to COM components. If you need to access a type in this assembly from From af8bbfc711f4ec7607e50ed1edf97a9dad5c4a4e Mon Sep 17 00:00:00 2001 From: David Bronshtein Date: Wed, 19 Dec 2018 11:07:22 +0200 Subject: [PATCH 9/9] #80807 - Health message should be in formattable by string.Format, because Metrics.NET are using the string.Format to display it. --- .../Objects/ConfigObjectCreator.cs | 8 ++++---- .../Monitor/AggregatingHealthStatus.cs | 3 ++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Gigya.Microdot.Configuration/Objects/ConfigObjectCreator.cs b/Gigya.Microdot.Configuration/Objects/ConfigObjectCreator.cs index 26c005fb..9dd95267 100644 --- a/Gigya.Microdot.Configuration/Objects/ConfigObjectCreator.cs +++ b/Gigya.Microdot.Configuration/Objects/ConfigObjectCreator.cs @@ -79,20 +79,20 @@ private void Init() Reload(); ConfigCache.ConfigChanged.LinkTo(new ActionBlock(c => Reload())); InitializeBroadcast(); - healthStatus.RegisterCheck(ObjectType.Name, HealthCheck); + healthStatus.Register(ObjectType.Name, HealthCheck); } - private HealthCheckResult HealthCheck() + private HealthMessage HealthCheck() { if (ValidationErrors != null) { - return HealthCheckResult.Unhealthy("The config object failed validation.\r\n" + + return new HealthMessage(Health.Unhealthy, "The config object failed validation.\r\n" + $"ConfigObjectType={ObjectType.FullName}\r\n" + $"ConfigObjectPath={ConfigPath}\r\n" + $"ValidationErrors={ValidationErrors}"); } - return HealthCheckResult.Healthy(); + return new HealthMessage(Health.Healthy, "OK"); } /// diff --git a/Gigya.Microdot.SharedLogic/Monitor/AggregatingHealthStatus.cs b/Gigya.Microdot.SharedLogic/Monitor/AggregatingHealthStatus.cs index 4b4a0974..8538627e 100644 --- a/Gigya.Microdot.SharedLogic/Monitor/AggregatingHealthStatus.cs +++ b/Gigya.Microdot.SharedLogic/Monitor/AggregatingHealthStatus.cs @@ -63,7 +63,8 @@ private HealthCheckResult HealthCheck() : "") + $"{r.Name}: {r.Result.Message}")); - return allHealthy ? HealthCheckResult.Healthy(message) : HealthCheckResult.Unhealthy(message); + var formattableMessage = message.Replace("{", "{{").Replace("}", "}}"); // HealthCheckResult is formatting the message string using "string.Format" + return allHealthy ? HealthCheckResult.Healthy(formattableMessage) : HealthCheckResult.Unhealthy(formattableMessage); } [Obsolete("Please use method Register(string,Func) instead")]