public async Task Setup() { IDiscovery discovery = Substitute.For <IDiscovery>(); _discoveryConfig = new DiscoveryConfig { Services = new ServiceDiscoveryCollection(new Dictionary <string, ServiceDiscoveryConfig>(), new ServiceDiscoveryConfig(), new PortAllocationConfig()) }; Dictionary <string, string> configDic = new Dictionary <string, string>(); _currentEnvironment = Prod; var environment = Substitute.For <IEnvironment>(); environment.DeploymentEnvironment.Returns(_ => _currentEnvironment); _unitTestingKernel = new TestingKernel <ConsoleLog>(mockConfig: configDic); _unitTestingKernel.Rebind <IEnvironment>().ToConstant(environment); _unitTestingKernel.Rebind <IDiscovery>().ToConstant(discovery); _unitTestingKernel.Rebind <Func <DiscoveryConfig> >().ToMethod(_ => () => _discoveryConfig); _loadBalancerByEnvironment = new Dictionary <string, ILoadBalancer>(); _loadBalancerByEnvironment.Add(Prod, new MasterLoadBalancer()); _loadBalancerByEnvironment.Add(Staging, new StagingLoadBalancer()); _loadBalancerByEnvironment.Add(Canary, new PreferredEnvironmentLoadBalancer()); discovery.CreateLoadBalancer(Arg.Any <DeploymentIdentifier>(), Arg.Any <ReachabilityCheck>(), TrafficRoutingStrategy.RandomByRequestID) .ReturnsForAnyArgs(c => _loadBalancerByEnvironment[c.Arg <DeploymentIdentifier>().DeploymentEnvironment]); _serviceDiscovery = _unitTestingKernel.Get <Func <string, ReachabilityCheck, IMultiEnvironmentServiceDiscovery> >()(ServiceName, (x, y) => new Task(null)); TracingContext.SetPreferredEnvironment(null); }
private void SetupServiceMock() { _serviceMock = Substitute.For <ICachingTestService>(); _serviceMock.CallService().Returns(_ => { return(Task.FromResult(_serviceResult)); }); _serviceMock.CallServiceWithAttRefreshTime().Returns(_ => { return(Task.FromResult(_serviceResult)); }); _serviceMock.CallServiceWithoutAttRefreshTime().Returns(_ => { return(Task.FromResult(_serviceResult)); }); _serviceMock.CallRevocableService(Arg.Any <string>()).Returns(async s => { var result = _serviceResult; await _revokeDelay; return(new Revocable <string> { Value = result, RevokeKeys = new[] { s.Args()[0].ToString() } }); }); _serviceResult = FirstResult; var serviceProxyMock = Substitute.For <IServiceProxyProvider <ICachingTestService> >(); serviceProxyMock.Client.Returns(_serviceMock); _kernel.Rebind <IServiceProxyProvider <ICachingTestService> >().ToConstant(serviceProxyMock); }
public void SetUp() { try { Environment.SetEnvironmentVariable("GIGYA_CONFIG_ROOT", AppDomain.CurrentDomain.BaseDirectory, EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("REGION", "us1", EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("ZONE", "us1a", EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("ENV", "_Test", EnvironmentVariableTarget.Process); kernel = new TestingKernel <ConsoleLog>((kernel) => { var revokingManager = new FakeRevokingManager(); kernel.Rebind <IRevokeListener>().ToConstant(revokingManager); kernel.Rebind <ICacheRevoker>().ToConstant(revokingManager); kernel.Rebind <IMetricsInitializer>().To <MetricsInitializerFake>(); kernel.Rebind <IEventPublisher>().To <SpyEventPublisher>().InSingletonScope(); }); ResolutionRoot = kernel; } catch (Exception ex) { Console.Write(ex); throw; } }
private void SetupServiceMock() { _serviceMock = Substitute.For <ICachingTestService>(); _serviceMock.CallService().Returns(_ => Task.FromResult(_serviceResult)); _serviceResult = FirstResult; var serviceProxyMock = Substitute.For <IServiceProxyProvider <ICachingTestService> >(); serviceProxyMock.Client.Returns(_serviceMock); _kernel.Rebind <IServiceProxyProvider <ICachingTestService> >().ToConstant(serviceProxyMock); }
public void OneTimeSetup() { _configDic = new Dictionary <string, string>(); _kernel = new TestingKernel <ConsoleLog>(mockConfig: _configDic); _kernel.Rebind(typeof(CachingProxyProvider <>)) .ToSelf() .InTransientScope(); var fakeRevokingManager = new FakeRevokingManager(); _kernel.Rebind <IRevokeListener>().ToConstant(fakeRevokingManager); _kernel.Rebind <ICacheRevoker>().ToConstant(fakeRevokingManager); }
public void OneTimeSetup() { _configDic = new Dictionary <string, string>(); _kernel = new TestingKernel <ConsoleLog>(mockConfig: _configDic); _kernel.Rebind(typeof(CachingProxyProvider <>)) .ToSelf() .InTransientScope(); }
public void SetUp() { try { Environment.SetEnvironmentVariable("GIGYA_CONFIG_ROOT", AppDomain.CurrentDomain.BaseDirectory, EnvironmentVariableTarget.Process); kernel = new TestingKernel <ConsoleLog>((kernel) => { var revokingManager = new FakeRevokingManager(); kernel.Rebind <IRevokeListener>().ToConstant(revokingManager); kernel.Rebind <ICacheRevoker>().ToConstant(revokingManager); }); ResolutionRoot = kernel; } catch (Exception ex) { Console.Write(ex); throw; } }
public void MasterShouldNotFallBack() { _environment = Substitute.For <IEnvironment>(); _environment.Zone.Returns("il3"); _environment.DeploymentEnvironment.Returns(MASTER_ENVIRONMENT); _unitTestingKernel.Rebind <IEnvironment>().ToConstant(_environment); SetMockToReturnServiceNotDefined(MasterService); Should.ThrowAsync <EnvironmentException>(() => GetServiceDiscovery().GetNode()); }
public void MasterShouldNotFallBack() { _environmentVariableProvider = Substitute.For <IEnvironmentVariableProvider>(); _environmentVariableProvider.DataCenter.Returns("il3"); _environmentVariableProvider.DeploymentEnvironment.Returns(MASTER_ENVIRONMENT); _unitTestingKernel.Rebind <IEnvironmentVariableProvider>().ToConstant(_environmentVariableProvider); SetMockToReturnQueryNotFound(_masterService); Should.Throw <EnvironmentException>(() => GetServiceDiscovey.GetNextHost()); }
private async Task <HttpRequestMessage> GetRequestFor <T>(Func <T, Task> action) { HttpRequestMessage request = null; string requestContent = null; Func <HttpClientConfiguration, HttpMessageHandler> messageHandlerFactory = _ => { var mockHandler = new MockHttpMessageHandler(); mockHandler.When("*").Respond(async r => { if (r.Method != HttpMethod.Get) { request = r; requestContent = await r.Content.ReadAsStringAsync(); } return(HttpResponseFactory.GetResponse(content: "''")); }); return(mockHandler); }; var kernel = new TestingKernel <ConsoleLog>(); kernel.Rebind <Func <HttpClientConfiguration, HttpMessageHandler> >().ToMethod(c => messageHandlerFactory); var client = kernel.Get <ServiceProxyProviderSpy <T> >(); client.DefaultPort = _testinghost.BasePort; await action(client.Client); var contentClone = new StringContent(requestContent, Encoding.UTF8, "application/json"); foreach (KeyValuePair <string, IEnumerable <string> > header in request.Content.Headers.Where(h => h.Key.StartsWith("X"))) { contentClone.Headers.Add(header.Key, header.Value); } kernel.Dispose(); return(new HttpRequestMessage(request.Method, request.RequestUri) { Content = contentClone }); }
public void OneTimeSetup() { _kernel = new TestingKernel <ConsoleLog>(); _kernel.Rebind <Func <DiscoveryConfig> >().ToMethod(c => () => _discoveryConfig); }