Files
syncthing/lib/protocol/common_test.go
T

92 lines
1.9 KiB
Go
Raw Normal View History

// Copyright (C) 2014 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
2014-09-22 21:42:11 +02:00
package protocol
import (
"time"
)
2014-09-22 21:42:11 +02:00
type TestModel struct {
data []byte
folder string
name string
offset int64
size int32
hash []byte
fromTemporary bool
indexFn func(string, []FileInfo)
2024-01-31 08:18:27 +01:00
ccFn func(*ClusterConfig)
closedCh chan struct{}
closedErr error
2014-09-22 21:42:11 +02:00
}
func newTestModel() *TestModel {
return &TestModel{
2016-01-12 09:19:44 +01:00
closedCh: make(chan struct{}),
2014-09-22 21:42:11 +02:00
}
}
2024-01-31 08:18:27 +01:00
func (t *TestModel) Index(_ Connection, idx *Index) error {
if t.indexFn != nil {
2024-01-31 08:18:27 +01:00
t.indexFn(idx.Folder, idx.Files)
}
return nil
2014-09-22 21:42:11 +02:00
}
2024-01-31 08:18:27 +01:00
func (*TestModel) IndexUpdate(Connection, *IndexUpdate) error {
return nil
2014-09-22 21:42:11 +02:00
}
2024-01-31 08:18:27 +01:00
func (t *TestModel) Request(_ Connection, req *Request) (RequestResponse, error) {
t.folder = req.Folder
t.name = req.Name
t.offset = req.Offset
t.size = int32(req.Size)
t.hash = req.Hash
t.fromTemporary = req.FromTemporary
buf := make([]byte, len(t.data))
2015-07-29 21:23:43 +01:00
copy(buf, t.data)
return &fakeRequestResponse{buf}, nil
2014-09-22 21:42:11 +02:00
}
func (t *TestModel) Closed(_ Connection, err error) {
2016-01-12 09:19:44 +01:00
t.closedErr = err
2014-09-22 21:42:11 +02:00
close(t.closedCh)
}
2024-01-31 08:18:27 +01:00
func (t *TestModel) ClusterConfig(_ Connection, config *ClusterConfig) error {
if t.ccFn != nil {
t.ccFn(config)
}
return nil
2014-09-22 21:42:11 +02:00
}
2024-01-31 08:18:27 +01:00
func (*TestModel) DownloadProgress(Connection, *DownloadProgress) error {
return nil
}
2016-01-12 09:19:44 +01:00
func (t *TestModel) closedError() error {
2014-09-22 21:42:11 +02:00
select {
case <-t.closedCh:
2016-01-12 09:19:44 +01:00
return t.closedErr
2014-09-22 21:42:11 +02:00
case <-time.After(1 * time.Second):
2016-01-12 09:19:44 +01:00
return nil // Timeout
2014-09-22 21:42:11 +02:00
}
}
type fakeRequestResponse struct {
data []byte
}
func (r *fakeRequestResponse) Data() []byte {
return r.data
}
2022-07-28 17:32:45 +02:00
func (*fakeRequestResponse) Close() {}
2022-07-28 17:32:45 +02:00
func (*fakeRequestResponse) Wait() {}