File: devicedownloadstate_test.go

package info (click to toggle)
syncthing 0.14.18%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 7,388 kB
  • ctags: 4,608
  • sloc: xml: 781; sh: 271; makefile: 45
file content (111 lines) | stat: -rw-r--r-- 4,685 bytes parent folder | download
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (C) 2016 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.

package model

import (
	"testing"

	"github.com/syncthing/syncthing/lib/protocol"
)

func TestDeviceDownloadState(t *testing.T) {
	v1 := (protocol.Vector{}).Update(0)
	v2 := (protocol.Vector{}).Update(1)

	// file 1 version 1 part 1
	f1v1p1 := protocol.FileDownloadProgressUpdate{UpdateType: protocol.UpdateTypeAppend, Name: "f1", Version: v1, BlockIndexes: []int32{0, 1, 2}}
	f1v1p2 := protocol.FileDownloadProgressUpdate{UpdateType: protocol.UpdateTypeAppend, Name: "f1", Version: v1, BlockIndexes: []int32{3, 4, 5}}
	f1v1del := protocol.FileDownloadProgressUpdate{UpdateType: protocol.UpdateTypeForget, Name: "f1", Version: v1, BlockIndexes: nil}
	f1v2p1 := protocol.FileDownloadProgressUpdate{UpdateType: protocol.UpdateTypeAppend, Name: "f1", Version: v2, BlockIndexes: []int32{10, 11, 12}}
	f1v2p2 := protocol.FileDownloadProgressUpdate{UpdateType: protocol.UpdateTypeAppend, Name: "f1", Version: v2, BlockIndexes: []int32{13, 14, 15}}
	f1v2del := protocol.FileDownloadProgressUpdate{UpdateType: protocol.UpdateTypeForget, Name: "f1", Version: v2, BlockIndexes: nil}

	f2v1p1 := protocol.FileDownloadProgressUpdate{UpdateType: protocol.UpdateTypeAppend, Name: "f2", Version: v1, BlockIndexes: []int32{20, 21, 22}}
	f2v1p2 := protocol.FileDownloadProgressUpdate{UpdateType: protocol.UpdateTypeAppend, Name: "f2", Version: v1, BlockIndexes: []int32{23, 24, 25}}
	f2v1del := protocol.FileDownloadProgressUpdate{UpdateType: protocol.UpdateTypeForget, Name: "f2", Version: v1, BlockIndexes: nil}

	tests := []struct {
		updates                  []protocol.FileDownloadProgressUpdate
		shouldHaveIndexesFrom    []protocol.FileDownloadProgressUpdate
		shouldNotHaveIndexesFrom []protocol.FileDownloadProgressUpdate
	}{
		{ //1
			[]protocol.FileDownloadProgressUpdate{f1v1p1},
			[]protocol.FileDownloadProgressUpdate{f1v1p1},
			[]protocol.FileDownloadProgressUpdate{f1v1p2, f1v2p1, f1v2p2},
		},
		{ //2
			[]protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2},
			[]protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2},
			[]protocol.FileDownloadProgressUpdate{f1v2p1, f1v2p2},
		},
		{ //3
			[]protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2, f1v1del},
			nil,
			[]protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2, f1v2p1, f1v2p2}},
		{ //4
			[]protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2, f1v2del},
			[]protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2},
			[]protocol.FileDownloadProgressUpdate{f1v2p1, f1v2p2},
		},
		{ //5
			// v2 replaces old v1 data
			[]protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2, f1v2p1},
			[]protocol.FileDownloadProgressUpdate{f1v2p1},
			[]protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2, f1v2p2},
		},
		{ //6
			// v1 delete on v2 data does nothing
			[]protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2, f1v2p1, f1v1del},
			[]protocol.FileDownloadProgressUpdate{f1v2p1},
			[]protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2, f1v2p2},
		},
		{ //7
			// v2 replacees v1, v2 gets deleted, and v2 part 2 gets added.
			[]protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2, f1v2p1, f1v2del, f1v2p2},
			[]protocol.FileDownloadProgressUpdate{f1v2p2},
			[]protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2, f1v2p1},
		},
		// Multiple files in one go
		{ //8
			[]protocol.FileDownloadProgressUpdate{f1v1p1, f2v1p1},
			[]protocol.FileDownloadProgressUpdate{f1v1p1, f2v1p1},
			[]protocol.FileDownloadProgressUpdate{f1v1p2, f2v1p2},
		},
		{ //9
			[]protocol.FileDownloadProgressUpdate{f1v1p1, f2v1p1, f2v1del},
			[]protocol.FileDownloadProgressUpdate{f1v1p1},
			[]protocol.FileDownloadProgressUpdate{f2v1p1, f2v1p1},
		},
		{ //10
			[]protocol.FileDownloadProgressUpdate{f1v1p1, f2v1del, f2v1p1},
			[]protocol.FileDownloadProgressUpdate{f1v1p1, f2v1p1},
			[]protocol.FileDownloadProgressUpdate{f1v1p2, f2v1p2},
		},
	}

	for i, test := range tests {
		s := newDeviceDownloadState()
		s.Update("folder", test.updates)

		for _, expected := range test.shouldHaveIndexesFrom {
			for _, n := range expected.BlockIndexes {
				if !s.Has("folder", expected.Name, expected.Version, n) {
					t.Error("Test", i+1, "error:", expected.Name, expected.Version, "missing", n)
				}
			}
		}

		for _, unexpected := range test.shouldNotHaveIndexesFrom {
			for _, n := range unexpected.BlockIndexes {
				if s.Has("folder", unexpected.Name, unexpected.Version, n) {
					t.Error("Test", i+1, "error:", unexpected.Name, unexpected.Version, "has extra", n)
				}
			}
		}
	}
}