Commit 9ca71674 authored by Adphi's avatar Adphi

fix rtts and tests when pinger stopped

parent 3f8a1bfc
......@@ -34,7 +34,11 @@ func (u *destination) ping(pinger *ping.Pinger, timeout time.Duration) {
func (s *history) addResult(rtt time.Duration, err error) {
s.mtx.Lock()
s.results[(s.received+s.lost)%len(s.results)] = rtt
if len(s.results) > 1 {
s.results = append([]time.Duration{rtt}, s.results[:len(s.results)-2]...)
} else if len(s.results) == 1 {
s.results[0] = rtt
}
if err == nil {
s.received++
} else {
......
......@@ -54,13 +54,16 @@ func TestPingerLocalhost(t *testing.T) {
for {
select {
case <-c.C:
if !p.IsRunning() {
return
}
count++
s := p.Statistics()
assert.NotEmpty(t, s)
l, ok := s[ip]
assert.True(t, ok)
logrus.Debug(l)
assert.Equal(t, count, l.PacketsRecv)
assert.InDelta(t, count, l.PacketsRecv, 1)
rtts := filterZeros(l.Rtts)
assert.Equal(t, count, len(rtts))
case <-ctx.Done():
......@@ -85,6 +88,9 @@ func TestPingerTimeout(t *testing.T) {
select {
case <-c.C:
count++
if !p.IsRunning() {
return
}
s := p.Statistics()
assert.NotEmpty(t, s)
l, ok := s[ip]
......@@ -134,6 +140,9 @@ func TestPingerReset(t *testing.T) {
continue
}
if resetDone {
if !p.IsRunning() {
return
}
s := p.Statistics()
logrus.Debug(s)
......@@ -208,6 +217,9 @@ func TestTwoIPs(t *testing.T) {
select {
case <-c.C:
count++
if !p.IsRunning() {
return
}
s := p.Statistics()
assert.NotEmpty(t, s)
logrus.Debug(s)
......@@ -234,6 +246,7 @@ func TestPinger(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
p, err := NewPinger(ctx, "127.0.0.1", "255.0.0.255", "127.0.0.2")
p.SetLogger(logrus.StandardLogger())
assert.NoError(t, err)
if err != nil {
t.FailNow()
......@@ -264,6 +277,9 @@ func TestPinger(t *testing.T) {
select {
case <-tk.C:
count++
if !p.IsRunning() {
return
}
s := p.Statistics()
logrus.Debugf("count: %d", count)
for _, v := range s {
......@@ -319,6 +335,9 @@ func TestPingerStatistics(t *testing.T) {
for {
select {
case <-tk.C:
if !p.IsRunning() {
return
}
count++
s := p.Statistics()
ls, ok := s["127.0.0.1"]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment