diff --git a/src/Adaptive.Aeron.Tests/CncFileDescriptorTest.cs b/src/Adaptive.Aeron.Tests/CncFileDescriptorTest.cs index b7c2b70..5fb5474 100644 --- a/src/Adaptive.Aeron.Tests/CncFileDescriptorTest.cs +++ b/src/Adaptive.Aeron.Tests/CncFileDescriptorTest.cs @@ -35,10 +35,8 @@ class CncFileDescriptorTest [Test] public void ShouldAllocateCapacityForCounterMetadataBuffer() { - string aeronDir = Aeron.Context.GetAeronDirectoryName(); - MappedByteBuffer cncByteBuffer = IoUtil.MapExistingFile( - Path.Combine(aeronDir, "cnc.dat"), + Path.Combine(_driver.AeronDirectoryName, "cnc.dat"), MapMode.ReadOnly ); diff --git a/src/Adaptive.Aeron.Tests/ContextText.cs b/src/Adaptive.Aeron.Tests/ContextText.cs index 9d62f2d..f8c65bb 100644 --- a/src/Adaptive.Aeron.Tests/ContextText.cs +++ b/src/Adaptive.Aeron.Tests/ContextText.cs @@ -23,9 +23,14 @@ namespace Adaptive.Aeron.Tests public class ContextText { private EmbeddedMediaDriver _driver; + private Aeron.Context _ctx; [SetUp] - public void StartDriver() => _driver = new EmbeddedMediaDriver(); + public void SetUp() + { + _driver = new EmbeddedMediaDriver(); + _ctx = new Aeron.Context().AeronDirectoryName(_driver.AeronDirectoryName); + } [TearDown] public void StopDriver() => _driver?.Dispose(); @@ -33,20 +38,16 @@ public class ContextText [Test] public void ShouldNotAllowConcludeMoreThanOnce() { - var ctx = new Aeron.Context(); - - ctx.Conclude(); - Assert.Throws(typeof(ConcurrentConcludeException), () => ctx.Conclude()); + _ctx.Conclude(); + Assert.Throws(typeof(ConcurrentConcludeException), () => _ctx.Conclude()); } [Test] public void ShouldAllowConcludeOfClonedContext() { - var ctx = new Aeron.Context(); - - var ctx2 = ctx.Clone(); + var ctx2 = _ctx.Clone(); - ctx.Conclude(); + _ctx.Conclude(); ctx2.Conclude(); } } diff --git a/src/Adaptive.Aeron.Tests/EmbeddedMediaDriver.cs b/src/Adaptive.Aeron.Tests/EmbeddedMediaDriver.cs index 9b41ca6..c7c34e4 100644 --- a/src/Adaptive.Aeron.Tests/EmbeddedMediaDriver.cs +++ b/src/Adaptive.Aeron.Tests/EmbeddedMediaDriver.cs @@ -33,7 +33,7 @@ internal sealed class EmbeddedMediaDriver : IDisposable public EmbeddedMediaDriver() { - _aeronDir = Aeron.Context.GetAeronDirectoryName(); + _aeronDir = Path.Combine(Path.GetTempPath(), "aeron-" + Guid.NewGuid().ToString("N")); if (Directory.Exists(_aeronDir)) { try @@ -72,11 +72,21 @@ public EmbeddedMediaDriver() "-Daeron.driver.termination.validator=io.aeron.driver.DefaultAllowTerminationValidator" ); psi.ArgumentList.Add("-Daeron.threading.mode=SHARED"); + psi.ArgumentList.Add("-Daeron.dir.delete.on.shutdown=true"); psi.ArgumentList.Add("io.aeron.driver.MediaDriver"); _driver = Process.Start(psi) ?? throw new InvalidOperationException("failed to start media driver"); - WaitForDriverReady(); + try + { + WaitForDriverReady(); + } + catch + { + ShutdownDriver(); + _driver.Dispose(); + throw; + } } public string AeronDirectoryName => _aeronDir; @@ -94,20 +104,60 @@ public void Dispose() } if (!_driver.WaitForExit(ShutdownTimeoutMs)) + { + ShutdownDriver(); + } + + bool exited; + try + { + exited = _driver.HasExited; + } + catch + { + exited = false; + } + + _driver.Dispose(); + + if (exited) { try { - _driver.Kill(entireProcessTree: true); + if (Directory.Exists(_aeronDir)) + { + Directory.Delete(_aeronDir, recursive: true); + } } catch { } + } + } + + private void ShutdownDriver() + { + try + { + if (!_driver.HasExited) + { + _driver.Kill(entireProcessTree: true); + } + } + catch + { + } + + try + { _driver.WaitForExit(ShutdownTimeoutMs); } - _driver.Dispose(); + catch + { + } } - private static void WaitForDriverReady() + private void WaitForDriverReady() { var clock = new SystemEpochClock(); var deadline = clock.Time() + StartupTimeoutMs; @@ -115,9 +165,15 @@ private static void WaitForDriverReady() while (clock.Time() < deadline) { + if (_driver.HasExited) + { + throw new InvalidOperationException( + $"driver process exited prematurely with code {_driver.ExitCode}"); + } + try { - using var aeron = Aeron.Connect(); + using var aeron = Aeron.Connect(new Aeron.Context().AeronDirectoryName(_aeronDir)); return; } catch (Exception e) diff --git a/src/Adaptive.Aeron.Tests/SystemTest.cs b/src/Adaptive.Aeron.Tests/SystemTest.cs index cbdeb5f..4453b17 100644 --- a/src/Adaptive.Aeron.Tests/SystemTest.cs +++ b/src/Adaptive.Aeron.Tests/SystemTest.cs @@ -37,7 +37,7 @@ public class SystemTest [Test] public void BasicMessageTest() { - using var aeron = Aeron.Connect(); + using var aeron = Aeron.Connect(new Aeron.Context().AeronDirectoryName(_driver.AeronDirectoryName)); var publication = aeron.AddPublication("aeron:ipc", 1); var subscription = aeron.AddSubscription("aeron:ipc", 1); Await(() => publication.IsConnected); diff --git a/src/Adaptive.Archiver.IntegrationTests/PersistentSubscriptionTest.cs b/src/Adaptive.Archiver.IntegrationTests/PersistentSubscriptionTest.cs index faa030e..30d2680 100644 --- a/src/Adaptive.Archiver.IntegrationTests/PersistentSubscriptionTest.cs +++ b/src/Adaptive.Archiver.IntegrationTests/PersistentSubscriptionTest.cs @@ -2087,8 +2087,6 @@ public void ShouldContinueConsumingFromLiveWhileArchiveIsUnavailable() // Kill the archive JVM. PS is already in LIVE so its archive control session is idle; // it should keep consuming the live channel without noticing. - DisposeWithTimeout(AeronArchive, 3_000, "AeronArchive (mid-test)"); - AeronArchive = null; Archive.Dispose(); Archive = null;