8febe12d7d
真实本机 PG/ES/MinIO:v071 目录镜像 10 模式、结构引用/游标 15 模式、 8 表×2 外部范围身份矩阵、13 模式自然键(含软删除与合法同 parent 正例)、 备份期间三注入、收尾故障与授权撤销两个真实治理 PG 用例。 需隔离服务的用例在 test-scope.json 显式声明:普通 CI 跳过不算通过。
60 lines
2.2 KiB
Go
60 lines
2.2 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"knowledge-governance/internal/governance"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestContractExpiredAttemptCannotRenewOrWrite(t *testing.T) {
|
|
store, pool := newIdentityStore(t)
|
|
ctx := context.Background()
|
|
now := time.Now().UTC()
|
|
seedOperationJob(t, store, pool, "internal", "contract-expired", "sync", now)
|
|
attempt, err := store.BeginLibraryAttempt(ctx, "contract-expired", "writer", time.Minute, now)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = pool.Exec(ctx, `UPDATE library_jobs SET lease_until=now()-interval '1 minute'; UPDATE library_attempts SET lease_until=now()-interval '1 minute'`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = store.RenewLibraryAttempt(ctx, attempt, time.Minute, now); !errors.Is(err, governance.ErrLibraryEpochStale) {
|
|
t.Errorf("expired renewal resurrected ownership: %v", err)
|
|
}
|
|
if err = store.AdvanceLibraryJobGuarded(ctx, attempt, governance.LibraryJobRunning, "database", "", "", "", "", now); !errors.Is(err, governance.ErrLibraryEpochStale) {
|
|
t.Errorf("expired owner still writes: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestContractRenewalIsAtomicAcrossJobAndAttempt(t *testing.T) {
|
|
store, pool := newIdentityStore(t)
|
|
ctx := context.Background()
|
|
now := time.Now().UTC()
|
|
op := seedOperationJob(t, store, pool, "internal", "contract-revoke", "sync", now)
|
|
attempt, err := store.BeginLibraryAttempt(ctx, "contract-revoke", "writer", time.Minute, now)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = store.RenewLibraryAttempt(ctx, attempt, 2*time.Minute, now); err != nil {
|
|
t.Fatalf("valid renewal: %v", err)
|
|
}
|
|
if _, err = pool.Exec(ctx, `UPDATE library_jobs SET lease_owner='new-owner' WHERE id=$1`, attempt.JobID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var before, after time.Time
|
|
if err = pool.QueryRow(ctx, `SELECT lease_until FROM library_attempts WHERE operation_id=$1`, op).Scan(&before); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = store.RenewLibraryAttempt(ctx, attempt, time.Hour, now); !errors.Is(err, governance.ErrLibraryEpochStale) {
|
|
t.Errorf("revoked job still renews: %v", err)
|
|
}
|
|
if err = pool.QueryRow(ctx, `SELECT lease_until FROM library_attempts WHERE operation_id=$1`, op).Scan(&after); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !before.Equal(after) {
|
|
t.Fatal("partial renewal escaped transaction")
|
|
}
|
|
}
|