check / check (push) Waiting to run
The slowest test packages spent their time waiting rather than testing. db.Params gains an optional PasswordCost (zero means bcrypt.DefaultCost), and the db, ircserver and handlers tests hash at bcrypt.MinCost. A test IRC server's relay checks its queue on broker notification instead of polling. The service and handlers tests build config.Config directly and run in parallel; config exports DefaultTrustedProxies and ParseTrustedProxies for them. Fixed sleeps give way to waits for the event. Production defaults are unchanged. What still takes time is CPU work under -race or a timeout a test is about. Model: opus-5-5
123 lines
3.1 KiB
Go
123 lines
3.1 KiB
Go
package ircserver
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"net"
|
|
"time"
|
|
|
|
"sneak.berlin/go/neoirc/internal/broker"
|
|
"sneak.berlin/go/neoirc/internal/config"
|
|
"sneak.berlin/go/neoirc/internal/db"
|
|
"sneak.berlin/go/neoirc/internal/globals"
|
|
"sneak.berlin/go/neoirc/internal/service"
|
|
)
|
|
|
|
// NewTestServer creates a Server suitable for testing, whose
|
|
// connections use interval, deadline and poll in place of
|
|
// pingInterval, pongDeadline and pollInterval, and resolve in
|
|
// place of resolveHost.
|
|
// The caller must call Stop() when finished.
|
|
func NewTestServer(
|
|
log *slog.Logger,
|
|
cfg *config.Config,
|
|
database *db.Database,
|
|
brk *broker.Broker,
|
|
interval, deadline, poll time.Duration,
|
|
resolve func(ctx context.Context, addr string) string,
|
|
) *Server {
|
|
globs := &globals.Globals{
|
|
Appname: "neoirc",
|
|
Version: "test",
|
|
StartTime: time.Now(),
|
|
}
|
|
|
|
svc := service.NewTestService(
|
|
database, brk, cfg, globs, log,
|
|
)
|
|
|
|
return &Server{ //nolint:exhaustruct
|
|
log: log,
|
|
cfg: cfg,
|
|
database: database,
|
|
brk: brk,
|
|
svc: svc,
|
|
conns: make(map[*Conn]struct{}),
|
|
|
|
pingInterval: interval,
|
|
pongDeadline: deadline,
|
|
pollInterval: poll,
|
|
stopTimeout: stopTimeout,
|
|
resolveHost: resolve,
|
|
}
|
|
}
|
|
|
|
// SetStopTimeout makes Stop wait at most timeout for the
|
|
// connections' cleanup.
|
|
func (s *Server) SetStopTimeout(timeout time.Duration) {
|
|
s.stopTimeout = timeout
|
|
}
|
|
|
|
// ResolveHost is the reverse DNS lookup that a server made by
|
|
// New does for each client.
|
|
func ResolveHost(ctx context.Context, addr string) string {
|
|
return resolveHost(ctx, addr)
|
|
}
|
|
|
|
// Start exposes the unexported start method for tests.
|
|
func (s *Server) Start(addr string) error {
|
|
return s.start(context.Background(), addr)
|
|
}
|
|
|
|
// Stop exposes the unexported stop method for tests.
|
|
func (s *Server) Stop() {
|
|
s.stop()
|
|
}
|
|
|
|
// Listener returns the server's net.Listener for tests.
|
|
func (s *Server) Listener() net.Listener {
|
|
return s.listener
|
|
}
|
|
|
|
// Service returns the service the server's connections use.
|
|
func (s *Server) Service() *service.Service {
|
|
return s.svc
|
|
}
|
|
|
|
// PingTimes returns how long the server's connections wait
|
|
// with nothing read before they are sent a PING, and after it
|
|
// before they are closed.
|
|
func (s *Server) PingTimes() (time.Duration, time.Duration) {
|
|
return s.pingInterval, s.pongDeadline
|
|
}
|
|
|
|
// KillWriteWindow is how long Disconnect allows each write
|
|
// to a killed victim.
|
|
const KillWriteWindow = killWriteWindow
|
|
|
|
// PollInterval is how often the relay checks the client
|
|
// output queue when no broker notification arrives.
|
|
const PollInterval = pollInterval
|
|
|
|
// PingInterval is how long the server waits with nothing read
|
|
// from a connection before it sends a PING.
|
|
const PingInterval = pingInterval
|
|
|
|
// PongDeadline is how long the server waits after the PING
|
|
// for a line before it closes the connection.
|
|
const PongDeadline = pongDeadline
|
|
|
|
// NewTestConn returns a Conn for nick on tcpConn, without a
|
|
// server, database or service behind it.
|
|
func NewTestConn(
|
|
log *slog.Logger,
|
|
cfg *config.Config,
|
|
tcpConn net.Conn,
|
|
nick string,
|
|
) *Conn {
|
|
conn := newConn(tcpConn, log, nil, nil, cfg, nil, pollInterval)
|
|
conn.nick = nick
|
|
|
|
return conn
|
|
}
|