syncthing/internal/protocol/common_test.go

88 lines
2.0 KiB
Go
Raw Normal View History

2014-07-12 22:45:33 +00:00
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
2014-09-29 19:43:32 +00:00
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along
// with this program. If not, see <http://www.gnu.org/licenses/>.
2014-06-01 20:50:14 +00:00
2013-12-19 23:01:34 +00:00
package protocol
2014-03-22 16:06:15 +00:00
import (
"io"
"time"
)
2013-12-19 23:01:34 +00:00
type TestModel struct {
2014-03-22 16:06:15 +00:00
data []byte
2014-09-28 11:05:25 +00:00
folder string
2014-03-22 16:06:15 +00:00
name string
offset int64
size int
closedCh chan bool
}
func newTestModel() *TestModel {
return &TestModel{
closedCh: make(chan bool),
}
2013-12-19 23:01:34 +00:00
}
func (t *TestModel) Index(deviceID DeviceID, folder string, files []FileInfo) {
2013-12-19 23:01:34 +00:00
}
func (t *TestModel) IndexUpdate(deviceID DeviceID, folder string, files []FileInfo) {
2013-12-28 15:30:02 +00:00
}
func (t *TestModel) Request(deviceID DeviceID, folder, name string, offset int64, size int) ([]byte, error) {
t.folder = folder
2013-12-19 23:01:34 +00:00
t.name = name
t.offset = offset
t.size = size
return t.data, nil
}
func (t *TestModel) Close(deviceID DeviceID, err error) {
2014-03-22 16:06:15 +00:00
close(t.closedCh)
}
func (t *TestModel) ClusterConfig(deviceID DeviceID, config ClusterConfigMessage) {
}
2014-03-22 16:06:15 +00:00
func (t *TestModel) isClosed() bool {
select {
case <-t.closedCh:
return true
case <-time.After(1 * time.Second):
return false // Timeout
}
2013-12-19 23:01:34 +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
}
2014-02-24 12:29:30 +00:00
return e.PipeWriter.Write(data)
2013-12-19 23:01:34 +00:00
}