Skip to content
quarry Artifact [5c65bd75c8]
Login

Artifact [5c65bd75c8]

Artifact 5c65bd75c85a2cbe43837e4d8b4a6808e3e15f63e3407b604b6ff2cd41a7072d:


package timing

import (
	"testing"
	"time"
)

// The footer reported "ops 11.09s×8, zit 32.9ms" on a page whose real
// cost was six seconds of a zit call made on context.Background(). The
// timer lives in the request context, so a detached call is invisible
// to it while the render still waits. The store note is what makes that
// wait visible.
func TestStoreBusyNamesWaitTheContextMissed(t *testing.T) {
	got := WithStoreBusy("ops 11.09s×8, zit 32.9ms", 6*time.Second)
	if got == "ops 11.09s×8, zit 32.9ms" {
		t.Fatal("six seconds of store wait went unreported")
	}
	if want := "store "; !contains(got, want) {
		t.Errorf("breakdown %q does not name the store wait", got)
	}
}

// When the render's own zit total already covers the busy window there
// is nothing new to say, and a second number would only invite adding
// the two together.
func TestStoreBusyStaysQuietWhenAlreadyAttributed(t *testing.T) {
	in := "ops 500ms×3, zit 400ms"
	if got := WithStoreBusy(in, 300*time.Millisecond); got != in {
		t.Errorf("added a redundant note: %q", got)
	}
	if got := WithStoreBusy(in, 0); got != in {
		t.Errorf("added a note for no busy time: %q", got)
	}
}

// A page with no other timing still reports the wait rather than
// dropping it.
func TestStoreBusyOnAnEmptyBreakdown(t *testing.T) {
	if got := WithStoreBusy("", 2*time.Second); got == "" {
		t.Error("store wait dropped when there was nothing else to say")
	}
}

func contains(h, n string) bool {
	for i := 0; i+len(n) <= len(h); i++ {
		if h[i:i+len(n)] == n {
			return true
		}
	}
	return false
}