blockpullreorderer_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright (C) 2020 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package model
  7. import (
  8. "reflect"
  9. "sort"
  10. "testing"
  11. "github.com/syncthing/syncthing/lib/protocol"
  12. )
  13. var (
  14. someBlocks = []protocol.BlockInfo{{Offset: 1}, {Offset: 2}, {Offset: 3}}
  15. )
  16. func Test_chunk(t *testing.T) {
  17. type args struct {
  18. blocks []protocol.BlockInfo
  19. partCount int
  20. }
  21. tests := []struct {
  22. name string
  23. args args
  24. want [][]protocol.BlockInfo
  25. }{
  26. {"one", args{someBlocks, 1}, [][]protocol.BlockInfo{someBlocks}},
  27. {"two", args{someBlocks, 2}, [][]protocol.BlockInfo{someBlocks[:2], someBlocks[2:]}},
  28. {"three", args{someBlocks, 3}, [][]protocol.BlockInfo{someBlocks[:1], someBlocks[1:2], someBlocks[2:]}},
  29. {"four", args{someBlocks, 4}, [][]protocol.BlockInfo{someBlocks[:1], someBlocks[1:2], someBlocks[2:]}},
  30. // Never happens as myIdx would be -1, so we'd return in order.
  31. {"zero", args{someBlocks, 0}, [][]protocol.BlockInfo{someBlocks}},
  32. {"empty-one", args{nil, 1}, [][]protocol.BlockInfo{}},
  33. {"empty-zero", args{nil, 0}, [][]protocol.BlockInfo{nil}},
  34. }
  35. for _, tt := range tests {
  36. t.Run(tt.name, func(t *testing.T) {
  37. if got := chunk(tt.args.blocks, tt.args.partCount); !reflect.DeepEqual(got, tt.want) {
  38. t.Errorf("chunk() = %v, want %v", got, tt.want)
  39. }
  40. })
  41. }
  42. }
  43. func Test_inOrderBlockPullReorderer_Reorder(t *testing.T) {
  44. tests := []struct {
  45. name string
  46. blocks []protocol.BlockInfo
  47. want []protocol.BlockInfo
  48. }{
  49. {"basic", someBlocks, someBlocks},
  50. }
  51. for _, tt := range tests {
  52. t.Run(tt.name, func(t *testing.T) {
  53. in := inOrderBlockPullReorderer{}
  54. if got := in.Reorder(tt.blocks); !reflect.DeepEqual(got, tt.want) {
  55. t.Errorf("Reorder() = %v, want %v", got, tt.want)
  56. }
  57. })
  58. }
  59. }
  60. func Test_standardBlockPullReorderer_Reorder(t *testing.T) {
  61. // Order the devices, so we know their ordering ahead of time.
  62. devices := []protocol.DeviceID{myID, device1, device2}
  63. sort.Slice(devices, func(i, j int) bool {
  64. return devices[i].Compare(devices[j]) == -1
  65. })
  66. blocks := func(i ...int) []protocol.BlockInfo {
  67. b := make([]protocol.BlockInfo, 0, len(i))
  68. for _, v := range i {
  69. b = append(b, protocol.BlockInfo{Offset: int64(v)})
  70. }
  71. return b
  72. }
  73. tests := []struct {
  74. name string
  75. myId protocol.DeviceID
  76. devices []protocol.DeviceID
  77. blocks []protocol.BlockInfo
  78. want []protocol.BlockInfo
  79. }{
  80. {"front", devices[0], []protocol.DeviceID{devices[1], devices[2]}, blocks(1, 2, 3), blocks(1, 2, 3)},
  81. {"back", devices[2], []protocol.DeviceID{devices[0], devices[1]}, blocks(1, 2, 3), blocks(3, 1, 2)},
  82. {"few-blocks", devices[2], []protocol.DeviceID{devices[0], devices[1]}, blocks(1), blocks(1)},
  83. {"more-than-one-block", devices[1], []protocol.DeviceID{devices[0]}, blocks(1, 2, 3, 4), blocks(3, 4, 1, 2)},
  84. {"empty-blocks", devices[0], []protocol.DeviceID{devices[1]}, blocks(), blocks()},
  85. }
  86. for _, tt := range tests {
  87. t.Run(tt.name, func(t *testing.T) {
  88. p := newStandardBlockPullReorderer(tt.myId, tt.devices)
  89. p.shuffle = func(i interface{}) {} // Noop shuffle
  90. if got := p.Reorder(tt.blocks); !reflect.DeepEqual(got, tt.want) {
  91. t.Errorf("reorderBlocksForDevices() = %v, want %v (my idx: %d, count %d)", got, tt.want, p.myIndex, p.count)
  92. }
  93. })
  94. }
  95. }