2024-12-01 16:50:17 +01:00
|
|
|
// 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
|
|
|
|
|
|
2024-12-01 16:50:17 +01:00
|
|
|
import (
|
|
|
|
|
"time"
|
|
|
|
|
)
|
2014-09-22 21:42:11 +02:00
|
|
|
|
|
|
|
|
type TestModel struct {
|
2016-07-04 10:40:29 +00:00
|
|
|
data []byte
|
|
|
|
|
folder string
|
|
|
|
|
name string
|
|
|
|
|
offset int64
|
2018-11-13 08:53:55 +01:00
|
|
|
size int32
|
2016-07-04 10:40:29 +00:00
|
|
|
hash []byte
|
|
|
|
|
fromTemporary bool
|
2023-07-29 10:24:44 +02:00
|
|
|
indexFn func(string, []FileInfo)
|
2024-01-31 08:18:27 +01:00
|
|
|
ccFn func(*ClusterConfig)
|
2016-07-04 10:40:29 +00:00
|
|
|
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 {
|
2019-05-25 21:08:07 +02:00
|
|
|
if t.indexFn != nil {
|
2024-01-31 08:18:27 +01:00
|
|
|
t.indexFn(idx.Folder, idx.Files)
|
2019-05-25 21:08:07 +02:00
|
|
|
}
|
2019-12-04 10:46:55 +01:00
|
|
|
return nil
|
2014-09-22 21:42:11 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-31 08:18:27 +01:00
|
|
|
func (*TestModel) IndexUpdate(Connection, *IndexUpdate) error {
|
2019-12-04 10:46:55 +01:00
|
|
|
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
|
2018-11-13 08:53:55 +01:00
|
|
|
buf := make([]byte, len(t.data))
|
2015-07-29 21:23:43 +01:00
|
|
|
copy(buf, t.data)
|
2018-11-13 08:53:55 +01:00
|
|
|
return &fakeRequestResponse{buf}, nil
|
2014-09-22 21:42:11 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-29 10:24:44 +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 {
|
2019-06-14 19:04:41 +02:00
|
|
|
if t.ccFn != nil {
|
2023-07-29 10:24:44 +02:00
|
|
|
t.ccFn(config)
|
2019-06-14 19:04:41 +02:00
|
|
|
}
|
2019-12-04 10:46:55 +01:00
|
|
|
return nil
|
2014-09-22 21:42:11 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-31 08:18:27 +01:00
|
|
|
func (*TestModel) DownloadProgress(Connection, *DownloadProgress) error {
|
2019-12-04 10:46:55 +01:00
|
|
|
return nil
|
2016-04-15 10:59:41 +00:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
2018-11-13 08:53:55 +01: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() {}
|
2018-11-13 08:53:55 +01:00
|
|
|
|
2022-07-28 17:32:45 +02:00
|
|
|
func (*fakeRequestResponse) Wait() {}
|