Skip to content

Commit

Permalink
Add in boundary builder unit tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Edwin Buck <[email protected]>
  • Loading branch information
edwbuck committed Sep 27, 2024
1 parent 791b503 commit aad7444
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 16 deletions.
36 changes: 20 additions & 16 deletions pkg/server/endpoints/eventTracker.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package endpoints

import (
// "fmt"
"maps"
"slices"
"time"
)
Expand Down Expand Up @@ -65,30 +65,34 @@ func PollPeriods(pollTime time.Duration, trackTime time.Duration) uint {
func BoundaryBuilder(pollTime time.Duration, trackTime time.Duration) []uint {
pollPeriods := PollPeriods(pollTime, trackTime)

pollBoundaries := make([]uint, 0)
// number of polls in a minute
pollsPerMinute := uint(time.Duration(1) * time.Minute / pollTime)
// number of polls in ten minutes
pollsPerTenMinutes := uint(time.Duration(10) * time.Minute / pollTime)

// for first minute, poll at cacheReloadInterval
pollBoundaries = append(pollBoundaries, pollsPerMinute)
// next 9 minutes, poll at 1/2 minute
currentBoundary := pollsPerMinute
for currentBoundary < pollsPerTenMinutes {
pollBoundaries = append(pollBoundaries, currentBoundary+(pollsPerMinute/2))
pollBoundaries = append(pollBoundaries, currentBoundary+pollsPerMinute)
currentBoundary += pollsPerMinute
}
// rest of polling at 1 minute
// initialize poll boundaries one minute out
boundaries := make(map[uint]struct{})
currentBoundary := uint(pollsPerMinute)
for currentBoundary < pollPeriods {
pollBoundaries = append(pollBoundaries, currentBoundary+pollsPerMinute)
if currentBoundary < pollsPerTenMinutes {
boundaries[currentBoundary] = struct{}{}
boundaries[currentBoundary+(pollsPerMinute/2)] = struct{}{}
} else {
boundaries[currentBoundary] = struct{}{}
}
currentBoundary += pollsPerMinute
}
// always poll at end of transaction timeout
pollBoundaries = append(pollBoundaries, pollPeriods-1)
if 0 < len(boundaries) {
boundaries[pollPeriods-1] = struct{}{}
}

boundaryList := slices.Collect(maps.Keys(boundaries))
slices.Sort(boundaryList)
if boundaryList == nil {
boundaryList = []uint{}
}

return pollBoundaries
return boundaryList
}

func NewEventTracker(pollPeriods uint, boundaries []uint) *eventTracker {
Expand Down
147 changes: 147 additions & 0 deletions pkg/server/endpoints/eventTracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -971,4 +971,151 @@ func TestEvenDispersion(t *testing.T) {
}

func TestBoundaryBuilder(t *testing.T) {
for _, tt := range []struct {
name string
pollInterval string
pollDuration string

expectedPollPeriods uint
expectedBoundaries []uint
}{
{
name: "poll every second, over 1 minute",
pollInterval: "1s",
pollDuration: "1m",

expectedPollPeriods: 60,
expectedBoundaries: []uint{},
},
{
name: "poll every second, over 10 minutes",
pollInterval: "1s",
pollDuration: "10m",

expectedPollPeriods: 600,
expectedBoundaries: []uint{
60, 90, 120, 150, 180, 210, 240, 270, 300, 330,
360, 390, 420, 450, 480, 510, 540, 570, 599,
},
},
{
name: "poll every second, over 20 minutes",
pollInterval: "1s",
pollDuration: "20m",

expectedPollPeriods: 1200,
expectedBoundaries: []uint{
60, 90, 120, 150, 180, 210, 240, 270, 300, 330,
360, 390, 420, 450, 480, 510, 540, 570,
600, 660, 720, 780, 840, 900, 960, 1020,
1080, 1140, 1199,
},
},
{
name: "poll every 5 seconds, over 1 minute",
pollInterval: "5s",
pollDuration: "1m",

expectedPollPeriods: 12,
expectedBoundaries: []uint{},
},
{
name: "poll every 5 seconds, over 10 minutes",
pollInterval: "5s",
pollDuration: "10m",

expectedPollPeriods: 120,
expectedBoundaries: []uint{
12, 18, 24, 30, 36, 42, 48, 54, 60, 66,
72, 78, 84, 90, 96, 102, 108, 114, 119,
},
},
{
name: "poll every 5 seconds, over 20 minutes",
pollInterval: "5s",
pollDuration: "20m",

expectedPollPeriods: 240,
expectedBoundaries: []uint{
12, 18, 24, 30, 36, 42, 48, 54, 60, 66,
72, 78, 84, 90, 96, 102, 108, 114, 120,
132, 144, 156, 168, 180, 192, 204, 216, 228, 239,
},
},
{
name: "poll every 10 seconds, over 1 minute",
pollInterval: "10s",
pollDuration: "1m",

expectedPollPeriods: 6,
expectedBoundaries: []uint{},
},
{
name: "poll every 10 seconds, over 10 minutes",
pollInterval: "10s",
pollDuration: "10m",

expectedPollPeriods: 60,
expectedBoundaries: []uint{
6, 9, 12, 15, 18, 21, 24, 27, 30,
33, 36, 39, 42, 45, 48, 51, 54, 57, 59,
},
},
{
name: "poll every 10 seconds, over 20 minutes",
pollInterval: "10s",
pollDuration: "20m",

expectedPollPeriods: 120,
expectedBoundaries: []uint{
6, 9, 12, 15, 18, 21, 24, 27, 30,
33, 36, 39, 42, 45, 48, 51, 54, 57, 60,
66, 72, 78, 84, 90, 96, 102, 108, 114, 119,
},
},
{
name: "poll every 20 seconds, over 1 minute",
pollInterval: "20s",
pollDuration: "1m",

expectedPollPeriods: 3,
expectedBoundaries: []uint{},
},
{
name: "poll every 20 seconds, over 10 minutes",
pollInterval: "20s",
pollDuration: "10m",

expectedPollPeriods: 30,
expectedBoundaries: []uint{
3, 4, 6, 7, 9, 10, 12, 13, 15, 16,
18, 19, 21, 22, 24, 25, 27, 28, 29,
},
},
{
name: "poll every 20 seconds, over 20 minutes",
pollInterval: "20s",
pollDuration: "20m",

expectedPollPeriods: 60,
expectedBoundaries: []uint{
3, 4, 6, 7, 9, 10, 12, 13, 15, 16,
18, 19, 21, 22, 24, 25, 27, 28, 30,
33, 36, 39, 42, 45, 48, 51, 54, 57, 59,
},
},
} {
tt := tt
t.Run(tt.name, func(t *testing.T) {
pollInterval, err := time.ParseDuration(tt.pollInterval)
require.NoError(t, err, "error in specifying test poll interval")
pollDuration, err := time.ParseDuration(tt.pollDuration)
require.NoError(t, err, "error in specifying test poll duration")
pollPeriods := endpoints.PollPeriods(pollInterval, pollDuration)

require.Equal(t, tt.expectedPollPeriods, pollPeriods)
boundaries := endpoints.BoundaryBuilder(pollInterval, pollDuration)
require.Equal(t, tt.expectedBoundaries, boundaries)
})
}
}

0 comments on commit aad7444

Please sign in to comment.