This repository has been archived on 2022-07-30. You can view files and clone it, but cannot push or open issues/pull-requests.
mdb/kvstore/wal/util_test.go

67 lines
1.2 KiB
Go

package wal
import (
"crypto/rand"
"encoding/hex"
"fmt"
mrand "math/rand"
"os"
"path/filepath"
"reflect"
)
func _b(in string) []byte {
return []byte(in)
}
func randString() string {
buf := make([]byte, 1+mrand.Intn(20))
rand.Read(buf)
return hex.EncodeToString(buf)
}
func randID() uint64 {
return uint64(mrand.Uint32())
}
func randPath() string {
buf := make([]byte, 8)
rand.Read(buf)
return filepath.Join(os.TempDir(), hex.EncodeToString(buf))
}
func readWAL(walPath string) (l []Record) {
f := NewFollower(walPath)
defer f.Close()
f.Replay(0, func(rec Record) error {
l = append(l, rec)
return nil
})
return l
}
func walEqual(walPath string, expected []Record) error {
recs := readWAL(walPath)
return recsEqual(recs, expected)
}
func recsEqual(recs, expected []Record) error {
if len(recs) != len(expected) {
return fmt.Errorf("Expected %d records but found %d",
len(expected), len(recs))
}
for i, rec := range recs {
exp := expected[i]
if !reflect.DeepEqual(rec, exp) {
return fmt.Errorf("Mismatched records: %v != %v", rec, exp)
}
}
return nil
}
func walsEqual(path1, path2 string) error {
return recsEqual(readWAL(path1), readWAL(path2))
}