mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-06-13 21:54:13 +08:00
fix rttStorage test
This commit is contained in:
parent
dd70b3f6af
commit
bc01961d34
@ -90,10 +90,9 @@ func (h *rttStorage) calcIndex(step int) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *rttStorage) getStatistics() RTTStats {
|
func (h *rttStorage) getStatistics() RTTStats {
|
||||||
stats := RTTStats{}
|
stats := RTTStats{
|
||||||
stats.Fail = 0
|
Min: math.MaxInt64,
|
||||||
stats.Max = 0
|
}
|
||||||
stats.Min = rttFailed
|
|
||||||
sum := time.Duration(0)
|
sum := time.Duration(0)
|
||||||
cnt := 0
|
cnt := 0
|
||||||
validRTTs := make([]time.Duration, 0, h.cap)
|
validRTTs := make([]time.Duration, 0, h.cap)
|
||||||
@ -116,11 +115,9 @@ func (h *rttStorage) getStatistics() RTTStats {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
stats.All = cnt + stats.Fail
|
stats.All = cnt + stats.Fail
|
||||||
if cnt == 0 {
|
if cnt > 0 {
|
||||||
stats.Min = 0
|
stats.Average = time.Duration(int(sum) / cnt)
|
||||||
return healthPingStatsUntested
|
|
||||||
}
|
}
|
||||||
stats.Average = time.Duration(int(sum) / cnt)
|
|
||||||
switch {
|
switch {
|
||||||
case stats.All == 0:
|
case stats.All == 0:
|
||||||
return healthPingStatsUntested
|
return healthPingStatsUntested
|
||||||
|
@ -9,12 +9,12 @@ import (
|
|||||||
|
|
||||||
func TestRTTStorage(t *testing.T) {
|
func TestRTTStorage(t *testing.T) {
|
||||||
rtts := []int64{60, 140, 60, 140, 60, 60, 140, 60, 140}
|
rtts := []int64{60, 140, 60, 140, 60, 60, 140, 60, 140}
|
||||||
hr := newRTTStorage(4, time.Hour)
|
s := newRTTStorage(4, time.Hour)
|
||||||
for _, rtt := range rtts {
|
for _, rtt := range rtts {
|
||||||
hr.Put(time.Duration(rtt))
|
s.Put(time.Duration(rtt))
|
||||||
}
|
}
|
||||||
rttFailed := time.Duration(math.MaxInt64)
|
rttFailed := time.Duration(math.MaxInt64)
|
||||||
expected := &RTTStats{
|
want := RTTStats{
|
||||||
All: 4,
|
All: 4,
|
||||||
Fail: 0,
|
Fail: 0,
|
||||||
Deviation: 40,
|
Deviation: 40,
|
||||||
@ -22,45 +22,45 @@ func TestRTTStorage(t *testing.T) {
|
|||||||
Max: 140,
|
Max: 140,
|
||||||
Min: 60,
|
Min: 60,
|
||||||
}
|
}
|
||||||
actual := hr.Get()
|
got := s.Get()
|
||||||
if !reflect.DeepEqual(expected, actual) {
|
if !reflect.DeepEqual(want, got) {
|
||||||
t.Errorf("expected: %v, actual: %v", expected, actual)
|
t.Errorf("want: %v, got: %v", want, got)
|
||||||
}
|
}
|
||||||
hr.Put(rttFailed)
|
s.Put(rttFailed)
|
||||||
hr.Put(rttFailed)
|
s.Put(rttFailed)
|
||||||
expected.Fail = 2
|
want.Fail = 2
|
||||||
actual = hr.Get()
|
got = s.Get()
|
||||||
if !reflect.DeepEqual(expected, actual) {
|
if !reflect.DeepEqual(want, got) {
|
||||||
t.Errorf("failed half-failures test, expected: %v, actual: %v", expected, actual)
|
t.Errorf("failed half-failures test, want: %v, got: %v", want, got)
|
||||||
}
|
}
|
||||||
hr.Put(rttFailed)
|
s.Put(rttFailed)
|
||||||
hr.Put(rttFailed)
|
s.Put(rttFailed)
|
||||||
expected = &RTTStats{
|
want = RTTStats{
|
||||||
All: 4,
|
All: 4,
|
||||||
Fail: 4,
|
Fail: 4,
|
||||||
Deviation: 0,
|
Deviation: rttFailed,
|
||||||
Average: 0,
|
Average: rttFailed,
|
||||||
Max: 0,
|
Max: rttFailed,
|
||||||
Min: 0,
|
Min: rttFailed,
|
||||||
}
|
}
|
||||||
actual = hr.Get()
|
got = s.Get()
|
||||||
if !reflect.DeepEqual(expected, actual) {
|
if !reflect.DeepEqual(want, got) {
|
||||||
t.Errorf("failed all-failures test, expected: %v, actual: %v", expected, actual)
|
t.Errorf("failed all-failures test, want: %v, got: %v", want, got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHealthPingResultsIgnoreOutdated(t *testing.T) {
|
func TestHealthPingResultsIgnoreOutdated(t *testing.T) {
|
||||||
rtts := []int64{60, 140, 60, 140}
|
rtts := []int64{60, 140, 60, 140}
|
||||||
hr := newRTTStorage(4, time.Duration(10)*time.Millisecond)
|
s := newRTTStorage(4, time.Duration(10)*time.Millisecond)
|
||||||
for i, rtt := range rtts {
|
for i, rtt := range rtts {
|
||||||
if i == 2 {
|
if i == 2 {
|
||||||
// wait for previous 2 outdated
|
// wait for previous 2 outdated
|
||||||
time.Sleep(time.Duration(10) * time.Millisecond)
|
time.Sleep(time.Duration(10) * time.Millisecond)
|
||||||
}
|
}
|
||||||
hr.Put(time.Duration(rtt))
|
s.Put(time.Duration(rtt))
|
||||||
}
|
}
|
||||||
hr.Get()
|
s.Get()
|
||||||
expected := &RTTStats{
|
want := RTTStats{
|
||||||
All: 2,
|
All: 2,
|
||||||
Fail: 0,
|
Fail: 0,
|
||||||
Deviation: 40,
|
Deviation: 40,
|
||||||
@ -68,27 +68,27 @@ func TestHealthPingResultsIgnoreOutdated(t *testing.T) {
|
|||||||
Max: 140,
|
Max: 140,
|
||||||
Min: 60,
|
Min: 60,
|
||||||
}
|
}
|
||||||
actual := hr.Get()
|
got := s.Get()
|
||||||
if !reflect.DeepEqual(expected, actual) {
|
if !reflect.DeepEqual(want, got) {
|
||||||
t.Errorf("failed 'half-outdated' test, expected: %v, actual: %v", expected, actual)
|
t.Errorf("failed 'half-outdated' test, want: %v, got: %v", want, got)
|
||||||
}
|
}
|
||||||
// wait for all outdated
|
// wait for all outdated
|
||||||
time.Sleep(time.Duration(10) * time.Millisecond)
|
time.Sleep(time.Duration(10) * time.Millisecond)
|
||||||
expected = &RTTStats{
|
want = RTTStats{
|
||||||
All: 0,
|
All: 0,
|
||||||
Fail: 0,
|
Fail: 0,
|
||||||
Deviation: 0,
|
Deviation: rttUntested,
|
||||||
Average: 0,
|
Average: rttUntested,
|
||||||
Max: 0,
|
Max: rttUntested,
|
||||||
Min: 0,
|
Min: rttUntested,
|
||||||
}
|
}
|
||||||
actual = hr.Get()
|
got = s.Get()
|
||||||
if !reflect.DeepEqual(expected, actual) {
|
if !reflect.DeepEqual(want, got) {
|
||||||
t.Errorf("failed 'outdated / not-tested' test, expected: %v, actual: %v", expected, actual)
|
t.Errorf("failed 'outdated / not-tested' test, want: %v, got: %v", want, got)
|
||||||
}
|
}
|
||||||
|
|
||||||
hr.Put(time.Duration(60))
|
s.Put(time.Duration(60))
|
||||||
expected = &RTTStats{
|
want = RTTStats{
|
||||||
All: 1,
|
All: 1,
|
||||||
Fail: 0,
|
Fail: 0,
|
||||||
// 1 sample, std=0.5rtt
|
// 1 sample, std=0.5rtt
|
||||||
@ -97,8 +97,8 @@ func TestHealthPingResultsIgnoreOutdated(t *testing.T) {
|
|||||||
Max: 60,
|
Max: 60,
|
||||||
Min: 60,
|
Min: 60,
|
||||||
}
|
}
|
||||||
actual = hr.Get()
|
got = s.Get()
|
||||||
if !reflect.DeepEqual(expected, actual) {
|
if !reflect.DeepEqual(want, got) {
|
||||||
t.Errorf("expected: %v, actual: %v", expected, actual)
|
t.Errorf("want: %v, got: %v", want, got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user