public async void SetStatusAsync_CallsConsulClient()
        {
            var clientMoq = new Mock <IConsulClient>();
            var agentMoq  = new Mock <IAgentEndpoint>();

            clientMoq.Setup(c => c.Agent).Returns(agentMoq.Object);

            var opts = new ConsulDiscoveryOptions();
            var sch  = new TtlScheduler(opts, clientMoq.Object);

            var builder = new ConfigurationBuilder()
                          .AddInMemoryCollection(new Dictionary <string, string>()
            {
                { "spring:application:name", "foobar" }
            });
            var config       = builder.Build();
            var registration = ConsulRegistration.CreateRegistration(config, opts);

            var reg = new ConsulServiceRegistry(clientMoq.Object, opts, sch);
            await reg.SetStatusAsync(registration, "Up");

            agentMoq.Verify(a => a.DisableServiceMaintenance(registration.InstanceId, default(CancellationToken)), Times.Once);
            await reg.SetStatusAsync(registration, "Out_of_Service");

            agentMoq.Verify(a => a.EnableServiceMaintenance(registration.InstanceId, "OUT_OF_SERVICE", default(CancellationToken)), Times.Once);
        }
        public async void DeregisterAsync_CallsServiceDeregister_RemovesHeartbeatFromScheduler()
        {
            var clientMoq = new Mock <IConsulClient>();
            var agentMoq  = new Mock <IAgentEndpoint>();

            clientMoq.Setup(c => c.Agent).Returns(agentMoq.Object);

            var opts = new ConsulDiscoveryOptions();
            var sch  = new TtlScheduler(opts, clientMoq.Object);

            var reg     = new ConsulServiceRegistry(clientMoq.Object, opts, sch);
            var builder = new ConfigurationBuilder()
                          .AddInMemoryCollection(new Dictionary <string, string>()
            {
                { "spring:application:name", "foobar" }
            });
            var config       = builder.Build();
            var registration = ConsulRegistration.CreateRegistration(config, opts);
            await reg.RegisterAsync(registration);

            agentMoq.Verify(a => a.ServiceRegister(registration.Service, default(CancellationToken)), Times.Once);

            Assert.Single(sch._serviceHeartbeats);
            Assert.Contains(registration.InstanceId, sch._serviceHeartbeats.Keys);

            await reg.DeregisterAsync(registration);

            agentMoq.Verify(a => a.ServiceDeregister(registration.Service.ID, default(CancellationToken)), Times.Once);
            Assert.Empty(sch._serviceHeartbeats);
        }
 public ConsulServiceRegistry(ConsulClient client, IOptions <ConsulDiscoveryOptions> consulDiscoveryOptionsAccessor, TtlScheduler ttlScheduler, IOptions <HeartbeatOptions> heartbeatOptionsAccessor, ILogger <ConsulServiceRegistry> logger)
 {
     _client = client;
     _consulDiscoveryOptions = consulDiscoveryOptionsAccessor.Value;
     _ttlScheduler           = ttlScheduler;
     _heartbeatOptions       = heartbeatOptionsAccessor.Value;
     _logger = logger;
 }
示例#4
0
        public void Remove_Ignores_MissingInstanceId()
        {
            var clientMoq = new Mock <IConsulClient>();
            var client    = clientMoq.Object;
            var opts      = new ConsulDiscoveryOptions();
            var sch       = new TtlScheduler(opts, client);

            sch.Remove("barfoo");
        }
示例#5
0
        public void Remove_Throws_Invalid_InstanceId()
        {
            var clientMoq = new Mock <IConsulClient>();
            var client    = clientMoq.Object;
            var opts      = new ConsulDiscoveryOptions();
            var sch       = new TtlScheduler(opts, client);

            Assert.Throws <ArgumentException>(() => sch.Remove(string.Empty));
        }
示例#6
0
        public void Add_DoesNothing_NoHeartbeatOptionsConfigured()
        {
            var clientMoq = new Mock <IConsulClient>();
            var client    = clientMoq.Object;
            var opts      = new ConsulDiscoveryOptions()
            {
                Heartbeat = null
            };
            var sch = new TtlScheduler(opts, client);

            sch.Add("foobar");
            Assert.Empty(sch._serviceHeartbeats);
        }
        public void DeegisterAsync_ThrowsOnNull()
        {
            var clientMoq = new Mock <IConsulClient>();
            var agentMoq  = new Mock <IAgentEndpoint>();

            clientMoq.Setup(c => c.Agent).Returns(agentMoq.Object);

            var opts = new ConsulDiscoveryOptions();
            var sch  = new TtlScheduler(opts, clientMoq.Object);

            var reg = new ConsulServiceRegistry(clientMoq.Object, opts, sch);

            Assert.ThrowsAsync <ArgumentNullException>(() => reg.DeregisterAsync(null));
        }
