Go traps · Chapter 9 of 16

defer evaluates its arguments at once

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Defer
  • 3 tasks
  • Go
  • In the all labs plan

The first 2 chapters of Go traps are free. This one comes with the all labs plan.

Why you need this

It compiles and go test passes. The full go vet has one narrow rule for this, and only for time.Since itself:

$ go vet .
main.go:10:44: call to time.Since is not deferred

It says nothing about a variable like state, or about a duration read from an injected clock, which is how session.go measures it.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.go: a wrong audit line on purpose

    Write reproduce() in repro.go, which returns a string. Make a fake clock (a type with a Now() time.Time method that returns a time you keep in it, and a way to move that time forward), and run a session with RunSession(id, steps, clock, audit) from session.go: two steps, the first passes, the second moves the fake clock 90 seconds and returns an error. Keep the line RunSession passes to audit, and return it.

    The check runs your reproduce() twice, each time in a fresh build of the case's folder: beside the original session.go it must return a line that says state started, took 0s (for S901774 it is "session S901774: state started, took 0s"), and beside a fixed session.go it must say state failed, took 1m30s ("session S901774: state failed, took 1m30s"); any session id. So it must really run a session that fails 90 seconds in on the fake clock. No real waiting (no time.Sleep) and no input: reproduce() must finish within 3 seconds.

  2. Task 2

    session.go: the audit line says how the session really ended

    Fix RunSession(id string, steps []Step, clock Clock, audit func(line string)) string in session.go so the one audit line it writes has the session's final state and the time it took on the clock passed in, rounded to the second: when a step fails, it returns "failed" and the line is session <id>: state failed, took <time from start to the failure>; when every step passes, it returns "completed" and the line says state completed and the whole time (1m30s, 41m30s, 0s for no steps).

    Keep the line's wording, Clock, Step and RunSession's name and types as they are; steps after a failed one never run. The check uses its own fake clock and sessions, failed and completed, and counts the audit lines: exactly one per session, also when a step fails.

  3. Task 3

    session_test.go: a test that would have caught it

    Write a Go test file, session_test.go, that would have caught this bug before it reached the audit log. The check runs your file three times, each in a fresh build: beside the original session.go, where at least one test must fail; beside the fixed session.go, where every test must pass; and beside a wrong fix that writes the audit line at the end with no defer, so a failed session writes no line, where at least one test must fail.

    So run a session whose step fails, on a fake clock that moves, and check RunSession wrote exactly one audit line saying state failed and the right time; test a completed session too. The test file is in the same package as repro.go, so it can use the fake clock you wrote there (do not declare it twice). Each test must finish within a second or two (the whole file is stopped after 8 seconds).

What you will learn

  • repro.go: a wrong audit line on purpose
  • session.go: the audit line says how the session really ended
  • session_test.go: a test that would have caught it

More in Defer

Build "defer evaluates its arguments at once" yourself

BijliGo runs its EV chargers on Go, and Joseph, its senior engineer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Go traps are free. This one comes with the all labs plan.