Skip to content
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

fix TestHttpProbe/returns_the_status_code_if_the_request_is_successful #55

Merged
merged 3 commits into from
Jul 24, 2024

Conversation

abdoroot
Copy link
Contributor

@abdoroot abdoroot commented Jul 22, 2024

Fixes #52.

@jesusprubio
Copy link
Owner

Thank you! I would prefer to merge a proper solution instead this workaround.

The problem here is the function newTestHTTPServer returning before the server is listening. Because the call to ListenAndServe is made inside a goroutine:

up/pkg/protocols_test.go

Lines 66 to 67 in 520cd9e

go func() {
err := server.ListenAndServe()

Please check this solution. I think it should work: https://stackoverflow.com/a/53332769

@abdoroot
Copy link
Contributor Author

thanks for suggestion it work

Copy link
Owner

@jesusprubio jesusprubio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. Only a couple of formatting changes are we are ready to merge. :)

server := &http.Server{Addr: hostPort}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
mux := http.NewServeMux()
server := &http.Server{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, use one line to define server.

Addr: hostPort,
Handler:mux,
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, remove the empty lines to keep the formatting consistent.

go func() {
err := server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
if err := server.Serve(l); err != nil && err != http.ErrServerClosed {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use two different lines. Also to keep the formatting consistent with the codebase.

@@ -59,19 +59,30 @@ func TestHttpProbe(t *testing.T) {
// Creates an HTTP server for testing.
func newTestHTTPServer(t *testing.T) *http.Server {
hostPort := net.JoinHostPort("127.0.0.1", "8080")
server := &http.Server{Addr: hostPort}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
mux := http.NewServeMux()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need a mux for this simple scenario. The line 63 should be enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay got it

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you :)

@jesusprubio jesusprubio merged commit 4093d58 into jesusprubio:main Jul 24, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Flaky test: TestHttpProbe/returns_the_status_code_if_the_request_is_successful
2 participants