Skip to content

Commit

Permalink
basic predictor test
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Jun 28, 2024
1 parent 70f08c1 commit 96c26bd
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions certexchange/polling/predictor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package polling

import (
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestPredictor(t *testing.T) {
p := newPredictor(time.Second, 30*time.Second, 120*time.Second)

// Progress of 1 shouldn't update anything.
require.Equal(t, 30*time.Second, p.update(1))
require.Equal(t, 30*time.Second, p.update(1))

// Progress of 0 should predict the same interval, then twice that, etc.
require.Equal(t, 30*time.Second, p.update(0))
require.Equal(t, 60*time.Second, p.update(0))

// After that, the intervalA should increase slightly.
intervalA := p.update(1)
require.Less(t, 30*time.Second, intervalA)
require.Greater(t, 40*time.Second, intervalA)

// If the interval is too large, it should decrease, but not by as much because we're
// switching direction.
intervalB := p.update(2)
require.Less(t, 30*time.Second, intervalB)
require.Greater(t, intervalA, intervalB)

// It should keep getting smaller.
intervalC := p.update(2)
require.Greater(t, intervalB, intervalC)

// Until we stabilize.
require.Equal(t, intervalC, p.update(1))

// We should always stay above the minimum.
for i := 0; i < 100; i++ {
require.LessOrEqual(t, time.Second, p.update(2))
}
require.Equal(t, time.Second, p.update(1))

// And below the maximum (unless backing off).
for i := 0; i < 100; i++ {
require.GreaterOrEqual(t, 120*time.Second, p.update(0))
require.GreaterOrEqual(t, 120*time.Second, p.update(1))
}
require.Equal(t, 120*time.Second, p.update(1))

// But backoff should go much higher.
for i := 0; i < 100; i++ {
require.GreaterOrEqual(t, 10*120*time.Second, p.update(0))
}
require.Equal(t, 10*120*time.Second, p.update(0))

// And revert to the old time when done.
require.Equal(t, 120*time.Second, p.update(1))
}

0 comments on commit 96c26bd

Please sign in to comment.