Repository: square/okhttp
File: okhttp/src/jvmTest/java/okhttp3/internal/concurrent/TaskRunnerTest.kt (Kotlin)

Code before:

    blueQueue.execute("task two", 100.µs) {
      log += "two:run@${taskFaker.nanoTime} parallel=${taskFaker.isParallel}"
    }

    greenQueue.execute("task three", 100.µs) {
      log += "three:run@${taskFaker.nanoTime} parallel=${taskFaker.isParallel}"
    }

    taskFaker.advanceUntil(0.µs)
    assertThat(log).isEmpty()

    taskFaker.advanceUntil(100.µs)
    assertThat(log).containsExactlyInAnyOrder(
        "one:run@100000 parallel=true",
        "two:run@100000 parallel=true",
        "three:run@100000 parallel=true"
    )

    taskFaker.assertNoMoreTasks()

    assertThat(testLogHandler.takeAll()).containsExactlyInAnyOrder(
        "FINE: Q10000 scheduled after 100 µs: task one",
        "FINE: Q10001 scheduled after 100 µs: task two",
        "FINE: Q10002 scheduled after 100 µs: task three",
        "FINE: Q10000 starting              : task one",
        "FINE: Q10000 finished run in   0 µs: task one",
        "FINE: Q10001 starting              : task two",
        "FINE: Q10001 finished run in   0 µs: task two",
        "FINE: Q10002 starting              : task three",
        "FINE: Q10002 finished run in   0 µs: task three"
    )
  }

  /** Test the introspection method [TaskQueue.scheduledTasks]. */
  @Test fun scheduledTasks() {
    redQueue.execute("task one", 100.µs) {
      // Do nothing.
    }

    redQueue.execute("task two", 200.µs) {
      // Do nothing.
    }

    assertThat(redQueue.scheduledTasks.toString()).isEqualTo("[task one, task two]")

    assertThat(testLogHandler.takeAll()).containsExactly(
        "FINE: Q10000 scheduled after 100 µs: task one",
        "FINE: Q10000 scheduled after 200 µs: task two"
    )
  }

Review comment (suggestion):
I was able to get rid of this by introducing `TaskFaker.isRunningAllTasks`. This call was only introduced today and I wasn’t happy with it!

Code after:

  /** Inspect how many runnables have been enqueued. If non-zero then we're truly parallel. */
  @Test fun differentQueuesAreParallel() {
    redQueue.execute("task one", 100.µs) {
      log += "one:run@${taskFaker.nanoTime} parallel=${taskFaker.isParallel}"
    }

    blueQueue.execute("task two", 100.µs) {
      log += "two:run@${taskFaker.nanoTime} parallel=${taskFaker.isParallel}"
    }

    greenQueue.execute("task three", 100.µs) {
      log += "three:run@${taskFaker.nanoTime} parallel=${taskFaker.isParallel}"
    }

    taskFaker.advanceUntil(0.µs)
    assertThat(log).isEmpty()

    taskFaker.advanceUntil(100.µs)
    assertThat(log).containsExactlyInAnyOrder(
        "one:run@100000 parallel=true",
        "two:run@100000 parallel=true",
        "three:run@100000 parallel=true"
    )

    taskFaker.assertNoMoreTasks()

    assertThat(testLogHandler.takeAll()).containsExactlyInAnyOrder(
        "FINE: Q10000 scheduled after 100 µs: task one",
        "FINE: Q10001 scheduled after 100 µs: task two",
        "FINE: Q10002 scheduled after 100 µs: task three",
        "FINE: Q10000 starting              : task one",
        "FINE: Q10000 finished run in   0 µs: task one",
        "FINE: Q10001 starting              : task two",
        "FINE: Q10001 finished run in   0 µs: task two",
        "FINE: Q10002 starting              : task three",
        "FINE: Q10002 finished run in   0 µs: task three"
    )
  }

  /** Test the introspection method [TaskQueue.scheduledTasks]. */
  @Test fun scheduledTasks() {
    redQueue.execute("task one", 100.µs) {
      // Do nothing.
    }

    redQueue.execute("task two", 200.µs) {
      // Do nothing.
    }

    assertThat(redQueue.scheduledTasks.toString()).isEqualTo("[task one, task two]")