public async Task RegisterAsync(ConsulRegistration registration) { if (_logger.IsEnabled(LogLevel.Information)) { _logger.LogInformation("Registering service with consul: " + registration.ServiceId); } try { await _client.Agent.ServiceRegister(registration.AgentServiceRegistration); if (_heartbeatOptions.Enable) { _ttlScheduler?.Add(registration.InstanceId); } } catch (Exception e) { if (_consulDiscoveryOptions.FailFast) { _logger.LogError(e, "Error registering service with consul: " + registration.ServiceId); throw; } _logger.LogWarning(e, "Failfast is false. Error registering service with consul: " + registration.ServiceId); } }
public void Add_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.Add(string.Empty)); }
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 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 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"); }