syncthing/lib/protocol/common_test.go

88 lines
1.9 KiB
Go
Raw Normal View History

2015-01-13 13:31:14 +01:00
// Copyright (C) 2014 The Protocol Authors.
2014-09-22 21:42:11 +02:00
package protocol
2016-04-09 03:10:31 +02:00
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
weakHash uint32
fromTemporary bool
indexFn func(DeviceID, string, []FileInfo)
ccFn func(DeviceID, 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
}
}
func (t *TestModel) Index(deviceID DeviceID, folder string, files []FileInfo) error {
if t.indexFn != nil {
t.indexFn(deviceID, folder, files)
}
return nil
2014-09-22 21:42:11 +02:00
}
func (t *TestModel) IndexUpdate(deviceID DeviceID, folder string, files []FileInfo) error {
return nil
2014-09-22 21:42:11 +02:00
}
func (t *TestModel) Request(deviceID DeviceID, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error) {
t.folder = folder
2014-09-22 21:42:11 +02:00
t.name = name
t.offset = offset
t.size = size
2015-01-24 22:56:12 +01:00
t.hash = hash
t.weakHash = weakHash
t.fromTemporary = fromTemporary
buf := make([]byte, len(t.data))
2015-07-29 22:23:43 +02:00
copy(buf, t.data)
return &fakeRequestResponse{buf}, nil
2014-09-22 21:42:11 +02:00
}
func (t *TestModel) Closed(conn 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)
}
func (t *TestModel) ClusterConfig(deviceID DeviceID, config ClusterConfig) error {
if t.ccFn != nil {
t.ccFn(deviceID, config)
}
return nil
2014-09-22 21:42:11 +02:00
}
func (t *TestModel) DownloadProgress(DeviceID, string, []FileDownloadProgressUpdate) 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
}
func (r *fakeRequestResponse) Close() {}
func (r *fakeRequestResponse) Wait() {}