lib: Use counterfeiter to mock interfaces in tests (#7375)

This commit is contained in:
Simon Frei
2021-03-03 08:53:50 +01:00
committed by GitHub
parent 7945430e64
commit 3d91f7c975
46 changed files with 8947 additions and 887 deletions
+24 -84
View File
@@ -13,8 +13,8 @@ import (
"time"
"github.com/syncthing/syncthing/lib/protocol"
protocolmocks "github.com/syncthing/syncthing/lib/protocol/mocks"
"github.com/syncthing/syncthing/lib/scanner"
"github.com/syncthing/syncthing/lib/testutils"
)
type downloadProgressMessage struct {
@@ -22,97 +22,37 @@ type downloadProgressMessage struct {
updates []protocol.FileDownloadProgressUpdate
}
func newFakeConnection(id protocol.DeviceID, model Model) *fakeConnection {
f := &fakeConnection{
Connection: new(protocolmocks.Connection),
id: id,
model: model,
}
f.RequestCalls(func(ctx context.Context, folder, name string, blockNo int, offset int64, size int, hash []byte, weakHash uint32, fromTemporary bool) ([]byte, error) {
return f.fileData[name], nil
})
f.IDReturns(id)
f.CloseCalls(func(err error) {
model.Closed(f, err)
f.ClosedReturns(true)
})
return f
}
type fakeConnection struct {
testutils.FakeConnectionInfo
*protocolmocks.Connection
id protocol.DeviceID
downloadProgressMessages []downloadProgressMessage
closed bool
files []protocol.FileInfo
fileData map[string][]byte
folder string
model *testModel
indexFn func(context.Context, string, []protocol.FileInfo)
requestFn func(ctx context.Context, folder, name string, offset int64, size int, hash []byte, fromTemporary bool) ([]byte, error)
closeFn func(error)
clusterConfigFn func(protocol.ClusterConfig)
model Model
mut sync.Mutex
}
func (f *fakeConnection) Close(err error) {
f.mut.Lock()
defer f.mut.Unlock()
if f.closeFn != nil {
f.closeFn(err)
return
}
f.closed = true
f.model.Closed(f, err)
}
func (f *fakeConnection) Start() {
}
func (f *fakeConnection) ID() protocol.DeviceID {
return f.id
}
func (f *fakeConnection) Name() string {
return ""
}
func (f *fakeConnection) Option(string) string {
return ""
}
func (f *fakeConnection) Index(ctx context.Context, folder string, fs []protocol.FileInfo) error {
f.mut.Lock()
defer f.mut.Unlock()
if f.indexFn != nil {
f.indexFn(ctx, folder, fs)
}
return nil
}
func (f *fakeConnection) IndexUpdate(ctx context.Context, folder string, fs []protocol.FileInfo) error {
f.mut.Lock()
defer f.mut.Unlock()
if f.indexFn != nil {
f.indexFn(ctx, folder, fs)
}
return nil
}
func (f *fakeConnection) Request(ctx context.Context, folder, name string, blockNo int, offset int64, size int, hash []byte, weakHash uint32, fromTemporary bool) ([]byte, error) {
f.mut.Lock()
defer f.mut.Unlock()
if f.requestFn != nil {
return f.requestFn(ctx, folder, name, offset, size, hash, fromTemporary)
}
return f.fileData[name], nil
}
func (f *fakeConnection) ClusterConfig(cc protocol.ClusterConfig) {
f.mut.Lock()
defer f.mut.Unlock()
if f.clusterConfigFn != nil {
f.clusterConfigFn(cc)
}
}
func (f *fakeConnection) Ping() bool {
f.mut.Lock()
defer f.mut.Unlock()
return f.closed
}
func (f *fakeConnection) Closed() bool {
f.mut.Lock()
defer f.mut.Unlock()
return f.closed
}
func (f *fakeConnection) Statistics() protocol.Statistics {
return protocol.Statistics{}
func (f *fakeConnection) setIndexFn(fn func(_ context.Context, folder string, fs []protocol.FileInfo) error) {
f.IndexCalls(fn)
f.IndexUpdateCalls(fn)
}
func (f *fakeConnection) DownloadProgress(_ context.Context, folder string, updates []protocol.FileDownloadProgressUpdate) {
@@ -201,7 +141,7 @@ func (f *fakeConnection) sendIndexUpdate() {
}
func addFakeConn(m *testModel, dev protocol.DeviceID) *fakeConnection {
fc := &fakeConnection{id: dev, model: m}
fc := newFakeConnection(dev, m)
m.AddConnection(fc, protocol.Hello{})
m.ClusterConfig(dev, protocol.ClusterConfig{