Skip to content

Commit 1d1aa5a

Browse files
committed
Use ResetSystemProperties in SizeEstimatorSuite
1 parent dd9492b commit 1d1aa5a

File tree

1 file changed

+8
-30
lines changed

1 file changed

+8
-30
lines changed

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

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717

1818
package org.apache.spark.util
1919

20-
import org.scalatest.BeforeAndAfterAll
21-
import org.scalatest.FunSuite
22-
import org.scalatest.PrivateMethodTester
20+
import org.scalatest.{BeforeAndAfterEach, BeforeAndAfterAll, FunSuite, PrivateMethodTester}
2321

2422
class DummyClass1 {}
2523

@@ -46,20 +44,12 @@ class DummyString(val arr: Array[Char]) {
4644
}
4745

4846
class SizeEstimatorSuite
49-
extends FunSuite with BeforeAndAfterAll with PrivateMethodTester {
47+
extends FunSuite with ResetSystemProperties with BeforeAndAfterEach with PrivateMethodTester {
5048

51-
var oldArch: String = _
52-
var oldOops: String = _
53-
54-
override def beforeAll() {
49+
override def beforeEach() {
5550
// Set the arch to 64-bit and compressedOops to true to get a deterministic test-case
56-
oldArch = System.setProperty("os.arch", "amd64")
57-
oldOops = System.setProperty("spark.test.useCompressedOops", "true")
58-
}
59-
60-
override def afterAll() {
61-
resetOrClear("os.arch", oldArch)
62-
resetOrClear("spark.test.useCompressedOops", oldOops)
51+
System.setProperty("os.arch", "amd64")
52+
System.setProperty("spark.test.useCompressedOops", "true")
6353
}
6454

6555
test("simple classes") {
@@ -122,7 +112,7 @@ class SizeEstimatorSuite
122112
}
123113

124114
test("32-bit arch") {
125-
val arch = System.setProperty("os.arch", "x86")
115+
System.setProperty("os.arch", "x86")
126116

127117
val initialize = PrivateMethod[Unit]('initialize)
128118
SizeEstimator invokePrivate initialize()
@@ -131,31 +121,19 @@ class SizeEstimatorSuite
131121
assertResult(48)(SizeEstimator.estimate(DummyString("a")))
132122
assertResult(48)(SizeEstimator.estimate(DummyString("ab")))
133123
assertResult(56)(SizeEstimator.estimate(DummyString("abcdefgh")))
134-
resetOrClear("os.arch", arch)
135124
}
136125

137126
// NOTE: The String class definition varies across JDK versions (1.6 vs. 1.7) and vendors
138127
// (Sun vs IBM). Use a DummyString class to make tests deterministic.
139128
test("64-bit arch with no compressed oops") {
140-
val arch = System.setProperty("os.arch", "amd64")
141-
val oops = System.setProperty("spark.test.useCompressedOops", "false")
129+
System.setProperty("os.arch", "amd64")
130+
System.setProperty("spark.test.useCompressedOops", "false")
142131
val initialize = PrivateMethod[Unit]('initialize)
143132
SizeEstimator invokePrivate initialize()
144133

145134
assertResult(56)(SizeEstimator.estimate(DummyString("")))
146135
assertResult(64)(SizeEstimator.estimate(DummyString("a")))
147136
assertResult(64)(SizeEstimator.estimate(DummyString("ab")))
148137
assertResult(72)(SizeEstimator.estimate(DummyString("abcdefgh")))
149-
150-
resetOrClear("os.arch", arch)
151-
resetOrClear("spark.test.useCompressedOops", oops)
152-
}
153-
154-
def resetOrClear(prop: String, oldValue: String) {
155-
if (oldValue != null) {
156-
System.setProperty(prop, oldValue)
157-
} else {
158-
System.clearProperty(prop)
159-
}
160138
}
161139
}

0 commit comments

Comments
 (0)