From 3e6dc108db777ca2077e509e9c8431019b02bae2 Mon Sep 17 00:00:00 2001 From: clawbot Date: Tue, 17 Mar 2026 20:23:52 -0700 Subject: [PATCH] fix: speed up tests and reduce output verbosity MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Use bcrypt.MinCost in tests instead of DefaultCost (saves ~10s) - Remove unnecessary 100ms startup sleeps from test server creation (saves ~8s) - Remove -v flag from Makefile test target to reduce output noise Handler tests: 24.4s → 13.8s, DB tests: 2.6s → 1.5s Total make test: 38s → 28s (well under 30s timeout) --- Makefile | 2 +- internal/db/auth.go | 7 ++++++- internal/db/main_test.go | 14 ++++++++++++++ internal/handlers/api_test.go | 11 +++++++---- 4 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 internal/db/main_test.go diff --git a/Makefile b/Makefile index f90b554..d2f89ea 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ fmt-check: @test -z "$$(gofmt -l .)" || (echo "Files not formatted:" && gofmt -l . && exit 1) test: ensure-web-dist - go test -timeout 30s -v -race -cover ./... + go test -timeout 30s -race -cover ./... # check runs all validation without making changes # Used by CI and Docker build — fails if anything is wrong diff --git a/internal/db/auth.go b/internal/db/auth.go index 0367ace..c2da3da 100644 --- a/internal/db/auth.go +++ b/internal/db/auth.go @@ -10,7 +10,12 @@ import ( "golang.org/x/crypto/bcrypt" ) -const bcryptCost = bcrypt.DefaultCost +//nolint:gochecknoglobals // var so tests can override via SetBcryptCost +var bcryptCost = bcrypt.DefaultCost + +// SetBcryptCost overrides the bcrypt cost. +// Use bcrypt.MinCost in tests to avoid slow hashing. +func SetBcryptCost(cost int) { bcryptCost = cost } var errNoPassword = errors.New( "account has no password set", diff --git a/internal/db/main_test.go b/internal/db/main_test.go new file mode 100644 index 0000000..ec72330 --- /dev/null +++ b/internal/db/main_test.go @@ -0,0 +1,14 @@ +package db_test + +import ( + "os" + "testing" + + "git.eeqj.de/sneak/neoirc/internal/db" + "golang.org/x/crypto/bcrypt" +) + +func TestMain(m *testing.M) { + db.SetBcryptCost(bcrypt.MinCost) + os.Exit(m.Run()) +} diff --git a/internal/handlers/api_test.go b/internal/handlers/api_test.go index 85a8f9d..bbfc504 100644 --- a/internal/handlers/api_test.go +++ b/internal/handlers/api_test.go @@ -11,6 +11,7 @@ import ( "io" "net/http" "net/http/httptest" + "os" "path/filepath" "strconv" "strings" @@ -30,8 +31,14 @@ import ( "git.eeqj.de/sneak/neoirc/internal/stats" "go.uber.org/fx" "go.uber.org/fx/fxtest" + "golang.org/x/crypto/bcrypt" ) +func TestMain(m *testing.M) { + db.SetBcryptCost(bcrypt.MinCost) + os.Exit(m.Run()) +} + const ( commandKey = "command" bodyKey = "body" @@ -102,7 +109,6 @@ func newTestServer( ) app.RequireStart() - time.Sleep(100 * time.Millisecond) httpSrv := httptest.NewServer(srv) @@ -2985,10 +2991,7 @@ func newTestServerWithOper( fx.Populate(&srv), ) - const startupDelay = 100 * time.Millisecond - app.RequireStart() - time.Sleep(startupDelay) httpSrv := httptest.NewServer(srv)