1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
package raftchunking
import (
"crypto/rand"
"io"
"testing"
"time"
"github.com/go-test/deep"
proto "github.com/golang/protobuf/proto"
"github.com/hashicorp/go-raftchunking/types"
"github.com/hashicorp/raft"
)
func chunkData(t *testing.T) ([]byte, []raft.Log) {
data := make([]byte, 6000000)
n, err := rand.Read(data)
if err != nil && err != io.EOF {
t.Fatal(err)
}
if n != 6000000 {
t.Fatalf("expected 6000k bytes to test with, read %d", n)
}
logs := make([]raft.Log, 0)
dur := time.Second
applyFunc := func(l raft.Log, d time.Duration) raft.ApplyFuture {
if d != dur {
t.Fatalf("expected d to be %v, got %v", time.Second, dur)
}
logs = append(logs, l)
return raft.ApplyFuture(nil)
}
ChunkingApply(data, nil, dur, applyFunc)
return data, logs
}
func TestApplyChunking(t *testing.T) {
data, logs := chunkData(t)
var opNum uint64
var finalData []byte
for i, l := range logs {
var ci types.ChunkInfo
if err := proto.Unmarshal(l.Extensions, &ci); err != nil {
t.Fatal(err)
}
if i == 0 {
opNum = ci.OpNum
}
if ci.OpNum == 0 || ci.OpNum != opNum {
t.Fatalf("bad op num: %d", ci.OpNum)
}
if ci.SequenceNum != uint32(i) {
t.Fatalf("bad seqnum; expected %d, got %d", i, ci.SequenceNum)
}
finalData = append(finalData, l.Data...)
}
if diff := deep.Equal(data, finalData); diff != nil {
t.Fatal(diff)
}
}
|