syncthing/lib/protocol/common_test.go

84 lines
1.6 KiB
Go
Raw Normal View History

2015-01-13 12:31:14 +00:00
// Copyright (C) 2014 The Protocol Authors.
2014-09-22 19:42:11 +00:00
package protocol
import (
"io"
"time"
)
type TestModel struct {
2016-01-12 08:19:44 +00:00
data []byte
folder string
name string
offset int64
size int
hash []byte
flags uint32
options []Option
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
}
}
func (t *TestModel) Index(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option) {
2014-09-22 19:42:11 +00:00
}
func (t *TestModel) IndexUpdate(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option) {
2014-09-22 19:42:11 +00:00
}
2015-08-09 08:00:28 +00:00
func (t *TestModel) Request(deviceID DeviceID, folder, name string, offset int64, hash []byte, flags uint32, options []Option, buf []byte) error {
t.folder = folder
2014-09-22 19:42:11 +00:00
t.name = name
t.offset = offset
2015-08-09 08:00:28 +00:00
t.size = len(buf)
2015-01-24 21:56:12 +00:00
t.hash = hash
t.flags = flags
t.options = options
2015-07-29 20:23:43 +00:00
copy(buf, t.data)
return nil
2014-09-22 19:42:11 +00:00
}
func (t *TestModel) Close(deviceID DeviceID, err error) {
2016-01-12 08:19:44 +00:00
t.closedErr = err
2014-09-22 19:42:11 +00:00
close(t.closedCh)
}
func (t *TestModel) ClusterConfig(deviceID DeviceID, config ClusterConfigMessage) {
2014-09-22 19:42:11 +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
}
}
type ErrPipe struct {
io.PipeWriter
written int
max int
err error
closed bool
}
func (e *ErrPipe) Write(data []byte) (int, error) {
if e.closed {
return 0, e.err
}
if e.written+len(data) > e.max {
n, _ := e.PipeWriter.Write(data[:e.max-e.written])
e.PipeWriter.CloseWithError(e.err)
e.closed = true
return n, e.err
}
return e.PipeWriter.Write(data)
}