-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |