@@ -103,7 +103,10 @@ func TestTimeWindowSimple(t *testing.T) {
103103 for _ , tt := range tests {
104104 t .Run (tt .name , func (t * testing.T ) {
105105 // New implementation test.
106- buckets := NewTimeWindow (2 * time .Minute , tt .granularity )
106+ buckets , err := NewTimeWindow (2 * time .Minute , tt .granularity )
107+ if err != nil {
108+ t .Fatalf ("NewTimeWindow failed: %v" , err )
109+ }
107110 if ! buckets .IsEmpty (trunc1 ) {
108111 t .Error ("Unexpected non empty result" )
109112 }
@@ -130,7 +133,10 @@ func TestTimeWindowSimple(t *testing.T) {
130133
131134func TestTimeWindowManyReps (t * testing.T ) {
132135 trunc1 := time .Now ().Truncate (granularity )
133- buckets := NewTimeWindow (time .Minute , granularity )
136+ buckets , err := NewTimeWindow (time .Minute , granularity )
137+ if err != nil {
138+ t .Fatalf ("NewTimeWindow failed: %v" , err )
139+ }
134140 for p := range 5 {
135141 trunc1 = trunc1 .Add (granularity )
136142 for t := range 5 {
@@ -164,7 +170,10 @@ func TestTimeWindowManyReps(t *testing.T) {
164170func TestTimeWindowManyRepsWithNonMonotonicalOrder (t * testing.T ) {
165171 start := time .Now ().Truncate (granularity )
166172 end := start
167- buckets := NewTimeWindow (time .Minute , granularity )
173+ buckets , err := NewTimeWindow (time .Minute , granularity )
174+ if err != nil {
175+ t .Fatalf ("NewTimeWindow failed: %v" , err )
176+ }
168177
169178 d := []int {0 , 3 , 2 , 1 , 4 }
170179 for p := range 5 {
@@ -200,7 +209,10 @@ func TestTimeWindowManyRepsWithNonMonotonicalOrder(t *testing.T) {
200209
201210func TestTimeWindowWindowAverage (t * testing.T ) {
202211 now := time .Now ()
203- buckets := NewTimeWindow (5 * time .Second , granularity )
212+ buckets , err := NewTimeWindow (5 * time .Second , granularity )
213+ if err != nil {
214+ t .Fatalf ("NewTimeWindow failed: %v" , err )
215+ }
204216
205217 // This verifies that we properly use firstWrite. Without that we'd get 0.2.
206218 buckets .Record (now , 1 )
@@ -285,7 +297,10 @@ func TestTimeWindowWindowAverage(t *testing.T) {
285297func TestTimeWindowAverageWithLargeGap (t * testing.T ) {
286298 now := time .Now ()
287299 // Create a window with 30 buckets (60s window / 2s granularity)
288- buckets := NewTimeWindow (60 * time .Second , 2 * time .Second )
300+ buckets , err := NewTimeWindow (60 * time .Second , 2 * time .Second )
301+ if err != nil {
302+ t .Fatalf ("NewTimeWindow failed: %v" , err )
303+ }
289304
290305 // Record some initial data
291306 for i := range 10 {
@@ -320,7 +335,10 @@ func TestTimeWindowAverageWithLargeGap(t *testing.T) {
320335// TestTimeWindowAverageNegativeValues tests that the window can handle and average negative values correctly.
321336func TestTimeWindowAverageNegativeValues (t * testing.T ) {
322337 now := time .Now ()
323- buckets := NewTimeWindow (5 * time .Second , granularity )
338+ buckets , err := NewTimeWindow (5 * time .Second , granularity )
339+ if err != nil {
340+ t .Fatalf ("NewTimeWindow failed: %v" , err )
341+ }
324342
325343 // Record negative values
326344 buckets .Record (now , - 10 )
@@ -346,7 +364,10 @@ func TestTimeWindowAverageNegativeValues(t *testing.T) {
346364func TestTimeWindowAverageBoundaryConditions (t * testing.T ) {
347365 now := time .Now ()
348366 // Small window to make wraparound easier to test
349- buckets := NewTimeWindow (10 * time .Second , 2 * time .Second ) // 5 buckets
367+ buckets , err := NewTimeWindow (10 * time .Second , 2 * time .Second ) // 5 buckets
368+ if err != nil {
369+ t .Fatalf ("NewTimeWindow failed: %v" , err )
370+ }
350371
351372 // Fill all buckets
352373 for i := range 5 {
@@ -380,7 +401,10 @@ func TestTimeWindowAverageBoundaryConditions(t *testing.T) {
380401
381402func TestDescendingRecord (t * testing.T ) {
382403 now := time .Now ()
383- buckets := NewTimeWindow (5 * time .Second , 1 * time .Second )
404+ buckets , err := NewTimeWindow (5 * time .Second , 1 * time .Second )
405+ if err != nil {
406+ t .Fatalf ("NewTimeWindow failed: %v" , err )
407+ }
384408
385409 for i := 8 * time .Second ; i >= 0 * time .Second ; i -= time .Second {
386410 buckets .Record (now .Add (i ), 5 )
@@ -395,7 +419,10 @@ func TestDescendingRecord(t *testing.T) {
395419
396420func TestTimeWindowHoles (t * testing.T ) {
397421 now := time .Now ()
398- buckets := NewTimeWindow (5 * time .Second , granularity )
422+ buckets , err := NewTimeWindow (5 * time .Second , granularity )
423+ if err != nil {
424+ t .Fatalf ("NewTimeWindow failed: %v" , err )
425+ }
399426
400427 for i := range 5 {
401428 buckets .Record (now .Add (time .Duration (i )* time .Second ), float64 (i + 1 ))
@@ -431,7 +458,10 @@ func TestTimeWindowHoles(t *testing.T) {
431458
432459func TestTimeWindowResizeWindow (t * testing.T ) {
433460 startTime := time .Now ()
434- buckets := NewTimeWindow (5 * time .Second , granularity )
461+ buckets , err := NewTimeWindow (5 * time .Second , granularity )
462+ if err != nil {
463+ t .Fatalf ("NewTimeWindow failed: %v" , err )
464+ }
435465
436466 // Fill the whole bucketing list with rollover.
437467 buckets .Record (startTime , 1 )
@@ -523,7 +553,10 @@ func TestTimeWindowWindowUpdate3sGranularity(t *testing.T) {
523553 trunc1 := time .Now ().Truncate (granularity )
524554
525555 // So two buckets here (ceil(5/3)=ceil(1.6(6))=2).
526- buckets := NewTimeWindow (5 * time .Second , granularity )
556+ buckets , err := NewTimeWindow (5 * time .Second , granularity )
557+ if err != nil {
558+ t .Fatalf ("NewTimeWindow failed: %v" , err )
559+ }
527560 if got , want := len (buckets .buckets ), 2 ; got != want {
528561 t .Fatalf ("Initial bucket count = %d, want: %d" , got , want )
529562 }
@@ -602,7 +635,10 @@ func TestTimeWindowWindowUpdate3sGranularity(t *testing.T) {
602635
603636func TestTimeWindowWindowUpdateNoOp (t * testing.T ) {
604637 startTime := time .Now ().Add (- time .Minute )
605- buckets := NewTimeWindow (5 * time .Second , granularity )
638+ buckets , err := NewTimeWindow (5 * time .Second , granularity )
639+ if err != nil {
640+ t .Fatalf ("NewTimeWindow failed: %v" , err )
641+ }
606642 buckets .Record (startTime , 19.82 )
607643 if got , want := buckets .firstWrite , buckets .lastWrite ; ! got .Equal (want ) {
608644 t .Errorf ("FirstWrite = %v, want: %v" , got , want )
@@ -619,8 +655,10 @@ func BenchmarkWindowAverage(b *testing.B) {
619655 for _ , wl := range []int {30 , 60 , 120 , 240 , 600 } {
620656 b .Run (fmt .Sprintf ("%v-win-len" , wl ), func (b * testing.B ) {
621657 tn := time .Now ().Truncate (time .Second ) // To simplify everything.
622- buckets := NewTimeWindow (time .Duration (wl )* time .Second ,
623- time .Second /*granularity*/ )
658+ buckets , err := NewTimeWindow (time .Duration (wl )* time .Second , time .Second )
659+ if err != nil {
660+ b .Fatalf ("NewTimeWindow failed: %v" , err )
661+ }
624662 // Populate with some random data.
625663 for i := range wl {
626664 buckets .Record (tn .Add (time .Duration (i )* time .Second ), rand .Float64 ()* 100 )
@@ -671,7 +709,10 @@ func (t *TimeWindow) forEachBucket(now time.Time, acc func(time time.Time, bucke
671709
672710func TestTimeWindowAverageWithZeros (t * testing.T ) {
673711 now := time .Now ()
674- buckets := NewTimeWindow (10 * time .Second , granularity )
712+ buckets , err := NewTimeWindow (10 * time .Second , granularity )
713+ if err != nil {
714+ t .Fatalf ("NewTimeWindow failed: %v" , err )
715+ }
675716
676717 // Fill the window with zeros
677718 for i := range 10 {
@@ -696,7 +737,10 @@ func TestTimeWindowAverageWithZeros(t *testing.T) {
696737
697738func TestTimeWindowAverageWithPositiveValuesThenZeros (t * testing.T ) {
698739 now := time .Now ()
699- buckets := NewTimeWindow (10 * time .Second , granularity )
740+ buckets , err := NewTimeWindow (10 * time .Second , granularity )
741+ if err != nil {
742+ t .Fatalf ("NewTimeWindow failed: %v" , err )
743+ }
700744
701745 // Fill the window with random positive values
702746 for i := range 10 {
@@ -725,3 +769,15 @@ func TestTimeWindowAverageWithPositiveValuesThenZeros(t *testing.T) {
725769 t .Errorf ("WindowAverage of zeros (with gap) = %v, want: %v" , got , want )
726770 }
727771}
772+
773+ func TestNegativeWindowAndGranularity (t * testing.T ) {
774+ _ , err := NewTimeWindow (10 * time .Second , - 1 * time .Second )
775+ if err == nil {
776+ t .Errorf ("NewTimeWindow should fail with negative granularity" )
777+ }
778+
779+ _ , err = NewTimeWindow (- 10 * time .Second , 1 * time .Second )
780+ if err == nil {
781+ t .Errorf ("NewTimeWindow should fail with negative window" )
782+ }
783+ }
0 commit comments