Skip to content

Commit

Permalink
Parse system property switches correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Zschimmer committed Dec 11, 2024
1 parent 31119e9 commit 6e3cc41
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import js7.base.io.file.FileUtils.*
import js7.base.io.file.FileUtils.syntax.*
import js7.base.test.OurTestSuite
import js7.base.utils.ScalaUtils.syntax.*
import js7.base.utils.SystemPropertiesExtensions.asSwitch
import js7.cluster.ClusterConf
import js7.common.commandline.CommandLineArguments
import js7.common.pekkohttp.web.data.WebServerPort
Expand Down Expand Up @@ -46,7 +47,7 @@ final class AgentConfigurationTest extends OurTestSuite:
.orThrow
clusterConf.copy(
journalConf = clusterConf.journalConf.copy(
slowCheckState = sys.props.get("js7.test").fold(false)(StringAsBoolean(_))))
slowCheckState = sys.props.asSwitch("js7.test")))
},
name = AgentConfiguration.DefaultName)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import js7.base.utils.ByteUnits.toKiBGiB
import js7.base.utils.Missing.getOrElse
import js7.base.utils.ScalaUtils.*
import js7.base.utils.ScalaUtils.syntax.*
import js7.base.utils.SystemPropertiesExtensions.asSwitch
import js7.base.utils.Tests.isTestParallel
import js7.base.utils.{Missing, Tests}
import scala.concurrent.ExecutionContext
Expand All @@ -28,7 +29,7 @@ object OurIORuntime:
private lazy val logger = Logger[this.type]

val useCommonIORuntime: Boolean =
sys.props.contains("js7.test.commonIORuntime") || sys.props.contains("test.speed")
sys.props.asSwitch("js7.test.commonIORuntime") || sys.props.contains("test.speed")

val commonThreadPrefix = "js7"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import js7.base.BuildInfo
import js7.base.log.Log4jThreadContextMap.*
import js7.base.system.startup.StartUp
import js7.base.utils.Lazy
import js7.base.utils.SystemPropertiesExtensions.asSwitch
import js7.base.utils.Tests.isTest
import org.apache.logging.log4j.spi.{CopyOnWrite, ReadOnlyThreadContextMap, ThreadContextMap}
import org.apache.logging.log4j.util.StringMap
Expand Down Expand Up @@ -96,9 +97,7 @@ object Log4jThreadContextMap:
case lzy: Lazy[String] => lzy.value

private val isDebug: Boolean =
sys.props.get("log4j2.debug") match
case Some("" | "true") => true
case _ => false
sys.props.asSwitch("log4j2.debug")

def initialize(name: String): Unit =
keyToValue.put("js7.serverId", name) // May be overwritten later by a more specific value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package js7.base.thread
import java.util.concurrent.{ExecutorService, Executors, ThreadFactory}
import js7.base.log.Logger
import js7.base.utils.ScalaUtils.syntax.*
import js7.base.utils.SystemPropertiesExtensions.asSwitch

object VirtualThreads:

private val logger = Logger[this.type]
private var hasVirtualThreads =
Runtime.version.feature >= 19 && sys.props.contains("js7.virtualThreads")
Runtime.version.feature >= 19 && sys.props.asSwitch("js7.virtualThreads")

