respect the calculated deadline in the test check
@@ -13,6 +13,10 @@ import (
var reportFailure bool
var reportDelay time.Duration
+var (
+ timeLimit time.Duration
+)
+
func init() {
// enable debug logging on the check client
@@ -38,6 +42,23 @@ func init() {
log.Fatalln("Failed to parse REPORT_DELAY env var:", err)
}
}
+
+ // Set check time limit to default
+ timeLimit = time.Duration(time.Minute * 10)
+ // Get the deadline time in unix from the env var
+ unixDeadline, err := checkclient.GetDeadline()
+ if err != nil {
+ log.Println("There was an issue getting the check deadline:", err.Error())
+ }
+ // Calculate check run duration from the deadline and now
+ deadline, err := strconv.Atoi(unixDeadline)
+ if err != nil {
+ log.Fatalln("Failed to parse unix deadline from environment.")
+ }
+ if deadline > 0 {
+ log.Println("Parsed check deadline time from the environment:", deadline)
+ timeLimit = time.Duration((int64(deadline) - time.Now().Unix()) * 1e9) // Multiply by 1,000,000,000 because that's how many nanoseconds are in a second
+ }
}
func main() {
@@ -46,13 +67,27 @@ func main() {
log.Println("Waiting", reportDelay, "seconds before reporting...")
time.Sleep(reportDelay)
+ go func() {
+ select {
+ case <-time.After(timeLimit):
+ log.Println("Check took too long and timed out.")
+ os.Exit(1)
+ }
+ }()
+
var err error
if reportFailure {
log.Println("Reporting failure...")
err = checkclient.ReportFailure([]string{"Test has failed!"})
+ if err != nil {
+ log.Println(err.Error())
+ }
} else {
log.Println("Reporting success...")
err = checkclient.ReportSuccess()
+ if err != nil {
+ log.Println(err.Error())
+ }
}
if err != nil {
