Skip to content

Commit d4fdd09

Browse files
committed
add config lookup and force connect logic ~cut-release
1 parent 58347b2 commit d4fdd09

3 files changed

Lines changed: 62 additions & 0 deletions

File tree

runtime/src/main/scala/com/sparkutils/testing/TestUtilsEnvironment.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ import java.io.File
77

88
object TestUtilsEnvironment {
99

10+
/**
11+
* first attempts to get the system env, then system java property then sqlconf
12+
* @param name
13+
* @return
14+
*/
15+
def getConfig(name: String, default: String = "") = try {
16+
val res = System.getenv(name)
17+
if (res ne null)
18+
res
19+
else {
20+
val sp = System.getProperty(name)
21+
if (sp ne null)
22+
sp
23+
else
24+
SQLConf.get.getConfString(name, default)
25+
}
26+
} catch {
27+
case _: Throwable => default
28+
}
29+
1030
/**
1131
* Checks if the master is local, if so it's likely tests, if spark.master is not defined it's false
1232
*/

runtime/src/main/scala/com/sparkutils/testing/Testing.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,26 @@ object SparkVersions {
4141
lazy val sparkVersion = sparkFullVersion.split('.').take(2).mkString(".")
4242

4343
lazy val sparkMajorVersion = sparkFullVersion.split('.').head
44+
}
45+
46+
47+
object ConnectWhenForced {
48+
49+
val FORCED_CONNECT_PROPERTY_NAME = "SPARKUTILS_TESTING_FORCE_CONNECT"
50+
51+
/**
52+
* When SPARKUTILS_TESTING_FORCE_CONNECT is set to true in ENV or Sys properties then None is returned otherwise thunk.
53+
*
54+
* This is set/reset by the TestUtils.defaultAndforceConnect helper function
55+
*
56+
* @param thunk
57+
* @tparam T
58+
* @return
59+
*/
60+
def someOrForcedConnect[T](thunk: => T): Option[T] =
61+
if (TestUtilsEnvironment.getConfig(FORCED_CONNECT_PROPERTY_NAME, "false").toBoolean)
62+
None
63+
else
64+
Some(thunk)
65+
4466
}

testing/src/main/scala/com/sparkutils/testing/TestUtils.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,26 @@ trait TestUtils extends SessionStrategy with Serializable with ClassicTestUtils
131131
def not_Cluster(thunk: => Unit): Unit =
132132
if (TestUtilsEnvironment.shouldRunClusterTests) thunk
133133

134+
/**
135+
* When inConnect is true the thunk is run once with SPARKUTILS_TESTING_FORCE_CONNECT=false and then
136+
* with SPARKUTILS_TESTING_FORCE_CONNECT=true enabling functions using someOrForcedConnect to be tested.
137+
* @param thunk
138+
*/
139+
def defaultAndforceConnect(thunk: => Unit): Unit =
140+
if (inConnect.get()) {
141+
val prev = System.getProperty(ConnectWhenForced.FORCED_CONNECT_PROPERTY_NAME) // only possible to change property
142+
try {
143+
System.setProperty(ConnectWhenForced.FORCED_CONNECT_PROPERTY_NAME, "false")
144+
thunk
145+
System.setProperty(ConnectWhenForced.FORCED_CONNECT_PROPERTY_NAME, "true")
146+
thunk
147+
} finally {
148+
System.setProperty(ConnectWhenForced.FORCED_CONNECT_PROPERTY_NAME, prev)
149+
}
150+
} else {
151+
thunk // using whatever is current
152+
}
153+
134154
}
135155

136156
object TestUtils {

0 commit comments

Comments
 (0)