case studies

How CodeInstances Keeps the Failing Run of a Flaky CI Test

Here’s a situation most teams know. An integration test fails about one run in twenty. It calls an API that usually answers one way and occasionally answers another (a field missing, a list in a different order, a slow page that times out). Somebody hits rerun, it goes green, and the failure is gone. A week later it happens again, and the ticket still says “can’t reproduce”.

The trouble is that the one run worth looking at is the one nobody kept. Logs say the test failed. They don’t hold the answer that made it fail.

With CodeInstances you keep every CI run of that test as an instance. In a Go project, build the test binary first (so the recording holds the test’s traffic and nothing from the build), then put codeinstances record in front of it:

go test -c -o checkout.test ./integration
codeinstances record -o test-run.instance -- ./checkout.test -test.run TestCheckout

CI runs of the same test: most pass, one fails. The failing run is kept as failing.instance, a build artifact, and the pipeline still goes red because record exits with the test’s own code. Locally the instance is replayed offline as often as needed, the code is fixed, and the fixed build is replayed against the same file.

record exits with the program’s own exit code, so the step still fails when the test fails and the pipeline behaves exactly as before. The only change is that a file is left behind.

Get the Failing Run Off the Build Machine

A build machine is gone in an hour, so send the run somewhere it lasts. Run codeinstances serve on a machine of your own, and when the step fails, have CI write a bug report into the job summary and push the run to that store:

codeinstances report test-run.instance >> "$GITHUB_STEP_SUMMARY"
codeinstances push test-run.instance

push reads the store’s address and token from CODEINSTANCES_STORE and CODEINSTANCES_TOKEN, which CI keeps as a variable and a secret. The report names the error, the calls that failed and the fingerprint, and ends with the two commands that bring the run back. The CI example has the whole GitHub Actions job. On your machine:

codeinstances pull 3f2a9c1e77b0
codeinstances replay 3f2a9c1e77b0.instance

Replay never opens a connection. The test gets the same answers it got in CI, including the odd one, so it fails the same way, every time, offline. A one-in-twenty failure becomes a one-in-one failure you can step through with a debugger.

Is It One Bug or Three?

After a week the store holds a dozen failing runs of that test. codeinstances group fingerprints them by how they ended, the error they printed and the calls that failed, and puts the ones that fail the same way together. The store’s own page shows the same grouping. Twelve red builds might be one flaky field and a timeout, and now you know which to look at first.

Find the Answer That Does It

CI also has plenty of passing runs of the same test. Keep one, and let shrink work out which of the failing run’s answers actually cause the failure:

codeinstances shrink -pass passing.instance failing.instance

It replays the test offline over and over, swapping the passing run’s answers in, until it finds the fewest of the failing run’s answers that still make it fail. Twenty differing answers often come down to one: the response where the field was missing. It keeps that smallest failing run as shrunk.instance.

Keep It as a Test

Once you’ve fixed the code, the shrunk run becomes a regression test:

go test -c -o checkout-fixed.test ./integration
codeinstances test shrunk.instance -- ./checkout-fixed.test -test.run TestCheckout

A run that failed when it was recorded has to exit 0 now. Put the file in a folder with the others, run codeinstances test on the folder in CI, and that odd answer can’t quietly break the test again.

Where It Bites

The test has to send its traffic through HTTP_PROXY and HTTPS_PROXY, which most clients do. If the test is written in Go and talks to a server on localhost, Go’s client skips the proxy for that address, so point it at a host name and send that name to the local address with -resolve:

codeinstances record -resolve api.test=127.0.0.1:8080 -- go test ./integration

On macOS and Windows, Go ignores SSL_CERT_FILE, so a Go test there needs hook.Client() for HTTPS. If the flakiness comes from the clock or random numbers in Go code, the hook package records those too. In other languages only the HTTP side is captured, so a failure that depends on timing inside the program may not replay. And shrink trusts that the same answers give the same failure; a test that fails at random even offline can mislead it. The reference has the rest of the limits.