wp
This commit is contained in:
@@ -1,113 +1,24 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"gordenko.dev/dima/qb"
|
||||
"gordenko.dev/dima/qb/storage"
|
||||
)
|
||||
|
||||
func TestWriteReadSnapshot(t *testing.T) {
|
||||
metrics := make(map[uint32]*_metric)
|
||||
metrics[1] = &_metric{
|
||||
metricType: qb.Cumulative,
|
||||
fracDigits: 3,
|
||||
lastPageNo: 1000,
|
||||
buffer: []byte{},
|
||||
}
|
||||
in := writeSnapshotIn{
|
||||
SnapshotNumber: 1,
|
||||
Dir: ".",
|
||||
WriteBufferSize: 8 * 1024 * 1024,
|
||||
Metrics: metrics,
|
||||
FrozenIndexPagesCount: 7,
|
||||
IndexPageNumbers: []uint32{
|
||||
1, 2, 3, 4, 5,
|
||||
},
|
||||
FrozenDataPagesCount: 8,
|
||||
DataPageNumbers: []uint32{
|
||||
6, 7, 8,
|
||||
},
|
||||
}
|
||||
err := writeSnapshot(in)
|
||||
if err != nil {
|
||||
t.Fatalf("writeSnapshot: %s", err)
|
||||
}
|
||||
|
||||
out, err := readSnapshot("1.snapshot")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if out.FrozenIndexPagesCount != in.FrozenIndexPagesCount {
|
||||
t.Fatalf("FrozenIndexPagesCount: got %d are not equal expected %d",
|
||||
out.FrozenIndexPagesCount, in.FrozenIndexPagesCount)
|
||||
}
|
||||
if out.FrozenDataPagesCount != in.FrozenDataPagesCount {
|
||||
t.Fatalf("FrozenDataPagesCount: got %d are not equal expected %d",
|
||||
out.FrozenDataPagesCount, in.FrozenDataPagesCount)
|
||||
}
|
||||
if !slices.Equal(out.IndexPageNumbers, in.IndexPageNumbers) {
|
||||
t.Fatalf("IndexPageNumbers: got %v are not equal expected %v",
|
||||
out.IndexPageNumbers, in.IndexPageNumbers)
|
||||
}
|
||||
if !slices.Equal(out.DataPageNumbers, in.DataPageNumbers) {
|
||||
t.Fatalf("DataPageNumbers: got %v are not equal expected %v",
|
||||
out.DataPageNumbers, in.DataPageNumbers)
|
||||
}
|
||||
for metricID, replay := range out.Metrics {
|
||||
origin, ok := metrics[metricID]
|
||||
if !ok {
|
||||
t.Fatalf("decoded metricID %d not found in metrics", metricID)
|
||||
}
|
||||
err = cmpMetric(replay, origin)
|
||||
if err != nil {
|
||||
t.Fatalf("metric %d: %s", metricID, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func cmpMetric(replay *ReplayMetric, origin *_metric) error {
|
||||
if replay.metricType != origin.metricType {
|
||||
return fmt.Errorf("metricType: got %d are not equal expected %d",
|
||||
replay.metricType, origin.metricType)
|
||||
}
|
||||
if replay.fracDigits != origin.fracDigits {
|
||||
return fmt.Errorf("fracDigits: got %d are not equal expected %d",
|
||||
replay.fracDigits, origin.fracDigits)
|
||||
}
|
||||
if replay.lastPageNo != origin.lastPageNo {
|
||||
return fmt.Errorf("lastPageNo: got %d are not equal expected %d",
|
||||
replay.lastPageNo, origin.lastPageNo)
|
||||
}
|
||||
// if !refect.DeepEq{
|
||||
// return fmt.Errorf("lastPageNo: got %d are not equal expected %d",
|
||||
// replay.lastPageNo, origin.lastPageNo)
|
||||
// }
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestComposeHeadIndexPage(t *testing.T) {
|
||||
var (
|
||||
levelIdx = 0
|
||||
level = storage.IndexLevelTail{
|
||||
Buffer: []byte{
|
||||
1, 2, 3,
|
||||
},
|
||||
RecordsCount: 2,
|
||||
}
|
||||
head = &storage.IndexPageTail{
|
||||
PageNo: 100,
|
||||
CRC32: 12345,
|
||||
Records: []byte{},
|
||||
}
|
||||
)
|
||||
page := composeHeadIndexPage(levelIdx, level, head)
|
||||
if page.PageNo != head.PageNo {
|
||||
t.Fatalf("PageNo: got %d are not equal expected %v",
|
||||
page.PageNo, head.PageNo)
|
||||
}
|
||||
// fix compare pages
|
||||
}
|
||||
// func TestComposeHeadIndexPage(t *testing.T) {
|
||||
// var (
|
||||
// levelIdx = 0
|
||||
// level = storage.IndexLevelTail{
|
||||
// Buffer: []byte{
|
||||
// 1, 2, 3,
|
||||
// },
|
||||
// RecordsCount: 2,
|
||||
// }
|
||||
// head = &storage.IndexPageTail{
|
||||
// PageNo: 100,
|
||||
// CRC32: 12345,
|
||||
// Records: []byte{},
|
||||
// }
|
||||
// )
|
||||
// page := composeHeadIndexPage(levelIdx, level, head)
|
||||
// if page.PageNo != head.PageNo {
|
||||
// t.Fatalf("PageNo: got %d are not equal expected %v",
|
||||
// page.PageNo, head.PageNo)
|
||||
// }
|
||||
// // fix compare pages
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user