Files
go-trustlog/internal/helpers/uuid_test.go
ryan 4b72a37120 feat: 完善数据库持久化与存证功能
主要更新:

1. 数据库持久化功能
   - 支持三种策略:仅落库、既落库又存证、仅存证
   - 实现 Cursor Worker 异步扫描和存证机制
   - 实现 Retry Worker 失败重试机制
   - 支持 PostgreSQL、MySQL、SQLite 等多种数据库
   - 添加 ClientIP 和 ServerIP 字段(可空,仅落库)

2. 集群并发安全
   - 使用 SELECT FOR UPDATE SKIP LOCKED 防止重复处理
   - 实现 CAS (Compare-And-Set) 原子状态更新
   - 添加 updated_at 字段支持并发控制

3. Cursor 初始化优化
   - 自动基于历史数据初始化 cursor
   - 确保不遗漏任何历史记录
   - 修复 UPSERT 逻辑

4. 测试完善
   - 添加 E2E 集成测试(含 Pulsar 消费者验证)
   - 添加 PostgreSQL 集成测试
   - 添加 Pulsar 集成测试
   - 添加集群并发安全测试
   - 添加 Cursor 初始化验证测试
   - 补充大量单元测试,提升覆盖率

5. 工具脚本
   - 添加数据库迁移脚本
   - 添加 Cursor 状态检查工具
   - 添加 Cursor 初始化工具
   - 添加 Pulsar 消息验证工具

6. 文档清理
   - 删除冗余文档,只保留根目录 README

测试结果:
- 所有 E2E 测试通过(100%)
- 数据库持久化与异步存证流程验证通过
- 集群环境下的并发安全性验证通过
- Cursor 自动初始化和历史数据处理验证通过
2025-12-24 15:31:11 +08:00

152 lines
3.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package helpers_test
import (
"regexp"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.yandata.net/iod/iod/go-trustlog/internal/helpers"
)
func TestNewUUIDv7(t *testing.T) {
// UUID v7 格式无连字符32个十六进制字符
uuidPattern := regexp.MustCompile(`^[0-9a-f]{32}$`)
t.Run("生成有效的UUID", func(t *testing.T) {
uuid := helpers.NewUUIDv7()
// 验证格式
assert.Len(t, uuid, 32, "UUID长度应该是32个字符")
assert.Regexp(t, uuidPattern, uuid, "UUID应该只包含小写十六进制字符")
})
t.Run("每次生成的UUID应该不同", func(t *testing.T) {
uuid1 := helpers.NewUUIDv7()
uuid2 := helpers.NewUUIDv7()
uuid3 := helpers.NewUUIDv7()
assert.NotEqual(t, uuid1, uuid2)
assert.NotEqual(t, uuid2, uuid3)
assert.NotEqual(t, uuid1, uuid3)
})
t.Run("UUID格式验证", func(t *testing.T) {
uuid := helpers.NewUUIDv7()
// UUID v7 应该是 32 个十六进制字符
require.Len(t, uuid, 32)
// 检查每个字符都是有效的十六进制
for i, c := range uuid {
assert.True(t,
(c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'),
"字符 %c 在位置 %d 不是有效的十六进制字符", c, i)
}
})
t.Run("并发生成UUID", func(t *testing.T) {
const concurrency = 100
uuids := make([]string, concurrency)
var wg sync.WaitGroup
wg.Add(concurrency)
for i := range concurrency {
go func(idx int) {
defer wg.Done()
uuids[idx] = helpers.NewUUIDv7()
}(i)
}
wg.Wait()
// 验证所有 UUID 都不为空且格式正确
for i, uuid := range uuids {
assert.NotEmpty(t, uuid, "UUID %d 不应该为空", i)
assert.Regexp(t, uuidPattern, uuid, "UUID %d 格式不正确", i)
}
// 验证所有 UUID 都是唯一的
uniqueMap := make(map[string]bool)
for _, uuid := range uuids {
assert.False(t, uniqueMap[uuid], "UUID重复: %s", uuid)
uniqueMap[uuid] = true
}
assert.Len(t, uniqueMap, concurrency, "应该生成%d个唯一的UUID", concurrency)
})
t.Run("UUID包含时间戳信息", func(t *testing.T) {
// 连续生成多个UUID它们的时间戳部分应该相近或递增
uuid1 := helpers.NewUUIDv7()
uuid2 := helpers.NewUUIDv7()
// UUID v7 的前12个字符主要是时间戳
// 在很短的时间内生成的UUID时间戳部分应该相同或非常接近
timePrefix1 := uuid1[:12]
timePrefix2 := uuid2[:12]
// 时间戳应该相同或第二个略大(因为时间在递增)
assert.True(t,
timePrefix1 == timePrefix2 || timePrefix1 <= timePrefix2,
"UUID的时间戳部分应该单调递增")
})
t.Run("批量生成UUID性能测试", func(t *testing.T) {
const iterations = 1000
uuids := make([]string, iterations)
for i := range iterations {
uuids[i] = helpers.NewUUIDv7()
}
// 验证所有UUID都有效
for i, uuid := range uuids {
assert.Regexp(t, uuidPattern, uuid, "UUID %d 格式不正确", i)
}
// 简单的唯一性检查
uniqueMap := make(map[string]bool)
for _, uuid := range uuids {
uniqueMap[uuid] = true
}
assert.Len(t, uniqueMap, iterations, "应该生成%d个唯一的UUID", iterations)
})
}
func TestNewUUIDv7_Format(t *testing.T) {
// 测试UUID v7的具体格式要求
uuid := helpers.NewUUIDv7()
// 总长度 32
assert.Len(t, uuid, 32)
// 全部小写
for _, c := range uuid {
if c >= 'a' && c <= 'f' {
assert.True(t, c >= 'a' && c <= 'f')
} else {
assert.True(t, c >= '0' && c <= '9')
}
}
}
func TestNewUUIDv7_EdgeCases(t *testing.T) {
t.Run("快速连续生成", func(t *testing.T) {
// 在极短时间内生成多个UUID
uuids := make([]string, 10)
for i := range 10 {
uuids[i] = helpers.NewUUIDv7()
}
// 所有UUID应该都有效且唯一
seen := make(map[string]bool)
for _, uuid := range uuids {
assert.Len(t, uuid, 32)
assert.False(t, seen[uuid], "UUID不应该重复")
seen[uuid] = true
}
})
}