Skip to content
quarry Artifact [7e3655276e]
Login

Artifact [7e3655276e]

Artifact 7e3655276ebdb37ab756baceb6512599cab42f0c43920e4cb3a99f3000ae25f3:


package oltp

import (
	"path/filepath"
	"testing"
)

// The waiting list keeps first-wait order, answers by check-in, forgets
// on undefer, and the running count sees only unfinished runs.
func TestDeferredCheckinsRoundTrip(t *testing.T) {
	db, err := Open(filepath.Join(t.TempDir(), "oltp.db"))
	if err != nil {
		t.Fatal(err)
	}
	defer db.Close()
	if err := db.DeferCI(CIDeferred{Repo: "r", Checkin: "c1", Branch: "feat/a", User: "ada"}); err != nil {
		t.Fatal(err)
	}
	if err := db.DeferCI(CIDeferred{Repo: "r", Checkin: "c2", Branch: "feat/b"}); err != nil {
		t.Fatal(err)
	}
	if err := db.DeferCI(CIDeferred{Repo: "r", Checkin: "c1", Branch: "feat/a"}); err != nil {
		t.Fatal(err) // twice keeps the first wait
	}
	if err := db.DeferCI(CIDeferred{Checkin: "x"}); err == nil {
		t.Error("a row without a repository was accepted")
	}
	rows, err := db.CIDeferred("r")
	if err != nil || len(rows) != 2 || rows[0].Checkin != "c1" || rows[1].Checkin != "c2" || rows[0].Since == "" {
		t.Fatalf("waiting = %+v, %v", rows, err)
	}
	if w, ok, _ := db.CIDeferredFor("r", "c2"); !ok || w.Branch != "feat/b" {
		t.Errorf("by check-in = %+v %v", w, ok)
	}
	if _, ok, _ := db.CIDeferredFor("r", "nope"); ok {
		t.Error("an unknown check-in reads as waiting")
	}
	if err := db.UndeferCI("r", "c1"); err != nil {
		t.Fatal(err)
	}
	if rows, _ = db.CIDeferred("r"); len(rows) != 1 || rows[0].Checkin != "c2" {
		t.Errorf("after undefer = %+v", rows)
	}
	if err := db.AddCIRun(CIRun{RunID: "run1", Repo: "r", Checkin: "c9", Workflow: "w", Pipeline: "p", Status: "running"}); err != nil {
		t.Fatal(err)
	}
	if err := db.AddCIRun(CIRun{RunID: "run2", Repo: "r", Checkin: "c8", Workflow: "w", Pipeline: "p", Status: "completed"}); err != nil {
		t.Fatal(err)
	}
	if n, _ := db.RunningCIRuns(); n != 1 {
		t.Errorf("running = %d, want 1 (the completed one does not count)", n)
	}
}