1 Commits
Author SHA1 Message Date
sneak c492e5969e Stop the slow test packages waiting (closes #263)
check / check (push) Failing after 7s
- Tests hash passwords at bcrypt.MinCost through db.PasswordCost.
- Test IRC servers check the queue only on broker notification;
  one test keeps the 100 ms fallback poll.
- The stop-timeout test shortens the timeout through a test-only
  setter.
- service and handlers tests build config.Config directly instead
  of through config.New, so they run in parallel: service had run
  serially, and handlers started its servers one at a time.
- Repeated race trials run as parallel subtests.
- Fixed sleeps become waits on the broker.
- A new test covers the relay's failed queue read, which only the
  100 ms poll had reached.

Model: opus-5-5
2026-11-10 20:37:33 +00:00
6 changed files with 34 additions and 29 deletions

No files matched your search

+5 -1
View File
@@ -14,6 +14,10 @@ var errNoPassword = errors.New(
"account has no password set", "account has no password set",
) )
// PasswordCost is the bcrypt cost at which SetPassword hashes a
// password. Tests lower it to bcrypt.MinCost before they start.
var PasswordCost = bcrypt.DefaultCost //nolint:gochecknoglobals // tests lower it
// SetPassword sets a bcrypt-hashed password on a session, // SetPassword sets a bcrypt-hashed password on a session,
// enabling multi-client login via POST /api/v1/login. // enabling multi-client login via POST /api/v1/login.
func (database *Database) SetPassword( func (database *Database) SetPassword(
@@ -22,7 +26,7 @@ func (database *Database) SetPassword(
password string, password string,
) error { ) error {
hash, err := bcrypt.GenerateFromPassword( hash, err := bcrypt.GenerateFromPassword(
[]byte(password), database.passwordCost, []byte(password), PasswordCost,
) )
if err != nil { if err != nil {
return fmt.Errorf("hash password: %w", err) return fmt.Errorf("hash password: %w", err)
+5 -18
View File
@@ -15,7 +15,6 @@ import (
"strings" "strings"
"go.uber.org/fx" "go.uber.org/fx"
"golang.org/x/crypto/bcrypt"
"sneak.berlin/go/neoirc/internal/config" "sneak.berlin/go/neoirc/internal/config"
"sneak.berlin/go/neoirc/internal/logger" "sneak.berlin/go/neoirc/internal/logger"
@@ -40,19 +39,13 @@ type Params struct {
Logger *logger.Logger Logger *logger.Logger
Config *config.Config Config *config.Config
// PasswordCost is the bcrypt cost at which SetPassword hashes
// a password. The server leaves it unset, which means
// bcrypt.DefaultCost. Tests set a lower cost to save time.
PasswordCost int `optional:"true"`
} }
// Database manages the SQLite connection and migrations. // Database manages the SQLite connection and migrations.
type Database struct { type Database struct {
conn *sql.DB conn *sql.DB
log *slog.Logger log *slog.Logger
params *Params params *Params
passwordCost int
} }
// New creates a new Database and registers lifecycle hooks. // New creates a new Database and registers lifecycle hooks.
@@ -60,15 +53,9 @@ func New(
lifecycle fx.Lifecycle, lifecycle fx.Lifecycle,
params Params, params Params,
) (*Database, error) { ) (*Database, error) {
passwordCost := params.PasswordCost
if passwordCost == 0 {
passwordCost = bcrypt.DefaultCost
}
database := &Database{ //nolint:exhaustruct // conn set in OnStart database := &Database{ //nolint:exhaustruct // conn set in OnStart
params: &params, params: &params,
log: params.Logger.Get(), log: params.Logger.Get(),
passwordCost: passwordCost,
} }
database.log.Info("Database instantiated") database.log.Info("Database instantiated")
+8
View File
@@ -10,6 +10,7 @@ import (
"time" "time"
"go.uber.org/fx/fxtest" "go.uber.org/fx/fxtest"
"golang.org/x/crypto/bcrypt"
"sneak.berlin/go/neoirc/internal/config" "sneak.berlin/go/neoirc/internal/config"
"sneak.berlin/go/neoirc/internal/db" "sneak.berlin/go/neoirc/internal/db"
"sneak.berlin/go/neoirc/internal/dbtest" "sneak.berlin/go/neoirc/internal/dbtest"
@@ -29,6 +30,13 @@ const (
endlessQueryTimeout = 100 * time.Millisecond endlessQueryTimeout = 100 * time.Millisecond
) )
func TestMain(m *testing.M) {
// A password hashed at full cost takes seconds under -race.
db.PasswordCost = bcrypt.MinCost
m.Run()
}
// TestDefaultDBURLSetsWALAndBusyTimeout opens a file database // TestDefaultDBURLSetsWALAndBusyTimeout opens a file database
// with the default URL's parameters the way the server does, // with the default URL's parameters the way the server does,
// and checks the journal mode and busy timeout it ends up with. // and checks the journal mode and busy timeout it ends up with.
+3 -6
View File
@@ -8,12 +8,10 @@ import (
"testing" "testing"
"time" "time"
"golang.org/x/crypto/bcrypt"
"sneak.berlin/go/neoirc/internal/dbtest" "sneak.berlin/go/neoirc/internal/dbtest"
) )
// NewTestDatabase creates an in-memory database for testing. It // NewTestDatabase creates an in-memory database for testing.
// hashes passwords at bcrypt.MinCost.
func NewTestDatabase(tb testing.TB) (*Database, error) { func NewTestDatabase(tb testing.TB) (*Database, error) {
tb.Helper() tb.Helper()
@@ -25,9 +23,8 @@ func NewTestDatabase(tb testing.TB) (*Database, error) {
} }
database := &Database{ //nolint:exhaustruct // test helper, params not needed database := &Database{ //nolint:exhaustruct // test helper, params not needed
conn: conn, conn: conn,
log: slog.Default(), log: slog.Default(),
passwordCost: bcrypt.MinCost,
} }
err = database.runMigrations(context.Background()) err = database.runMigrations(context.Background())
+4 -1
View File
@@ -47,6 +47,9 @@ import (
const templateDBURL = "file:template?mode=memory&cache=shared" const templateDBURL = "file:template?mode=memory&cache=shared"
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
// A password hashed at full cost takes seconds under -race.
db.PasswordCost = bcrypt.MinCost
template := fx.New( template := fx.New(
fx.NopLogger, fx.NopLogger,
fx.Supply( fx.Supply(
@@ -234,7 +237,7 @@ func newTestDB(
cfg *config.Config, cfg *config.Config,
) (*db.Database, error) { ) (*db.Database, error) {
database, err := db.New(lifecycle, db.Params{ //nolint:exhaustruct database, err := db.New(lifecycle, db.Params{ //nolint:exhaustruct
Logger: log, Config: cfg, PasswordCost: bcrypt.MinCost, Logger: log, Config: cfg,
}) })
if err != nil { if err != nil {
return nil, fmt.Errorf("test db: %w", err) return nil, fmt.Errorf("test db: %w", err)
+9 -3
View File
@@ -39,6 +39,13 @@ const testTimeout = 5 * time.Second
// notification, and a test fails when one is missing. // notification, and a test fails when one is missing.
const testPollInterval = time.Hour const testPollInterval = time.Hour
func TestMain(m *testing.M) {
// A password hashed at full cost takes seconds under -race.
db.PasswordCost = bcrypt.MinCost
m.Run()
}
// testEnv holds the shared test infrastructure. // testEnv holds the shared test infrastructure.
type testEnv struct { type testEnv struct {
dbURL string dbURL string
@@ -126,9 +133,8 @@ func newTestEnvWithResolver(
} }
database, err := db.New(lifecycle, db.Params{ database, err := db.New(lifecycle, db.Params{
Logger: dbLog, Logger: dbLog,
Config: &config.Config{DBURL: dbURL}, Config: &config.Config{DBURL: dbURL},
PasswordCost: bcrypt.MinCost,
}) })
if err != nil { if err != nil {
t.Fatalf("new database: %v", err) t.Fatalf("new database: %v", err)