From ce620178a579566513ef6c93cf832d16b0917474 Mon Sep 17 00:00:00 2001 From: Erik Stel Date: Fri, 21 Jun 2019 21:37:04 +0200 Subject: [PATCH] Fix forked processes of TestResource from being terminated between TestCase methods being executed (P7) --- src/SUnit-Core/TestResource.class.st | 8 +++- ...ResourceWithForkedProcessTestCase.class.st | 37 ++++++++++++++++++ .../WithForkedProcessTestResource.class.st | 39 +++++++++++++++++++ 3 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 src/SUnit-Tests/TestResourceWithForkedProcessTestCase.class.st create mode 100644 src/SUnit-Tests/WithForkedProcessTestResource.class.st diff --git a/src/SUnit-Core/TestResource.class.st b/src/SUnit-Core/TestResource.class.st index a882e5e4272..53c98d31758 100644 --- a/src/SUnit-Core/TestResource.class.st +++ b/src/SUnit-Core/TestResource.class.st @@ -97,9 +97,13 @@ TestResource class >> makeAvailable [ current := nil. candidate := self new. self resources do: [:each | each availableFor: candidate]. - [candidate setUp. + + "Execute the TestResource's #setUp method within the DefaultExecutionEnvironment to prevent + forked processes created within #setUp from being terminated by the TestExecutionEnvironment + (in #checkForkedProcesses) it is running in." + DefaultExecutionEnvironment beActiveDuring: [[candidate setUp. candidate isAvailable ifTrue: [current := candidate]] - ensure: [current == candidate ifFalse: [candidate tearDown]] + ensure: [current == candidate ifFalse: [candidate tearDown]]] ] { #category : #'instance creation' } diff --git a/src/SUnit-Tests/TestResourceWithForkedProcessTestCase.class.st b/src/SUnit-Tests/TestResourceWithForkedProcessTestCase.class.st new file mode 100644 index 00000000000..37cedfae623 --- /dev/null +++ b/src/SUnit-Tests/TestResourceWithForkedProcessTestCase.class.st @@ -0,0 +1,37 @@ +" +SUnit tests for forked processes in test resources +" +Class { + #name : #TestResourceWithForkedProcessTestCase, + #superclass : #TestCase, + #category : #'SUnit-Tests-Core' +} + +{ #category : #accessing } +TestResourceWithForkedProcessTestCase class >> resources [ + + "Answer the TestResource class having a forked process" + + ^ Array with: WithForkedProcessTestResource +] + +{ #category : #tests } +TestResourceWithForkedProcessTestCase >> testFirst [ + + "Test whether the TestResource's forked process is not terminated. + A second test method will do the same and thereby validate that forked processes + of a TestResource do not get terminated (in between tests)." + + self + assert: WithForkedProcessTestResource current forkedProcess isTerminated not + description: 'A forked process within a TestResource should not be terminated' +] + +{ #category : #tests } +TestResourceWithForkedProcessTestCase >> testSecond [ + + "Test whether the TestResource's forked process is not terminated between tests" + + "Use the other test method's implementation" + self testFirst +] diff --git a/src/SUnit-Tests/WithForkedProcessTestResource.class.st b/src/SUnit-Tests/WithForkedProcessTestResource.class.st new file mode 100644 index 00000000000..758e72d6686 --- /dev/null +++ b/src/SUnit-Tests/WithForkedProcessTestResource.class.st @@ -0,0 +1,39 @@ +" +I am a TestResource for testing whether my forked processes do not get terminated +between individual TestCases being executed. +" +Class { + #name : #WithForkedProcessTestResource, + #superclass : #TestResource, + #instVars : [ + 'forkedProcess' + ], + #category : #'SUnit-Tests-Resources' +} + +{ #category : #accessing } +WithForkedProcessTestResource >> forkedProcess [ + + "Answer the receiver's forked process" + + ^ forkedProcess +] + +{ #category : #running } +WithForkedProcessTestResource >> setUp [ + + "Create a forked process which should live until the #tearDown message is received. + The process is and should remain in suspended state." + + super setUp. + forkedProcess := [ "empty process" ] newProcess +] + +{ #category : #running } +WithForkedProcessTestResource >> tearDown [ + + "Terminate forked process" + + super tearDown. + forkedProcess ifNotNil: [ forkedProcess terminate ] +]