From b6bdf901a9254642eb6603e1e1146b83c8623f2a Mon Sep 17 00:00:00 2001 From: Hubert Plociniczak Date: Wed, 29 Nov 2023 20:02:59 +0100 Subject: [PATCH] Cleanup after each test run (#8418) Reducing leaks when running our test suite. Potentially fixes #8408. # Important Notes Managed to keep thread count and memory in between runs relatively stable. Initially: ![Screenshot from 2023-11-29 11-06-04](https://github.com/enso-org/enso/assets/292128/af437d8a-9111-4bd6-9033-a59030c7ebed) Now: ![Screenshot from 2023-11-29 15-57-34](https://github.com/enso-org/enso/assets/292128/3c1f8aef-fe7e-4f5b-a236-12c86ea8b906) The screenshot illustrates for `RuntimeVisualizationsTest` only. Will need to be applied in other places as well. Applying the same style to `runtime-with-instruments`. Before: ![Screenshot from 2023-11-29 16-55-11](https://github.com/enso-org/enso/assets/292128/385e11be-0265-431d-b0d7-c5096df11c50) After: ![Screenshot from 2023-11-29 16-50-07](https://github.com/enso-org/enso/assets/292128/5e8c28ea-e921-484a-a82c-9f2d3e827e8b) --- .../execution/CommandExecutionEngine.scala | 1 + .../test/instrument/BuiltinTypesTest.scala | 18 +++++++++++++----- .../instrument/RuntimeAsyncCommandsTest.scala | 18 +++++++++++++----- .../instrument/RuntimeComponentsTest.scala | 15 +++++++++++---- .../test/instrument/RuntimeErrorsTest.scala | 16 +++++++++++----- .../RuntimeExecutionEnvironmentTest.scala | 15 +++++++++++---- .../instrument/RuntimeInstrumentTest.scala | 18 +++++++++++++----- .../instrument/RuntimeRefactoringTest.scala | 18 +++++++++++++----- .../test/instrument/RuntimeServerTest.scala | 19 ++++++++++++++----- .../test/instrument/RuntimeStdlibTest.scala | 15 +++++++++++---- .../RuntimeSuggestionUpdatesTest.scala | 18 +++++++++++++----- .../RuntimeVisualizationsTest.scala | 15 +++++++++++++-- .../instrument/InstrumentTestContext.scala | 4 ++++ .../locking/ThreadSafeFileLockManager.scala | 2 ++ .../locking/ThreadSafeLockManager.scala | 4 +++- .../test/TestLocalLockManager.scala | 2 ++ 16 files changed, 148 insertions(+), 50 deletions(-) diff --git a/engine/runtime-instrument-common/src/main/scala/org/enso/interpreter/instrument/execution/CommandExecutionEngine.scala b/engine/runtime-instrument-common/src/main/scala/org/enso/interpreter/instrument/execution/CommandExecutionEngine.scala index f786373102..ff3672c7bc 100644 --- a/engine/runtime-instrument-common/src/main/scala/org/enso/interpreter/instrument/execution/CommandExecutionEngine.scala +++ b/engine/runtime-instrument-common/src/main/scala/org/enso/interpreter/instrument/execution/CommandExecutionEngine.scala @@ -103,6 +103,7 @@ class CommandExecutionEngine(interpreterContext: InterpreterContext) /** @inheritdoc */ override def stop(): Unit = { jobExecutionEngine.stop() + sequentialExecutionService.shutdownNow() commandExecutor.shutdownNow() } diff --git a/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/BuiltinTypesTest.scala b/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/BuiltinTypesTest.scala index 8a8ac9e614..b47a805472 100644 --- a/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/BuiltinTypesTest.scala +++ b/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/BuiltinTypesTest.scala @@ -1,6 +1,6 @@ package org.enso.interpreter.test.instrument -import org.enso.distribution.FileSystem +import org.apache.commons.io.FileUtils import org.enso.distribution.locking.ThreadSafeFileLockManager import org.enso.interpreter.runtime.`type`.ConstantsGen import org.enso.interpreter.test.Metadata @@ -16,6 +16,8 @@ import java.io.{ByteArrayOutputStream, File} import java.nio.file.{Files, Path, Paths} import java.util.UUID import java.util.logging.Level +import scala.concurrent.Await +import scala.concurrent.duration._ @scala.annotation.nowarn("msg=multiarg infix syntax") class BuiltinTypesTest @@ -30,8 +32,7 @@ class BuiltinTypesTest class TestContext(packageName: String) extends InstrumentTestContext { val tmpDir: Path = Files.createTempDirectory("enso-test-packages") - sys.addShutdownHook(FileSystem.removeDirectoryIfExists(tmpDir)) - val lockManager = new ThreadSafeFileLockManager(tmpDir.resolve("locks")) + val lockManager = new ThreadSafeFileLockManager(tmpDir.resolve("locks")) val runtimeServerEmulator = new RuntimeServerEmulator(messageQueue, lockManager) @@ -98,8 +99,15 @@ class BuiltinTypesTest val Some(Api.Response(_, Api.InitializedNotification())) = context.receive } override protected def afterEach(): Unit = { - context.executionContext.context.close() - context.runtimeServerEmulator.terminate() + if (context != null) { + context.reset() + context.executionContext.context.close() + Await.ready(context.runtimeServerEmulator.terminate(), 5.seconds) + context.lockManager.reset() + context.out.reset() + FileUtils.deleteQuietly(context.tmpDir.toFile) + context = null + } } def runCode(contextId: UUID, requestId: UUID, contents: String): Unit = { diff --git a/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeAsyncCommandsTest.scala b/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeAsyncCommandsTest.scala index c16c9c6fe3..78b27421d7 100644 --- a/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeAsyncCommandsTest.scala +++ b/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeAsyncCommandsTest.scala @@ -1,6 +1,6 @@ package org.enso.interpreter.test.instrument -import org.enso.distribution.FileSystem +import org.apache.commons.io.FileUtils import org.enso.distribution.locking.ThreadSafeFileLockManager import org.enso.interpreter.test.Metadata import org.enso.pkg.{Package, PackageManager} @@ -16,6 +16,8 @@ import java.io.{ByteArrayOutputStream, File} import java.nio.file.{Files, Path, Paths} import java.util.UUID import java.util.logging.Level +import scala.concurrent.Await +import scala.concurrent.duration._ @scala.annotation.nowarn("msg=multiarg infix syntax") class RuntimeAsyncCommandsTest @@ -29,8 +31,7 @@ class RuntimeAsyncCommandsTest class TestContext(packageName: String) extends InstrumentTestContext { val tmpDir: Path = Files.createTempDirectory("enso-test-packages") - sys.addShutdownHook(FileSystem.removeDirectoryIfExists(tmpDir)) - val lockManager = new ThreadSafeFileLockManager(tmpDir.resolve("locks")) + val lockManager = new ThreadSafeFileLockManager(tmpDir.resolve("locks")) val runtimeServerEmulator = new RuntimeServerEmulator(messageQueue, lockManager) @@ -103,8 +104,15 @@ class RuntimeAsyncCommandsTest val Some(Api.Response(_, Api.InitializedNotification())) = context.receive } override protected def afterEach(): Unit = { - context.executionContext.context.close() - context.runtimeServerEmulator.terminate() + if (context != null) { + context.reset() + context.executionContext.context.close() + Await.ready(context.runtimeServerEmulator.terminate(), 5.seconds) + context.lockManager.reset() + context.out.reset() + FileUtils.deleteQuietly(context.tmpDir.toFile) + context = null + } } it should "interrupt stopped execution context" in { diff --git a/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeComponentsTest.scala b/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeComponentsTest.scala index 210d1fe194..f1ec49acb0 100644 --- a/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeComponentsTest.scala +++ b/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeComponentsTest.scala @@ -1,6 +1,6 @@ package org.enso.interpreter.test.instrument -import org.enso.distribution.FileSystem +import org.apache.commons.io.FileUtils import org.enso.distribution.locking.ThreadSafeFileLockManager import org.enso.editions.LibraryName import org.enso.interpreter.runtime @@ -28,6 +28,7 @@ import java.nio.file.{Files, Path, Paths} import java.util.UUID import java.util.concurrent.{LinkedBlockingQueue, TimeUnit} import java.util.logging.Level +import scala.concurrent.Await import scala.concurrent.duration._ @scala.annotation.nowarn("msg=multiarg infix syntax") @@ -74,7 +75,6 @@ class RuntimeComponentsTest new LinkedBlockingQueue() val tmpDir: Path = Files.createTempDirectory("enso-test-packages") - sys.addShutdownHook(FileSystem.removeDirectoryIfExists(tmpDir)) val distributionHome: File = Paths.get("../../distribution/component").toFile.getAbsoluteFile val editionHome: File = @@ -224,8 +224,15 @@ class RuntimeComponentsTest val Some(Api.Response(_, Api.InitializedNotification())) = context.receive } override protected def afterEach(): Unit = { - context.executionContext.context.close() - context.runtimeServerEmulator.terminate() + if (context != null) { + context.messageQueue.clear() + context.executionContext.context.close() + Await.ready(context.runtimeServerEmulator.terminate(), 5.seconds) + context.lockManager.reset() + context.out.reset() + FileUtils.deleteQuietly(context.tmpDir.toFile) + context = null + } } it should "load library extended by the component group" in { diff --git a/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeErrorsTest.scala b/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeErrorsTest.scala index da9aa47c81..c68999ab12 100644 --- a/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeErrorsTest.scala +++ b/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeErrorsTest.scala @@ -1,6 +1,6 @@ package org.enso.interpreter.test.instrument -import org.enso.distribution.FileSystem +import org.apache.commons.io.FileUtils import org.enso.distribution.locking.ThreadSafeFileLockManager import org.enso.interpreter.runtime.`type`.ConstantsGen import org.enso.interpreter.test.Metadata @@ -35,8 +35,7 @@ class RuntimeErrorsTest class TestContext(packageName: String) extends InstrumentTestContext { val tmpDir: Path = Files.createTempDirectory("enso-test-packages") - sys.addShutdownHook(FileSystem.removeDirectoryIfExists(tmpDir)) - val lockManager = new ThreadSafeFileLockManager(tmpDir.resolve("locks")) + val lockManager = new ThreadSafeFileLockManager(tmpDir.resolve("locks")) val runtimeServerEmulator = new RuntimeServerEmulator(messageQueue, lockManager) @@ -116,8 +115,15 @@ class RuntimeErrorsTest } override protected def afterEach(): Unit = { - context.executionContext.context.close() - Await.ready(context.runtimeServerEmulator.terminate(), 5.seconds) + if (context != null) { + context.reset() + context.executionContext.context.close() + Await.ready(context.runtimeServerEmulator.terminate(), 5.seconds) + context.lockManager.reset() + context.out.reset() + FileUtils.deleteQuietly(context.tmpDir.toFile) + context = null + } } it should "return panic sentinels in method body" in { diff --git a/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeExecutionEnvironmentTest.scala b/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeExecutionEnvironmentTest.scala index 90fc180267..b669e60d63 100644 --- a/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeExecutionEnvironmentTest.scala +++ b/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeExecutionEnvironmentTest.scala @@ -1,6 +1,6 @@ package org.enso.interpreter.test.instrument -import org.enso.distribution.FileSystem +import org.apache.commons.io.FileUtils import org.enso.distribution.locking.ThreadSafeFileLockManager import org.enso.interpreter.runtime.EnsoContext import org.enso.interpreter.runtime.`type`.ConstantsGen @@ -19,6 +19,7 @@ import java.io.{ByteArrayOutputStream, File} import java.nio.file.{Files, Path, Paths} import java.util.UUID import java.util.logging.Level +import scala.concurrent.Await import scala.concurrent.duration._ @scala.annotation.nowarn("msg=multiarg infix syntax") @@ -42,7 +43,6 @@ class RuntimeExecutionEnvironmentTest class TestContext(packageName: String) extends InstrumentTestContext { val tmpDir: Path = Files.createTempDirectory("enso-test-packages") - sys.addShutdownHook(FileSystem.removeDirectoryIfExists(tmpDir)) val distributionHome: File = Paths.get("../../distribution/component").toFile.getAbsoluteFile val editionHome: File = @@ -129,8 +129,15 @@ class RuntimeExecutionEnvironmentTest } override protected def afterEach(): Unit = { - context.executionContext.context.close() - context.runtimeServerEmulator.terminate() + if (context != null) { + context.reset() + context.executionContext.context.close() + Await.ready(context.runtimeServerEmulator.terminate(), 5.seconds) + context.lockManager.reset() + context.out.reset() + FileUtils.deleteQuietly(context.tmpDir.toFile) + context = null + } } it should "panic when output context is not enabled" in { diff --git a/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeInstrumentTest.scala b/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeInstrumentTest.scala index 20811f34ba..7d9ffdc677 100644 --- a/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeInstrumentTest.scala +++ b/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeInstrumentTest.scala @@ -1,6 +1,6 @@ package org.enso.interpreter.test.instrument -import org.enso.distribution.FileSystem +import org.apache.commons.io.FileUtils import org.enso.distribution.locking.ThreadSafeFileLockManager import org.enso.interpreter.runtime.`type`.{Constants, ConstantsGen} import org.enso.interpreter.test.Metadata @@ -16,6 +16,8 @@ import org.scalatest.matchers.should.Matchers import java.io.{ByteArrayOutputStream, File} import java.nio.file.{Files, Path, Paths} import java.util.UUID +import scala.concurrent.Await +import scala.concurrent.duration._ @scala.annotation.nowarn("msg=multiarg infix syntax") class RuntimeInstrumentTest @@ -29,8 +31,7 @@ class RuntimeInstrumentTest class TestContext(packageName: String) extends InstrumentTestContext { val tmpDir: Path = Files.createTempDirectory("enso-test-packages") - sys.addShutdownHook(FileSystem.removeDirectoryIfExists(tmpDir)) - val lockManager = new ThreadSafeFileLockManager(tmpDir.resolve("locks")) + val lockManager = new ThreadSafeFileLockManager(tmpDir.resolve("locks")) val runtimeServerEmulator = new RuntimeServerEmulator(messageQueue, lockManager) @@ -101,8 +102,15 @@ class RuntimeInstrumentTest } override protected def afterEach(): Unit = { - context.executionContext.context.close() - context.runtimeServerEmulator.terminate() + if (context != null) { + context.reset() + context.executionContext.context.close() + Await.ready(context.runtimeServerEmulator.terminate(), 5.seconds) + context.lockManager.reset() + context.out.reset() + FileUtils.deleteQuietly(context.tmpDir.toFile) + context = null + } } it should "instrument simple expression" in { diff --git a/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeRefactoringTest.scala b/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeRefactoringTest.scala index 6bff1ce079..ff79ddffd1 100644 --- a/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeRefactoringTest.scala +++ b/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeRefactoringTest.scala @@ -1,7 +1,7 @@ package org.enso.interpreter.test.instrument +import org.apache.commons.io.FileUtils import org.apache.commons.io.output.TeeOutputStream -import org.enso.distribution.FileSystem import org.enso.distribution.locking.ThreadSafeFileLockManager import org.enso.interpreter.runtime.`type`.ConstantsGen import org.enso.interpreter.test.Metadata @@ -20,6 +20,8 @@ import java.io.{ByteArrayOutputStream, File} import java.nio.file.{Files, Path, Paths} import java.util.UUID import java.util.logging.Level +import scala.concurrent.Await +import scala.concurrent.duration._ @scala.annotation.nowarn("msg=multiarg infix syntax") class RuntimeRefactoringTest @@ -34,8 +36,7 @@ class RuntimeRefactoringTest class TestContext(packageName: String) extends InstrumentTestContext { val tmpDir: Path = Files.createTempDirectory("enso-test-packages") - sys.addShutdownHook(FileSystem.removeDirectoryIfExists(tmpDir)) - val lockManager = new ThreadSafeFileLockManager(tmpDir.resolve("locks")) + val lockManager = new ThreadSafeFileLockManager(tmpDir.resolve("locks")) val runtimeServerEmulator = new RuntimeServerEmulator(messageQueue, lockManager) @@ -101,8 +102,15 @@ class RuntimeRefactoringTest } override protected def afterEach(): Unit = { - context.executionContext.context.close() - context.runtimeServerEmulator.terminate() + if (context != null) { + context.reset() + context.executionContext.context.close() + Await.ready(context.runtimeServerEmulator.terminate(), 5.seconds) + context.lockManager.reset() + context.out.reset() + FileUtils.deleteQuietly(context.tmpDir.toFile) + context = null + } } "RuntimeServer" should "rename operator in main body" in { diff --git a/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeServerTest.scala b/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeServerTest.scala index b70dd2957f..af346aca1a 100644 --- a/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeServerTest.scala +++ b/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeServerTest.scala @@ -1,6 +1,6 @@ package org.enso.interpreter.test.instrument -import org.enso.distribution.FileSystem +import org.apache.commons.io.FileUtils import org.enso.distribution.locking.ThreadSafeFileLockManager import org.enso.interpreter.runtime.`type`.{Constants, ConstantsGen, Types} import org.enso.interpreter.runtime.EnsoContext @@ -21,6 +21,9 @@ import java.nio.file.{Files, Path, Paths} import java.util.UUID import org.apache.commons.io.output.TeeOutputStream +import scala.concurrent.Await +import scala.concurrent.duration._ + @scala.annotation.nowarn("msg=multiarg infix syntax") class RuntimeServerTest extends AnyFlatSpec @@ -34,8 +37,7 @@ class RuntimeServerTest class TestContext(packageName: String) extends InstrumentTestContext { val tmpDir: Path = Files.createTempDirectory("enso-test-packages") - sys.addShutdownHook(FileSystem.removeDirectoryIfExists(tmpDir)) - val lockManager = new ThreadSafeFileLockManager(tmpDir.resolve("locks")) + val lockManager = new ThreadSafeFileLockManager(tmpDir.resolve("locks")) val runtimeServerEmulator = new RuntimeServerEmulator(messageQueue, lockManager) @@ -368,8 +370,15 @@ class RuntimeServerTest } override protected def afterEach(): Unit = { - context.executionContext.context.close() - context.runtimeServerEmulator.terminate() + if (context != null) { + context.reset() + context.executionContext.context.close() + Await.ready(context.runtimeServerEmulator.terminate(), 5.seconds) + context.lockManager.reset() + context.out.reset() + FileUtils.deleteQuietly(context.tmpDir.toFile) + context = null + } } "RuntimeServer" should "push and pop functions on the stack" in { diff --git a/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeStdlibTest.scala b/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeStdlibTest.scala index fd2b563dc9..65b3fbc4ea 100644 --- a/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeStdlibTest.scala +++ b/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeStdlibTest.scala @@ -1,6 +1,6 @@ package org.enso.interpreter.test.instrument -import org.enso.distribution.FileSystem +import org.apache.commons.io.FileUtils import org.enso.distribution.locking.ThreadSafeFileLockManager import org.enso.interpreter.test.Metadata import org.enso.pkg.{Package, PackageManager, QualifiedName} @@ -19,6 +19,7 @@ import java.util.UUID import java.util.concurrent.{LinkedBlockingQueue, TimeUnit} import java.util.logging.Level import scala.collection.mutable +import scala.concurrent.Await import scala.concurrent.duration._ @scala.annotation.nowarn("msg=multiarg infix syntax") @@ -45,7 +46,6 @@ class RuntimeStdlibTest new LinkedBlockingQueue() val tmpDir: Path = Files.createTempDirectory("enso-test-packages") - sys.addShutdownHook(FileSystem.removeDirectoryIfExists(tmpDir)) val distributionHome: File = Paths.get("../../distribution/component").toFile.getAbsoluteFile val editionHome: File = @@ -187,8 +187,15 @@ class RuntimeStdlibTest } override protected def afterEach(): Unit = { - context.executionContext.context.close() - context.runtimeServerEmulator.terminate() + if (context != null) { + context.messageQueue.clear() + context.executionContext.context.close() + Await.ready(context.runtimeServerEmulator.terminate(), 5.seconds) + context.lockManager.reset() + context.out.reset() + FileUtils.deleteQuietly(context.tmpDir.toFile) + context = null + } } it should "import Base modules" in { diff --git a/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeSuggestionUpdatesTest.scala b/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeSuggestionUpdatesTest.scala index d1a258703e..9e902a8970 100644 --- a/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeSuggestionUpdatesTest.scala +++ b/engine/runtime-with-instruments/src/test/scala/org/enso/interpreter/test/instrument/RuntimeSuggestionUpdatesTest.scala @@ -1,6 +1,6 @@ package org.enso.interpreter.test.instrument -import org.enso.distribution.FileSystem +import org.apache.commons.io.FileUtils import org.enso.distribution.locking.ThreadSafeFileLockManager import org.enso.interpreter.runtime.`type`.ConstantsGen import org.enso.pkg.{Package, PackageManager} @@ -18,6 +18,8 @@ import java.io.{ByteArrayOutputStream, File} import java.nio.file.{Files, Path, Paths} import java.util.UUID import java.util.logging.Level +import scala.concurrent.Await +import scala.concurrent.duration._ @scala.annotation.nowarn("msg=multiarg infix syntax") class RuntimeSuggestionUpdatesTest @@ -30,8 +32,7 @@ class RuntimeSuggestionUpdatesTest class TestContext(packageName: String) extends InstrumentTestContext { val tmpDir: Path = Files.createTempDirectory("enso-test-packages") - sys.addShutdownHook(FileSystem.removeDirectoryIfExists(tmpDir)) - val lockManager = new ThreadSafeFileLockManager(tmpDir.resolve("locks")) + val lockManager = new ThreadSafeFileLockManager(tmpDir.resolve("locks")) val runtimeServerEmulator = new RuntimeServerEmulator(messageQueue, lockManager) @@ -104,8 +105,15 @@ class RuntimeSuggestionUpdatesTest } override protected def afterEach(): Unit = { - context.executionContext.context.close() - context.runtimeServerEmulator.terminate() + if (context != null) { + context.reset() + context.executionContext.context.close() + Await.ready(context.runtimeServerEmulator.terminate(), 5.seconds) + context.lockManager.reset() + context.out.reset() + FileUtils.deleteQuietly(context.tmpDir.toFile) + context = null + } } it should "send suggestion updates after file modification" in { diff --git a/engine/runtime-with-polyglot/src/test/scala/org/enso/interpreter/test/instrument/RuntimeVisualizationsTest.scala b/engine/runtime-with-polyglot/src/test/scala/org/enso/interpreter/test/instrument/RuntimeVisualizationsTest.scala index 013a4a9b78..c3c4126ef9 100644 --- a/engine/runtime-with-polyglot/src/test/scala/org/enso/interpreter/test/instrument/RuntimeVisualizationsTest.scala +++ b/engine/runtime-with-polyglot/src/test/scala/org/enso/interpreter/test/instrument/RuntimeVisualizationsTest.scala @@ -32,8 +32,7 @@ class RuntimeVisualizationsTest class TestContext(packageName: String) extends InstrumentTestContext { val tmpDir: Path = Files.createTempDirectory("enso-test-packages") - sys.addShutdownHook(FileUtils.deleteQuietly(tmpDir.toFile)) - val lockManager = new ThreadSafeFileLockManager(tmpDir.resolve("locks")) + val lockManager = new ThreadSafeFileLockManager(tmpDir.resolve("locks")) val runtimeServerEmulator = new RuntimeServerEmulator(messageQueue, lockManager) @@ -316,6 +315,18 @@ class RuntimeVisualizationsTest val Some(Api.Response(_, Api.InitializedNotification())) = context.receive } + override protected def afterEach(): Unit = { + if (context != null) { + context.reset() + context.executionContext.context.close() + context.runtimeServerEmulator.terminate() + context.lockManager.reset() + context.out.reset() + FileUtils.deleteQuietly(context.tmpDir.toFile) + context = null + } + } + it should "emit visualization update when expression is computed" in { val idMainRes = context.Main.metadata.addItem(99, 1) val contents = context.Main.code diff --git a/engine/runtime/src/test/scala/org/enso/interpreter/test/instrument/InstrumentTestContext.scala b/engine/runtime/src/test/scala/org/enso/interpreter/test/instrument/InstrumentTestContext.scala index 496f7aef69..812ecfc280 100644 --- a/engine/runtime/src/test/scala/org/enso/interpreter/test/instrument/InstrumentTestContext.scala +++ b/engine/runtime/src/test/scala/org/enso/interpreter/test/instrument/InstrumentTestContext.scala @@ -106,6 +106,10 @@ class InstrumentTestContext { true } + def reset(): Unit = { + messageQueue.clear() + } + } object InstrumentTestContext { diff --git a/lib/scala/distribution-manager/src/main/scala/org/enso/distribution/locking/ThreadSafeFileLockManager.scala b/lib/scala/distribution-manager/src/main/scala/org/enso/distribution/locking/ThreadSafeFileLockManager.scala index e2e50886a8..6f42d20faf 100644 --- a/lib/scala/distribution-manager/src/main/scala/org/enso/distribution/locking/ThreadSafeFileLockManager.scala +++ b/lib/scala/distribution-manager/src/main/scala/org/enso/distribution/locking/ThreadSafeFileLockManager.scala @@ -325,4 +325,6 @@ class ThreadSafeFileLockManager(locksRoot: Path) extends ThreadSafeLockManager { case LockType.Shared => lock.tryAcquireReader() } } + + override def reset(): Unit = localLocks.clear() } diff --git a/lib/scala/distribution-manager/src/main/scala/org/enso/distribution/locking/ThreadSafeLockManager.scala b/lib/scala/distribution-manager/src/main/scala/org/enso/distribution/locking/ThreadSafeLockManager.scala index 9aaee3d83e..3637c977d6 100644 --- a/lib/scala/distribution-manager/src/main/scala/org/enso/distribution/locking/ThreadSafeLockManager.scala +++ b/lib/scala/distribution-manager/src/main/scala/org/enso/distribution/locking/ThreadSafeLockManager.scala @@ -1,4 +1,6 @@ package org.enso.distribution.locking /** A [[LockManager]] which guarantees to be thread-safe. */ -trait ThreadSafeLockManager extends LockManager +trait ThreadSafeLockManager extends LockManager { + def reset(): Unit +} diff --git a/lib/scala/runtime-version-manager-test/src/main/scala/org/enso/runtimeversionmanager/test/TestLocalLockManager.scala b/lib/scala/runtime-version-manager-test/src/main/scala/org/enso/runtimeversionmanager/test/TestLocalLockManager.scala index d5c17b289e..9747871b48 100644 --- a/lib/scala/runtime-version-manager-test/src/main/scala/org/enso/runtimeversionmanager/test/TestLocalLockManager.scala +++ b/lib/scala/runtime-version-manager-test/src/main/scala/org/enso/runtimeversionmanager/test/TestLocalLockManager.scala @@ -58,4 +58,6 @@ class TestLocalLockManager extends ThreadSafeLockManager { case LockType.Shared => rwLock.readLock() } } + + override def reset(): Unit = {} }