github.com/outbrain/consul@v1.4.5/lib/rtt_test.go (about) 1 package lib 2 3 import ( 4 "math" 5 "testing" 6 "time" 7 8 "github.com/hashicorp/serf/coordinate" 9 "github.com/pascaldekloe/goe/verify" 10 ) 11 12 func TestRTT_ComputeDistance(t *testing.T) { 13 tests := []struct { 14 desc string 15 a *coordinate.Coordinate 16 b *coordinate.Coordinate 17 dist float64 18 }{ 19 { 20 "10 ms", 21 GenerateCoordinate(0), 22 GenerateCoordinate(10 * time.Millisecond), 23 0.010, 24 }, 25 { 26 "0 ms", 27 GenerateCoordinate(10 * time.Millisecond), 28 GenerateCoordinate(10 * time.Millisecond), 29 0.0, 30 }, 31 { 32 "2 ms", 33 GenerateCoordinate(8 * time.Millisecond), 34 GenerateCoordinate(10 * time.Millisecond), 35 0.002, 36 }, 37 { 38 "2 ms reversed", 39 GenerateCoordinate(10 * time.Millisecond), 40 GenerateCoordinate(8 * time.Millisecond), 41 0.002, 42 }, 43 { 44 "a nil", 45 nil, 46 GenerateCoordinate(8 * time.Millisecond), 47 math.Inf(1.0), 48 }, 49 { 50 "b nil", 51 GenerateCoordinate(8 * time.Millisecond), 52 nil, 53 math.Inf(1.0), 54 }, 55 { 56 "both nil", 57 nil, 58 nil, 59 math.Inf(1.0), 60 }, 61 } 62 for _, tt := range tests { 63 t.Run(tt.desc, func(t *testing.T) { 64 dist := ComputeDistance(tt.a, tt.b) 65 verify.Values(t, "", dist, tt.dist) 66 }) 67 } 68 } 69 70 func TestRTT_Intersect(t *testing.T) { 71 // The numbers here don't matter, we just want a unique coordinate for 72 // each one. 73 server1 := CoordinateSet{ 74 "": GenerateCoordinate(1 * time.Millisecond), 75 "alpha": GenerateCoordinate(2 * time.Millisecond), 76 "beta": GenerateCoordinate(3 * time.Millisecond), 77 } 78 server2 := CoordinateSet{ 79 "": GenerateCoordinate(4 * time.Millisecond), 80 "alpha": GenerateCoordinate(5 * time.Millisecond), 81 "beta": GenerateCoordinate(6 * time.Millisecond), 82 } 83 clientAlpha := CoordinateSet{ 84 "alpha": GenerateCoordinate(7 * time.Millisecond), 85 } 86 clientBeta1 := CoordinateSet{ 87 "beta": GenerateCoordinate(8 * time.Millisecond), 88 } 89 clientBeta2 := CoordinateSet{ 90 "beta": GenerateCoordinate(9 * time.Millisecond), 91 } 92 93 tests := []struct { 94 desc string 95 a CoordinateSet 96 b CoordinateSet 97 c1 *coordinate.Coordinate 98 c2 *coordinate.Coordinate 99 }{ 100 { 101 "nil maps", 102 nil, nil, 103 nil, nil, 104 }, 105 { 106 "two servers", 107 server1, server2, 108 server1[""], server2[""], 109 }, 110 { 111 "two clients", 112 clientBeta1, clientBeta2, 113 clientBeta1["beta"], clientBeta2["beta"], 114 }, 115 { 116 "server1 and client alpha", 117 server1, clientAlpha, 118 server1["alpha"], clientAlpha["alpha"], 119 }, 120 { 121 "server1 and client beta 1", 122 server1, clientBeta1, 123 server1["beta"], clientBeta1["beta"], 124 }, 125 { 126 "server1 and client alpha reversed", 127 clientAlpha, server1, 128 clientAlpha["alpha"], server1["alpha"], 129 }, 130 { 131 "server1 and client beta 1 reversed", 132 clientBeta1, server1, 133 clientBeta1["beta"], server1["beta"], 134 }, 135 { 136 "nothing in common", 137 clientAlpha, clientBeta1, 138 nil, clientBeta1["beta"], 139 }, 140 { 141 "nothing in common reversed", 142 clientBeta1, clientAlpha, 143 nil, clientAlpha["alpha"], 144 }, 145 } 146 for _, tt := range tests { 147 t.Run(tt.desc, func(t *testing.T) { 148 r1, r2 := tt.a.Intersect(tt.b) 149 verify.Values(t, "", r1, tt.c1) 150 verify.Values(t, "", r2, tt.c2) 151 }) 152 } 153 }