927 lines
27 KiB
Go
927 lines
27 KiB
Go
package questiontable
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Test doubles
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// stubRepo implements ExportRepository for tests.
|
|
type stubRepo struct {
|
|
mu sync.Mutex
|
|
|
|
claimFn func(ctx context.Context, limit int, maxAttempts int, staleBefore time.Time) ([]PendingExport, error)
|
|
|
|
// captured params from last Claim call.
|
|
lastClaimLimit int
|
|
lastClaimMaxAttempts int
|
|
lastClaimStaleBefore time.Time
|
|
|
|
// exported and failed records for assertions.
|
|
exported []exportedRecord
|
|
failed []failedRecord
|
|
|
|
// markExportedErr is returned by MarkExported when non-nil.
|
|
markExportedErr error
|
|
// markFailedErr is returned by MarkFailed when non-nil.
|
|
markFailedErr error
|
|
}
|
|
|
|
type exportedRecord struct {
|
|
ExportID string
|
|
Location string
|
|
}
|
|
|
|
type failedRecord struct {
|
|
ExportID string
|
|
FailureCode string
|
|
}
|
|
|
|
func newStubRepo() *stubRepo {
|
|
return &stubRepo{}
|
|
}
|
|
|
|
func (r *stubRepo) Claim(ctx context.Context, limit int, maxAttempts int, staleBefore time.Time) ([]PendingExport, error) {
|
|
r.mu.Lock()
|
|
r.lastClaimLimit = limit
|
|
r.lastClaimMaxAttempts = maxAttempts
|
|
r.lastClaimStaleBefore = staleBefore
|
|
r.mu.Unlock()
|
|
|
|
if r.claimFn != nil {
|
|
return r.claimFn(ctx, limit, maxAttempts, staleBefore)
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func (r *stubRepo) MarkExported(ctx context.Context, exportID, location string) error {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
r.exported = append(r.exported, exportedRecord{ExportID: exportID, Location: location})
|
|
if r.markExportedErr != nil {
|
|
return r.markExportedErr
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *stubRepo) MarkFailed(ctx context.Context, exportID, failureCode string) error {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
r.failed = append(r.failed, failedRecord{ExportID: exportID, FailureCode: failureCode})
|
|
if r.markFailedErr != nil {
|
|
return r.markFailedErr
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// stubInserter implements RecordInserter for tests.
|
|
type stubInserter struct {
|
|
mu sync.Mutex
|
|
rows []Row
|
|
recordID string
|
|
err error
|
|
blockCh chan struct{} // if set, blocks until closed
|
|
callCount int
|
|
|
|
// perCallErrors maps call index (1-based) to error to return.
|
|
// If an entry exists for a call index, that error is returned instead of a.err.
|
|
perCallErrors map[int]error
|
|
}
|
|
|
|
func newStubInserter() *stubInserter {
|
|
return &stubInserter{recordID: "rec-test-001"}
|
|
}
|
|
|
|
func (a *stubInserter) InsertRecord(ctx context.Context, row Row) (string, error) {
|
|
a.mu.Lock()
|
|
a.callCount++
|
|
call := a.callCount
|
|
a.rows = append(a.rows, row)
|
|
blockCh := a.blockCh
|
|
a.mu.Unlock()
|
|
|
|
if blockCh != nil {
|
|
select {
|
|
case <-ctx.Done():
|
|
return "", ctx.Err()
|
|
case <-blockCh:
|
|
}
|
|
}
|
|
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
|
|
// Check per-call error override.
|
|
if a.perCallErrors != nil {
|
|
if e, ok := a.perCallErrors[call]; ok {
|
|
return a.recordID, e
|
|
}
|
|
}
|
|
return a.recordID, a.err
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Test 1: empty queue
|
|
// ---------------------------------------------------------------------------
|
|
|
|
func TestDispatcherRunOnceEmptyQueue(t *testing.T) {
|
|
repo := newStubRepo()
|
|
inserter := newStubInserter()
|
|
|
|
d, err := NewDispatcher(repo, inserter, nil)
|
|
if err != nil {
|
|
t.Fatalf("NewDispatcher: %v", err)
|
|
}
|
|
|
|
stats, err := d.RunOnce(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("RunOnce: %v", err)
|
|
}
|
|
if stats.Claimed != 0 {
|
|
t.Errorf("Claimed = %d, want 0", stats.Claimed)
|
|
}
|
|
if stats.Exported != 0 {
|
|
t.Errorf("Exported = %d, want 0", stats.Exported)
|
|
}
|
|
if stats.Failed != 0 {
|
|
t.Errorf("Failed = %d, want 0", stats.Failed)
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Test 2: success and location mark
|
|
// ---------------------------------------------------------------------------
|
|
|
|
func TestDispatcherRunOnceSuccess(t *testing.T) {
|
|
repo := newStubRepo()
|
|
inserter := newStubInserter()
|
|
inserter.recordID = "rec-notable-abc-123"
|
|
|
|
exportID := "exp-001"
|
|
repo.claimFn = func(ctx context.Context, limit int, maxAttempts int, staleBefore time.Time) ([]PendingExport, error) {
|
|
return []PendingExport{
|
|
{ExportID: exportID, Row: Row{Question: "hello", Answer: "world", AskerName: "alice", AskerUnionID: "union-alice"}, Attempts: 0},
|
|
}, nil
|
|
}
|
|
|
|
d, err := NewDispatcher(repo, inserter, nil)
|
|
if err != nil {
|
|
t.Fatalf("NewDispatcher: %v", err)
|
|
}
|
|
|
|
stats, err := d.RunOnce(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("RunOnce: %v", err)
|
|
}
|
|
if stats.Claimed != 1 {
|
|
t.Errorf("Claimed = %d, want 1", stats.Claimed)
|
|
}
|
|
if stats.Exported != 1 {
|
|
t.Errorf("Exported = %d, want 1", stats.Exported)
|
|
}
|
|
if stats.Failed != 0 {
|
|
t.Errorf("Failed = %d, want 0", stats.Failed)
|
|
}
|
|
|
|
// Check location was marked with the Notable record ID.
|
|
repo.mu.Lock()
|
|
defer repo.mu.Unlock()
|
|
if len(repo.exported) != 1 {
|
|
t.Fatalf("exported records = %d, want 1", len(repo.exported))
|
|
}
|
|
if repo.exported[0].ExportID != exportID {
|
|
t.Errorf("exported exportID = %q, want %q", repo.exported[0].ExportID, exportID)
|
|
}
|
|
if repo.exported[0].Location != "rec-notable-abc-123" {
|
|
t.Errorf("exported Location = %q, want %q", repo.exported[0].Location, "rec-notable-abc-123")
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Test 3: insert error => stable failure code, raw error absent
|
|
// ---------------------------------------------------------------------------
|
|
|
|
func TestDispatcherRunOnceInsertErrorStableCode(t *testing.T) {
|
|
repo := newStubRepo()
|
|
inserter := newStubInserter()
|
|
// Use an error that contains "secrets" that must not leak.
|
|
inserter.err = fmt.Errorf("network: connect to https://api.dingtalk.com?operatorId=secret-operator failed: row contains secret-data-xyz")
|
|
|
|
exportID := "exp-002"
|
|
repo.claimFn = func(ctx context.Context, limit int, maxAttempts int, staleBefore time.Time) ([]PendingExport, error) {
|
|
return []PendingExport{
|
|
{ExportID: exportID, Row: Row{Question: "q", Answer: "a", AskerName: "x", AskerUnionID: "union-x"}, Attempts: 0},
|
|
}, nil
|
|
}
|
|
|
|
d, err := NewDispatcher(repo, inserter, nil)
|
|
if err != nil {
|
|
t.Fatalf("NewDispatcher: %v", err)
|
|
}
|
|
|
|
stats, err := d.RunOnce(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("RunOnce: %v", err)
|
|
}
|
|
if stats.Claimed != 1 {
|
|
t.Errorf("Claimed = %d, want 1", stats.Claimed)
|
|
}
|
|
if stats.Exported != 0 {
|
|
t.Errorf("Exported = %d, want 0", stats.Exported)
|
|
}
|
|
if stats.Failed != 1 {
|
|
t.Errorf("Failed = %d, want 1", stats.Failed)
|
|
}
|
|
|
|
// Check failure code is stable, not raw error.
|
|
repo.mu.Lock()
|
|
defer repo.mu.Unlock()
|
|
if len(repo.failed) != 1 {
|
|
t.Fatalf("failed records = %d, want 1", len(repo.failed))
|
|
}
|
|
if repo.failed[0].ExportID != exportID {
|
|
t.Errorf("failed exportID = %q, want %q", repo.failed[0].ExportID, exportID)
|
|
}
|
|
if repo.failed[0].FailureCode != FailureCodeRemoteInsertFailed {
|
|
t.Errorf("failure code = %q, want %q", repo.failed[0].FailureCode, FailureCodeRemoteInsertFailed)
|
|
}
|
|
|
|
// The raw error with secrets must not appear in stats or returned values.
|
|
// (Already verified: stats don't leak error strings.)
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Test 4: blocking inserter respects per-item timeout
|
|
// ---------------------------------------------------------------------------
|
|
|
|
func TestDispatcherRunOncePerItemTimeout(t *testing.T) {
|
|
repo := newStubRepo()
|
|
inserter := newStubInserter()
|
|
inserter.blockCh = make(chan struct{}) // never unblocks
|
|
|
|
exportID := "exp-timeout"
|
|
repo.claimFn = func(ctx context.Context, limit int, maxAttempts int, staleBefore time.Time) ([]PendingExport, error) {
|
|
return []PendingExport{
|
|
{ExportID: exportID, Row: Row{Question: "q", Answer: "a", AskerName: "x", AskerUnionID: "union-x"}, Attempts: 0},
|
|
}, nil
|
|
}
|
|
|
|
opts := &DispatcherOptions{SendTimeout: 50 * time.Millisecond}
|
|
d, err := NewDispatcher(repo, inserter, opts)
|
|
if err != nil {
|
|
t.Fatalf("NewDispatcher: %v", err)
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
stats, err := d.RunOnce(ctx)
|
|
if err != nil {
|
|
t.Fatalf("RunOnce: %v", err)
|
|
}
|
|
|
|
if stats.Failed != 1 {
|
|
t.Errorf("Failed = %d, want 1 (timeout should cause mark failed)", stats.Failed)
|
|
}
|
|
if stats.Exported != 0 {
|
|
t.Errorf("Exported = %d, want 0", stats.Exported)
|
|
}
|
|
|
|
// Verify failure code is stable.
|
|
repo.mu.Lock()
|
|
defer repo.mu.Unlock()
|
|
if len(repo.failed) != 1 {
|
|
t.Fatalf("failed records = %d, want 1", len(repo.failed))
|
|
}
|
|
if repo.failed[0].FailureCode != FailureCodeRemoteInsertFailed {
|
|
t.Errorf("failure code = %q, want %q", repo.failed[0].FailureCode, FailureCodeRemoteInsertFailed)
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Test 5: claim receives batch/max/stale time near expected
|
|
// ---------------------------------------------------------------------------
|
|
|
|
func TestDispatcherRunOnceClaimParams(t *testing.T) {
|
|
repo := newStubRepo()
|
|
inserter := newStubInserter()
|
|
|
|
opts := &DispatcherOptions{
|
|
Batch: 7,
|
|
MaxAttempts: 3,
|
|
ClaimLease: 2 * time.Minute,
|
|
SendTimeout: 5 * time.Second,
|
|
}
|
|
d, err := NewDispatcher(repo, inserter, opts)
|
|
if err != nil {
|
|
t.Fatalf("NewDispatcher: %v", err)
|
|
}
|
|
|
|
before := time.Now()
|
|
_, err = d.RunOnce(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("RunOnce: %v", err)
|
|
}
|
|
after := time.Now()
|
|
|
|
repo.mu.Lock()
|
|
limit := repo.lastClaimLimit
|
|
maxAtt := repo.lastClaimMaxAttempts
|
|
staleBefore := repo.lastClaimStaleBefore
|
|
repo.mu.Unlock()
|
|
|
|
if limit != 7 {
|
|
t.Errorf("claim limit = %d, want 7", limit)
|
|
}
|
|
if maxAtt != 3 {
|
|
t.Errorf("claim maxAttempts = %d, want 3", maxAtt)
|
|
}
|
|
|
|
// staleBefore should be approximately now - 2m.
|
|
expectedStale := before.Add(-2 * time.Minute)
|
|
// Allow some tolerance.
|
|
if staleBefore.Before(expectedStale.Add(-2*time.Second)) || staleBefore.After(after.Add(-2*time.Minute).Add(2*time.Second)) {
|
|
t.Errorf("staleBefore = %v, expected near %v (before=%v, after=%v)", staleBefore, expectedStale, before, after)
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Test 6: parent cancellation before claim and between items
|
|
// ---------------------------------------------------------------------------
|
|
|
|
func TestDispatcherRunOnceParentCancelledBeforeClaim(t *testing.T) {
|
|
repo := newStubRepo()
|
|
inserter := newStubInserter()
|
|
|
|
d, err := NewDispatcher(repo, inserter, nil)
|
|
if err != nil {
|
|
t.Fatalf("NewDispatcher: %v", err)
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
|
|
_, err = d.RunOnce(ctx)
|
|
if err == nil {
|
|
t.Fatal("expected error for cancelled context")
|
|
}
|
|
if !strings.Contains(err.Error(), "context invalid") {
|
|
t.Errorf("error = %v, want 'context invalid'", err)
|
|
}
|
|
}
|
|
|
|
func TestDispatcherRunOnceParentCancelledBetweenItems(t *testing.T) {
|
|
repo := newStubRepo()
|
|
inserter := newStubInserter()
|
|
inserter.recordID = "rec-1"
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
repo.claimFn = func(ctx context.Context, limit int, maxAttempts int, staleBefore time.Time) ([]PendingExport, error) {
|
|
return []PendingExport{
|
|
{ExportID: "exp-1", Row: Row{Question: "q1", Answer: "a1", AskerName: "x", AskerUnionID: "union-x"}, Attempts: 0},
|
|
{ExportID: "exp-2", Row: Row{Question: "q2", Answer: "a2", AskerName: "y", AskerUnionID: "union-y"}, Attempts: 0},
|
|
}, nil
|
|
}
|
|
|
|
// Wrap inserter to cancel parent context after the first InsertRecord call.
|
|
cancellingInserter := &cancellingInserter{
|
|
inner: inserter,
|
|
cancel: cancel,
|
|
}
|
|
|
|
d, err := NewDispatcher(repo, cancellingInserter, nil)
|
|
if err != nil {
|
|
t.Fatalf("NewDispatcher: %v", err)
|
|
}
|
|
|
|
stats, err := d.RunOnce(ctx)
|
|
if err == nil {
|
|
t.Fatal("expected error for cancelled context between items")
|
|
}
|
|
if !strings.Contains(err.Error(), "context cancelled during dispatch") {
|
|
t.Errorf("error = %v, want 'context cancelled during dispatch'", err)
|
|
}
|
|
// First item should have been exported before cancellation.
|
|
if stats.Exported != 1 {
|
|
t.Errorf("Exported = %d, want 1", stats.Exported)
|
|
}
|
|
}
|
|
|
|
// cancellingInserter wraps a RecordInserter and cancels a context after the first call.
|
|
type cancellingInserter struct {
|
|
inner RecordInserter
|
|
cancel context.CancelFunc
|
|
mu sync.Mutex
|
|
calls int
|
|
}
|
|
|
|
func (a *cancellingInserter) InsertRecord(ctx context.Context, row Row) (string, error) {
|
|
a.mu.Lock()
|
|
a.calls++
|
|
calls := a.calls
|
|
a.mu.Unlock()
|
|
|
|
result, err := a.inner.InsertRecord(ctx, row)
|
|
|
|
if calls == 1 {
|
|
a.cancel()
|
|
}
|
|
return result, err
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Test 7: claim/mark errors and stats semantics
|
|
// ---------------------------------------------------------------------------
|
|
|
|
func TestDispatcherRunOnceClaimError(t *testing.T) {
|
|
repo := newStubRepo()
|
|
inserter := newStubInserter()
|
|
|
|
claimErr := errors.New("database connection refused")
|
|
repo.claimFn = func(ctx context.Context, limit int, maxAttempts int, staleBefore time.Time) ([]PendingExport, error) {
|
|
return nil, claimErr
|
|
}
|
|
|
|
d, err := NewDispatcher(repo, inserter, nil)
|
|
if err != nil {
|
|
t.Fatalf("NewDispatcher: %v", err)
|
|
}
|
|
|
|
stats, err := d.RunOnce(context.Background())
|
|
if err == nil {
|
|
t.Fatal("expected error for claim failure")
|
|
}
|
|
if !strings.Contains(err.Error(), "claim failed") {
|
|
t.Errorf("error = %v, want 'claim failed' stage wrapper", err)
|
|
}
|
|
if stats.Claimed != 0 {
|
|
t.Errorf("Claimed = %d, want 0 on claim error", stats.Claimed)
|
|
}
|
|
}
|
|
|
|
func TestDispatcherRunOnceMarkExportedError(t *testing.T) {
|
|
repo := newStubRepo()
|
|
inserter := newStubInserter()
|
|
inserter.recordID = "rec-mark-err"
|
|
|
|
repo.markExportedErr = errors.New("mark exported db error")
|
|
|
|
exportID := "exp-mark-err"
|
|
repo.claimFn = func(ctx context.Context, limit int, maxAttempts int, staleBefore time.Time) ([]PendingExport, error) {
|
|
return []PendingExport{
|
|
{ExportID: exportID, Row: Row{Question: "q", Answer: "a", AskerName: "x", AskerUnionID: "union-x"}, Attempts: 0},
|
|
}, nil
|
|
}
|
|
|
|
d, err := NewDispatcher(repo, inserter, nil)
|
|
if err != nil {
|
|
t.Fatalf("NewDispatcher: %v", err)
|
|
}
|
|
|
|
stats, err := d.RunOnce(context.Background())
|
|
if err == nil {
|
|
t.Fatal("expected error for mark exported failure")
|
|
}
|
|
if !strings.Contains(err.Error(), "mark exported failed") {
|
|
t.Errorf("error = %v, want 'mark exported failed' stage wrapper", err)
|
|
}
|
|
// Stats should reflect the insert succeeded before mark failed.
|
|
if stats.Exported != 0 {
|
|
t.Errorf("Exported = %d, want 0 (mark failed before increment)", stats.Exported)
|
|
}
|
|
}
|
|
|
|
func TestDispatcherRunOnceStatsSemantics(t *testing.T) {
|
|
repo := newStubRepo()
|
|
inserter := newStubInserter()
|
|
inserter.recordID = "rec-ok"
|
|
|
|
// 2 success, 1 failure.
|
|
inserter.perCallErrors = map[int]error{
|
|
2: errors.New("network error"),
|
|
}
|
|
|
|
repo.claimFn = func(ctx context.Context, limit int, maxAttempts int, staleBefore time.Time) ([]PendingExport, error) {
|
|
return []PendingExport{
|
|
{ExportID: "exp-ok1", Row: Row{Question: "q1", Answer: "a1", AskerName: "x", AskerUnionID: "union-x"}, Attempts: 0},
|
|
{ExportID: "exp-fail", Row: Row{Question: "q2", Answer: "a2", AskerName: "y", AskerUnionID: "union-y"}, Attempts: 0},
|
|
{ExportID: "exp-ok2", Row: Row{Question: "q3", Answer: "a3", AskerName: "z", AskerUnionID: "union-z"}, Attempts: 0},
|
|
}, nil
|
|
}
|
|
|
|
d, err := NewDispatcher(repo, inserter, nil)
|
|
if err != nil {
|
|
t.Fatalf("NewDispatcher: %v", err)
|
|
}
|
|
|
|
stats, err := d.RunOnce(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("RunOnce: %v", err)
|
|
}
|
|
if stats.Claimed != 3 {
|
|
t.Errorf("Claimed = %d, want 3", stats.Claimed)
|
|
}
|
|
if stats.Exported != 2 {
|
|
t.Errorf("Exported = %d, want 2", stats.Exported)
|
|
}
|
|
if stats.Failed != 1 {
|
|
t.Errorf("Failed = %d, want 1", stats.Failed)
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Test 8: constructor invalid bounds, nil, typed-nil
|
|
// ---------------------------------------------------------------------------
|
|
|
|
func TestNewDispatcherNilDeps(t *testing.T) {
|
|
inserter := newStubInserter()
|
|
|
|
_, err := NewDispatcher(nil, inserter, nil)
|
|
if err == nil {
|
|
t.Fatal("expected error for nil repo")
|
|
}
|
|
if !strings.Contains(err.Error(), "repo is nil") {
|
|
t.Errorf("error = %v, want 'repo is nil'", err)
|
|
}
|
|
|
|
repo := newStubRepo()
|
|
_, err = NewDispatcher(repo, nil, nil)
|
|
if err == nil {
|
|
t.Fatal("expected error for nil inserter")
|
|
}
|
|
if !strings.Contains(err.Error(), "inserter is nil") {
|
|
t.Errorf("error = %v, want 'inserter is nil'", err)
|
|
}
|
|
}
|
|
|
|
func TestNewDispatcherTypedNil(t *testing.T) {
|
|
// Typed nil: interface value is non-nil but underlying concrete is nil.
|
|
var typedNilRepo *stubRepo = nil
|
|
var repo ExportRepository = typedNilRepo
|
|
|
|
inserter := newStubInserter()
|
|
_, err := NewDispatcher(repo, inserter, nil)
|
|
if err == nil {
|
|
t.Fatal("expected error for typed-nil repo")
|
|
}
|
|
if !strings.Contains(err.Error(), "repo is nil") {
|
|
t.Errorf("error = %v, want 'repo is nil'", err)
|
|
}
|
|
|
|
repo2 := newStubRepo()
|
|
var typedNilInserter *stubInserter = nil
|
|
var inserter2 RecordInserter = typedNilInserter
|
|
|
|
_, err = NewDispatcher(repo2, inserter2, nil)
|
|
if err == nil {
|
|
t.Fatal("expected error for typed-nil inserter")
|
|
}
|
|
if !strings.Contains(err.Error(), "inserter is nil") {
|
|
t.Errorf("error = %v, want 'inserter is nil'", err)
|
|
}
|
|
}
|
|
|
|
func TestNewDispatcherInvalidBounds(t *testing.T) {
|
|
repo := newStubRepo()
|
|
inserter := newStubInserter()
|
|
|
|
tests := []struct {
|
|
name string
|
|
opts DispatcherOptions
|
|
want string
|
|
}{
|
|
{"negative batch", DispatcherOptions{Batch: -1}, "batch must be >= 0"},
|
|
{"negative maxAttempts", DispatcherOptions{MaxAttempts: -1}, "maxAttempts must be >= 0"},
|
|
{"negative claimLease", DispatcherOptions{ClaimLease: -1 * time.Second}, "claimLease must be >= 0"},
|
|
{"negative sendTimeout", DispatcherOptions{SendTimeout: -1 * time.Second}, "sendTimeout must be >= 0"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
_, err := NewDispatcher(repo, inserter, &tt.opts)
|
|
if err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
if !strings.Contains(err.Error(), tt.want) {
|
|
t.Errorf("error = %v, want containing %q", err, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNewDispatcherDefaults(t *testing.T) {
|
|
repo := newStubRepo()
|
|
inserter := newStubInserter()
|
|
|
|
d, err := NewDispatcher(repo, inserter, nil)
|
|
if err != nil {
|
|
t.Fatalf("NewDispatcher: %v", err)
|
|
}
|
|
if d.batch != 10 {
|
|
t.Errorf("batch = %d, want 10", d.batch)
|
|
}
|
|
if d.maxAttempts != 5 {
|
|
t.Errorf("maxAttempts = %d, want 5", d.maxAttempts)
|
|
}
|
|
if d.claimLease != 5*time.Minute {
|
|
t.Errorf("claimLease = %v, want 5m", d.claimLease)
|
|
}
|
|
if d.sendTimeout != 10*time.Second {
|
|
t.Errorf("sendTimeout = %v, want 10s", d.sendTimeout)
|
|
}
|
|
}
|
|
|
|
func TestNewDispatcherZeroBatch(t *testing.T) {
|
|
// Zero batch is allowed (validates as >= 0, but not > 0 so use default).
|
|
repo := newStubRepo()
|
|
inserter := newStubInserter()
|
|
|
|
opts := &DispatcherOptions{Batch: 0}
|
|
d, err := NewDispatcher(repo, inserter, opts)
|
|
if err != nil {
|
|
t.Fatalf("NewDispatcher with batch=0: %v", err)
|
|
}
|
|
if d.batch != 10 {
|
|
t.Errorf("batch = %d, want 10 (default when opts.Batch=0)", d.batch)
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Test 9: exact Row passed to inserter
|
|
// ---------------------------------------------------------------------------
|
|
|
|
func TestDispatcherRunOnceExactRowPassed(t *testing.T) {
|
|
repo := newStubRepo()
|
|
inserter := newStubInserter()
|
|
|
|
row := Row{
|
|
Question: "怎么修?",
|
|
Answer: "按步骤操作",
|
|
AskedTime: time.Date(2025, 6, 15, 14, 30, 0, 0, time.FixedZone("CST", 8*60*60)),
|
|
AskerName: "张三",
|
|
AskerUnionID: "union-zhang-san",
|
|
TriggerPerson: "李四",
|
|
Department: "研发部",
|
|
Region: "华东",
|
|
}
|
|
|
|
exportID := "exp-cells"
|
|
repo.claimFn = func(ctx context.Context, limit int, maxAttempts int, staleBefore time.Time) ([]PendingExport, error) {
|
|
return []PendingExport{
|
|
{ExportID: exportID, Row: row, Attempts: 0},
|
|
}, nil
|
|
}
|
|
|
|
d, err := NewDispatcher(repo, inserter, nil)
|
|
if err != nil {
|
|
t.Fatalf("NewDispatcher: %v", err)
|
|
}
|
|
|
|
_, err = d.RunOnce(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("RunOnce: %v", err)
|
|
}
|
|
|
|
inserter.mu.Lock()
|
|
defer inserter.mu.Unlock()
|
|
if len(inserter.rows) != 1 {
|
|
t.Fatalf("inserter received %d rows, want 1", len(inserter.rows))
|
|
}
|
|
|
|
got := inserter.rows[0]
|
|
|
|
if got.Question != row.Question {
|
|
t.Errorf("Question = %q, want %q", got.Question, row.Question)
|
|
}
|
|
if got.Answer != row.Answer {
|
|
t.Errorf("Answer = %q, want %q", got.Answer, row.Answer)
|
|
}
|
|
if !got.AskedTime.Equal(row.AskedTime) {
|
|
t.Errorf("AskedTime = %v, want %v", got.AskedTime, row.AskedTime)
|
|
}
|
|
if got.AskerName != row.AskerName {
|
|
t.Errorf("AskerName = %q, want %q", got.AskerName, row.AskerName)
|
|
}
|
|
if got.AskerUnionID != row.AskerUnionID {
|
|
t.Errorf("AskerUnionID = %q, want %q", got.AskerUnionID, row.AskerUnionID)
|
|
}
|
|
if got.TriggerPerson != row.TriggerPerson {
|
|
t.Errorf("TriggerPerson = %q, want %q", got.TriggerPerson, row.TriggerPerson)
|
|
}
|
|
if got.Department != row.Department {
|
|
t.Errorf("Department = %q, want %q", got.Department, row.Department)
|
|
}
|
|
if got.Region != row.Region {
|
|
t.Errorf("Region = %q, want %q", got.Region, row.Region)
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Additional: RunOnce returns error when sendCtx expires but parent OK
|
|
// ---------------------------------------------------------------------------
|
|
|
|
func TestDispatcherRunOnceSendTimeoutDoesNotKillParent(t *testing.T) {
|
|
repo := newStubRepo()
|
|
inserter := newStubInserter()
|
|
inserter.blockCh = make(chan struct{}) // never unblocks
|
|
|
|
exportID := "exp-timeout2"
|
|
repo.claimFn = func(ctx context.Context, limit int, maxAttempts int, staleBefore time.Time) ([]PendingExport, error) {
|
|
return []PendingExport{
|
|
{ExportID: exportID, Row: Row{Question: "q", Answer: "a", AskerName: "x", AskerUnionID: "union-x"}, Attempts: 0},
|
|
}, nil
|
|
}
|
|
|
|
opts := &DispatcherOptions{SendTimeout: 20 * time.Millisecond}
|
|
d, err := NewDispatcher(repo, inserter, opts)
|
|
if err != nil {
|
|
t.Fatalf("NewDispatcher: %v", err)
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
stats, err := d.RunOnce(ctx)
|
|
if err != nil {
|
|
t.Fatalf("RunOnce should not fail on per-item timeout: %v", err)
|
|
}
|
|
if stats.Failed != 1 {
|
|
t.Errorf("Failed = %d, want 1", stats.Failed)
|
|
}
|
|
|
|
// Parent context should still be alive.
|
|
if ctx.Err() != nil {
|
|
t.Errorf("parent context should still be alive, got %v", ctx.Err())
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Additional: RecordInserter interface satisfaction check
|
|
// ---------------------------------------------------------------------------
|
|
|
|
func TestClientSatisfiesRecordInserter(t *testing.T) {
|
|
// Compile-time check: *Client implements RecordInserter.
|
|
var _ RecordInserter = (*Client)(nil)
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Error leakage: repository errors must not leak secrets
|
|
// ---------------------------------------------------------------------------
|
|
|
|
func TestDispatcherRunOnceClaimErrorNoLeak(t *testing.T) {
|
|
repo := newStubRepo()
|
|
inserter := newStubInserter()
|
|
|
|
// Claim error containing SQL/turn/export/person secrets.
|
|
repo.claimFn = func(ctx context.Context, limit int, maxAttempts int, staleBefore time.Time) ([]PendingExport, error) {
|
|
return nil, fmt.Errorf("SQLSTATE 42P01: relation \"turn_export_person\" does not exist")
|
|
}
|
|
|
|
d, err := NewDispatcher(repo, inserter, nil)
|
|
if err != nil {
|
|
t.Fatalf("NewDispatcher: %v", err)
|
|
}
|
|
|
|
_, err = d.RunOnce(context.Background())
|
|
if err == nil {
|
|
t.Fatal("expected error for claim failure")
|
|
}
|
|
|
|
errStr := err.Error()
|
|
secrets := []string{"SQLSTATE", "42P01", "turn_export_person", "relation"}
|
|
for _, s := range secrets {
|
|
if strings.Contains(errStr, s) {
|
|
t.Errorf("error leaks secret %q: %v", s, errStr)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDispatcherRunOnceMarkFailedNoLeak(t *testing.T) {
|
|
repo := newStubRepo()
|
|
inserter := newStubInserter()
|
|
inserter.err = errors.New("some insert error")
|
|
|
|
repo.markFailedErr = fmt.Errorf("SQLSTATE 23505: duplicate key in export_person_report")
|
|
|
|
exportID := "exp-leak-mf"
|
|
repo.claimFn = func(ctx context.Context, limit int, maxAttempts int, staleBefore time.Time) ([]PendingExport, error) {
|
|
return []PendingExport{
|
|
{ExportID: exportID, Row: Row{Question: "q", Answer: "a", AskerName: "x", AskerUnionID: "union-x"}, Attempts: 0},
|
|
}, nil
|
|
}
|
|
|
|
d, err := NewDispatcher(repo, inserter, nil)
|
|
if err != nil {
|
|
t.Fatalf("NewDispatcher: %v", err)
|
|
}
|
|
|
|
_, err = d.RunOnce(context.Background())
|
|
if err == nil {
|
|
t.Fatal("expected error for mark failed failure")
|
|
}
|
|
|
|
errStr := err.Error()
|
|
secrets := []string{"SQLSTATE", "23505", "export_person_report", "duplicate"}
|
|
for _, s := range secrets {
|
|
if strings.Contains(errStr, s) {
|
|
t.Errorf("error leaks secret %q: %v", s, errStr)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDispatcherRunOnceMarkExportedNoLeak(t *testing.T) {
|
|
repo := newStubRepo()
|
|
inserter := newStubInserter()
|
|
inserter.recordID = "rec-leak"
|
|
|
|
repo.markExportedErr = fmt.Errorf("SQLSTATE 08006: connection lost for turn_export_write")
|
|
|
|
exportID := "exp-leak-me"
|
|
repo.claimFn = func(ctx context.Context, limit int, maxAttempts int, staleBefore time.Time) ([]PendingExport, error) {
|
|
return []PendingExport{
|
|
{ExportID: exportID, Row: Row{Question: "q", Answer: "a", AskerName: "x", AskerUnionID: "union-x"}, Attempts: 0},
|
|
}, nil
|
|
}
|
|
|
|
d, err := NewDispatcher(repo, inserter, nil)
|
|
if err != nil {
|
|
t.Fatalf("NewDispatcher: %v", err)
|
|
}
|
|
|
|
_, err = d.RunOnce(context.Background())
|
|
if err == nil {
|
|
t.Fatal("expected error for mark exported failure")
|
|
}
|
|
|
|
errStr := err.Error()
|
|
secrets := []string{"SQLSTATE", "08006", "turn_export_write", "connection"}
|
|
for _, s := range secrets {
|
|
if strings.Contains(errStr, s) {
|
|
t.Errorf("error leaks secret %q: %v", s, errStr)
|
|
}
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Value-type constructor: non-pointer implementations should not panic
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// valueRepo is a non-pointer ExportRepository implementation.
|
|
type valueRepo struct{}
|
|
|
|
func (v valueRepo) Claim(ctx context.Context, limit int, maxAttempts int, staleBefore time.Time) ([]PendingExport, error) {
|
|
return nil, nil
|
|
}
|
|
func (v valueRepo) MarkExported(ctx context.Context, exportID, location string) error { return nil }
|
|
func (v valueRepo) MarkFailed(ctx context.Context, exportID, failureCode string) error { return nil }
|
|
|
|
// valueInserter is a non-pointer RecordInserter implementation.
|
|
type valueInserter struct{}
|
|
|
|
func (v valueInserter) InsertRecord(ctx context.Context, row Row) (string, error) {
|
|
return "rec-value-001", nil
|
|
}
|
|
|
|
func TestNewDispatcherValueTypeRepo(t *testing.T) {
|
|
// value-type repo should not panic (was: reflect.ValueOf(x).IsNil() on non-nilable kind).
|
|
var repo ExportRepository = valueRepo{}
|
|
inserter := newStubInserter()
|
|
|
|
d, err := NewDispatcher(repo, inserter, nil)
|
|
if err != nil {
|
|
t.Fatalf("NewDispatcher with value-type repo: %v", err)
|
|
}
|
|
_ = d
|
|
}
|
|
|
|
func TestNewDispatcherValueTypeInserter(t *testing.T) {
|
|
repo := newStubRepo()
|
|
var inserter RecordInserter = valueInserter{}
|
|
|
|
d, err := NewDispatcher(repo, inserter, nil)
|
|
if err != nil {
|
|
t.Fatalf("NewDispatcher with value-type inserter: %v", err)
|
|
}
|
|
_ = d
|
|
}
|
|
|
|
func TestNewDispatcherValueTypeBoth(t *testing.T) {
|
|
var repo ExportRepository = valueRepo{}
|
|
var inserter RecordInserter = valueInserter{}
|
|
|
|
d, err := NewDispatcher(repo, inserter, nil)
|
|
if err != nil {
|
|
t.Fatalf("NewDispatcher with value-type both: %v", err)
|
|
}
|
|
_ = d
|
|
}
|