private lazy val maybeNewVirtualThreadPerTaskExecutor: Option[() => ExecutorService] =
for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import js7.base.system.JavaHeapDump.dumpHeapTo
import js7.base.test.LoggingTestAdder.Result
import js7.base.thread.VirtualThreads.newMaybeVirtualThread
import js7.base.utils.ScalaUtils.syntax.{RichBoolean, RichThrowable}
import js7.base.utils.SystemPropertiesExtensions.asSwitch
import org.scalatest.exceptions.TestPendingException
import scala.collection.mutable
import scala.jdk.CollectionConverters.MapHasAsScala
Expand All @@ -32,7 +33,7 @@ private object TestResultCollector:
newMaybeVirtualThread("TestResultCollector-shutdown-hook") {
logThreads()
logger.info(s"Test summary:\n$asString\n")
if sys.props.contains("js7.dumpHeap") then dumpJavaHeap()
if sys.props.asSwitch("js7.dumpHeap") then dumpJavaHeap()
if false then Log4j.shutdown() // Set shutdownHook="disable" in project/log4j2.xml !!!
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package js7.base.log

import js7.base.utils.SystemPropertiesExtensions.asSwitch

object LoggingEscapeCodes:

val isColorAllowed: Boolean =
sys.props.get("js7.log.colored").forall(Set("true", "yes", "on", ""))
sys.props.asSwitch("js7.log.colored")

/** Reset color and mode. */
val resetColor: String = if !isColorAllowed then "" else AnsiEscapeCodes.resetColor
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package js7.base.utils

import js7.base.convert.As.StringAsBoolean
import scala.sys.SystemProperties

object SystemPropertiesExtensions:

extension (properties: SystemProperties)
def asSwitch(key: String): Boolean =
properties.get(key) match
case None => false
case Some("") => true
case Some(string) => StringAsBoolean(string)
8 changes: 5 additions & 3 deletions js7-base/shared/src/main/scala/js7/base/utils/Tests.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package js7.base.utils

import js7.base.convert.As.StringAsBoolean
import js7.base.log.Logger
import js7.base.utils.SystemPropertiesExtensions.asSwitch

object Tests:

val (isIntelliJIdea, isScalaTest, isSbt, isTest, isStrict) =
val classNames = TestsPlatform.allActiveClasses

Expand All @@ -15,8 +16,9 @@ object Tests:
val isScalaTest = hasPackagePrefix(Set("org.scalatest."))
val isSbt = hasPackagePrefix(Set("xsbt.boot."))
val isTest = (isScalaTest || isSbt)
&& !sys.props.get("js7.noTest").fold(false)(StringAsBoolean(_))
val isStrict = isTest || sys.props.get("js7.strict").fold(false)(StringAsBoolean(_))
&& !sys.props.asSwitch("js7.noTest")
&& !sys.props.contains("test.speed")
val isStrict = isTest || sys.props.asSwitch("js7.strict")

(isIntelliJIdea, isScalaTest, isSbt, isTest, isStrict)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package js7.base.utils

import js7.base.test.OurTestSuite
import js7.base.utils.SystemPropertiesExtensions.asSwitch

final class SystemPropertiesExtensionsTest extends OurTestSuite:

"asSwitch" in:
assert(sys.props.asSwitch("js7.SystemPropertiesExtensionsTest.undefined") == false)

Seq(
"" -> true,
"false" -> false,
"off" -> false,
"no" -> false,
"true" -> true,
"on" -> true,
"yes" -> true
).foreach: (string, bool) =>
val key = "js7.SystemPropertiesExtensionsTest"
sys.props.put(key, string)
assert(sys.props.asSwitch(key) == bool)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.typesafe.config.{Config, ConfigFactory}
import js7.base.BuildInfo
import js7.base.configutils.Configs
import js7.base.io.JavaResource
import js7.base.utils.Tests.isTest
import scala.jdk.CollectionConverters.*

object Js7Configuration:
Expand All @@ -12,11 +13,7 @@ object Js7Configuration:
"js7.version" -> BuildInfo.version,
"js7.longVersion" -> BuildInfo.longVersion,
"js7.prettyVersion" -> BuildInfo.prettyVersion,
"js7.test" -> (
if sys.props.contains("test.speed") then
"off"
else
sys.props.getOrElse("js7.test", "off")))
"js7.test" -> isTest.toString)
ConfigFactory
.parseMap(map.asJava)
.withFallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import js7.base.io.file.FileUtils.syntax.*
import js7.base.test.OurTestSuite
import js7.base.time.ScalaTime.*
import js7.base.utils.DelayConf
import js7.base.utils.SystemPropertiesExtensions.asSwitch
import js7.cluster.ClusterConf
import js7.common.commandline.CommandLineArguments
import js7.common.http.configuration.RecouplingStreamReaderConf
Expand Down Expand Up @@ -50,7 +51,7 @@ final class ControllerConfigurationTest extends OurTestSuite, BeforeAndAfterAll:
pekkoAskTimeout = 1.h,
clusterConf = ClusterConf(
JournalConf.fromConfig(configuration.config)
.copy(slowCheckState = sys.props.get("js7.test").fold(false)(StringAsBoolean(_))),
.copy(slowCheckState = sys.props.asSwitch("js7.test")),
NodeId("Primary"), isBackup = false, None,
RecouplingStreamReaderConf(
timeout = Some(6500.ms), // Between 3s and 10s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import js7.base.utils.{AsyncLock, Atomic}
import js7.base.utils.Atomic.extensions.*
import js7.base.utils.CatsUtils.syntax.*
import js7.base.utils.ScalaUtils.syntax.RichAny
import js7.base.utils.SystemPropertiesExtensions.asSwitch
import js7.base.utils.Tests.isTest
import js7.base.version.Js7Versions.checkNonMatchingVersion
import js7.base.version.Version
Expand Down Expand Up @@ -125,8 +126,7 @@ trait HttpSessionApi extends SessionApi, HasSessionToken:

object HttpSessionApi:
private val logger = Logger[this.type]
private val isPasswordLoggable = isTest &&
sys.props.get("js7.test.log-password").fold(false)(StringAsBoolean.apply)
private val isPasswordLoggable = isTest && sys.props.asSwitch("js7.test.log-password")

private[session] def logNonMatchingVersion(
otherVersion: Version,
Expand Down

0 comments on commit 6e3cc41

Please sign in to comment.