summaryrefslogtreecommitdiffstats
path: root/protocol/common_test.go
blob: 143039ea4ac512a594465047e65efc3142c1786e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package protocol

import (
	"io"
	"time"
)

type TestModel struct {
	data     []byte
	repo     string
	name     string
	offset   int64
	size     int
	closedCh chan bool
}

func newTestModel() *TestModel {
	return &TestModel{
		closedCh: make(chan bool),
	}
}

func (t *TestModel) Index(nodeID string, repo string, files []FileInfo) {
}

func (t *TestModel) IndexUpdate(nodeID string, repo string, files []FileInfo) {
}

func (t *TestModel) Request(nodeID, repo, name string, offset int64, size int) ([]byte, error) {
	t.repo = repo
	t.name = name
	t.offset = offset
	t.size = size
	return t.data, nil
}

func (t *TestModel) Close(nodeID string, err error) {
	close(t.closedCh)
}

func (t *TestModel) ClusterConfig(nodeID string, config ClusterConfigMessage) {
}

func (t *TestModel) isClosed() bool {
	select {
	case <-t.closedCh:
		return true
	case <-time.After(1 * time.Second):
		return false // Timeout
	}
}

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)
}