syncthing/internal/protocol/common_test.go

77 lines
1.5 KiB
Go
Raw Normal View History

2014-07-12 15:45:33 -07:00
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
// All rights reserved. Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
2014-06-01 13:50:14 -07:00
2013-12-19 16:01:34 -07:00
package protocol
2014-03-22 09:06:15 -07:00
import (
"io"
"time"
)
2013-12-19 16:01:34 -07:00
type TestModel struct {
2014-03-22 09:06:15 -07:00
data []byte
repo string
name string
offset int64
size int
closedCh chan bool
}
func newTestModel() *TestModel {
return &TestModel{
closedCh: make(chan bool),
}
2013-12-19 16:01:34 -07:00
}
func (t *TestModel) Index(nodeID NodeID, repo string, files []FileInfo) {
2013-12-19 16:01:34 -07:00
}
func (t *TestModel) IndexUpdate(nodeID NodeID, repo string, files []FileInfo) {
2013-12-28 08:30:02 -07:00
}
func (t *TestModel) Request(nodeID NodeID, repo, name string, offset int64, size int) ([]byte, error) {
t.repo = repo
2013-12-19 16:01:34 -07:00
t.name = name
t.offset = offset
t.size = size
return t.data, nil
}
func (t *TestModel) Close(nodeID NodeID, err error) {
2014-03-22 09:06:15 -07:00
close(t.closedCh)
}
func (t *TestModel) ClusterConfig(nodeID NodeID, config ClusterConfigMessage) {
}
2014-03-22 09:06:15 -07:00
func (t *TestModel) isClosed() bool {
select {
case <-t.closedCh:
return true
case <-time.After(1 * time.Second):
return false // Timeout
}
2013-12-19 16:01:34 -07: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 05:29:30 -07:00
return e.PipeWriter.Write(data)
2013-12-19 16:01:34 -07:00
}