Skip to content

Commit

Permalink
Minor code cleanuop
Browse files Browse the repository at this point in the history
  • Loading branch information
tshak committed Jul 2, 2024
1 parent acd3c8a commit 149f7b3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fmt:
go fmt ./...

lint:
golangci-lint run -e SA5004
golangci-lint run

docker-run: docker-build
docker run -it --rm -p 8000:8000 testdummy
Expand Down
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func main() {
stressDuration, err := time.ParseDuration(rc.StressCpuDuration)
ExitIfErr(err, "Invalid StressCpuDuration")

addRoute("", func(w http.ResponseWriter, r *http.Request) { pingHandler(w, r, stressDuration) })
addRoute("ping", func(w http.ResponseWriter, r *http.Request) { pingHandler(w, r, stressDuration) })
addRoute("", func(w http.ResponseWriter, r *http.Request) { pingHandler(w, stressDuration) })
addRoute("ping", func(w http.ResponseWriter, r *http.Request) { pingHandler(w, stressDuration) })
addRoute("echo", echoHandler)
addRoute("health", healthHandler)
addRoute("healthcheck", createHealthcheckHandlerFunc(rc))
Expand Down Expand Up @@ -104,7 +104,7 @@ func envHandler(w http.ResponseWriter, r *http.Request) {
}
}

func pingHandler(w http.ResponseWriter, r *http.Request, stressDuration time.Duration) {
func pingHandler(w http.ResponseWriter, stressDuration time.Duration) {
if stressDuration > 0 {
log.Printf("Stressing CPU for %s", stressDuration)
stressCpu(stressDuration)
Expand Down Expand Up @@ -150,7 +150,7 @@ func stressCpu(duration time.Duration) {
select {
case <-done:
return
//SA5004 intentionally ignored
//nolint:staticcheck // SA5004 intentionally ignored: spinning CPU for stress testing
default:
}
}
Expand Down
6 changes: 2 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@ import (
)

func Test_PingHandler(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/ping", nil)
w := httptest.NewRecorder()
pingHandler(w, req, time.Duration(0))
pingHandler(http.ResponseWriter(w), time.Duration(0))
resp := w.Result()

assert.Equal(t, http.StatusOK, resp.StatusCode)
}

func Test_PingHandler_WithDuration(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/ping", nil)
w := httptest.NewRecorder()
now := time.Now()
workDuration := time.Duration(10 * time.Millisecond)
pingHandler(w, req, workDuration)
pingHandler(http.ResponseWriter(w), workDuration)
resp := w.Result()
elapsed := time.Since(now)

Expand Down

0 comments on commit 149f7b3

Please sign in to comment.