Skip to content

Commit 4742a5b

Browse files
committed
Clarify ResetSystemProperties trait inheritance ordering.
1 parent 0eaf0b6 commit 4742a5b

File tree

5 files changed

+31
-15
lines changed

5 files changed

+31
-15
lines changed

core/src/test/scala/org/apache/spark/scheduler/SparkListenerSuite.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ import org.apache.spark.{LocalSparkContext, SparkContext}
2828
import org.apache.spark.executor.TaskMetrics
2929
import org.apache.spark.util.ResetSystemProperties
3030

31-
class SparkListenerSuite extends FunSuite with ResetSystemProperties with LocalSparkContext
32-
with Matchers with BeforeAndAfter with BeforeAndAfterAll {
31+
class SparkListenerSuite extends FunSuite with LocalSparkContext with Matchers with BeforeAndAfter
32+
with BeforeAndAfterAll with ResetSystemProperties {
3333

3434
/** Length of time to wait while draining listener events. */
3535
val WAIT_TIMEOUT_MILLIS = 10000

core/src/test/scala/org/apache/spark/storage/BlockManagerSuite.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import akka.util.Timeout
3333

3434
import org.mockito.Mockito.{mock, when}
3535

36-
import org.scalatest.{BeforeAndAfter, FunSuite, Matchers, PrivateMethodTester}
36+
import org.scalatest._
3737
import org.scalatest.concurrent.Eventually._
3838
import org.scalatest.concurrent.Timeouts._
3939

@@ -47,8 +47,8 @@ import org.apache.spark.storage.BlockManagerMessages.BlockManagerHeartbeat
4747
import org.apache.spark.util._
4848

4949

50-
class BlockManagerSuite extends FunSuite with ResetSystemProperties with Matchers
51-
with BeforeAndAfter with PrivateMethodTester {
50+
class BlockManagerSuite extends FunSuite with Matchers with BeforeAndAfterEach
51+
with PrivateMethodTester with ResetSystemProperties {
5252

5353
private val conf = new SparkConf(false)
5454
var store: BlockManager = null
@@ -78,7 +78,7 @@ class BlockManagerSuite extends FunSuite with ResetSystemProperties with Matcher
7878
manager
7979
}
8080

81-
before {
81+
override def beforeEach(): Unit = {
8282
val (actorSystem, boundPort) = AkkaUtils.createActorSystem(
8383
"test", "localhost", 0, conf = conf, securityManager = securityMgr)
8484
this.actorSystem = actorSystem
@@ -99,7 +99,7 @@ class BlockManagerSuite extends FunSuite with ResetSystemProperties with Matcher
9999
SizeEstimator invokePrivate initialize()
100100
}
101101

102-
after {
102+
override def afterEach(): Unit = {
103103
if (store != null) {
104104
store.stop()
105105
store = null

core/src/test/scala/org/apache/spark/util/AkkaUtilsSuite.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import org.apache.spark.storage.BlockManagerId
3131
/**
3232
* Test the AkkaUtils with various security settings.
3333
*/
34-
class AkkaUtilsSuite extends FunSuite with ResetSystemProperties with LocalSparkContext {
34+
class AkkaUtilsSuite extends FunSuite with LocalSparkContext with ResetSystemProperties {
3535

3636
test("remote fetch security bad password") {
3737
val conf = new SparkConf

core/src/test/scala/org/apache/spark/util/ResetSystemProperties.scala

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,39 @@ package org.apache.spark.util
1919

2020
import java.util.Properties
2121

22-
import org.scalatest.{Suite, SuiteMixin}
23-
22+
import org.scalatest.{BeforeAndAfterEach, Suite}
2423

2524
/**
2625
* Mixin for automatically resetting system properties that are modified in ScalaTest tests.
2726
* This resets the properties after each individual test.
2827
*
28+
* The order in which fixtures are mixed in affects the order in which they are invoked by tests.
29+
* If we have a suite `MySuite extends FunSuite with Foo with Bar`, then
30+
* Bar's `super` is Foo, so Bar's beforeEach() will and afterEach() methods will be invoked first
31+
* by the rest runner.
32+
*
33+
* This means that ResetSystemProperties should appear as the last trait in test suites that it's
34+
* mixed into in order to ensure that the system properties snapshot occurs as early as possible.
35+
* ResetSystemProperties calls super.afterEach() before performing its own cleanup, ensuring that
36+
* the old properties are restored as late as possible.
37+
*
2938
* See the "Composing fixtures by stacking traits" section at
3039
* http://www.scalatest.org/user_guide/sharing_fixtures for more details about this pattern.
3140
*/
32-
private[spark] trait ResetSystemProperties extends SuiteMixin { this: Suite =>
33-
abstract override def withFixture(test: NoArgTest) = {
34-
val oldProperties = new Properties(System.getProperties)
41+
private[spark] trait ResetSystemProperties extends BeforeAndAfterEach { this: Suite =>
42+
var oldProperties: Properties = null
43+
44+
override def beforeEach(): Unit = {
45+
oldProperties = new Properties(System.getProperties)
46+
super.beforeEach()
47+
}
48+
49+
override def afterEach(): Unit = {
3550
try {
36-
super.withFixture(test)
51+
super.afterEach()
3752
} finally {
3853
System.setProperties(oldProperties)
54+
oldProperties = null
3955
}
4056
}
4157
}

core/src/test/scala/org/apache/spark/util/SizeEstimatorSuite.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class DummyString(val arr: Array[Char]) {
4444
}
4545

4646
class SizeEstimatorSuite
47-
extends FunSuite with ResetSystemProperties with BeforeAndAfterEach with PrivateMethodTester {
47+
extends FunSuite with BeforeAndAfterEach with PrivateMethodTester with ResetSystemProperties {
4848

4949
override def beforeEach() {
5050
// Set the arch to 64-bit and compressedOops to true to get a deterministic test-case

0 commit comments

Comments
 (0)