|
| 1 | +/* |
| 2 | +Copyright 2024. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package utils |
| 18 | + |
| 19 | +import ( |
| 20 | + "time" |
| 21 | + |
| 22 | + . "github.com/onsi/ginkgo/v2" |
| 23 | + . "github.com/onsi/gomega" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + |
| 26 | + prefectiov1 "github.com/PrefectHQ/prefect-operator/api/v1" |
| 27 | +) |
| 28 | + |
| 29 | +var _ = Describe("Backoff utilities", func() { |
| 30 | + Describe("CalculateBackoffDelay", func() { |
| 31 | + It("should return progressive delays", func() { |
| 32 | + testCases := []struct { |
| 33 | + attempts int |
| 34 | + expected time.Duration |
| 35 | + }{ |
| 36 | + {0, 15 * time.Second}, |
| 37 | + {1, 30 * time.Second}, |
| 38 | + {2, 60 * time.Second}, |
| 39 | + {3, 120 * time.Second}, |
| 40 | + {4, 120 * time.Second}, // Max delay |
| 41 | + {10, 120 * time.Second}, // Still max delay |
| 42 | + } |
| 43 | + |
| 44 | + for _, tc := range testCases { |
| 45 | + delay := CalculateBackoffDelay(tc.attempts) |
| 46 | + Expect(delay).To(Equal(tc.expected), "Attempt %d should have delay %v", tc.attempts, tc.expected) |
| 47 | + } |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + Describe("Retry count management", func() { |
| 52 | + var workPool *prefectiov1.PrefectWorkPool |
| 53 | + |
| 54 | + BeforeEach(func() { |
| 55 | + workPool = &prefectiov1.PrefectWorkPool{ |
| 56 | + ObjectMeta: metav1.ObjectMeta{ |
| 57 | + Name: "test-pool", |
| 58 | + Namespace: "default", |
| 59 | + }, |
| 60 | + } |
| 61 | + }) |
| 62 | + |
| 63 | + It("should start with zero retry count", func() { |
| 64 | + count := GetRetryCount(workPool) |
| 65 | + Expect(count).To(Equal(0)) |
| 66 | + }) |
| 67 | + |
| 68 | + It("should increment retry count", func() { |
| 69 | + IncrementRetryCount(workPool) |
| 70 | + Expect(GetRetryCount(workPool)).To(Equal(1)) |
| 71 | + |
| 72 | + IncrementRetryCount(workPool) |
| 73 | + Expect(GetRetryCount(workPool)).To(Equal(2)) |
| 74 | + }) |
| 75 | + |
| 76 | + It("should reset retry count", func() { |
| 77 | + IncrementRetryCount(workPool) |
| 78 | + IncrementRetryCount(workPool) |
| 79 | + Expect(GetRetryCount(workPool)).To(Equal(2)) |
| 80 | + |
| 81 | + ResetRetryCount(workPool) |
| 82 | + Expect(GetRetryCount(workPool)).To(Equal(0)) |
| 83 | + }) |
| 84 | + |
| 85 | + It("should detect when to stop retrying", func() { |
| 86 | + for i := 0; i < MaxRetryAttempts-1; i++ { |
| 87 | + IncrementRetryCount(workPool) |
| 88 | + Expect(ShouldStopRetrying(workPool)).To(BeFalse()) |
| 89 | + } |
| 90 | + |
| 91 | + IncrementRetryCount(workPool) |
| 92 | + Expect(ShouldStopRetrying(workPool)).To(BeTrue()) |
| 93 | + }) |
| 94 | + |
| 95 | + It("should handle missing annotations gracefully", func() { |
| 96 | + workPool.SetAnnotations(nil) |
| 97 | + count := GetRetryCount(workPool) |
| 98 | + Expect(count).To(Equal(0)) |
| 99 | + |
| 100 | + IncrementRetryCount(workPool) |
| 101 | + Expect(GetRetryCount(workPool)).To(Equal(1)) |
| 102 | + }) |
| 103 | + |
| 104 | + It("should handle invalid annotation values", func() { |
| 105 | + workPool.SetAnnotations(map[string]string{ |
| 106 | + RetryCountAnnotation: "invalid", |
| 107 | + }) |
| 108 | + count := GetRetryCount(workPool) |
| 109 | + Expect(count).To(Equal(0)) |
| 110 | + }) |
| 111 | + }) |
| 112 | +}) |
0 commit comments