MCPcopy Index your code
hub / github.com/syncthing/syncthing / TestDialQueueSort

Function TestDialQueueSort

lib/connections/dialqueue_test.go:17–111  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

15)
16
17func TestDialQueueSort(t *testing.T) {
18 t.Parallel()
19
20 t.Run("ByLastSeen", func(t *testing.T) {
21 t.Parallel()
22
23 // Devices seen within the last week or so should be sorted strictly in order.
24 now := time.Now()
25 queue := dialQueue{
26 {id: device1, lastSeen: now.Add(-5 * time.Hour)}, // 1
27 {id: device2, lastSeen: now.Add(-50 * time.Hour)}, // 3
28 {id: device3, lastSeen: now.Add(-25 * time.Hour)}, // 2
29 {id: device4, lastSeen: now.Add(-2 * time.Hour)}, // 0
30 }
31 expected := []protocol.ShortID{device4.Short(), device1.Short(), device3.Short(), device2.Short()}
32
33 queue.Sort()
34
35 if !reflect.DeepEqual(shortDevices(queue), expected) {
36 t.Error("expected different order")
37 }
38 })
39
40 t.Run("OldConnections", func(t *testing.T) {
41 t.Parallel()
42
43 // Devices seen long ago should be randomized.
44 now := time.Now()
45 queue := dialQueue{
46 {id: device1, lastSeen: now.Add(-5 * time.Hour)}, // 1
47 {id: device2, lastSeen: now.Add(-50 * 24 * time.Hour)}, // 2, 3
48 {id: device3, lastSeen: now.Add(-25 * 24 * time.Hour)}, // 2, 3
49 {id: device4, lastSeen: now.Add(-2 * time.Hour)}, // 0
50 }
51
52 expected1 := []protocol.ShortID{device4.Short(), device1.Short(), device3.Short(), device2.Short()}
53 expected2 := []protocol.ShortID{device4.Short(), device1.Short(), device2.Short(), device3.Short()}
54
55 var seen1, seen2 int
56
57 for i := 0; i < 100; i++ {
58 queue.Sort()
59 res := shortDevices(queue)
60 if reflect.DeepEqual(res, expected1) {
61 seen1++
62 continue
63 }
64 if reflect.DeepEqual(res, expected2) {
65 seen2++
66 continue
67 }
68 t.Fatal("expected different order")
69 }
70
71 if seen1 < 10 || seen2 < 10 {
72 t.Error("expected more even distribution", seen1, seen2)
73 }
74 })

Callers

nothing calls this directly

Calls 8

SortMethod · 0.95
shortDevicesFunction · 0.85
ShortMethod · 0.80
FatalMethod · 0.80
NowMethod · 0.65
ErrorMethod · 0.65
RunMethod · 0.45
AddMethod · 0.45

Tested by

no test coverage detected