Example #1
0
        public async Task Register_ConsulReturnsNonSuccessfulHttpStatusBelow200_CallsConsulAndThrowException()
        {
            var loggerFactory = Substitute.For <ILoggerFactory>();
            var consulClient  = Substitute.For <IConsulClient>();

            consulClient.Agent.ServiceRegister(Arg.Any <AgentServiceRegistration>())
            .Returns(new WriteResult {
                RequestTime = TimeSpan.Zero, StatusCode = HttpStatusCode.Continue
            });

            var service   = new AgentServiceRegistration();
            var registrar = new ConsulServiceRegistrar(loggerFactory, consulClient, service);

            Exception exception = null;

            try
            {
                await registrar.Register();
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            Assert.NotNull(exception);
            await consulClient.Agent.Received(1).ServiceRegister(Arg.Any <AgentServiceRegistration>());
        }
Example #2
0
        public async Task Register_ConsulClientThrowsException_CallsConsulAndThrowException()
        {
            var loggerFactory = Substitute.For <ILoggerFactory>();
            var consulClient  = Substitute.For <IConsulClient>();

            var expectedException = new Exception();

            consulClient.Agent.ServiceRegister(Arg.Any <AgentServiceRegistration>())
            .Throws(expectedException);

            var service   = new AgentServiceRegistration();
            var registrar = new ConsulServiceRegistrar(loggerFactory, consulClient, service);

            Exception actualException = null;

            try
            {
                await registrar.Register();
            }
            catch (Exception ex)
            {
                actualException = ex;
            }

            Assert.Same(expectedException, actualException);
            await consulClient.Agent.Received(1).ServiceRegister(Arg.Any <AgentServiceRegistration>());
        }
Example #3
0
        static void Register()
        {
            var consulClient = ConsulClientFactory.Create(new ConsulDiscoveryConfiguration()
            {
                Address = consulAddress
            });
            ConsulServiceRegistrar registrar = new ConsulServiceRegistrar(consulClient);
            var info = registrar.RegisterServiceAsync("testServiceName", "v1.0", "192.168.0.81", 8080).Result;

            registrar.DeregisterServiceAsync(info.Id).Wait();
        }
Example #4
0
        public void Start_CallsRegistry()
        {
            var regMoq       = new Mock <IConsulServiceRegistry>();
            var options      = new ConsulDiscoveryOptions();
            var registration = new ConsulRegistration();
            var reg          = new ConsulServiceRegistrar(regMoq.Object, options, registration);

            reg.Start();
            Assert.Equal(1, reg._running);
            regMoq.Verify(a => a.Register(registration), Times.Once);
        }
Example #5
0
        public void Deregister_CallsRegistry()
        {
            var regMoq       = new Mock <IConsulServiceRegistry>();
            var options      = new ConsulDiscoveryOptions();
            var registration = new ConsulRegistration();

            var reg = new ConsulServiceRegistrar(regMoq.Object, options, registration);

            reg.Deregister();
            regMoq.Verify(a => a.Deregister(registration), Times.Once);
        }
Example #6
0
        public void Start_DoesNotStart()
        {
            var regMoq  = new Mock <IConsulServiceRegistry>();
            var options = new ConsulDiscoveryOptions()
            {
                Enabled = false
            };
            var registration = new ConsulRegistration();
            var reg          = new ConsulServiceRegistrar(regMoq.Object, options, registration);

            reg.Start();
            Assert.Equal(0, reg._running);
        }
Example #7
0
        public void Deregister_DoesNotCallRegistry()
        {
            var regMoq  = new Mock <IConsulServiceRegistry>();
            var options = new ConsulDiscoveryOptions()
            {
                Deregister = false
            };
            var registration = new ConsulRegistration();

            var reg = new ConsulServiceRegistrar(regMoq.Object, options, registration);

            reg.Deregister();
            regMoq.Verify(a => a.Deregister(registration), Times.Never);
        }
Example #8
0
        public async Task Register_SuccessfulRegistration_CallsConsulAndDoesNotThrowException()
        {
            var loggerFactory = Substitute.For <ILoggerFactory>();
            var consulClient  = Substitute.For <IConsulClient>();

            consulClient.Agent.ServiceRegister(Arg.Any <AgentServiceRegistration>())
            .Returns(new WriteResult {
                RequestTime = TimeSpan.Zero, StatusCode = HttpStatusCode.Created
            });

            var service   = new AgentServiceRegistration();
            var registrar = new ConsulServiceRegistrar(loggerFactory, consulClient, service);

            await registrar.Register();

            await consulClient.Agent.Received(1).ServiceRegister(Arg.Any <AgentServiceRegistration>());
        }
Example #9
0
        static void RegisterAndGetServices()
        {
            var consulClient = ConsulClientFactory.Create(new ConsulDiscoveryConfiguration()
            {
                Address = consulAddress
            });
            ConsulServiceRegistrar registrar = new ConsulServiceRegistrar(consulClient);
            var info  = registrar.RegisterServiceAsync("testServiceName", "v1.0", "192.168.0.81", 8080).Result;
            var info2 = registrar.RegisterServiceAsync("testServiceName", "v1.1", "192.168.0.81", 8090).Result;
            var info3 = registrar.RegisterServiceAsync("testServiceName", "v1.0", "192.168.0.81", 10101).Result;

            ConsulServiceDiscovery discovery = new ConsulServiceDiscovery(consulClient, new ConsulDiscoveryConfiguration()
            {
                Address = consulAddress
            });

            var services = discovery.GetServicesAsync("testServiceName").Result;

            var services2 = discovery.GetServicesAsync("testServiceName", "v1.0").Result;

            var service = discovery.GetServiceAsync("testServiceName").Result;
        }