示例#8
0
        public void Add_AddsTimer()
        {
            var clientMoq = new Mock <IConsulClient>();
            var agentMoq  = new Mock <IAgentEndpoint>();

            clientMoq.Setup(c => c.Agent).Returns(agentMoq.Object);
            var client = clientMoq.Object;
            var opts   = new ConsulDiscoveryOptions()
            {
                Heartbeat = new ConsulHeartbeatOptions()
            };
            var sch = new TtlScheduler(opts, client);

            sch.Add("foobar");
            Assert.NotEmpty(sch._serviceHeartbeats);
            Assert.True(sch._serviceHeartbeats.TryRemove("foobar", out Timer timer));
            Assert.NotNull(timer);
            timer.Dispose();
        }
        public async void GetStatusAsync_ReturnsExpected()
        {
            var opts    = new ConsulDiscoveryOptions();
            var builder = new ConfigurationBuilder()
                          .AddInMemoryCollection(new Dictionary <string, string>()
            {
                { "spring:application:name", "foobar" }
            });
            var config       = builder.Build();
            var registration = ConsulRegistration.CreateRegistration(config, opts);

            var queryResult = new QueryResult <HealthCheck[]>()
            {
                Response = new HealthCheck[]
                {
                    new HealthCheck()
                    {
                        ServiceID = registration.InstanceId,
                        Name      = "Service Maintenance Mode"
                    },
                    new HealthCheck()
                    {
                        ServiceID = "foobar",
                        Name      = "Service Maintenance Mode"
                    }
                }
            };
            var result = Task.FromResult(queryResult);

            var clientMoq = new Mock <IConsulClient>();
            var healthMoq = new Mock <IHealthEndpoint>();

            clientMoq.Setup(c => c.Health).Returns(healthMoq.Object);
            healthMoq.Setup(h => h.Checks(registration.ServiceId, QueryOptions.Default, default(CancellationToken))).Returns(result);

            var sch = new TtlScheduler(opts, clientMoq.Object);
            var reg = new ConsulServiceRegistry(clientMoq.Object, opts, sch);

            var ret = await reg.GetStatusAsync(registration);

            Assert.Equal("OUT_OF_SERVICE", ret);
        }
示例#10
0
        public void Timer_CallsPassTtl()
        {
            var clientMoq = new Mock <IConsulClient>();
            var agentMoq  = new Mock <IAgentEndpoint>();

            clientMoq.Setup(c => c.Agent).Returns(agentMoq.Object);
            var client = clientMoq.Object;
            var opts   = new ConsulDiscoveryOptions()
            {
                Heartbeat = new ConsulHeartbeatOptions()
                {
                    TtlValue = 2
                }
            };
            var sch = new TtlScheduler(opts, client);

            sch.Add("foobar");
            Thread.Sleep(5000);
            agentMoq.Verify(a => a.PassTTL("service:foobar", "ttl", default(CancellationToken)), Times.AtLeastOnce);
            sch.Remove("foobar");
        }
        public void SetStatusAsync_ThrowsInvalidStatus()
        {
            var clientMoq = new Mock <IConsulClient>();
            var agentMoq  = new Mock <IAgentEndpoint>();

            clientMoq.Setup(c => c.Agent).Returns(agentMoq.Object);

            var opts = new ConsulDiscoveryOptions();
            var sch  = new TtlScheduler(opts, clientMoq.Object);

            var builder = new ConfigurationBuilder()
                          .AddInMemoryCollection(new Dictionary <string, string>()
            {
                { "spring:application:name", "foobar" }
            });
            var config       = builder.Build();
            var registration = ConsulRegistration.CreateRegistration(config, opts);

            var reg = new ConsulServiceRegistry(clientMoq.Object, opts, sch);

            Assert.ThrowsAsync <ArgumentException>(() => reg.SetStatusAsync(registration, string.Empty));
        }