From 0a12c9178f28369a1680212f7515f6c54a9e6d21 Mon Sep 17 00:00:00 2001 From: William Chong Date: Wed, 14 Feb 2024 20:10:52 +0400 Subject: [PATCH] Use SemVer --- .../EventStore.Client.Tests.Common.csproj | 1 + .../Fixtures/Base/EventStoreTestServer.cs | 17 +++++--- .../Fixtures/EventStoreFixture.cs | 42 ++++++++++--------- 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/test/EventStore.Client.Tests.Common/EventStore.Client.Tests.Common.csproj b/test/EventStore.Client.Tests.Common/EventStore.Client.Tests.Common.csproj index f666f3871..9cf152fb7 100644 --- a/test/EventStore.Client.Tests.Common/EventStore.Client.Tests.Common.csproj +++ b/test/EventStore.Client.Tests.Common/EventStore.Client.Tests.Common.csproj @@ -15,6 +15,7 @@ + diff --git a/test/EventStore.Client.Tests.Common/Fixtures/Base/EventStoreTestServer.cs b/test/EventStore.Client.Tests.Common/Fixtures/Base/EventStoreTestServer.cs index fb7e5f601..3cd95b5e7 100644 --- a/test/EventStore.Client.Tests.Common/Fixtures/Base/EventStoreTestServer.cs +++ b/test/EventStore.Client.Tests.Common/Fixtures/Base/EventStoreTestServer.cs @@ -1,18 +1,20 @@ using System.Net; using System.Net.Http; +using System.Text.RegularExpressions; using Ductus.FluentDocker.Builders; using Ductus.FluentDocker.Extensions; using Ductus.FluentDocker.Model.Builders; using Ductus.FluentDocker.Services; using Ductus.FluentDocker.Services.Extensions; using Polly; +using Semver; namespace EventStore.Client.Tests; public class EventStoreTestServer : IEventStoreTestServer { static readonly string ContainerName = "es-client-dotnet-test"; - static Version? _version; + static SemVersion? _version; readonly IContainerService _eventStore; readonly string _hostCertificatePath; readonly HttpClient _httpClient; @@ -69,7 +71,7 @@ public EventStoreTestServer( .Build(); } - public static Version Version => _version ??= GetVersion(); + public static SemVersion Version => _version ??= GetVersion(); public async Task StartAsync(CancellationToken cancellationToken = default) { _eventStore.Start(); @@ -99,7 +101,7 @@ public ValueTask DisposeAsync() { return new ValueTask(Task.CompletedTask); } - static Version GetVersion() { + static SemVersion GetVersion() { const string versionPrefix = "EventStoreDB version"; using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30)); @@ -111,9 +113,12 @@ static Version GetVersion() { using var log = eventstore.Logs(true, cts.Token); foreach (var line in log.ReadToEnd()) - if (line.StartsWith(versionPrefix) && - Version.TryParse(line[(versionPrefix.Length + 1)..].Split(' ')[0], out var version)) - return version; + if (line.StartsWith(versionPrefix)) { + var versionString = line[(versionPrefix.Length + 1)..].Split(' ')[0]; + if (SemVersion.TryParse(versionString, SemVersionStyles.Strict, out var version)) { + return version; + } + } throw new InvalidOperationException("Could not determine server version."); } diff --git a/test/EventStore.Client.Tests.Common/Fixtures/EventStoreFixture.cs b/test/EventStore.Client.Tests.Common/Fixtures/EventStoreFixture.cs index e59e7149d..ceb6b717a 100644 --- a/test/EventStore.Client.Tests.Common/Fixtures/EventStoreFixture.cs +++ b/test/EventStore.Client.Tests.Common/Fixtures/EventStoreFixture.cs @@ -5,6 +5,7 @@ using EventStore.Client.Tests.FluentDocker; using Serilog; using static System.TimeSpan; +using Semver; namespace EventStore.Client.Tests; @@ -24,7 +25,7 @@ this with { public EventStoreFixtureOptions WithoutDefaultCredentials() => this with { ClientSettings = ClientSettings.With(x => x.DefaultCredentials = null) }; - + public EventStoreFixtureOptions WithMaxAppendSize(uint maxAppendSize) => this with { Environment = Environment.With(x => x["EVENTSTORE_MAX_APPEND_SIZE"] = $"{maxAppendSize}") }; } @@ -64,12 +65,12 @@ protected EventStoreFixture(ConfigureFixture configure) { List TestRuns { get; } = new(); public ILogger Log => Logger; - + public ITestService Service { get; } public EventStoreFixtureOptions Options { get; } public Faker Faker { get; } = new Faker(); - public Version EventStoreVersion { get; private set; } = null!; + public SemVersion EventStoreVersion { get; private set; } = null!; public bool EventStoreHasLastStreamPosition { get; private set; } public EventStoreClient Streams { get; private set; } = null!; @@ -80,7 +81,7 @@ protected EventStoreFixture(ConfigureFixture configure) { public Func OnSetup { get; init; } = () => Task.CompletedTask; public Func OnTearDown { get; init; } = () => Task.CompletedTask; - + /// /// must test this /// @@ -96,7 +97,7 @@ protected EventStoreFixture(ConfigureFixture configure) { DefaultCredentials = Options.ClientSettings.DefaultCredentials, DefaultDeadline = Options.ClientSettings.DefaultDeadline }; - + InterlockedBoolean WarmUpCompleted { get; } = new InterlockedBoolean(); SemaphoreSlim WarmUpGatekeeper { get; } = new(1, 1); @@ -107,15 +108,15 @@ public void CaptureTestRun(ITestOutputHelper outputHelper) { Logger.Information(">>> Test Run {TestRunId} {Operation} <<<", testRunId, "starting"); Service.ReportStatus(); } - + public async Task InitializeAsync() { await Service.Start(); EventStoreVersion = GetEventStoreVersion(); EventStoreHasLastStreamPosition = (EventStoreVersion?.Major ?? int.MaxValue) >= 21; - + await WarmUpGatekeeper.WaitAsync(); - + try { if (!WarmUpCompleted.CurrentValue) { Logger.Warning("*** Warmup started ***"); @@ -127,9 +128,9 @@ await Task.WhenAll( InitClient(async x => Subscriptions = await x.WarmUp()), InitClient(async x => Operations = await x.WarmUp()) ); - + WarmUpCompleted.EnsureCalledOnce(); - + Logger.Warning("*** Warmup completed ***"); } else { @@ -139,9 +140,9 @@ await Task.WhenAll( finally { WarmUpGatekeeper.Release(); } - + await OnSetup(); - + return; async Task InitClient(Func action, bool execute = true) where T : EventStoreClientBase { @@ -150,9 +151,9 @@ async Task InitClient(Func action, bool execute = true) where T : await action(client); return client; } - - - static Version GetEventStoreVersion() { + + + static SemVersion GetEventStoreVersion() { const string versionPrefix = "EventStoreDB version"; using var cancellator = new CancellationTokenSource(FromSeconds(30)); @@ -165,9 +166,12 @@ static Version GetEventStoreVersion() { using var log = eventstore.Logs(true, cancellator.Token); foreach (var line in log.ReadToEnd()) - if (line.StartsWith(versionPrefix) && - Version.TryParse(line[(versionPrefix.Length + 1)..].Split(' ')[0], out var version)) - return version; + if (line.StartsWith(versionPrefix)) { + var versionString = line[(versionPrefix.Length + 1)..].Split(' ')[0]; + if (SemVersion.TryParse(versionString, SemVersionStyles.Strict, out var version)) { + return version; + } + } throw new InvalidOperationException("Could not determine server version."); } @@ -205,4 +209,4 @@ public abstract class EventStoreTests : IClassFixture where [Collection(nameof(EventStoreSharedDatabaseFixture))] public abstract class EventStoreSharedDatabaseTests(ITestOutputHelper output, TFixture fixture) : EventStoreTests(output, fixture) - where TFixture : EventStoreFixture; \ No newline at end of file + where TFixture : EventStoreFixture;