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,113 @@
|
||||
package estimate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"archer/internal/schema"
|
||||
)
|
||||
|
||||
func validRange(minRaw, maxRaw string) RangeQuery {
|
||||
return func(context.Context, string) (sql.NullString, sql.NullString, error) {
|
||||
return sql.NullString{String: minRaw, Valid: true}, sql.NullString{String: maxRaw, Valid: true}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeyColumnApplicability(t *testing.T) {
|
||||
cases := []struct {
|
||||
cols, types []string
|
||||
want bool
|
||||
}{
|
||||
{[]string{"id"}, []string{"bigint"}, true},
|
||||
{[]string{"id"}, []string{"int"}, true},
|
||||
{[]string{"a", "b"}, []string{"bigint", "bigint"}, false},
|
||||
{[]string{"code"}, []string{"varchar"}, false},
|
||||
{[]string{"payload"}, []string{"blob"}, false},
|
||||
{[]string{"bin"}, []string{"varbinary"}, false},
|
||||
{[]string{"ts"}, []string{"datetime"}, false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
col, ok := KeyColumn(tc.cols, tc.types)
|
||||
if ok != tc.want {
|
||||
t.Fatalf("%v/%v: got %v want %v", tc.cols, tc.types, ok, tc.want)
|
||||
}
|
||||
if ok && col != tc.cols[0] {
|
||||
t.Fatalf("%v: wrong column %q", tc.cols, col)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBoundedDayRangeFromDates(t *testing.T) {
|
||||
spec := Spec{Kind: "day", Column: "created_at", ShardKind: schema.ShardDate, Loc: time.UTC}
|
||||
got := Bounded(context.Background(), validRange("2025-01-01", "2025-01-11"), spec, time.Second)
|
||||
if got == nil || got.Kind != "day" {
|
||||
t.Fatalf("got %+v", got)
|
||||
}
|
||||
if d := time.Duration(got.Hi-got.Lo) * time.Second; d != 10*24*time.Hour {
|
||||
t.Fatalf("span = %s", d)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBoundedDayRangeFromDatetimeAndEpoch(t *testing.T) {
|
||||
dt := Spec{Kind: "day", Column: "c", ShardKind: schema.ShardDateTime, Loc: time.UTC}
|
||||
if got := Bounded(context.Background(), validRange("2025-01-01 10:00:00", "2025-01-03 23:00:00"), dt, time.Second); got == nil {
|
||||
t.Fatal("datetime range unavailable")
|
||||
}
|
||||
ep := Spec{Kind: "day", Column: "ts", ShardKind: schema.ShardEpoch, Unit: 1, Loc: time.UTC}
|
||||
lo := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
|
||||
hi := time.Date(2025, 1, 4, 0, 0, 0, 0, time.UTC).Unix()
|
||||
got := Bounded(context.Background(), validRange(strconv.FormatInt(lo, 10), strconv.FormatInt(hi, 10)), ep, time.Second)
|
||||
if got == nil || got.Hi-got.Lo != 3*86400 {
|
||||
t.Fatalf("epoch range = %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBoundedKeyRange(t *testing.T) {
|
||||
spec := Spec{Kind: "key", Column: "id"}
|
||||
got := Bounded(context.Background(), validRange("100", "1100"), spec, time.Second)
|
||||
if got == nil || got.Kind != "key" || got.Lo != 100 || got.Hi != 1100 {
|
||||
t.Fatalf("got %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBoundedSkipsUnavailableMetrics(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
q RangeQuery
|
||||
spec Spec
|
||||
}{
|
||||
{"no metric", validRange("1", "10"), Spec{}},
|
||||
{"query error", func(context.Context, string) (sql.NullString, sql.NullString, error) {
|
||||
return sql.NullString{}, sql.NullString{}, context.DeadlineExceeded
|
||||
}, Spec{Kind: "key", Column: "id"}},
|
||||
{"null bounds", func(context.Context, string) (sql.NullString, sql.NullString, error) {
|
||||
return sql.NullString{}, sql.NullString{}, nil
|
||||
}, Spec{Kind: "key", Column: "id"}},
|
||||
{"empty table", validRange("5", "5"), Spec{Kind: "key", Column: "id"}},
|
||||
{"zero date", validRange("0000-00-00", "2025-01-01"), Spec{Kind: "day", ShardKind: schema.ShardDate, Loc: time.UTC}},
|
||||
{"string key", validRange("a", "z"), Spec{Kind: "key", Column: "id"}},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
if got := Bounded(context.Background(), tc.q, tc.spec, time.Second); got != nil {
|
||||
t.Fatalf("%s: expected nil, got %+v", tc.name, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBoundedTimeoutDegrades(t *testing.T) {
|
||||
blocked := func(ctx context.Context, _ string) (sql.NullString, sql.NullString, error) {
|
||||
<-ctx.Done()
|
||||
return sql.NullString{}, sql.NullString{}, ctx.Err()
|
||||
}
|
||||
start := time.Now()
|
||||
got := Bounded(context.Background(), blocked, Spec{Kind: "key", Column: "id"}, 20*time.Millisecond)
|
||||
if got != nil {
|
||||
t.Fatalf("timed-out estimate must be nil, got %+v", got)
|
||||
}
|
||||
if elapsed := time.Since(start); elapsed > time.Second {
|
||||
t.Fatalf("timeout took too long: %s", elapsed)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user