lib/model, lib/protocol: Index sending/receiving debugging (#9657)

This adds guardrails to the index sending and receiving, to verify that
what we thinks is happening is what actually happens.
This commit is contained in:
Jakob Borg
2024-08-28 15:00:19 +02:00
committed by GitHub
parent feff334547
commit 42e677c055
6 changed files with 497 additions and 228 deletions
+16 -1
View File
@@ -20,10 +20,14 @@ type FileInfoBatch struct {
infos []protocol.FileInfo
size int
flushFn func([]protocol.FileInfo) error
error error
}
// NewFileInfoBatch returns a new FileInfoBatch that calls fn when it's time
// to flush.
// to flush. Errors from the flush function are considered non-recoverable;
// once an error is returned the flush function wil not be called again, and
// any further calls to Flush will return the same error (unless Reset is
// called).
func NewFileInfoBatch(fn func([]protocol.FileInfo) error) *FileInfoBatch {
return &FileInfoBatch{flushFn: fn}
}
@@ -33,6 +37,9 @@ func (b *FileInfoBatch) SetFlushFunc(fn func([]protocol.FileInfo) error) {
}
func (b *FileInfoBatch) Append(f protocol.FileInfo) {
if b.error != nil {
panic("bug: calling append on a failed batch")
}
if b.infos == nil {
b.infos = make([]protocol.FileInfo, 0, MaxBatchSizeFiles)
}
@@ -45,6 +52,9 @@ func (b *FileInfoBatch) Full() bool {
}
func (b *FileInfoBatch) FlushIfFull() error {
if b.error != nil {
return b.error
}
if b.Full() {
return b.Flush()
}
@@ -52,10 +62,14 @@ func (b *FileInfoBatch) FlushIfFull() error {
}
func (b *FileInfoBatch) Flush() error {
if b.error != nil {
return b.error
}
if len(b.infos) == 0 {
return nil
}
if err := b.flushFn(b.infos); err != nil {
b.error = err
return err
}
b.Reset()
@@ -64,6 +78,7 @@ func (b *FileInfoBatch) Flush() error {
func (b *FileInfoBatch) Reset() {
b.infos = nil
b.error = nil
b.size = 0
}
+52
View File
@@ -8,12 +8,14 @@ package db
import (
"encoding/json"
"errors"
"io"
"os"
"testing"
"github.com/syncthing/syncthing/lib/db/backend"
"github.com/syncthing/syncthing/lib/events"
"github.com/syncthing/syncthing/lib/protocol"
)
// writeJSONS serializes the database to a JSON stream that can be checked
@@ -178,3 +180,53 @@ func snapshot(t testing.TB, fset *FileSet) *Snapshot {
// defer fd.Close()
// writeJSONS(fd, db)
// }
func TestFileInfoBatchError(t *testing.T) {
// Verify behaviour of the flush function returning an error.
var errReturn error
var called int
b := NewFileInfoBatch(func([]protocol.FileInfo) error {
called += 1
return errReturn
})
// Flush should work when the flush function error is nil
b.Append(protocol.FileInfo{Name: "test"})
if err := b.Flush(); err != nil {
t.Fatalf("expected nil, got %v", err)
}
if called != 1 {
t.Fatalf("expected 1, got %d", called)
}
// Flush should fail with an error retur
errReturn = errors.New("problem")
b.Append(protocol.FileInfo{Name: "test"})
if err := b.Flush(); err != errReturn {
t.Fatalf("expected %v, got %v", errReturn, err)
}
if called != 2 {
t.Fatalf("expected 2, got %d", called)
}
// Flush function should not be called again when it's already errored,
// same error should be returned by Flush()
if err := b.Flush(); err != errReturn {
t.Fatalf("expected %v, got %v", errReturn, err)
}
if called != 2 {
t.Fatalf("expected 2, got %d", called)
}
// Reset should clear the error (and the file list)
errReturn = nil
b.Reset()
b.Append(protocol.FileInfo{Name: "test"})
if err := b.Flush(); err != nil {
t.Fatalf("expected nil, got %v", err)
}
if called != 3 {
t.Fatalf("expected 3, got %d", called)
}
}