This commit is contained in:
2026-06-14 23:12:03 +03:00
parent 181031a753
commit b32279deaf
35 changed files with 1395 additions and 1062 deletions

View File

@@ -1,25 +1,74 @@
package worker
import (
"bytes"
"fmt"
"os"
"reflect"
"slices"
"testing"
"gordenko.dev/dima/qb"
"gordenko.dev/dima/qb/enc"
"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{},
storage.DataPageSize = 32
storage.DataPagePayloadSize = 20
storage.IndexPageSize = 16
buf := []byte{
// values
0x8f, // base value
0x80, // delta 0
0x01, // run (len = 3)
0x81, // delta 1
0x81, // literal (len = 2)
// empty space
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
// timestamps
0x80, // h-byte (literal, len=1)
0x94, // delta 20
0x01, // h-byte (run, len=3)
0xbc, // delta 60
0x28, 0x80, 0x24, 0x6a, // since
// footer
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
}
databuf := buf[:storage.DataPagePayloadSize]
timestampsSize := 8
valuesSize := 5
metrics := map[uint32]*Metric{
1: {
metricType: qb.Cumulative,
fracDigits: 3,
lastPageNo: 1000,
buffer: buf,
timestamps: enc.NewTimeDeltaCompressor(databuf, timestampsSize),
values: enc.NewValueDeltaCompressor(qb.Cumulative, 3, databuf, valuesSize),
indexLevelTails: []storage.IndexLevelTail{
{
Buffer: []byte{
0x01, 0x01, 0x01, 0x01,
0x02, 0x02, 0x02, 0x02,
0x00,
// footer
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
},
RecordsCount: 1,
},
},
},
}
in := WriteSnapshotIn{
SnapshotNumber: 1,
Dir: ".",
WriteBufferSize: 8 * 1024 * 1024,
Metrics: metrics,
FrozenIndexPagesCount: 7,
@@ -31,14 +80,17 @@ func TestWriteReadSnapshot(t *testing.T) {
6, 7, 8,
},
}
err := WriteSnapshot(in)
fileName := "test.snapshot"
err := writeSnapshot(fileName, in)
if err != nil {
t.Fatalf("writeSnapshot: %s", err)
}
out, err := ReadSnapshot("1.snapshot")
out, err := ReadSnapshot(fileName)
if err != nil {
return
t.Fatal(err)
}
if out.FrozenIndexPagesCount != in.FrozenIndexPagesCount {
@@ -67,6 +119,7 @@ func TestWriteReadSnapshot(t *testing.T) {
t.Fatalf("metric %d: %s", metricID, err)
}
}
os.Remove(fileName)
}
func cmpMetric(replay *storage.ReplayMetric, origin *Metric) error {
@@ -82,9 +135,21 @@ func cmpMetric(replay *storage.ReplayMetric, origin *Metric) error {
return fmt.Errorf("lastPageNo: got %d are not equal expected %d",
replay.LastPageNo, origin.lastPageNo)
}
// if !reflect.DeepEqual() {
// return fmt.Errorf("lastPageNo: got %d are not equal expected %d",
// replay.LastPageNo, origin.lastPageNo)
// }
if !reflect.DeepEqual(replay.IndexLevelTails, origin.indexLevelTails) {
return fmt.Errorf("indexLevelTails: got %#v are not equal expected %#v",
replay.IndexLevelTails, origin.indexLevelTails)
}
if !bytes.Equal(replay.Buf, origin.buffer) {
return fmt.Errorf("buffer: got % x are not equal expected % x",
replay.Buf, origin.buffer)
}
if replay.TimestampsSize != origin.timestamps.Size() {
return fmt.Errorf("timestampsSize: got %d are not equal expected %d",
replay.TimestampsSize, origin.timestamps.Size())
}
if replay.ValuesSize != origin.values.Size() {
return fmt.Errorf("valuesSize: got %d are not equal expected %d",
replay.ValuesSize, origin.values.Size())
}
return nil
}