Skip to content

Commit

Permalink
Finish tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ivov committed Nov 27, 2024
1 parent 7dbce76 commit 818afbd
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion internal/http/check_until_broker_ready_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package http

import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
)

func TestCheckUntilBrokerReady(t *testing.T) {
func TestCheckUntilBrokerReadyHappyPath(t *testing.T) {
tests := []struct {
name string
serverFn func(http.ResponseWriter, *http.Request, int)
Expand Down Expand Up @@ -60,6 +61,54 @@ func TestCheckUntilBrokerReady(t *testing.T) {
}
}

func TestCheckUntilBrokerReadyErrors(t *testing.T) {
tests := []struct {
name string
handler func(w http.ResponseWriter, r *http.Request)
}{
{
name: "error - closed server",
handler: func(_ http.ResponseWriter, _ *http.Request) {},
},
{
name: "error - bad status code",
handler: func(w http.ResponseWriter, r *http.Request) {

Check failure on line 75 in internal/http/check_until_broker_ready_test.go

View workflow job for this annotation

GitHub Actions / audit

unused-parameter: parameter 'r' seems to be unused, consider removing or renaming it as _ (revive)
w.WriteHeader(http.StatusServiceUnavailable)
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(tt.handler))
if tt.name == "error - closed server" {
server.Close()
} else {
defer server.Close()
}

// CheckUntilBrokerReady retries forever, so set up
// - context timeout to show retry loop keeps running without returning
// - channel to catch any unexpected early returns
// - goroutine to prevent this infinite retries from blocking tests
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()

brokerUnexpectedlyReady := make(chan error)
go func() {
brokerUnexpectedlyReady <- CheckUntilBrokerReady(server.URL)
}()

select {
case <-ctx.Done():
// expected timeout
case err := <-brokerUnexpectedlyReady:
t.Errorf("expected timeout, got: %v", err)
}
})
}
}

func TestSendReadinessRequest(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit 818afbd

Please sign in to comment.