2017-03-14 23:39:00 +00:00
|
|
|
package lib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/serf/coordinate"
|
2020-06-02 16:41:25 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-03-14 23:39:00 +00:00
|
|
|
)
|
|
|
|
|
2017-08-14 14:36:07 +00:00
|
|
|
func TestRTT_ComputeDistance(t *testing.T) {
|
|
|
|
tests := []struct {
|
2020-06-02 16:41:25 +00:00
|
|
|
desc string
|
|
|
|
a *coordinate.Coordinate
|
|
|
|
b *coordinate.Coordinate
|
|
|
|
expected float64
|
2017-03-14 23:39:00 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-08-14 14:36:07 +00:00
|
|
|
"10 ms",
|
2017-03-14 23:39:00 +00:00
|
|
|
GenerateCoordinate(0),
|
|
|
|
GenerateCoordinate(10 * time.Millisecond),
|
|
|
|
0.010,
|
|
|
|
},
|
|
|
|
{
|
2017-08-14 14:36:07 +00:00
|
|
|
"0 ms",
|
2017-03-14 23:39:00 +00:00
|
|
|
GenerateCoordinate(10 * time.Millisecond),
|
|
|
|
GenerateCoordinate(10 * time.Millisecond),
|
|
|
|
0.0,
|
|
|
|
},
|
|
|
|
{
|
2017-08-14 14:36:07 +00:00
|
|
|
"2 ms",
|
2017-03-14 23:39:00 +00:00
|
|
|
GenerateCoordinate(8 * time.Millisecond),
|
|
|
|
GenerateCoordinate(10 * time.Millisecond),
|
|
|
|
0.002,
|
|
|
|
},
|
|
|
|
{
|
2017-08-14 14:36:07 +00:00
|
|
|
"2 ms reversed",
|
2017-03-14 23:39:00 +00:00
|
|
|
GenerateCoordinate(10 * time.Millisecond),
|
|
|
|
GenerateCoordinate(8 * time.Millisecond),
|
|
|
|
0.002,
|
|
|
|
},
|
|
|
|
{
|
2017-08-14 14:36:07 +00:00
|
|
|
"a nil",
|
2017-03-14 23:39:00 +00:00
|
|
|
nil,
|
|
|
|
GenerateCoordinate(8 * time.Millisecond),
|
|
|
|
math.Inf(1.0),
|
|
|
|
},
|
|
|
|
{
|
2017-08-14 14:36:07 +00:00
|
|
|
"b nil",
|
2017-03-14 23:39:00 +00:00
|
|
|
GenerateCoordinate(8 * time.Millisecond),
|
|
|
|
nil,
|
|
|
|
math.Inf(1.0),
|
|
|
|
},
|
2017-08-14 14:36:07 +00:00
|
|
|
{
|
|
|
|
"both nil",
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
math.Inf(1.0),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.desc, func(t *testing.T) {
|
2020-06-02 16:41:25 +00:00
|
|
|
actual := ComputeDistance(tt.a, tt.b)
|
|
|
|
require.Equal(t, tt.expected, actual)
|
2017-08-14 14:36:07 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRTT_Intersect(t *testing.T) {
|
|
|
|
// The numbers here don't matter, we just want a unique coordinate for
|
|
|
|
// each one.
|
2018-01-28 18:40:13 +00:00
|
|
|
server1 := CoordinateSet{
|
2017-08-14 14:36:07 +00:00
|
|
|
"": GenerateCoordinate(1 * time.Millisecond),
|
|
|
|
"alpha": GenerateCoordinate(2 * time.Millisecond),
|
|
|
|
"beta": GenerateCoordinate(3 * time.Millisecond),
|
|
|
|
}
|
2018-01-28 18:40:13 +00:00
|
|
|
server2 := CoordinateSet{
|
2017-08-14 14:36:07 +00:00
|
|
|
"": GenerateCoordinate(4 * time.Millisecond),
|
|
|
|
"alpha": GenerateCoordinate(5 * time.Millisecond),
|
|
|
|
"beta": GenerateCoordinate(6 * time.Millisecond),
|
|
|
|
}
|
2018-01-28 18:40:13 +00:00
|
|
|
clientAlpha := CoordinateSet{
|
2017-08-14 14:36:07 +00:00
|
|
|
"alpha": GenerateCoordinate(7 * time.Millisecond),
|
|
|
|
}
|
2018-01-28 18:40:13 +00:00
|
|
|
clientBeta1 := CoordinateSet{
|
2017-08-14 14:36:07 +00:00
|
|
|
"beta": GenerateCoordinate(8 * time.Millisecond),
|
|
|
|
}
|
2018-01-28 18:40:13 +00:00
|
|
|
clientBeta2 := CoordinateSet{
|
2017-08-14 14:36:07 +00:00
|
|
|
"beta": GenerateCoordinate(9 * time.Millisecond),
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
a CoordinateSet
|
|
|
|
b CoordinateSet
|
|
|
|
c1 *coordinate.Coordinate
|
|
|
|
c2 *coordinate.Coordinate
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"nil maps",
|
|
|
|
nil, nil,
|
|
|
|
nil, nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"two servers",
|
2018-01-28 18:40:13 +00:00
|
|
|
server1, server2,
|
|
|
|
server1[""], server2[""],
|
2017-08-14 14:36:07 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"two clients",
|
2018-01-28 18:40:13 +00:00
|
|
|
clientBeta1, clientBeta2,
|
|
|
|
clientBeta1["beta"], clientBeta2["beta"],
|
2017-08-14 14:36:07 +00:00
|
|
|
},
|
|
|
|
{
|
2018-01-28 18:40:13 +00:00
|
|
|
"server1 and client alpha",
|
|
|
|
server1, clientAlpha,
|
|
|
|
server1["alpha"], clientAlpha["alpha"],
|
2017-08-14 14:36:07 +00:00
|
|
|
},
|
|
|
|
{
|
2018-01-28 18:40:13 +00:00
|
|
|
"server1 and client beta 1",
|
|
|
|
server1, clientBeta1,
|
|
|
|
server1["beta"], clientBeta1["beta"],
|
2017-08-14 14:36:07 +00:00
|
|
|
},
|
|
|
|
{
|
2018-01-28 18:40:13 +00:00
|
|
|
"server1 and client alpha reversed",
|
|
|
|
clientAlpha, server1,
|
|
|
|
clientAlpha["alpha"], server1["alpha"],
|
2017-08-14 14:36:07 +00:00
|
|
|
},
|
|
|
|
{
|
2018-01-28 18:40:13 +00:00
|
|
|
"server1 and client beta 1 reversed",
|
|
|
|
clientBeta1, server1,
|
|
|
|
clientBeta1["beta"], server1["beta"],
|
2017-08-14 14:36:07 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"nothing in common",
|
2018-01-28 18:40:13 +00:00
|
|
|
clientAlpha, clientBeta1,
|
|
|
|
nil, clientBeta1["beta"],
|
2017-08-14 14:36:07 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"nothing in common reversed",
|
2018-01-28 18:40:13 +00:00
|
|
|
clientBeta1, clientAlpha,
|
|
|
|
nil, clientAlpha["alpha"],
|
2017-08-14 14:36:07 +00:00
|
|
|
},
|
2017-03-14 23:39:00 +00:00
|
|
|
}
|
2017-08-14 14:36:07 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.desc, func(t *testing.T) {
|
|
|
|
r1, r2 := tt.a.Intersect(tt.b)
|
2020-06-02 16:41:25 +00:00
|
|
|
|
|
|
|
require.Equal(t, tt.c1, r1)
|
|
|
|
require.Equal(t, tt.c2, r2)
|
2017-08-14 14:36:07 +00:00
|
|
|
})
|
2017-03-14 23:39:00 +00:00
|
|
|
}
|
|
|
|
}
|