public async Task should_not_create_service_health_data_when_found() { var now = DateTime.UtcNow; var start = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0, DateTimeKind.Utc); var service = new Heimdall.Mongo.Infrastructure.Entities.Service() { Id = Guid.NewGuid(), Active = true, Name = "lorem", Endpoints = Enumerable.Empty <Heimdall.Mongo.Infrastructure.Entities.ServiceEndpoint>() }; var mockRepo = RepositoryUtils.MockRepository(service); var mockEventsRepo = RepositoryUtils.MockRepository <Heimdall.Mongo.Infrastructure.Entities.TraceEvent>(); var mockDbContext = new Mock <IDbContext>(); mockDbContext.Setup(db => db.Services).Returns(mockRepo.Object); mockDbContext.Setup(db => db.TraceEvents).Returns(mockEventsRepo.Object); var serviceHealth = new Infrastructure.Entities.ServiceHealth() { ServiceId = service.Id, TimestampMinute = start.Ticks }; var mockServicesHealthRepo = RepositoryUtils.MockRepository <Infrastructure.Entities.ServiceHealth>(); mockServicesHealthRepo.Setup(r => r.FindOneAndUpdateAsync(It.IsAny <Expression <Func <Infrastructure.Entities.ServiceHealth, bool> > >(), It.IsAny <MongoDB.Driver.UpdateDefinition <Infrastructure.Entities.ServiceHealth> >())) .ReturnsAsync((Expression <Func <Infrastructure.Entities.ServiceHealth, bool> > filter, MongoDB.Driver.UpdateDefinition <Infrastructure.Entities.ServiceHealth> update) => { return(serviceHealth); }); var mockAnalyticsDb = new Mock <IAnalyticsDbContext>(); mockAnalyticsDb.Setup(db => db.ServicesHealth).Returns(mockServicesHealthRepo.Object); var @event = new ServiceRefreshed(service.Id); var sut = new ServiceRefreshedHandler(mockDbContext.Object, mockAnalyticsDb.Object); await sut.Handle(@event); mockServicesHealthRepo.Verify(m => m.InsertOneAsync(It.Is <Infrastructure.Entities.ServiceHealth>( sh => null != sh && sh.ServiceId == service.Id && sh.TimestampMinute >= start.Ticks && null != sh.Details && sh.Details.Count() == 1 ) ), Times.Never); }
public async Task should_append_service_health_data_when_found() { var endpoint = new Heimdall.Mongo.Infrastructure.Entities.ServiceEndpoint() { Active = true, CreationDate = DateTime.UtcNow.Ticks, RoundtripTime = 1, Address = "ipsum" }; var service = new Heimdall.Mongo.Infrastructure.Entities.Service() { Active = true, CreationDate = DateTime.UtcNow.Ticks, Endpoints = new[] { endpoint }, Id = Guid.NewGuid(), Name = "lorem" }; await base.DbContext.Services.InsertOneAsync(service); var now = DateTime.UtcNow; var start = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0, DateTimeKind.Utc); var serviceHealth = new Infrastructure.Entities.ServiceHealth() { ServiceId = service.Id, TimestampMinute = start.Ticks, Details = Enumerable.Empty <Infrastructure.Entities.ServiceHealthDetails>() }; await base.AnalyticsDbContext.ServicesHealth.InsertOneAsync(serviceHealth); var sut = new ServiceRefreshedHandler(base.DbContext, base.AnalyticsDbContext); var @event = new ServiceRefreshed(service.Id); await sut.Handle(@event); var foundServicesHealth = await base.AnalyticsDbContext.ServicesHealth.FindAsync(sh => sh.ServiceId == service.Id); foundServicesHealth.Should().NotBeNullOrEmpty(); foundServicesHealth.Count().ShouldBeEquivalentTo(1); var foundServiceHealth = foundServicesHealth.ElementAt(0); foundServiceHealth.Should().NotBeNull(); foundServiceHealth.Details.Should().NotBeNullOrEmpty(); foundServiceHealth.Details.Count().ShouldBeEquivalentTo(1); var foundDetails = foundServiceHealth.Details.ElementAt(0); foundDetails.BestEndpoint.ShouldBeEquivalentTo(endpoint.Address); foundDetails.RoundtripTime.ShouldBeEquivalentTo(endpoint.RoundtripTime); }
public void should_throw_ArgumentOutOfRangeException_when_service_does_not_exist() { var service = new Heimdall.Mongo.Infrastructure.Entities.Service() { Id = Guid.NewGuid(), Active = true, Name = "lorem", Endpoints = Enumerable.Empty <Heimdall.Mongo.Infrastructure.Entities.ServiceEndpoint>() }; var mockRepo = RepositoryUtils.MockRepository(service); var mockDbContext = new Mock <IDbContext>(); mockDbContext.Setup(db => db.Services).Returns(mockRepo.Object); var mockAnalyticsDb = new Mock <IAnalyticsDbContext>(); var @event = new ServiceRefreshed(Guid.NewGuid()); var sut = new ServiceRefreshedHandler(mockDbContext.Object, mockAnalyticsDb.Object); Assert.ThrowsAsync <ArgumentOutOfRangeException>(() => sut.Handle(@event)); }
public async Task should_create_service_health_data_when_not_found() { var endpoint = new Heimdall.Mongo.Infrastructure.Entities.ServiceEndpoint() { Active = true, CreationDate = DateTime.UtcNow.Ticks, RoundtripTime = 1, Address = "ipsum" }; var service = new Heimdall.Mongo.Infrastructure.Entities.Service() { Active = true, CreationDate = DateTime.UtcNow.Ticks, Endpoints = new[] { endpoint }, Id = Guid.NewGuid(), Name = "lorem" }; await base.DbContext.Services.InsertOneAsync(service); var sut = new ServiceRefreshedHandler(base.DbContext, base.AnalyticsDbContext); var @event = new ServiceRefreshed(service.Id); await sut.Handle(@event); var servicesHealth = await base.AnalyticsDbContext.ServicesHealth.FindAsync(sh => sh.ServiceId == service.Id); servicesHealth.Should().NotBeNullOrEmpty(); servicesHealth.Count().ShouldBeEquivalentTo(1); var serviceHealth = servicesHealth.ElementAt(0); serviceHealth.Should().NotBeNull(); serviceHealth.Details.Should().NotBeNullOrEmpty(); serviceHealth.Details.Count().ShouldBeEquivalentTo(1); var details = serviceHealth.Details.ElementAt(0); details.BestEndpoint.ShouldBeEquivalentTo(endpoint.Address); details.RoundtripTime.ShouldBeEquivalentTo(endpoint.RoundtripTime); }