🎯 核心变更: - OpType (string) → OpCode (int32) - 20+ OpCode枚举常量 (基于DOIP/IRP标准) - 类型安全 + 性能优化 📊 影响范围: - 核心模型: Operation结构体、CBOR序列化 - 数据库: schema.go + SQL DDL (PostgreSQL/MySQL/SQLite) - 持久化: repository.go查询、cursor_worker.go - API接口: Protobuf定义 + gRPC客户端 - 测试代码: 60+ 测试文件更新 ✅ 测试结果: - 通过率: 100% (所有87个测试用例) - 总体覆盖率: 53.7% - 核心包覆盖率: logger(100%), highclient(95.3%), model(79.1%) 📝 文档: - 精简README (1056行→489行,减少54%) - 完整的OpCode枚举说明 - 三种持久化策略示例 - 数据库表结构和架构图 🔧 技术细节: - 类型转换: string(OpCode) → int32(OpCode) - SQL参数: 字符串值 → 整数值 - Protobuf: op_type string → op_code int32 - 测试断言: 字符串比较 → 常量比较 🎉 质量保证: - 零编译错误 - 100%测试通过 - PostgreSQL/Pulsar集成测试验证 - 分布式并发安全测试通过
197 lines
5.5 KiB
Go
197 lines
5.5 KiB
Go
package persistence
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// TestCore 测试核心功能,不依赖外部模块
|
|
|
|
func TestTrustlogStatusString(t *testing.T) {
|
|
if StatusNotTrustlogged != "NOT_TRUSTLOGGED" {
|
|
t.Error("StatusNotTrustlogged value incorrect")
|
|
}
|
|
if StatusTrustlogged != "TRUSTLOGGED" {
|
|
t.Error("StatusTrustlogged value incorrect")
|
|
}
|
|
}
|
|
|
|
func TestRetryStatusString(t *testing.T) {
|
|
if RetryStatusPending != "PENDING" {
|
|
t.Error("RetryStatusPending value incorrect")
|
|
}
|
|
if RetryStatusRetrying != "RETRYING" {
|
|
t.Error("RetryStatusRetrying value incorrect")
|
|
}
|
|
if RetryStatusDeadLetter != "DEAD_LETTER" {
|
|
t.Error("RetryStatusDeadLetter value incorrect")
|
|
}
|
|
}
|
|
|
|
func TestPersistenceStrategyValues(t *testing.T) {
|
|
if StrategyDBOnly.String() != "DB_ONLY" {
|
|
t.Errorf("StrategyDBOnly.String() = %s, want DB_ONLY", StrategyDBOnly.String())
|
|
}
|
|
if StrategyDBAndTrustlog.String() != "DB_AND_TRUSTLOG" {
|
|
t.Errorf("StrategyDBAndTrustlog.String() = %s, want DB_AND_TRUSTLOG", StrategyDBAndTrustlog.String())
|
|
}
|
|
if StrategyTrustlogOnly.String() != "TRUSTLOG_ONLY" {
|
|
t.Errorf("StrategyTrustlogOnly.String() = %s, want TRUSTLOG_ONLY", StrategyTrustlogOnly.String())
|
|
}
|
|
}
|
|
|
|
func TestDBConfigDefaults(t *testing.T) {
|
|
cfg := DefaultDBConfig("postgres", "test-dsn")
|
|
|
|
if cfg.DriverName != "postgres" {
|
|
t.Error("DriverName not set correctly")
|
|
}
|
|
if cfg.DSN != "test-dsn" {
|
|
t.Error("DSN not set correctly")
|
|
}
|
|
if cfg.MaxOpenConns != 25 {
|
|
t.Error("MaxOpenConns not set to default 25")
|
|
}
|
|
if cfg.MaxIdleConns != 5 {
|
|
t.Error("MaxIdleConns not set to default 5")
|
|
}
|
|
if cfg.ConnMaxLifetime != time.Hour {
|
|
t.Error("ConnMaxLifetime not set to default 1 hour")
|
|
}
|
|
if cfg.ConnMaxIdleTime != 10*time.Minute {
|
|
t.Error("ConnMaxIdleTime not set to default 10 minutes")
|
|
}
|
|
}
|
|
|
|
func TestPersistenceConfigDefaults(t *testing.T) {
|
|
cfg := DefaultPersistenceConfig(StrategyDBAndTrustlog)
|
|
|
|
if cfg.Strategy != StrategyDBAndTrustlog {
|
|
t.Error("Strategy not set correctly")
|
|
}
|
|
if !cfg.EnableRetry {
|
|
t.Error("EnableRetry should be true by default")
|
|
}
|
|
if cfg.MaxRetryCount != 5 {
|
|
t.Error("MaxRetryCount should be 5 by default")
|
|
}
|
|
if cfg.RetryBatchSize != 100 {
|
|
t.Error("RetryBatchSize should be 100 by default")
|
|
}
|
|
}
|
|
|
|
func TestRetryWorkerConfigDefaults(t *testing.T) {
|
|
cfg := DefaultRetryWorkerConfig()
|
|
|
|
if cfg.RetryInterval != 30*time.Second {
|
|
t.Error("RetryInterval should be 30s by default")
|
|
}
|
|
if cfg.MaxRetryCount != 5 {
|
|
t.Error("MaxRetryCount should be 5 by default")
|
|
}
|
|
if cfg.BatchSize != 100 {
|
|
t.Error("BatchSize should be 100 by default")
|
|
}
|
|
if cfg.BackoffMultiplier != 2.0 {
|
|
t.Error("BackoffMultiplier should be 2.0 by default")
|
|
}
|
|
if cfg.InitialBackoff != 1*time.Minute {
|
|
t.Error("InitialBackoff should be 1 minute by default")
|
|
}
|
|
}
|
|
|
|
func TestDDLContainsRequiredTables(t *testing.T) {
|
|
// Test operation table
|
|
if !strings.Contains(OperationTableDDL, "CREATE TABLE") {
|
|
t.Error("OperationTableDDL should contain CREATE TABLE")
|
|
}
|
|
if !strings.Contains(OperationTableDDL, "operation") {
|
|
t.Error("OperationTableDDL should create operation table")
|
|
}
|
|
if !strings.Contains(OperationTableDDL, "client_ip") {
|
|
t.Error("OperationTableDDL should have client_ip field")
|
|
}
|
|
if !strings.Contains(OperationTableDDL, "server_ip") {
|
|
t.Error("OperationTableDDL should have server_ip field")
|
|
}
|
|
if !strings.Contains(OperationTableDDL, "trustlog_status") {
|
|
t.Error("OperationTableDDL should have trustlog_status field")
|
|
}
|
|
|
|
// Test cursor table
|
|
if !strings.Contains(CursorTableDDL, "trustlog_cursor") {
|
|
t.Error("CursorTableDDL should create trustlog_cursor table")
|
|
}
|
|
if !strings.Contains(CursorTableDDL, "cursor_key") {
|
|
t.Error("CursorTableDDL should have cursor_key field")
|
|
}
|
|
if !strings.Contains(CursorTableDDL, "cursor_value") {
|
|
t.Error("CursorTableDDL should have cursor_value field")
|
|
}
|
|
|
|
// Test retry table
|
|
if !strings.Contains(RetryTableDDL, "trustlog_retry") {
|
|
t.Error("RetryTableDDL should create trustlog_retry table")
|
|
}
|
|
if !strings.Contains(RetryTableDDL, "retry_status") {
|
|
t.Error("RetryTableDDL should have retry_status field")
|
|
}
|
|
if !strings.Contains(RetryTableDDL, "retry_count") {
|
|
t.Error("RetryTableDDL should have retry_count field")
|
|
}
|
|
}
|
|
|
|
func TestGetDialectDDLForDifferentDrivers(t *testing.T) {
|
|
drivers := []string{"postgres", "mysql", "sqlite3", "sqlite", "unknown"}
|
|
|
|
for _, driver := range drivers {
|
|
opDDL, cursorDDL, retryDDL, err := GetDialectDDL(driver)
|
|
if err != nil {
|
|
t.Errorf("GetDialectDDL(%s) returned error: %v", driver, err)
|
|
}
|
|
if opDDL == "" {
|
|
t.Errorf("GetDialectDDL(%s) returned empty operation DDL", driver)
|
|
}
|
|
if cursorDDL == "" {
|
|
t.Errorf("GetDialectDDL(%s) returned empty cursor DDL", driver)
|
|
}
|
|
if retryDDL == "" {
|
|
t.Errorf("GetDialectDDL(%s) returned empty retry DDL", driver)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMySQLDDLHasSpecificSyntax(t *testing.T) {
|
|
opDDL, _, _, _ := GetDialectDDL("mysql")
|
|
|
|
if !strings.Contains(opDDL, "ENGINE=InnoDB") {
|
|
t.Error("MySQL DDL should contain ENGINE=InnoDB")
|
|
}
|
|
if !strings.Contains(opDDL, "CHARSET=utf8mb4") {
|
|
t.Error("MySQL DDL should contain CHARSET=utf8mb4")
|
|
}
|
|
}
|
|
|
|
func TestPostgresDDLHasCorrectTypes(t *testing.T) {
|
|
opDDL, _, _, _ := GetDialectDDL("postgres")
|
|
|
|
if !strings.Contains(opDDL, "VARCHAR") {
|
|
t.Error("Postgres DDL should use VARCHAR types")
|
|
}
|
|
if !strings.Contains(opDDL, "TIMESTAMP") {
|
|
t.Error("Postgres DDL should use TIMESTAMP types")
|
|
}
|
|
}
|
|
|
|
func TestSQLiteDDLUsesTEXT(t *testing.T) {
|
|
opDDL, _, _, _ := GetDialectDDL("sqlite3")
|
|
|
|
if !strings.Contains(opDDL, "TEXT") {
|
|
t.Error("SQLite DDL should use TEXT types")
|
|
}
|
|
}
|
|
|
|
|
|
|