Repository: square/okhttp
File: okhttp-testing-support/src/main/kotlin/okhttp3/internal/concurrent/TaskFaker.kt (Kotlin)

Code before:
    taskRunner.assertThreadDoesntHoldLock()

    taskRunner.lock.withLock {
      val contextSwitchCountBefore = contextSwitchCount
      yieldUntil(resumeEagerly = true) { contextSwitchCount > contextSwitchCountBefore }
    }
  }

  /** Sleep until [durationNanos] elapses. For use by the task threads. */
  fun sleep(durationNanos: Long) {
    taskRunner.lock.withLock {
      val sleepUntil = nanoTime + durationNanos
      yieldUntil { nanoTime >= sleepUntil }
    }
  }

  /**
   * Artificially stall until manually resumed by the test thread with [runTasks]. Use this to
   * simulate races in tasks that doesn't have a deterministic sequence.
   */
  fun yield() {
    taskRunner.assertThreadDoesntHoldLock()
    taskRunner.lock.withLock {
      yieldUntil()
    }
  }

  /**
   * Process the queue until [condition] returns true.
   *
   * @param resumeEagerly true to prioritize the current task over other queued tasks.
   * @param yieldUntilExhausted true to keep yielding until there's no other runnable tasks.
   */
  private tailrec fun yieldUntil(
    resumeEagerly: Boolean = false,
    yieldUntilExhausted: Boolean = false,
    condition: () -> Boolean = { true },
  ) {
    taskRunner.assertThreadHoldsLock()
    val self = currentTask

    val yieldCompleteTask =
      object : SerialTask {
        override fun isReady() = condition()

        override fun start() {
          taskRunner.assertThreadHoldsLock()
          currentTask = self
          taskRunner.condition.signalAll()
        }
      }

Review comment (suggestion):
This method call is confusing at first glance until you go read all the default param values.

But I don't have any suggestions to make it better 🤷 

Code after:
    taskRunner.assertThreadDoesntHoldLock()

    taskRunner.lock.withLock {
      val contextSwitchCountBefore = contextSwitchCount
      yieldUntil(ResumePriority.BeforeOtherTasks) {
        contextSwitchCount > contextSwitchCountBefore
      }
    }
  }

  /** Sleep until [durationNanos] elapses. For use by the task threads. */
  fun sleep(durationNanos: Long) {
    taskRunner.lock.withLock {
      val sleepUntil = nanoTime + durationNanos
      yieldUntil { nanoTime >= sleepUntil }
    }
  }

  /**
   * Artificially stall until manually resumed by the test thread with [runTasks]. Use this to
   * simulate races in tasks that doesn't have a deterministic sequence.
   */
  fun yield() {
    taskRunner.assertThreadDoesntHoldLock()
    taskRunner.lock.withLock {
      yieldUntil()
    }
  }

  /** Process the queue until [condition] returns true. */
  private tailrec fun yieldUntil(
    strategy: ResumePriority = ResumePriority.AfterEnqueuedTasks,
    condition: () -> Boolean = { true },
  ) {
    taskRunner.assertThreadHoldsLock()
    val self = currentTask

    val yieldCompleteTask =
      object : SerialTask {
        override fun isReady() = condition()

        override fun start() {
          taskRunner.assertThreadHoldsLock()
          currentTask = self
          taskRunner.condition.signalAll()
        }
      }

    if (strategy == ResumePriority.BeforeOtherTasks) {
      serialTaskQueue.addFirst(yieldCompleteTask)
    } else {