Initial commit: archer, a Go-native MySQL/MariaDB table archiver
Renamed from arcdb: module path, cmd/archer, internal/archererr, docs, build scripts and ARCHER_* environment variables. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
commit
bc4a6b504f
99 files changed
+12752
No files matched your search
@@ -0,0 +1,252 @@
|
||||
package progress
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type clock struct{ t time.Time }
|
||||
|
||||
func (c *clock) now() time.Time { return c.t }
|
||||
func (c *clock) add(d time.Duration) { c.t = c.t.Add(d) }
|
||||
|
||||
func newTestReporter(t *testing.T, live, enabled bool, opts ...func(*Options)) (*Reporter, *bytes.Buffer, *clock) {
|
||||
t.Helper()
|
||||
var buf bytes.Buffer
|
||||
clk := &clock{t: time.Unix(1_700_000_000, 0)}
|
||||
o := Options{Out: &buf, Enabled: enabled, Live: live, Interval: time.Second, Now: clk.now}
|
||||
for _, fn := range opts {
|
||||
fn(&o)
|
||||
}
|
||||
r := New(o)
|
||||
r.mu.Lock()
|
||||
r.started = true
|
||||
r.initLocked(clk.t)
|
||||
r.mu.Unlock()
|
||||
return r, &buf, clk
|
||||
}
|
||||
|
||||
func TestLiveLineHasRequiredFields(t *testing.T) {
|
||||
r, buf, clk := newTestReporter(t, true, true)
|
||||
r.SetEstimate(&Estimate{Kind: "day", Lo: 0, Hi: 10 * 86400})
|
||||
r.Phase("scan", "")
|
||||
r.SetShard(12)
|
||||
r.SetPosition(3 * 86400)
|
||||
r.AddRows(1000)
|
||||
clk.add(time.Second)
|
||||
r.SetPosition(4 * 86400)
|
||||
r.AddRows(1000)
|
||||
r.maybeRender(true)
|
||||
|
||||
out := buf.String()
|
||||
if !strings.HasPrefix(out, "\r") {
|
||||
t.Fatalf("live line must start with carriage return: %q", out)
|
||||
}
|
||||
if strings.Contains(out, "\n") {
|
||||
t.Fatalf("live line must not emit a newline: %q", out)
|
||||
}
|
||||
for _, want := range []string{"rows", "~40%", "day 5/11", "shard 12", "scan", "eta ~", "elapsed"} {
|
||||
if !strings.Contains(out, want) {
|
||||
t.Fatalf("live line %q missing %q", out, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLiveLineOmitsEstimateWhenUnavailable(t *testing.T) {
|
||||
r, buf, _ := newTestReporter(t, true, true)
|
||||
r.Phase("scan", "")
|
||||
r.AddRows(10)
|
||||
r.maybeRender(true)
|
||||
out := buf.String()
|
||||
if strings.Contains(out, "%") || strings.Contains(out, "eta") {
|
||||
t.Fatalf("unexpected estimate in %q", out)
|
||||
}
|
||||
if !strings.Contains(out, "rows") {
|
||||
t.Fatalf("missing rows in %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLineRenderingIsStableKeyValue(t *testing.T) {
|
||||
r, buf, clk := newTestReporter(t, false, true)
|
||||
r.SetEstimate(&Estimate{Kind: "key", Lo: 0, Hi: 1000})
|
||||
r.Phase("db", "")
|
||||
r.SetPosition(100)
|
||||
r.AddRows(500)
|
||||
clk.add(time.Second)
|
||||
r.SetPosition(500)
|
||||
r.AddRows(500)
|
||||
r.maybeRender(true)
|
||||
|
||||
out := buf.String()
|
||||
if strings.Contains(out, "\r") {
|
||||
t.Fatalf("line mode must not use carriage returns: %q", out)
|
||||
}
|
||||
if !strings.HasSuffix(out, "\n") {
|
||||
t.Fatalf("line mode must end with a newline: %q", out)
|
||||
}
|
||||
for _, want := range []string{
|
||||
"progress: rows=1000", "rate=", "phase=db", "phase_elapsed=", "elapsed=",
|
||||
"estimate=key", "pct=~50.0", "eta=~",
|
||||
} {
|
||||
if !strings.Contains(out, want) {
|
||||
t.Fatalf("line %q missing %q", out, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLineRenderingMarksEstimateNone(t *testing.T) {
|
||||
r, buf, _ := newTestReporter(t, false, true)
|
||||
r.Phase("scan", "")
|
||||
r.AddRows(100)
|
||||
r.maybeRender(true)
|
||||
if out := buf.String(); !strings.Contains(out, "estimate=none") {
|
||||
t.Fatalf("want estimate=none, got %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntervalThrottle(t *testing.T) {
|
||||
r, buf, clk := newTestReporter(t, false, true)
|
||||
r.maybeRender(true)
|
||||
if n := strings.Count(buf.String(), "\n"); n != 1 {
|
||||
t.Fatalf("first frame not drawn: %q", buf.String())
|
||||
}
|
||||
clk.add(200 * time.Millisecond)
|
||||
r.maybeRender(false)
|
||||
if n := strings.Count(buf.String(), "\n"); n != 1 {
|
||||
t.Fatalf("throttled frame drawn early: %q", buf.String())
|
||||
}
|
||||
clk.add(time.Second)
|
||||
r.maybeRender(false)
|
||||
if n := strings.Count(buf.String(), "\n"); n != 2 {
|
||||
t.Fatalf("frame after interval not drawn: %q", buf.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRowThresholdRenders(t *testing.T) {
|
||||
r, buf, _ := newTestReporter(t, false, true, func(o *Options) { o.RowEvery = 100 })
|
||||
r.AddRows(100)
|
||||
if n := strings.Count(buf.String(), "\n"); n != 1 {
|
||||
t.Fatalf("crossing the first threshold did not render: %q", buf.String())
|
||||
}
|
||||
r.AddRows(50)
|
||||
if n := strings.Count(buf.String(), "\n"); n != 1 {
|
||||
t.Fatalf("sub-threshold rows must not render: %q", buf.String())
|
||||
}
|
||||
r.AddRows(50)
|
||||
if n := strings.Count(buf.String(), "\n"); n != 2 {
|
||||
t.Fatalf("crossing the next threshold did not render: %q", buf.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessageClearsLiveLine(t *testing.T) {
|
||||
r, buf, _ := newTestReporter(t, true, true)
|
||||
r.AddRows(1000)
|
||||
r.maybeRender(true)
|
||||
width := r.lastWidth
|
||||
buf.Reset()
|
||||
r.Message("warning: %s", "something")
|
||||
|
||||
out := buf.String()
|
||||
if !strings.Contains(out, "\r"+strings.Repeat(" ", width)+"\r") {
|
||||
t.Fatalf("message did not clear the live line: %q", out)
|
||||
}
|
||||
if !strings.Contains(out, "warning: something\n") {
|
||||
t.Fatalf("message missing: %q", out)
|
||||
}
|
||||
if strings.Contains(out, "rows=") {
|
||||
t.Fatalf("live line leaked into message: %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestShortLineIsPadded(t *testing.T) {
|
||||
r, buf, clk := newTestReporter(t, true, true)
|
||||
r.SetEstimate(&Estimate{Kind: "day", Lo: 0, Hi: 100 * 86400})
|
||||
r.SetPosition(50 * 86400)
|
||||
r.AddRows(1000)
|
||||
r.maybeRender(true)
|
||||
first := r.lastWidth
|
||||
|
||||
buf.Reset()
|
||||
r.SetEstimate(nil)
|
||||
clk.add(2 * time.Second)
|
||||
r.AddRows(1000)
|
||||
r.maybeRender(true)
|
||||
second := buf.String()
|
||||
if r.lastWidth >= first {
|
||||
t.Fatalf("second frame should be shorter: %d -> %d (%q)", first, r.lastWidth, second)
|
||||
}
|
||||
if !strings.HasSuffix(second, strings.Repeat(" ", first-r.lastWidth)) {
|
||||
t.Fatalf("short frame not padded to previous width: %q", second)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDoneSummary(t *testing.T) {
|
||||
r, buf, clk := newTestReporter(t, false, true)
|
||||
r.SetShard(3)
|
||||
r.SetShards(3)
|
||||
r.SetBytes(4096)
|
||||
r.AddRows(1500)
|
||||
clk.add(3 * time.Second)
|
||||
r.Done()
|
||||
out := buf.String()
|
||||
for _, want := range []string{"done rows=1500", "elapsed=3s", "shards=3", "bytes=4096"} {
|
||||
if !strings.Contains(out, want) {
|
||||
t.Fatalf("done line %q missing %q", out, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInterruptedSnapshot(t *testing.T) {
|
||||
r, buf, _ := newTestReporter(t, true, true)
|
||||
r.Phase("file", "")
|
||||
r.AddRows(42)
|
||||
r.maybeRender(true)
|
||||
buf.Reset()
|
||||
r.Interrupted()
|
||||
out := buf.String()
|
||||
if !strings.Contains(out, "progress: rows=42") {
|
||||
t.Fatalf("final snapshot missing: %q", out)
|
||||
}
|
||||
if !strings.Contains(out, "phase=file") {
|
||||
t.Fatalf("final snapshot missing phase: %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDisabledReporterIsSilentButMessagesWork(t *testing.T) {
|
||||
r, buf, _ := newTestReporter(t, false, false)
|
||||
r.AddRows(10)
|
||||
r.maybeRender(true)
|
||||
r.Done()
|
||||
if buf.Len() != 0 {
|
||||
t.Fatalf("disabled reporter emitted progress: %q", buf.String())
|
||||
}
|
||||
r.Message("warning: still visible")
|
||||
if !strings.Contains(buf.String(), "warning: still visible") {
|
||||
t.Fatalf("messages must survive disabled progress: %q", buf.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepeatedPhaseKeepsClock(t *testing.T) {
|
||||
r, buf, clk := newTestReporter(t, false, true)
|
||||
r.Phase("lag-wait", "reason a")
|
||||
clk.add(3 * time.Second)
|
||||
r.Phase("lag-wait", "reason b")
|
||||
r.maybeRender(true)
|
||||
out := buf.String()
|
||||
if !strings.Contains(out, "phase=lag-wait") || !strings.Contains(out, "phase_elapsed=3s") {
|
||||
t.Fatalf("repeated phase reset its clock: %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLagWaitShowsReason(t *testing.T) {
|
||||
r, buf, _ := newTestReporter(t, false, true)
|
||||
r.Phase("lag-wait", "replica lag 3s exceeds 1s")
|
||||
r.AddRows(10)
|
||||
r.maybeRender(true)
|
||||
out := buf.String()
|
||||
if !strings.Contains(out, "phase=lag-wait") || !strings.Contains(out, "reason=replica_lag_3s_exceeds_1s") {
|
||||
t.Fatalf("lag reason missing: %q", out)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user