Skip to content
Permalink
Browse files

fix ttl expiration check

License: MIT
Signed-off-by: Adrian Lanzafame <adrianlanzafame92@gmail.com>
  • Loading branch information...
lanzafame committed Jun 25, 2019
1 parent 563a0da commit 2255ba737ba9613887300eccf2f021d0d83940c5
Showing with 16 additions and 9 deletions.
  1. +6 −0 monitor/metrics/checker.go
  2. +10 −9 monitor/metrics/checker_test.go
@@ -203,6 +203,12 @@ func (mc *Checker) failed(metric string, pid peer.ID) (float64, []float64, float
return 0.0, nil, 0.0, false
}

prevMetricExpiry := time.Unix(0, pmtrs[1].Expire)
latestMetricReceived := time.Unix(0, pmtrs[0].ReceivedAt)
if !latestMetricReceived.After(prevMetricExpiry) {
return 0.0, nil, 0.0, false
}

v := time.Now().UnixNano() - latest.ReceivedAt
dv := mc.metrics.Distribution(metric, pid)
switch {
@@ -158,21 +158,22 @@ func TestChecker_Failed(t *testing.T) {
})

t.Run("ttl must expire before phiv causes failure", func(t *testing.T) {
// With the threshold at 0.05 and a time difference (j) between
// metrics increasing at a rate of double the previous, the phi value
// is greater than the threshold on the 5th iteration (i = 4), but the TTL
// still has a valid TTL at this point so the test continues until
// the 7th iteration (i = 6) when the previous metric has expired and the phi
// value is greater than the threshold.
metrics := NewStore()
checker := NewChecker(context.Background(), metrics, 0.05)

for i, j := 0, 100; i < 7; i, j = i+1, j*2 {
for i := 0; i < 10; i++ {
metrics.Add(makePeerMetric(test.PeerID1, "1", 10*time.Millisecond))
time.Sleep(time.Duration(200) * time.Millisecond)
}
for i, j := 0, 10; i < 8; i, j = i+1, j*2 {
time.Sleep(time.Duration(j) * time.Millisecond)
metrics.Add(makePeerMetric(test.PeerID1, "1", 5000*time.Millisecond))
metrics.Add(makePeerMetric(test.PeerID1, "1", 10*time.Millisecond))
v, _, phiv, got := checker.failed("ping", test.PeerID1)
t.Logf("i: %d: j: %d v: %f, phiv: %f, got: %v\n", i, j, v, phiv, got)
if i > 5 && !got {
if i < 7 && got {
t.Fatal("threshold should not have been reached already")
}
if i >= 7 && !got {
t.Fatal("threshold should have been reached by now")
}
}

0 comments on commit 2255ba7

Please sign in to comment.
You can’t perform that action at this time.