coder/testutil/logger.go
Spike Curtis 5861e516b9 chore: add standard test logger ignoring db canceled (#15556)
Refactors our use of `slogtest` to instantiate a "standard logger" across most of our tests.  This standard logger incorporates https://github.com/coder/slog/pull/217 to also ignore database query canceled errors by default, which are a source of low-severity flakes.

Any test that has set non-default `slogtest.Options` is left alone. In particular, `coderdtest` defaults to ignoring all errors. We might consider revisiting that decision now that we have better tools to target the really common flaky Error logs on shutdown.
2024-11-18 14:09:22 +04:00

48 lines
1.5 KiB
Go

package testutil
import (
"context"
"strings"
"testing"
"golang.org/x/xerrors"
"cdr.dev/slog"
"cdr.dev/slog/sloggers/slogtest"
)
// Logger returns a "standard" testing logger, with debug level and common flaky
// errors ignored.
func Logger(t testing.TB) slog.Logger {
return slogtest.Make(
t, &slogtest.Options{IgnoreErrorFn: IgnoreLoggedError},
).Leveled(slog.LevelDebug)
}
func IgnoreLoggedError(entry slog.SinkEntry) bool {
err, ok := slogtest.FindFirstError(entry)
if !ok {
return false
}
// Canceled queries usually happen when we're shutting down tests, and so
// ignoring them should reduce flakiness. This also includes
// context.Canceled and context.DeadlineExceeded errors, even if they are
// not part of a query.
return isQueryCanceledError(err)
}
// isQueryCanceledError checks if the error is due to a query being canceled. This reproduces
// database.IsQueryCanceledError, but is reimplemented here to avoid importing the database package,
// which would result in import loops. We also use string matching on the error PostgreSQL returns
// to us, rather than the pq error type, because we don't want testutil, which is imported in lots
// of places to import lib/pq, which we have our own fork of.
func isQueryCanceledError(err error) bool {
if xerrors.Is(err, context.Canceled) || xerrors.Is(err, context.DeadlineExceeded) {
return true
}
if strings.Contains(err.Error(), "canceling statement due to user request") {
return true
}
return false
}