Skip to content

Commit

Permalink
feat: add timeout function for check approval (#257)
Browse files Browse the repository at this point in the history
Co-authored-by: qingliu <[email protected]>
  • Loading branch information
l-qing and l-qing authored Aug 24, 2022
1 parent 61fda33 commit f81cf03
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
10 changes: 10 additions & 0 deletions apis/meta/v1alpha1/check_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,13 @@ func (as ApprovalStatuses) GetBySubject(subject rbacv1.Subject) *ApprovalStatus
}
return nil
}

// IsTimeout determine if the current is timeout.
// Take the current time as a reference, and compare it to the start time and the timeout duration.
func IsTimeout(startTime *metav1.Time, timeout metav1.Duration) bool {
if startTime.IsZero() || timeout.Duration == 0 {
return false
}
runtime := time.Since(startTime.Time)
return runtime >= timeout.Duration
}
38 changes: 38 additions & 0 deletions apis/meta/v1alpha1/check_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ limitations under the License.
package v1alpha1

import (
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var _ = Describe("Test.Check", func() {
Expand Down Expand Up @@ -83,3 +86,38 @@ var _ = Describe("Test.Check", func() {
),
)
})

var _ = Describe("Test.IsTimeout", func() {
DescribeTable("IsTimeout",
func(startTime *metav1.Time, timeout time.Duration, expected bool) {
v1time := metav1.Duration{Duration: timeout}
actual := IsTimeout(startTime, v1time)
Expect(actual).To(Equal(expected))
},
Entry("nil startTime",
nil,
1*time.Second,
false,
),
Entry("timeout is zero",
&metav1.Time{Time: time.Now()},
0*time.Second,
false,
),
Entry("not timeout",
&metav1.Time{Time: time.Now().Add(1 * time.Minute)},
30*time.Second,
false,
),
Entry("timeout",
&metav1.Time{Time: time.Now().Add(-1 * time.Minute)},
30*time.Second,
true,
),
Entry("just a timeout",
&metav1.Time{Time: time.Now().Add(-1 * time.Minute)},
60*time.Second,
true,
),
)
})

0 comments on commit f81cf03

Please sign in to comment.