blockpullreorderer_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. "slices"
  10. "testing"
  11. "github.com/syncthing/syncthing/lib/protocol"
  12. )
  13. var someBlocks = []protocol.BlockInfo{{Offset: 1}, {Offset: 2}, {Offset: 3}}
  14. func Test_chunk(t *testing.T) {
  15. type args struct {
  16. blocks []protocol.BlockInfo
  17. partCount int
  18. }
  19. tests := []struct {
  20. name string
  21. args args
  22. want [][]protocol.BlockInfo
  23. }{
  24. {"one", args{someBlocks, 1}, [][]protocol.BlockInfo{someBlocks}},
  25. {"two", args{someBlocks, 2}, [][]protocol.BlockInfo{someBlocks[:2], someBlocks[2:]}},
  26. {"three", args{someBlocks, 3}, [][]protocol.BlockInfo{someBlocks[:1], someBlocks[1:2], someBlocks[2:]}},
  27. {"four", args{someBlocks, 4}, [][]protocol.BlockInfo{someBlocks[:1], someBlocks[1:2], someBlocks[2:]}},
  28. // Never happens as myIdx would be -1, so we'd return in order.
  29. {"zero", args{someBlocks, 0}, [][]protocol.BlockInfo{someBlocks}},
  30. {"empty-one", args{nil, 1}, [][]protocol.BlockInfo{}},
  31. {"empty-zero", args{nil, 0}, [][]protocol.BlockInfo{nil}},
  32. }
  33. for _, tt := range tests {
  34. t.Run(tt.name, func(t *testing.T) {
  35. if got := chunk(tt.args.blocks, tt.args.partCount); !reflect.DeepEqual(got, tt.want) {
  36. t.Errorf("chunk() = %v, want %v", got, tt.want)
  37. }
  38. })
  39. }
  40. }
  41. func Test_inOrderBlockPullReorderer_Reorder(t *testing.T) {
  42. tests := []struct {
  43. name string
  44. blocks []protocol.BlockInfo
  45. want []protocol.BlockInfo
  46. }{
  47. {"basic", someBlocks, someBlocks},
  48. }
  49. for _, tt := range tests {
  50. t.Run(tt.name, func(t *testing.T) {
  51. in := inOrderBlockPullReorderer{}
  52. if got := in.Reorder(tt.blocks); !reflect.DeepEqual(got, tt.want) {
  53. t.Errorf("Reorder() = %v, want %v", got, tt.want)
  54. }
  55. })
  56. }
  57. }
  58. func Test_standardBlockPullReorderer_Reorder(t *testing.T) {
  59. // Order the devices, so we know their ordering ahead of time.
  60. devices := []protocol.DeviceID{myID, device1, device2}
  61. slices.SortFunc(devices, func(a, b protocol.DeviceID) int {
  62. return a.Compare(b)
  63. })
  64. blocks := func(i ...int) []protocol.BlockInfo {
  65. b := make([]protocol.BlockInfo, 0, len(i))
  66. for _, v := range i {
  67. b = append(b, protocol.BlockInfo{Offset: int64(v)})
  68. }
  69. return b
  70. }
  71. tests := []struct {
  72. name string
  73. myId protocol.DeviceID
  74. devices []protocol.DeviceID
  75. blocks []protocol.BlockInfo
  76. want []protocol.BlockInfo
  77. }{
  78. {"front", devices[0], []protocol.DeviceID{devices[1], devices[2]}, blocks(1, 2, 3), blocks(1, 2, 3)},
  79. {"back", devices[2], []protocol.DeviceID{devices[0], devices[1]}, blocks(1, 2, 3), blocks(3, 1, 2)},
  80. {"few-blocks", devices[2], []protocol.DeviceID{devices[0], devices[1]}, blocks(1), blocks(1)},
  81. {"more-than-one-block", devices[1], []protocol.DeviceID{devices[0]}, blocks(1, 2, 3, 4), blocks(3, 4, 1, 2)},
  82. {"empty-blocks", devices[0], []protocol.DeviceID{devices[1]}, blocks(), blocks()},
  83. }
  84. for _, tt := range tests {
  85. t.Run(tt.name, func(t *testing.T) {
  86. p := newStandardBlockPullReorderer(tt.myId, tt.devices)
  87. p.shuffle = func(i interface{}) {} // Noop shuffle
  88. if got := p.Reorder(tt.blocks); !reflect.DeepEqual(got, tt.want) {
  89. t.Errorf("reorderBlocksForDevices() = %v, want %v (my idx: %d, count %d)", got, tt.want, p.myIndex, p.count)
  90. }
  91. })
  92. }
  93. }