consul/internal/gossip/librtt/rtt_test.go

159 lines
3.1 KiB
Go
Raw Permalink Normal View History

// Copyright (c) HashiCorp, Inc.
[COMPLIANCE] License changes (#18443) * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2023-08-11 13:12:13 +00:00
// SPDX-License-Identifier: BUSL-1.1
package librtt
import (
"math"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/hashicorp/serf/coordinate"
)
func TestRTT_ComputeDistance(t *testing.T) {
tests := []struct {
desc string
a *coordinate.Coordinate
b *coordinate.Coordinate
expected float64
}{
{
"10 ms",
GenerateCoordinate(0),
GenerateCoordinate(10 * time.Millisecond),
0.010,
},
{
"0 ms",
GenerateCoordinate(10 * time.Millisecond),
GenerateCoordinate(10 * time.Millisecond),
0.0,
},
{
"2 ms",
GenerateCoordinate(8 * time.Millisecond),
GenerateCoordinate(10 * time.Millisecond),
0.002,
},
{
"2 ms reversed",
GenerateCoordinate(10 * time.Millisecond),
GenerateCoordinate(8 * time.Millisecond),
0.002,
},
{
"a nil",
nil,
GenerateCoordinate(8 * time.Millisecond),
math.Inf(1.0),
},
{
"b nil",
GenerateCoordinate(8 * time.Millisecond),
nil,
math.Inf(1.0),
},
{
"both nil",
nil,
nil,
math.Inf(1.0),
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
actual := ComputeDistance(tt.a, tt.b)
require.Equal(t, tt.expected, actual)
})
}
}
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{
"": GenerateCoordinate(1 * time.Millisecond),
"alpha": GenerateCoordinate(2 * time.Millisecond),
"beta": GenerateCoordinate(3 * time.Millisecond),
}
2018-01-28 18:40:13 +00:00
server2 := CoordinateSet{
"": GenerateCoordinate(4 * time.Millisecond),
"alpha": GenerateCoordinate(5 * time.Millisecond),
"beta": GenerateCoordinate(6 * time.Millisecond),
}
2018-01-28 18:40:13 +00:00
clientAlpha := CoordinateSet{
"alpha": GenerateCoordinate(7 * time.Millisecond),
}
2018-01-28 18:40:13 +00:00
clientBeta1 := CoordinateSet{
"beta": GenerateCoordinate(8 * time.Millisecond),
}
2018-01-28 18:40:13 +00:00
clientBeta2 := CoordinateSet{
"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[""],
},
{
"two clients",
2018-01-28 18:40:13 +00:00
clientBeta1, clientBeta2,
clientBeta1["beta"], clientBeta2["beta"],
},
{
2018-01-28 18:40:13 +00:00
"server1 and client alpha",
server1, clientAlpha,
server1["alpha"], clientAlpha["alpha"],
},
{
2018-01-28 18:40:13 +00:00
"server1 and client beta 1",
server1, clientBeta1,
server1["beta"], clientBeta1["beta"],
},
{
2018-01-28 18:40:13 +00:00
"server1 and client alpha reversed",
clientAlpha, server1,
clientAlpha["alpha"], server1["alpha"],
},
{
2018-01-28 18:40:13 +00:00
"server1 and client beta 1 reversed",
clientBeta1, server1,
clientBeta1["beta"], server1["beta"],
},
{
"nothing in common",
2018-01-28 18:40:13 +00:00
clientAlpha, clientBeta1,
nil, clientBeta1["beta"],
},
{
"nothing in common reversed",
2018-01-28 18:40:13 +00:00
clientBeta1, clientAlpha,
nil, clientAlpha["alpha"],
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
r1, r2 := tt.a.Intersect(tt.b)
require.Equal(t, tt.c1, r1)
require.Equal(t, tt.c2, r2)
})
}
}