This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Provide option to specify readiness condition (#464)
* feat: Allow to specify readiness condition in health handler Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com> * added unit tests Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com> * add health check path option Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com> * adapted unit test Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com> * added comments to new options Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com> * added comment Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
- Loading branch information
Showing
2 changed files
with
120 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"net/http" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestRunHealthEndpoint(t *testing.T) { | ||
go RunHealthEndpoint("8080") | ||
|
||
require.Eventually(t, func() bool { | ||
get, err := http.Get("http://localhost:8080/health") | ||
if err != nil { | ||
return false | ||
} | ||
if get.StatusCode != http.StatusOK { | ||
return false | ||
} | ||
return true | ||
}, 2*time.Second, 50*time.Millisecond) | ||
} | ||
|
||
func TestRunHealthEndpoint_WithReadinessCondition(t *testing.T) { | ||
ready := false | ||
go RunHealthEndpoint("8080", WithPath("/ready"), WithReadinessConditionFunc(func() bool { | ||
return ready | ||
})) | ||
|
||
require.Eventually(t, func() bool { | ||
get, err := http.Get("http://localhost:8080/ready") | ||
if err != nil { | ||
return false | ||
} | ||
if get.StatusCode != http.StatusPreconditionFailed { | ||
return false | ||
} | ||
return true | ||
}, 2*time.Second, 50*time.Millisecond) | ||
|
||
ready = true | ||
|
||
require.Eventually(t, func() bool { | ||
get, err := http.Get("http://localhost:8080/ready") | ||
if err != nil { | ||
return false | ||
} | ||
if get.StatusCode != http.StatusOK { | ||
return false | ||
} | ||
return true | ||
}, 2*time.Second, 50*time.Millisecond) | ||
} | ||
|
||
func TestRunHealthEndpointCustomPath(t *testing.T) { | ||
go RunHealthEndpoint("8080", WithPath("/readiness")) | ||
|
||
require.Eventually(t, func() bool { | ||
get, err := http.Get("http://localhost:8080/readiness") | ||
if err != nil { | ||
return false | ||
} | ||
if get.StatusCode != http.StatusOK { | ||
return false | ||
} | ||
return true | ||
}, 2*time.Second, 50*time.Millisecond) | ||
} |