Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: Move FunSuite.unitTimeout to trait, to support stackable overriding trait #616

Open
dsbos opened this issue Jan 6, 2023 · 0 comments

Comments

@dsbos
Copy link

dsbos commented Jan 6, 2023

Can you move FunSuite.munitTimeout out to a separate trait, to better support stackable overriding?

I'd like to do something like the code below, but without extending FunSuite (so the client code (the extending test class) is still responsible for separately pulling in FunSuite).

package whatever

import scala.concurrent.duration.Duration

// ?? TODO:  If MUnit factors munitTimeout out of class FunSuite into a separate
//  trait (supporting stackable overriding), adjust the `extends munit.FunSuite`
//  and `this: munit.FunSuite =>` part to refer to that trait.

/** Mix-in to disable MUnit timeouts when running under a debugger.
  *
  * Note:  This _will not take effect_ if a client test class overrides `munit.FunSuite.unitTimeout` to a fixed value.
  * However, it can still take effect if the client derives the final munitTimeout value in terms of `super.munitTimeout`.
  * For example, a client could use `override val munitTimeout = super.munitTimeout * 2` (to use double the default
  * timeout), because method `Duration.*` is smart about `Duration.Inf`.
  *
  */
trait DisableMunitTimeoutsUnderDebugger extends munit.FunSuite {
  this: munit.FunSuite =>

  private val startedUnderDebugger: Boolean = {
    import scala.jdk.CollectionConverters._ // for .asScala

    val runtimeBean = java.lang.management.ManagementFactory.getRuntimeMXBean
    val jvmArgs = runtimeBean.getInputArguments.asScala
    jvmArgs.exists { arg => arg.startsWith("-agentlib:jdwp") || arg.startsWith("-Xrunjdwp") }
  }

  abstract override def munitTimeout: Duration = {
    if (!startedUnderDebugger) {
      // Normally use default timeout:
      super.munitTimeout
    } else {
      println("Debugger detected; overriding munitTimeout to infinity")
      Duration.Inf
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant