-
Notifications
You must be signed in to change notification settings - Fork 825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor of workerqueue health testing #164
Merged
markmandel
merged 1 commit into
googleforgames:master
from
markmandel:refactor/health-tests
Apr 6, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -18,12 +18,20 @@ import ( | |
"testing" | ||
"time" | ||
|
||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
|
||
"github.com/heptiolabs/healthcheck" | ||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
"k8s.io/client-go/tools/cache" | ||
) | ||
|
||
func TestWorkerQueueRun(t *testing.T) { | ||
t.Parallel() | ||
|
||
received := make(chan string) | ||
defer close(received) | ||
|
||
|
@@ -56,6 +64,8 @@ func TestWorkerQueueRun(t *testing.T) { | |
} | ||
|
||
func TestWorkerQueueHealthy(t *testing.T) { | ||
t.Parallel() | ||
|
||
done := make(chan struct{}) | ||
handler := func(string) error { | ||
<-done | ||
|
@@ -81,3 +91,48 @@ func TestWorkerQueueHealthy(t *testing.T) { | |
assert.Equal(t, 0, wq.RunCount()) | ||
assert.EqualError(t, wq.Healthy(), "want 1 worker goroutine(s), got 0") | ||
} | ||
|
||
func TestWorkQueueHealthCheck(t *testing.T) { | ||
t.Parallel() | ||
|
||
health := healthcheck.NewHandler() | ||
handler := func(string) error { | ||
return nil | ||
} | ||
wq := NewWorkerQueue(handler, logrus.WithField("source", "test"), "test") | ||
health.AddLivenessCheck("test", wq.Healthy) | ||
|
||
server := httptest.NewServer(health) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a great choice, as all the port handling between different tests isn't our responsibility. |
||
defer server.Close() | ||
|
||
stop := make(chan struct{}) | ||
go wq.Run(1, stop) | ||
|
||
url := server.URL + "/live" | ||
|
||
f := func(t *testing.T, url string, status int) { | ||
resp, err := http.Get(url) | ||
assert.Nil(t, err) | ||
defer resp.Body.Close() | ||
|
||
body, err := ioutil.ReadAll(resp.Body) | ||
assert.Nil(t, err) | ||
assert.Equal(t, status, resp.StatusCode) | ||
assert.Equal(t, []byte("{}\n"), body) | ||
} | ||
|
||
f(t, url, http.StatusOK) | ||
|
||
close(stop) | ||
// closing can take a short while | ||
err := wait.PollImmediate(time.Second, 5*time.Second, func() (bool, error) { | ||
rc := wq.RunCount() | ||
logrus.WithField("runcount", rc).Info("Checking run count") | ||
return rc == 0, nil | ||
}) | ||
assert.Nil(t, err) | ||
|
||
// gate | ||
assert.Error(t, wq.Healthy()) | ||
f(t, url, http.StatusServiceUnavailable) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you know of a convention for naming these liveness checks from Kubernetes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No idea. I just figured keep it closer to the names of the CRDs and objects that back them.