2015-01-13 12:31:14 +00:00
|
|
|
// Copyright (C) 2014 The Protocol Authors.
|
2014-09-22 19:42:11 +00:00
|
|
|
|
|
|
|
package protocol
|
|
|
|
|
2016-04-09 01:10:31 +00:00
|
|
|
import "time"
|
2014-09-22 19:42:11 +00:00
|
|
|
|
|
|
|
type TestModel struct {
|
2016-07-04 10:40:29 +00:00
|
|
|
data []byte
|
|
|
|
folder string
|
|
|
|
name string
|
|
|
|
offset int64
|
2018-11-13 07:53:55 +00:00
|
|
|
size int32
|
2016-07-04 10:40:29 +00:00
|
|
|
hash []byte
|
2018-05-05 08:24:44 +00:00
|
|
|
weakHash uint32
|
2016-07-04 10:40:29 +00:00
|
|
|
fromTemporary bool
|
2023-07-29 08:24:44 +00:00
|
|
|
indexFn func(string, []FileInfo)
|
|
|
|
ccFn func(ClusterConfig)
|
2016-07-04 10:40:29 +00:00
|
|
|
closedCh chan struct{}
|
|
|
|
closedErr error
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTestModel() *TestModel {
|
|
|
|
return &TestModel{
|
2016-01-12 08:19:44 +00:00
|
|
|
closedCh: make(chan struct{}),
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-29 08:24:44 +00:00
|
|
|
func (t *TestModel) Index(_ Connection, folder string, files []FileInfo) error {
|
2019-05-25 19:08:07 +00:00
|
|
|
if t.indexFn != nil {
|
2023-07-29 08:24:44 +00:00
|
|
|
t.indexFn(folder, files)
|
2019-05-25 19:08:07 +00:00
|
|
|
}
|
2019-12-04 09:46:55 +00:00
|
|
|
return nil
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 08:24:44 +00:00
|
|
|
func (*TestModel) IndexUpdate(Connection, string, []FileInfo) error {
|
2019-12-04 09:46:55 +00:00
|
|
|
return nil
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 08:24:44 +00:00
|
|
|
func (t *TestModel) Request(_ Connection, folder, name string, _, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error) {
|
2014-09-28 11:00:38 +00:00
|
|
|
t.folder = folder
|
2014-09-22 19:42:11 +00:00
|
|
|
t.name = name
|
|
|
|
t.offset = offset
|
2018-11-13 07:53:55 +00:00
|
|
|
t.size = size
|
2015-01-24 21:56:12 +00:00
|
|
|
t.hash = hash
|
2018-05-05 08:24:44 +00:00
|
|
|
t.weakHash = weakHash
|
2016-07-04 10:40:29 +00:00
|
|
|
t.fromTemporary = fromTemporary
|
2018-11-13 07:53:55 +00:00
|
|
|
buf := make([]byte, len(t.data))
|
2015-07-29 20:23:43 +00:00
|
|
|
copy(buf, t.data)
|
2018-11-13 07:53:55 +00:00
|
|
|
return &fakeRequestResponse{buf}, nil
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 08:24:44 +00:00
|
|
|
func (t *TestModel) Closed(_ Connection, err error) {
|
2016-01-12 08:19:44 +00:00
|
|
|
t.closedErr = err
|
2014-09-22 19:42:11 +00:00
|
|
|
close(t.closedCh)
|
|
|
|
}
|
|
|
|
|
2023-07-29 08:24:44 +00:00
|
|
|
func (t *TestModel) ClusterConfig(_ Connection, config ClusterConfig) error {
|
2019-06-14 17:04:41 +00:00
|
|
|
if t.ccFn != nil {
|
2023-07-29 08:24:44 +00:00
|
|
|
t.ccFn(config)
|
2019-06-14 17:04:41 +00:00
|
|
|
}
|
2019-12-04 09:46:55 +00:00
|
|
|
return nil
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 08:24:44 +00:00
|
|
|
func (*TestModel) DownloadProgress(Connection, string, []FileDownloadProgressUpdate) error {
|
2019-12-04 09:46:55 +00:00
|
|
|
return nil
|
2016-04-15 10:59:41 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 08:19:44 +00:00
|
|
|
func (t *TestModel) closedError() error {
|
2014-09-22 19:42:11 +00:00
|
|
|
select {
|
|
|
|
case <-t.closedCh:
|
2016-01-12 08:19:44 +00:00
|
|
|
return t.closedErr
|
2014-09-22 19:42:11 +00:00
|
|
|
case <-time.After(1 * time.Second):
|
2016-01-12 08:19:44 +00:00
|
|
|
return nil // Timeout
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-13 07:53:55 +00:00
|
|
|
|
|
|
|
type fakeRequestResponse struct {
|
|
|
|
data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *fakeRequestResponse) Data() []byte {
|
|
|
|
return r.data
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:32:45 +00:00
|
|
|
func (*fakeRequestResponse) Close() {}
|
2018-11-13 07:53:55 +00:00
|
|
|
|
2022-07-28 15:32:45 +00:00
|
|
|
func (*fakeRequestResponse) Wait() {}
|