diff --git a/SQLDocker/Start-SqlServer.ps1 b/SQLDocker/Start-SqlServer.ps1 index 64a678cc..9cf3b8b1 100644 --- a/SQLDocker/Start-SqlServer.ps1 +++ b/SQLDocker/Start-SqlServer.ps1 @@ -1,6 +1,6 @@ param( [Parameter(Mandatory = $false)] - [SecureString]$sa_password, + [string]$sa_password, [Parameter(Mandatory = $false)] [string]$attach_dbs diff --git a/Shared.EventStore.Tests/SubscriptionRepositoryTests.cs b/Shared.EventStore.Tests/SubscriptionRepositoryTests.cs index 367d5f9a..c9802ce9 100644 --- a/Shared.EventStore.Tests/SubscriptionRepositoryTests.cs +++ b/Shared.EventStore.Tests/SubscriptionRepositoryTests.cs @@ -1,4 +1,5 @@ using Shared.EventStore.Tests.TestObjects; +using SimpleResults; namespace Shared.EventStore.Tests; @@ -19,7 +20,7 @@ public async Task SubscriptionRepository_GetSubscriptions_ReturnsSubscriptions() { List allSubscriptions = (TestData.GetPersistentSubscriptions_DemoEstate()); - Func>> GetAllSubscriptions = async token => allSubscriptions; + Func>>> GetAllSubscriptions = async token => allSubscriptions; ISubscriptionRepository subscriptionRepository = SubscriptionRepository.Create(GetAllSubscriptions); diff --git a/Shared.EventStore/Aggregate/AggregateService.cs b/Shared.EventStore/Aggregate/AggregateService.cs index bce2dac6..d9cc7b86 100644 --- a/Shared.EventStore/Aggregate/AggregateService.cs +++ b/Shared.EventStore/Aggregate/AggregateService.cs @@ -247,11 +247,11 @@ public async Task Save(TAggregate aggregate, public static readonly ConcurrentDictionary DynamicHistogram = new(); private static readonly Func FormatMetricName = (methodName, - metricType) => $"{methodName}_{metricType}"; + metricType) => $"{methodName}_{metricType.ToLowerInvariant()}"; public static Histogram GetHistogramMetric(String methodName) { - String n = AggregateService.FormatMetricName(methodName, nameof(Histogram).ToLower()); + String n = AggregateService.FormatMetricName(methodName, nameof(Histogram)); HistogramConfiguration histogramConfiguration = new() { @@ -268,7 +268,7 @@ public static Histogram GetHistogramMetric(String methodName) public static Counter GetCounterMetric(String methodName) { - String n = AggregateService.FormatMetricName(methodName, nameof(Counter).ToLower()); + String n = AggregateService.FormatMetricName(methodName, nameof(Counter)); var counter = AggregateService.DynamicCounter.GetOrAdd(methodName, name => Metrics.CreateCounter(name: n, help: $"Total number times executed {n}")); diff --git a/Shared.EventStore/Aggregate/DomainEventFactory.cs b/Shared.EventStore/Aggregate/DomainEventFactory.cs index 229b2582..fbf36c2a 100644 --- a/Shared.EventStore/Aggregate/DomainEventFactory.cs +++ b/Shared.EventStore/Aggregate/DomainEventFactory.cs @@ -10,10 +10,6 @@ using Newtonsoft.Json.Linq; using Serialisation; -/// -/// -/// -/// public class DomainEventFactory : IDomainEventFactory { #region Constructors @@ -39,13 +35,6 @@ public DomainEventFactory() #region Methods - /// - /// Creates the agge aggregate snapshot. - /// - /// - /// The event. - /// - /// Failed to find a domain event with type {@event.Event.EventType} public DomainEvent CreateDomainEvent(Guid aggregateId, ResolvedEvent @event) { String json = @event.GetResolvedEventDataAsString(); @@ -59,7 +48,7 @@ public DomainEvent CreateDomainEvent(Guid aggregateId, ResolvedEvent @event) } if (eventType == null) - throw new Exception($"Failed to find a domain event with type {@event.Event.EventType}"); + throw new ArgumentException($"Failed to find a domain event with type {@event.Event.EventType}"); DomainEvent domainEvent = (DomainEvent)JsonConvert.DeserializeObject(json, eventType); @@ -86,19 +75,13 @@ public DomainEvent CreateDomainEvent(String json, Type eventType) } catch(Exception e) { - Exception ex = new($"Failed to convert json event {json} into a domain event. EventType was {eventType.Name}", e); + ApplicationException ex = new($"Failed to convert json event {json} into a domain event. EventType was {eventType.Name}", e); throw ex; } return domainEvent; } - /// - /// Creates the domain events. - /// - /// - /// The event. - /// public DomainEvent[] CreateDomainEvents(Guid aggregateId, IList @event) { return @event.Select(e => this.CreateDomainEvent(aggregateId, e)).ToArray(); diff --git a/Shared.EventStore/SubscriptionWorker/SubscriptionRepository.cs b/Shared.EventStore/SubscriptionWorker/SubscriptionRepository.cs index e941d6c2..1de27a7b 100644 --- a/Shared.EventStore/SubscriptionWorker/SubscriptionRepository.cs +++ b/Shared.EventStore/SubscriptionWorker/SubscriptionRepository.cs @@ -1,4 +1,6 @@ -namespace Shared.EventStore.SubscriptionWorker; +using SimpleResults; + +namespace Shared.EventStore.SubscriptionWorker; using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; @@ -12,7 +14,7 @@ public class SubscriptionRepository : ISubscriptionRepository { private Int32 CacheHits; private Int32 FullRefreshHits; - private Func>> GetAllSubscriptions; + private Func>>> GetAllSubscriptions; private readonly Func RefreshRequired; private Int32 running; private PersistentSubscriptions Subscriptions; @@ -29,23 +31,25 @@ public static SubscriptionRepository Create(String eventStoreConnectionString, Int32 cacheDuration = 120) { EventStoreClientSettings settings = EventStoreClientSettings.Create(eventStoreConnectionString); HttpClient httpClient = SubscriptionWorkerHelper.CreateHttpClient(settings); - return new SubscriptionRepository(cacheDuration) { GetAllSubscriptions = cancellationToken => SubscriptionRepository.GetSubscriptions(httpClient, cancellationToken) }; + + + return new SubscriptionRepository(cacheDuration) { GetAllSubscriptions = cancellationToken => GetSubscriptions(httpClient, cancellationToken) }; } - public static SubscriptionRepository Create(Task> func, + public static SubscriptionRepository Create(Task>> func, Int32 cacheDuration = 120) { return new(cacheDuration) { GetAllSubscriptions = _ => func }; } - public static SubscriptionRepository Create(Func>> func, + public static SubscriptionRepository Create(Func>>> func, Int32 cacheDuration = 120) { return new(cacheDuration) { GetAllSubscriptions = func }; } - public static async Task> GetSubscriptions(HttpClient httpClient, - CancellationToken cancellationToken) { + public static async Task>> GetSubscriptions(HttpClient httpClient, + CancellationToken cancellationToken) { try { HttpResponseMessage responseMessage = await httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, "subscriptions"), cancellationToken); String responseBody = await responseMessage.Content.ReadAsStringAsync(cancellationToken); @@ -53,13 +57,13 @@ public static async Task> GetSubscriptions(Http if (responseMessage.IsSuccessStatusCode) { List list = JsonConvert.DeserializeObject>(responseBody); - return list; + return Result.Success(list); } - throw new Exception($"Response was [{responseBody}] and status code was [{responseMessage.StatusCode}]"); + return Result.Failure($"Response was [{responseBody}] and status code was [{responseMessage.StatusCode}]"); } catch (Exception ex) { - throw new Exception($"Unable to get persistent subscription list. [{ex}]"); + return Result.Failure($"Unable to get persistent subscription list. [{ex}]"); } } @@ -76,19 +80,20 @@ public async Task GetSubscriptions(Boolean forceRefresh this.WriteTrace("Full refresh on repository"); - List list = await this.GetAllSubscriptions(cancellationToken); + Result> list = await this.GetAllSubscriptions(cancellationToken); + if (list.IsFailed) { + this.WriteTrace(list.Message); + return this.Subscriptions; + } this.FullRefreshHits++; - this.Subscriptions = this.Subscriptions.Update(list); + this.Subscriptions = this.Subscriptions.Update(list.Data); this.WriteTrace($"Full refresh on repository completed {this.FullRefreshHits}"); return this.Subscriptions; } - catch (Exception ex) { - throw new Exception($"Unable to get persistent subscription list. [{ex}]"); - } finally { Interlocked.Exchange(ref this.running, 0); } diff --git a/Shared.EventStoreContext.Tests/EventStoreDockerHelper.cs b/Shared.EventStoreContext.Tests/EventStoreDockerHelper.cs index 16d2671c..eef784ba 100644 --- a/Shared.EventStoreContext.Tests/EventStoreDockerHelper.cs +++ b/Shared.EventStoreContext.Tests/EventStoreDockerHelper.cs @@ -55,7 +55,7 @@ public override async Task CreateSubscriptions(){ } public override async Task StartContainersForScenarioRun(String scenarioName, DockerServices services) { - this.DockerPlatform = BaseDockerHelper.GetDockerEnginePlatform(); + this.DockerPlatform = BaseDockerHelper.GetDockerEnginePlatform().Data; this.TestId = Guid.NewGuid(); INetworkService networkService = this.SetupTestNetwork("eventstoretestnetwork", true); this.SetupContainerNames(); diff --git a/Shared.IntegrationTesting/BaseDockerHelper.cs b/Shared.IntegrationTesting/BaseDockerHelper.cs index 1e044f35..8ae44e02 100644 --- a/Shared.IntegrationTesting/BaseDockerHelper.cs +++ b/Shared.IntegrationTesting/BaseDockerHelper.cs @@ -1,4 +1,5 @@ using System.Net.Http.Headers; +using SimpleResults; namespace Shared.IntegrationTesting; @@ -134,8 +135,8 @@ protected BaseDockerHelper(Boolean skipHealthChecks=false) { })); // Setup the default image details - DockerEnginePlatform engineType = BaseDockerHelper.GetDockerEnginePlatform(); - if (engineType == DockerEnginePlatform.Windows){ + SimpleResults.Result engineType = BaseDockerHelper.GetDockerEnginePlatform(); + if (engineType.Data == DockerEnginePlatform.Windows){ this.ImageDetails.Add(ContainerType.SqlServer, ("iamrjindal/sqlserverexpress:2019", true)); this.ImageDetails.Add(ContainerType.EventStore, ("stuartferguson/eventstore_windows", true)); this.ImageDetails.Add(ContainerType.MessagingService, ("stuartferguson/messagingservicewindows:master", true)); @@ -222,22 +223,22 @@ public List GetCommonEnvironmentVariables(){ }; } - public static DockerEnginePlatform GetDockerEnginePlatform(){ + public static SimpleResults.Result GetDockerEnginePlatform(){ try{ IHostService docker = BaseDockerHelper.GetDockerHost(); if (docker.Host.IsLinuxEngine()){ - return DockerEnginePlatform.Linux; + return Result.Success(DockerEnginePlatform.Linux); } if (docker.Host.IsWindowsEngine()){ - return DockerEnginePlatform.Windows; + return Result.Success(DockerEnginePlatform.Windows); } - return DockerEnginePlatform.Unknown; + return Result.Success(DockerEnginePlatform.Unknown); } catch(Exception e){ - throw new ApplicationException("Unable to determine docker Engine Platform", e); + return Result.Failure($"Unable to determine docker Engine Platform. Exception [{e.Message}]"); } } @@ -551,11 +552,11 @@ public virtual ContainerBuilder SetupTestHostContainer(){ public virtual INetworkService SetupTestNetwork(String networkName = null, Boolean reuseIfExists = false){ networkName = String.IsNullOrEmpty(networkName) ? $"testnw{this.TestId:N}" : networkName; - DockerEnginePlatform engineType = BaseDockerHelper.GetDockerEnginePlatform(); + SimpleResults.Result engineType = BaseDockerHelper.GetDockerEnginePlatform(); - if (engineType == DockerEnginePlatform.Windows){ + if (engineType.Data == DockerEnginePlatform.Windows){ var docker = BaseDockerHelper.GetDockerHost(); - var network = docker.GetNetworks().Where(nw => nw.Name == networkName).SingleOrDefault(); + var network = docker.GetNetworks().SingleOrDefault(nw => nw.Name == networkName); if (network == null){ Dictionary driverOptions = new Dictionary(); driverOptions.Add("com.docker.network.windowsshim.networkname", networkName); @@ -571,7 +572,7 @@ public virtual INetworkService SetupTestNetwork(String networkName = null, return network; } - if (engineType == DockerEnginePlatform.Linux){ + if (engineType.Data == DockerEnginePlatform.Linux){ // Build a network NetworkBuilder networkService = new Builder().UseNetwork(networkName).ReuseIfExist(); diff --git a/Shared.IntegrationTesting/DockerHelper.cs b/Shared.IntegrationTesting/DockerHelper.cs index 828bec93..3530641f 100644 --- a/Shared.IntegrationTesting/DockerHelper.cs +++ b/Shared.IntegrationTesting/DockerHelper.cs @@ -71,7 +71,7 @@ protected virtual void SetHostTraceFolder(String scenarioName) { public override async Task StartContainersForScenarioRun(String scenarioName, DockerServices dockerServices){ - this.DockerPlatform = BaseDockerHelper.GetDockerEnginePlatform(); + this.DockerPlatform = BaseDockerHelper.GetDockerEnginePlatform().Data; this.DockerCredentials.ShouldNotBeNull(); this.SqlCredentials.ShouldNotBeNull(); diff --git a/Shared.IntegrationTesting/ReqnrollTableHelper.cs b/Shared.IntegrationTesting/ReqnrollTableHelper.cs index fe1e21e4..a800a05c 100644 --- a/Shared.IntegrationTesting/ReqnrollTableHelper.cs +++ b/Shared.IntegrationTesting/ReqnrollTableHelper.cs @@ -21,7 +21,7 @@ public static Boolean GetBooleanValue(DataTableRow row, public static DateTime GetDateForDateString(String dateString, DateTime today) { - switch(dateString.ToUpper()) + switch(dateString.ToUpperInvariant()) { case "TODAY": return today.Date; diff --git a/Shared.Results/PolicyFactory.cs b/Shared.Results/PolicyFactory.cs index 33204854..3554f83e 100644 --- a/Shared.Results/PolicyFactory.cs +++ b/Shared.Results/PolicyFactory.cs @@ -10,7 +10,7 @@ public static class PolicyFactory { private enum LogType { Retry, Final } - public static EventHandler Log; + public static readonly EventHandler Log; public static IAsyncPolicy CreatePolicy(int retryCount = 5, TimeSpan? retryDelay = null, string policyTag = "", Func? shouldRetry = null, diff --git a/Shared.Tests/MiddlewareTests.cs b/Shared.Tests/MiddlewareTests.cs index d99871e1..69ab9396 100644 --- a/Shared.Tests/MiddlewareTests.cs +++ b/Shared.Tests/MiddlewareTests.cs @@ -381,7 +381,7 @@ public Object GetService(Type serviceType){ } public class TestHealthChecksBuilder : IHealthChecksBuilder{ - public List Registrations = new List(); + internal readonly List Registrations = new List(); public TestHealthChecksBuilder(){ this.Services = new ServiceCollection();