meta_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (C) 2018 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 db
  7. import (
  8. "math/bits"
  9. "sort"
  10. "testing"
  11. "github.com/syncthing/syncthing/lib/protocol"
  12. )
  13. func TestEachFlagBit(t *testing.T) {
  14. cases := []struct {
  15. flags uint32
  16. iterations int
  17. }{
  18. {0, 0},
  19. {1<<0 | 1<<3, 2},
  20. {1 << 0, 1},
  21. {1 << 31, 1},
  22. {1<<10 | 1<<20 | 1<<30, 3},
  23. }
  24. for _, tc := range cases {
  25. var flags uint32
  26. iterations := 0
  27. eachFlagBit(tc.flags, func(f uint32) {
  28. iterations++
  29. flags |= f
  30. if bits.OnesCount32(f) != 1 {
  31. t.Error("expected exactly one bit to be set in every call")
  32. }
  33. })
  34. if flags != tc.flags {
  35. t.Errorf("expected 0x%x flags, got 0x%x", tc.flags, flags)
  36. }
  37. if iterations != tc.iterations {
  38. t.Errorf("expected %d iterations, got %d", tc.iterations, iterations)
  39. }
  40. }
  41. }
  42. func TestMetaDevices(t *testing.T) {
  43. d1 := protocol.DeviceID{1}
  44. d2 := protocol.DeviceID{2}
  45. meta := newMetadataTracker()
  46. meta.addFile(d1, protocol.FileInfo{Sequence: 1})
  47. meta.addFile(d1, protocol.FileInfo{Sequence: 2, LocalFlags: 1})
  48. meta.addFile(d2, protocol.FileInfo{Sequence: 1})
  49. meta.addFile(d2, protocol.FileInfo{Sequence: 2, LocalFlags: 2})
  50. meta.addFile(protocol.LocalDeviceID, protocol.FileInfo{Sequence: 1})
  51. // There are five device/flags combos
  52. if l := len(meta.counts.Counts); l < 5 {
  53. t.Error("expected at least five buckets, not", l)
  54. }
  55. // There are only two non-local devices
  56. devs := meta.devices()
  57. if l := len(devs); l != 2 {
  58. t.Fatal("expected two devices, not", l)
  59. }
  60. // Check that we got the two devices we expect
  61. sort.Slice(devs, func(a, b int) bool {
  62. return devs[a].Compare(devs[b]) == -1
  63. })
  64. if devs[0] != d1 {
  65. t.Error("first device should be d1")
  66. }
  67. if devs[1] != d2 {
  68. t.Error("second device should be d2")
  69. }
  70. }
  71. func TestMetaSequences(t *testing.T) {
  72. d1 := protocol.DeviceID{1}
  73. meta := newMetadataTracker()
  74. meta.addFile(d1, protocol.FileInfo{Sequence: 1})
  75. meta.addFile(d1, protocol.FileInfo{Sequence: 2, RawInvalid: true})
  76. meta.addFile(d1, protocol.FileInfo{Sequence: 3})
  77. meta.addFile(d1, protocol.FileInfo{Sequence: 4, RawInvalid: true})
  78. meta.addFile(protocol.LocalDeviceID, protocol.FileInfo{Sequence: 1})
  79. meta.addFile(protocol.LocalDeviceID, protocol.FileInfo{Sequence: 2})
  80. meta.addFile(protocol.LocalDeviceID, protocol.FileInfo{Sequence: 3, LocalFlags: 1})
  81. meta.addFile(protocol.LocalDeviceID, protocol.FileInfo{Sequence: 4, LocalFlags: 2})
  82. if seq := meta.Sequence(d1); seq != 4 {
  83. t.Error("sequence of first device should be 4, not", seq)
  84. }
  85. if seq := meta.Sequence(protocol.LocalDeviceID); seq != 4 {
  86. t.Error("sequence of first device should be 4, not", seq)
  87. }
  88. }