91 lines
2.4 KiB
Go
91 lines
2.4 KiB
Go
|
|
package worker
|
||
|
|
|
||
|
|
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 *storage.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 !reflect.DeepEqual() {
|
||
|
|
// return fmt.Errorf("lastPageNo: got %d are not equal expected %d",
|
||
|
|
// replay.LastPageNo, origin.lastPageNo)
|
||
|
|
// }
|
||
|
|
return nil
|
||
|
|
}
|