Indeed, that works, and finishes in 0.00 time - here is a Go Playground to prove it (using *T instead of Optional[T]).
Here is a hacky workaround to measure the actual time using a syscall: https://go.dev/play/p/kF6Bh048siE
It shows 979ms for the original version and 0.212ms for the fast version.
If you call `time.Sleep()` the reported duration will reflect that call, but it's only simulating the delay. The duration of a CPU-heavy task is always reported as 0.00s.
Demo: https://go.dev/play/p/UZToqFseAwO
Output:
=== RUN TestSleep // calls time.Sleep(2 * time.Second)
Real duration: 554µs
--- PASS: TestSleep (2.00s)
=== RUN TestBusyLoop // for i := 0; i < 1000000000; i++ { ... }
Real duration: 390.826ms
--- PASS: TestBusyLoop (0.00s)
PASS
The simulation is quite good: if you have multiple threads that use `time.Sleep()`, their output will be interleaved correctly. The output panel will even display the lines one at a time as if it's streaming them from the server, but in reality the entire output is received at once and the "streaming" effect is simulated on the client.Concurrency is one of Go's core features, so it figures the playground would allow some form of concurrency even when the clock is fake.