From f976a31808d17724bd60c0a4d0013f5ebf550aa7 Mon Sep 17 00:00:00 2001 From: John Simons Date: Thu, 13 Aug 2026 08:12:21 +1000 Subject: [PATCH] Move Event Source setup to shared acceptance test projects Relocates the Event Source creation and wait logic from RavenDB-specific folders to the base acceptance test projects. This ensures the setup is available for all persistence types and prevents race conditions when multiple tests attempt to create the same event source during parallel execution. --- .../SetupFixture.cs | 29 ------------- .../SetupFixture.cs | 42 +++++++++++++++++++ .../SetupFixture.cs | 29 ------------- .../SetupFixture.cs | 42 +++++++++++++++++++ 4 files changed, 84 insertions(+), 58 deletions(-) delete mode 100644 src/ServiceControl.AcceptanceTests.RavenDB/SetupFixture.cs create mode 100644 src/ServiceControl.AcceptanceTests/SetupFixture.cs delete mode 100644 src/ServiceControl.Audit.AcceptanceTests.RavenDB/SetupFixture.cs create mode 100644 src/ServiceControl.Audit.AcceptanceTests/SetupFixture.cs diff --git a/src/ServiceControl.AcceptanceTests.RavenDB/SetupFixture.cs b/src/ServiceControl.AcceptanceTests.RavenDB/SetupFixture.cs deleted file mode 100644 index 5a24a3417e..0000000000 --- a/src/ServiceControl.AcceptanceTests.RavenDB/SetupFixture.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace ServiceControl.AcceptanceTests.RavenDB; - -using System; -using System.Diagnostics; -using System.Runtime.InteropServices; -using System.Threading; -using System.Threading.Tasks; -using NUnit.Framework; - -[SetUpFixture] -public class SetupFixture -{ - [OneTimeSetUp] - public async Task Setup() - { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - ServiceBus.Management.Infrastructure.Installers.EventSourceCreator.Create(); - - //There is a delay for this becoming true, tests will fall over if they interleave in the wrong way. - var cts = new CancellationTokenSource(TimeSpan.FromSeconds(60)); - while (!EventLog.SourceExists(ServiceBus.Management.Infrastructure.Installers.EventSourceCreator.SourceName)) - { - cts.Token.ThrowIfCancellationRequested(); - await Task.Delay(500, CancellationToken.None); - } - } - } -} \ No newline at end of file diff --git a/src/ServiceControl.AcceptanceTests/SetupFixture.cs b/src/ServiceControl.AcceptanceTests/SetupFixture.cs new file mode 100644 index 0000000000..55ddf35101 --- /dev/null +++ b/src/ServiceControl.AcceptanceTests/SetupFixture.cs @@ -0,0 +1,42 @@ +namespace ServiceControl.AcceptanceTests; + +using System; +using System.Diagnostics; +using System.Runtime.InteropServices; +using System.Runtime.Versioning; +using System.Threading; +using System.Threading.Tasks; +using NUnit.Framework; +using ServiceBus.Management.Infrastructure.Installers; + +[SetUpFixture] +public class SetupFixture +{ + [OneTimeSetUp] + public async Task Setup() + { + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return; + } + + // Every test runs the setup command, which creates the event source if it is missing. Tests run in + // parallel, so without creating it up front several of them race and all but one fail with + // "Source ServiceControl already exists". + EventSourceCreator.Create(); + + await WaitForSource(EventSourceCreator.SourceName); + } + + //There is a delay for this becoming true, tests will fall over if they interleave in the wrong way. + [SupportedOSPlatform("windows")] + static async Task WaitForSource(string sourceName) + { + var cts = new CancellationTokenSource(TimeSpan.FromSeconds(60)); + while (!EventLog.SourceExists(sourceName)) + { + cts.Token.ThrowIfCancellationRequested(); + await Task.Delay(500, CancellationToken.None); + } + } +} diff --git a/src/ServiceControl.Audit.AcceptanceTests.RavenDB/SetupFixture.cs b/src/ServiceControl.Audit.AcceptanceTests.RavenDB/SetupFixture.cs deleted file mode 100644 index 100af7975a..0000000000 --- a/src/ServiceControl.Audit.AcceptanceTests.RavenDB/SetupFixture.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace ServiceControl.Audit.AcceptanceTests.RavenDB; - -using System; -using System.Diagnostics; -using System.Runtime.InteropServices; -using System.Threading; -using System.Threading.Tasks; -using NUnit.Framework; - -[SetUpFixture] -public class SetupFixture -{ - [OneTimeSetUp] - public async Task Setup() - { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - Infrastructure.EventSourceCreator.Create(); - - //There is a delay for this becoming true, tests will fall over if they interleave in the wrong way. - var cts = new CancellationTokenSource(TimeSpan.FromSeconds(60)); - while (!EventLog.SourceExists(Infrastructure.EventSourceCreator.SourceName)) - { - cts.Token.ThrowIfCancellationRequested(); - await Task.Delay(500, CancellationToken.None); - } - } - } -} \ No newline at end of file diff --git a/src/ServiceControl.Audit.AcceptanceTests/SetupFixture.cs b/src/ServiceControl.Audit.AcceptanceTests/SetupFixture.cs new file mode 100644 index 0000000000..70acaa0843 --- /dev/null +++ b/src/ServiceControl.Audit.AcceptanceTests/SetupFixture.cs @@ -0,0 +1,42 @@ +namespace ServiceControl.Audit.AcceptanceTests; + +using System; +using System.Diagnostics; +using System.Runtime.InteropServices; +using System.Runtime.Versioning; +using System.Threading; +using System.Threading.Tasks; +using NUnit.Framework; +using ServiceControl.Audit.Infrastructure; + +[SetUpFixture] +public class SetupFixture +{ + [OneTimeSetUp] + public async Task Setup() + { + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return; + } + + // Every test runs the setup command, which creates the event source if it is missing. Tests run in + // parallel, so without creating it up front several of them race and all but one fail with + // "Source ServiceControl.Audit already exists". + EventSourceCreator.Create(); + + await WaitForSource(EventSourceCreator.SourceName); + } + + //There is a delay for this becoming true, tests will fall over if they interleave in the wrong way. + [SupportedOSPlatform("windows")] + static async Task WaitForSource(string sourceName) + { + var cts = new CancellationTokenSource(TimeSpan.FromSeconds(60)); + while (!EventLog.SourceExists(sourceName)) + { + cts.Token.ThrowIfCancellationRequested(); + await Task.Delay(500, CancellationToken.None); + } + } +}