mysqlbinlog/summary_test.go
starainrt 0c9d9c6eae
feat(binlog): 增加 logical clock 元数据解析与统计支撑
- 暴露事务级 last_committed、sequence_number、transaction_length 和 commit timestamp
- 在 GTID event 转换时透传 logical clock 元数据
- 新增 ParseOptions、ParseProgress,支持上下文取消和解析进度回调
- 保留 TransactionPayloadEvent 展开后的 tablemap 与压缩类型信息
- 增加 TransactionSummary 辅助结构,便于上层统计事务结果、耗时、表分布和逻辑时钟
- 清理测试对外部大 binlog 样本的依赖,保证独立库测试可运行
- 修正跨平台测试与 GTID 输出格式断言,提升发版稳定性
2026-05-10 14:02:53 +08:00

67 lines
2.1 KiB
Go

package binlog
import (
"testing"
"time"
)
func TestSummarizeTransaction(t *testing.T) {
tx := Transaction{
GTID: "uuid:1",
Timestamp: 100,
StartPos: 120,
EndPos: 240,
Size: 120,
LastCommitted: 98,
SequenceNumber: 100,
TransactionLength: 4096,
Txs: []TxDetail{
{SqlType: "query", Sql: "BEGIN", Timestamp: 100},
{SqlType: "insert", Db: "Shop", Table: "Orders", Sql: "insert into orders values (1)", RowCount: 2, Timestamp: 101},
{SqlType: "delete", Db: "shop", Table: "orders", Sql: "delete from orders where id = 2", RowCount: 1, Timestamp: 102},
{SqlType: "query", Sql: "COMMIT", Timestamp: 103},
},
}
got := SummarizeTransaction(tx)
if got.Outcome != TransactionOutcomeCommit {
t.Fatalf("unexpected outcome: %s", got.Outcome)
}
if !got.HasBeginBoundary || !got.HasEndBoundary || !got.HasExplicitDuration || !got.HasDuration {
t.Fatalf("expected explicit duration markers: %#v", got)
}
if got.Duration != 3*time.Second {
t.Fatalf("unexpected duration: %s", got.Duration)
}
if got.RowsCount != 3 || got.StatementsCount != 2 {
t.Fatalf("unexpected rows/statements: rows=%d stmts=%d", got.RowsCount, got.StatementsCount)
}
if len(got.Tables) != 1 || got.Tables[0] != "shop.orders" {
t.Fatalf("unexpected tables: %#v", got.Tables)
}
if got.SampleSQL != "insert into orders values (1)" {
t.Fatalf("unexpected sample sql: %q", got.SampleSQL)
}
if got.LastCommitted != 98 || got.SequenceNumber != 100 || got.TransactionLength != 4096 {
t.Fatalf("logical metadata not preserved: %#v", got)
}
}
func TestSummarizeTransaction_Autocommit(t *testing.T) {
tx := Transaction{
GTID: "uuid:2",
Timestamp: 100,
Txs: []TxDetail{
{SqlType: "insert", Db: "db", Table: "tb", Sql: "insert into tb values (1)", RowCount: 1, Timestamp: 100},
},
}
got := SummarizeTransaction(tx)
if got.Outcome != TransactionOutcomeAutocommit {
t.Fatalf("unexpected outcome: %s", got.Outcome)
}
if got.SeenTime.IsZero() || got.BeginTime.IsZero() || got.EndTime.IsZero() {
t.Fatalf("expected lazy times to be filled: %#v", got)
}
}