2025-12-22 13:37:57 +08:00
|
|
|
package model_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
2025-12-26 14:35:39 +08:00
|
|
|
"go.yandata.net/wangsiyuan/go-trustlog/api/model"
|
2025-12-22 13:37:57 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// TestRecord_TimestampNanosecondPrecision 验证 Record 的时间戳在 CBOR 序列化/反序列化后能保留纳秒精度
|
|
|
|
|
func TestRecord_TimestampNanosecondPrecision(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
// 创建一个包含纳秒精度的时间戳
|
|
|
|
|
timestamp := time.Date(2024, 1, 1, 12, 30, 45, 123456789, time.UTC)
|
|
|
|
|
|
|
|
|
|
original := &model.Record{
|
|
|
|
|
ID: "rec-nanosecond-test",
|
|
|
|
|
DoPrefix: "test",
|
|
|
|
|
ProducerID: "producer-1",
|
|
|
|
|
Timestamp: timestamp,
|
|
|
|
|
Operator: "operator-1",
|
|
|
|
|
Extra: []byte("extra"),
|
|
|
|
|
RCType: "log",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := original.CheckAndInit()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
t.Logf("Original timestamp: %v", original.Timestamp)
|
|
|
|
|
t.Logf("Original nanoseconds: %d", original.Timestamp.Nanosecond())
|
|
|
|
|
|
|
|
|
|
// 序列化
|
|
|
|
|
data, err := original.MarshalBinary()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.NotNil(t, data)
|
|
|
|
|
|
|
|
|
|
// 反序列化
|
|
|
|
|
result := &model.Record{}
|
|
|
|
|
err = result.UnmarshalBinary(data)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
t.Logf("Decoded timestamp: %v", result.Timestamp)
|
|
|
|
|
t.Logf("Decoded nanoseconds: %d", result.Timestamp.Nanosecond())
|
|
|
|
|
|
|
|
|
|
// 验证纳秒精度被完整保留
|
|
|
|
|
assert.Equal(t, original.Timestamp.UnixNano(), result.Timestamp.UnixNano(),
|
|
|
|
|
"时间戳的纳秒精度应该被完整保留")
|
|
|
|
|
assert.Equal(t, original.Timestamp.Nanosecond(), result.Timestamp.Nanosecond(),
|
|
|
|
|
"纳秒部分应该相等")
|
|
|
|
|
}
|
2025-12-26 13:47:55 +08:00
|
|
|
|