Download project at step 13 (.zip)

13 / 14

Step 13 · Build a live chat app

🤖 Run the whole gate

make ci is every check the app has, in one command: vet, formatting, go fix, static analysis, lint, known vulnerabilities, leaked secrets, size and code limits, the route table, the content security policy, the tests (which also check the migrations against the models), the performance lane, and whether the generated files are up to date. CI runs the same command, so passing here means passing there.

The first run fails in the performance lane. That lane times every page, and the room and admin pages can't be timed without a room in the database and an admin signed in. Give the performance test what it needs: CHAT_ADMINS for its signed-in user, a room in the database, and a real URL for each page with an {id} in it.

Check it worked

make ci finishes without an error.

🤖 These pages were written by an LLM, on the ideas, instruction, and editing of humans. AI-written text here is always marked, visibly — that's factor X.

perf_test.go Edit

internal/chat/perf_test.go
//go:build perf

package chat_test

import (
	"io"
	"log/slog"
	"net/http"
	"testing"
	"time"

	"amadan.net/rastrillo/rastrillo"
	"amadan.net/rastrillo/rastrillo/background"
	"amadan.net/rastrillo/rastrillo/db"
	"amadan.net/rastrillo/rastrillo/mail"
	"amadan.net/rastrillo/rastrillo/perf/perftest"

	genlocales "chat/gen/locales"
	"chat/internal/chat"
)

// Every GET screen is held to 150ms to first byte and to its first-load
// weight: a new route fails until measured or in .rastrillo/budgets.txt.
// Never t.Parallel() in this file: a perf test measures its neighbours.
func TestPerfScreens(t *testing.T) {
	t.Setenv("CHAT_ADMINS", "perf@example.com")
	logger := slog.New(slog.NewTextHandler(io.Discard, nil))
	database, err := db.Open(schema.Path(t), logger)
	if err != nil {
		t.Fatal(err)
	}
	defer database.Close()
	group := &background.Group{}
	box := &outbox{}
	router, hub, err := chat.Router(
		database, testOrigin, group, box, logger)
	if err != nil {
		t.Fatal(err)
	}
	if err := database.G.Create(&chat.Room{Name: "General"}).Error; err != nil {
		t.Fatal(err)
	}
	mux := http.NewServeMux()
	mux.Handle("/", router)
	options := servingOptions(logger)
	chat.Configure(&options, mux, hub, time.Now())
	options.Background = group // closeAll stops it
	handler, closeAll, err := rastrillo.Handler(options)
	if err != nil {
		t.Fatal(err)
	}
	defer func() {
		if err := closeAll(); err != nil {
			t.Error(err)
		}
	}()
	perftest.Screens(t, perftest.ScreenConfig{
		Routes: router, Handler: handler,
		Paths: map[string]string{
			"/rooms/{id}":              "/rooms/1",
			"/admin/rooms/{id}":        "/admin/rooms/1",
			"/admin/rooms/{id}/delete": "/admin/rooms/1/delete",
		},
		Signin: func(client *http.Client, base string) {
			signInOver(t, client, base, box, "perf@example.com")
		},
	})
}

func TestPerfBoot(t *testing.T) {
	perftest.Boot(t, perftest.BootConfig{
		Prepare: schema.Path,
		Build:   boot,
		Path:    "/",
	})
}

func boot(path string) (http.Handler, func(), error) {
	started := time.Now()
	logger := slog.New(slog.NewTextHandler(io.Discard, nil))
	database, err := db.Open(path, logger)
	if err != nil {
		return nil, nil, err
	}
	group := &background.Group{}
	mailer := mail.Logged(logger)
	mux, hub, err := chat.New(
		database, testOrigin, group, mailer, logger)
	if err != nil {
		group.Stop()
		database.Close()
		return nil, nil, err
	}
	options := servingOptions(logger)
	chat.Configure(&options, mux, hub, started)
	options.Background = group // closeAll stops it
	handler, closeAll, err := rastrillo.Handler(options)
	if err != nil {
		database.Close()
		return nil, nil, err
	}
	// The child exits after this, so no one can report a close failure.
	return handler, func() { _ = closeAll(); database.Close() }, nil
}

// servingOptions are main.go's, so the lane measures what is served.
func servingOptions(logger *slog.Logger) rastrillo.Options {
	return rastrillo.Options{Logger: logger, Locales: genlocales.Locales,
		DefaultLocale: genlocales.DefaultLocale}
}
Terminal
make ci