Skip to content
quarry Artifact [75f2dd67c6]
Login

Artifact [75f2dd67c6]

Artifact 75f2dd67c62c117fe64b5e00444bafe991338ae16f60258f7bd6d1f2b87e6490:


package zitcli

import (
	"context"
	"errors"
	"testing"
	"time"

	"craftdesign.group/quarry/internal/obs"
	"craftdesign.group/quarry/internal/ops"
)

// The gate admits its bound and no more: the next caller waits, and a
// caller whose request is cancelled while waiting is told busy rather
// than queued. Busy is an ErrUnavailable, so the surfaces above answer
// 503 with Retry-After.
func TestAdmissionBoundsSubprocesses(t *testing.T) {
	a := newAdmission(2)
	if !a.acquire(context.Background()) || !a.acquire(context.Background()) {
		t.Fatal("the first two callers were refused")
	}
	ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
	defer cancel()
	start := time.Now()
	if a.acquire(ctx) {
		t.Fatal("a third caller was admitted past the bound")
	}
	if time.Since(start) > busyWait {
		t.Errorf("a cancelled caller waited the full busyWait")
	}
	a.release()
	if !a.acquire(context.Background()) {
		t.Fatal("a released slot was not reusable")
	}
	// The lane counts what happened, for the ops page.
	if a.admitted != 3 || a.refused != 1 {
		t.Errorf("lane counted admitted=%d refused=%d, want 3 and 1", a.admitted, a.refused)
	}
	if !errors.Is(ErrBusy, ops.ErrUnavailable) {
		t.Errorf("ErrBusy is not an ErrUnavailable")
	}
}

// A command minted while the gate is full fails at once with ErrBusy
// instead of spawning.
func TestCommandFailsBusyWhenTheGateIsFull(t *testing.T) {
	old := Parallel()
	SetParallel(1)
	t.Cleanup(func() { SetParallel(old) })
	if !gate.acquire(context.Background()) {
		t.Fatal("could not fill the gate")
	}
	defer gate.release()
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Millisecond)
	defer cancel()
	cmd, done := New(t.TempDir()).command(ctx, "version")
	defer done()
	if _, err := cmd.Output(); !errors.Is(err, ErrBusy) {
		t.Fatalf("command past the bound ran or failed otherwise: %v", err)
	}
}

// Background callers (a reason on the context) hold at most half the
// slots; an interactive caller still finds one.
func TestAdmissionCapsBackgroundShare(t *testing.T) {
	a := newAdmission(8)
	bgCtx := obs.WithReason(context.Background(), obs.ReasonWarm)
	for i := 0; i < 4; i++ {
		if !a.acquireAs(bgCtx, true) {
			t.Fatalf("FAIL: background holder %d refused under the cap", i)
		}
	}
	ctx, cancel := context.WithTimeout(bgCtx, 50*time.Millisecond)
	defer cancel()
	if a.acquireAs(ctx, true) {
		t.Errorf("FAIL: a fifth background caller was admitted past half the slots")
	}
	if !a.acquireAs(context.Background(), false) {
		t.Errorf("FAIL: an interactive caller was refused while slots were free")
	}
	if got := len(a.slots); got != 5 {
		t.Errorf("FAIL: slots held = %d, want 5 (a refused background caller must not leak one)", got)
	}
	a.releaseAs(true)
	if !a.acquireAs(context.Background(), true) {
		t.Errorf("FAIL: a released background share was not reusable")
	}
	if bgShare(1) != 1 || bgShare(2) != 1 || bgShare(8) != 4 {
		t.Errorf("FAIL: bgShare = %d,%d,%d", bgShare(1), bgShare(2), bgShare(8))
	}
	if !t.Failed() {
		t.Log("ok")
	}
}