func writeEntryTo(w io.Writer, ent *raftpb.Entry) error { size := ent.Size() if err := binary.Write(w, binary.BigEndian, uint64(size)); err != nil { return err } b, err := ent.Marshal() if err != nil { return err } _, err = w.Write(b) return err }
func (ew *entryWriter) writeEntry(ent *raftpb.Entry) error { size := ent.Size() if err := binary.Write(ew.w, binary.BigEndian, uint64(size)); err != nil { return err } b, err := ent.Marshal() if err != nil { return err } _, err = ew.w.Write(b) ew.bytes.AddBy(8 + int64(size)) return err }
func TestSlice(t *testing.T) { var i uint64 offset := uint64(100) num := uint64(100) last := offset + num half := offset + num/2 halfe := pb.Entry{Index: half, Term: half} storage := NewMemoryStorage() storage.ApplySnapshot(pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: offset}}) for i = 1; i < num/2; i++ { storage.Append([]pb.Entry{{Index: offset + i, Term: offset + i}}) } l := newLog(storage) for i = num / 2; i < num; i++ { l.append(pb.Entry{Index: offset + i, Term: offset + i}) } tests := []struct { from uint64 to uint64 limit uint64 w []pb.Entry wpanic bool }{ // test no limit {offset - 1, offset + 1, noLimit, nil, false}, {offset, offset + 1, noLimit, nil, false}, {half - 1, half + 1, noLimit, []pb.Entry{{Index: half - 1, Term: half - 1}, {Index: half, Term: half}}, false}, {half, half + 1, noLimit, []pb.Entry{{Index: half, Term: half}}, false}, {last - 1, last, noLimit, []pb.Entry{{Index: last - 1, Term: last - 1}}, false}, {last, last + 1, noLimit, nil, true}, // test limit {half - 1, half + 1, 0, []pb.Entry{{Index: half - 1, Term: half - 1}}, false}, {half - 1, half + 1, uint64(halfe.Size() + 1), []pb.Entry{{Index: half - 1, Term: half - 1}}, false}, {half - 1, half + 1, uint64(halfe.Size() * 2), []pb.Entry{{Index: half - 1, Term: half - 1}, {Index: half, Term: half}}, false}, {half - 1, half + 2, uint64(halfe.Size() * 3), []pb.Entry{{Index: half - 1, Term: half - 1}, {Index: half, Term: half}, {Index: half + 1, Term: half + 1}}, false}, {half, half + 2, uint64(halfe.Size()), []pb.Entry{{Index: half, Term: half}}, false}, {half, half + 2, uint64(halfe.Size() * 2), []pb.Entry{{Index: half, Term: half}, {Index: half + 1, Term: half + 1}}, false}, } for j, tt := range tests { func() { defer func() { if r := recover(); r != nil { if !tt.wpanic { t.Errorf("%d: panic = %v, want %v: %v", j, true, false, r) } } }() g, err := l.slice(tt.from, tt.to, tt.limit) if tt.from <= offset && err != ErrCompacted { t.Fatalf("#%d: err = %v, want %v", j, err, ErrCompacted) } if tt.from > offset && err != nil { t.Fatalf("#%d: unexpected error %v", j, err) } if !reflect.DeepEqual(g, tt.w) { t.Errorf("#%d: from %d to %d = %v, want %v", j, tt.from, tt.to, g, tt.w) } }() } }