dialqueue_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright (C) 2021 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 connections
  7. import (
  8. "reflect"
  9. "testing"
  10. "time"
  11. "github.com/syncthing/syncthing/lib/protocol"
  12. )
  13. func TestDialQueueSort(t *testing.T) {
  14. t.Parallel()
  15. t.Run("ByLastSeen", func(t *testing.T) {
  16. t.Parallel()
  17. // Devices seen within the last week or so should be sorted strictly in order.
  18. now := time.Now()
  19. queue := dialQueue{
  20. {id: device1, lastSeen: now.Add(-5 * time.Hour)}, // 1
  21. {id: device2, lastSeen: now.Add(-50 * time.Hour)}, // 3
  22. {id: device3, lastSeen: now.Add(-25 * time.Hour)}, // 2
  23. {id: device4, lastSeen: now.Add(-2 * time.Hour)}, // 0
  24. }
  25. expected := []protocol.ShortID{device4.Short(), device1.Short(), device3.Short(), device2.Short()}
  26. queue.Sort()
  27. if !reflect.DeepEqual(shortDevices(queue), expected) {
  28. t.Error("expected different order")
  29. }
  30. })
  31. t.Run("OldConnections", func(t *testing.T) {
  32. t.Parallel()
  33. // Devices seen long ago should be randomized.
  34. now := time.Now()
  35. queue := dialQueue{
  36. {id: device1, lastSeen: now.Add(-5 * time.Hour)}, // 1
  37. {id: device2, lastSeen: now.Add(-50 * 24 * time.Hour)}, // 2, 3
  38. {id: device3, lastSeen: now.Add(-25 * 24 * time.Hour)}, // 2, 3
  39. {id: device4, lastSeen: now.Add(-2 * time.Hour)}, // 0
  40. }
  41. expected1 := []protocol.ShortID{device4.Short(), device1.Short(), device3.Short(), device2.Short()}
  42. expected2 := []protocol.ShortID{device4.Short(), device1.Short(), device2.Short(), device3.Short()}
  43. var seen1, seen2 int
  44. for i := 0; i < 100; i++ {
  45. queue.Sort()
  46. res := shortDevices(queue)
  47. if reflect.DeepEqual(res, expected1) {
  48. seen1++
  49. continue
  50. }
  51. if reflect.DeepEqual(res, expected2) {
  52. seen2++
  53. continue
  54. }
  55. t.Fatal("expected different order")
  56. }
  57. if seen1 < 10 || seen2 < 10 {
  58. t.Error("expected more even distribution", seen1, seen2)
  59. }
  60. })
  61. t.Run("ShortLivedConnections", func(t *testing.T) {
  62. t.Parallel()
  63. // Short lived connections should be sorted as if they were long ago
  64. now := time.Now()
  65. queue := dialQueue{
  66. {id: device1, lastSeen: now.Add(-5 * time.Hour)}, // 1
  67. {id: device2, lastSeen: now.Add(-3 * time.Hour)}, // 0
  68. {id: device3, lastSeen: now.Add(-25 * 24 * time.Hour)}, // 2, 3
  69. {id: device4, lastSeen: now.Add(-2 * time.Hour), shortLived: true}, // 2, 3
  70. }
  71. expected1 := []protocol.ShortID{device2.Short(), device1.Short(), device3.Short(), device4.Short()}
  72. expected2 := []protocol.ShortID{device2.Short(), device1.Short(), device4.Short(), device3.Short()}
  73. var seen1, seen2 int
  74. for i := 0; i < 100; i++ {
  75. queue.Sort()
  76. res := shortDevices(queue)
  77. if reflect.DeepEqual(res, expected1) {
  78. seen1++
  79. continue
  80. }
  81. if reflect.DeepEqual(res, expected2) {
  82. seen2++
  83. continue
  84. }
  85. t.Fatal("expected different order")
  86. }
  87. if seen1 < 10 || seen2 < 10 {
  88. t.Error("expected more even distribution", seen1, seen2)
  89. }
  90. })
  91. }
  92. func shortDevices(queue dialQueue) []protocol.ShortID {
  93. res := make([]protocol.ShortID, len(queue))
  94. for i, qe := range queue {
  95. res[i] = qe.id.Short()
  96. }
  97. return res
  98. }