aboutsummaryrefslogtreecommitdiff
path: root/lib/protocol/common_test.go
blob: 38019b59046ac328d6e7897a16e9de113e74b546 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (C) 2014 The Protocol Authors.

package protocol

import "time"

type TestModel struct {
	data          []byte
	folder        string
	name          string
	offset        int64
	size          int32
	hash          []byte
	weakHash      uint32
	fromTemporary bool
	indexFn       func(string, []FileInfo)
	ccFn          func(*ClusterConfig)
	closedCh      chan struct{}
	closedErr     error
}

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

func (t *TestModel) Index(_ Connection, idx *Index) error {
	if t.indexFn != nil {
		t.indexFn(idx.Folder, idx.Files)
	}
	return nil
}

func (*TestModel) IndexUpdate(Connection, *IndexUpdate) error {
	return nil
}

func (t *TestModel) Request(_ Connection, req *Request) (RequestResponse, error) {
	t.folder = req.Folder
	t.name = req.Name
	t.offset = req.Offset
	t.size = int32(req.Size)
	t.hash = req.Hash
	t.weakHash = req.WeakHash
	t.fromTemporary = req.FromTemporary
	buf := make([]byte, len(t.data))
	copy(buf, t.data)
	return &fakeRequestResponse{buf}, nil
}

func (t *TestModel) Closed(_ Connection, err error) {
	t.closedErr = err
	close(t.closedCh)
}

func (t *TestModel) ClusterConfig(_ Connection, config *ClusterConfig) error {
	if t.ccFn != nil {
		t.ccFn(config)
	}
	return nil
}

func (*TestModel) DownloadProgress(Connection, *DownloadProgress) error {
	return nil
}

func (t *TestModel) closedError() error {
	select {
	case <-t.closedCh:
		return t.closedErr
	case <-time.After(1 * time.Second):
		return nil // Timeout
	}
}

type fakeRequestResponse struct {
	data []byte
}

func (r *fakeRequestResponse) Data() []byte {
	return r.data
}

func (*fakeRequestResponse) Close() {}

func (*fakeRequestResponse) Wait() {}