From a9696e6592b8fef6853ba5cfa2492939390c5a5b Mon Sep 17 00:00:00 2001 From: Gregory Albouy Date: Mon, 10 Oct 2022 22:49:57 +0200 Subject: [PATCH 1/4] feat: write test suite for demonstration --- demo-benchttp-tests.yml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 demo-benchttp-tests.yml diff --git a/demo-benchttp-tests.yml b/demo-benchttp-tests.yml new file mode 100644 index 0000000..4b8ab06 --- /dev/null +++ b/demo-benchttp-tests.yml @@ -0,0 +1,20 @@ +request: + url: http://localhost:9999?delay=200ms + +runner: + requests: 10 + concurrency: 2 + +tests: + - name: Delay is properly applied + field: ResponseTimes.Min + predicate: GTE + target: 200ms + - name: Delays are similar + field: ResponseTimes.StdDev + predicate: LT + target: 50ms + - name: 100% availability + field: RequestFailureCount + predicate: EQ + target: 0 From db80aeb8bbd74479e6fadc6b34343e9a2a6446db Mon Sep 17 00:00:00 2001 From: Gregory Albouy Date: Mon, 10 Oct 2022 22:50:12 +0200 Subject: [PATCH 2/4] ci: add benchttp action to ci --- .github/workflows/demo-benchttp-action.yml | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 .github/workflows/demo-benchttp-action.yml diff --git a/.github/workflows/demo-benchttp-action.yml b/.github/workflows/demo-benchttp-action.yml new file mode 100644 index 0000000..4c3a6e0 --- /dev/null +++ b/.github/workflows/demo-benchttp-action.yml @@ -0,0 +1,23 @@ +name: Demo Benchttp Action + +on: [pull_request] + +jobs: + run-benchttp: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.17 + + - name: Start server in the background + run: go run main.go -port=9999 & + + - name: Run Benchttp CLI + uses: benchttp/action@v1 + with: + options: -configFile ./demo-benchttp-tests.yml + version: v0.1.0 From 06afcbd872edd9a0ff29c34d0d76cab32a7ec425 Mon Sep 17 00:00:00 2001 From: Gregory Albouy Date: Mon, 10 Oct 2022 22:54:42 +0200 Subject: [PATCH 3/4] ci: fix regular CI - upgrade golangci-lint - add ignore directive for gosec - remove deprecated linters - remove irrelevant flaky test: "request with fib param" --- .github/workflows/ci.yml | 2 +- .golangci.yml | 3 --- internal/server/server.go | 2 +- internal/server/server_test.go | 15 --------------- 4 files changed, 2 insertions(+), 20 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 60c65ca..7b5f774 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,7 +17,7 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@v2 with: - version: v1.43.0 + version: v1.50.0 # Check #2: Test - name: Test diff --git a/.golangci.yml b/.golangci.yml index f208038..dbe9a62 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -129,7 +129,6 @@ linters: disable-all: true enable: - bodyclose # enforce resp.Body.Close() - - deadcode - dupl # duplicate code - errcheck - exportloopref @@ -143,10 +142,8 @@ linters: - prealloc # enforce capacity allocation when possible - revive # golint enhancement - staticcheck # go vet enhancement - - structcheck # unused struct fields - testpackage # checks on tests (*_test) - thelper # enforce t.Helper() - - varcheck # unused global var and const - wastedassign fast: false diff --git a/internal/server/server.go b/internal/server/server.go index 68ec408..2fc723a 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -26,7 +26,7 @@ func (s *Server) ListenAndServe() error { addr := "localhost:" + s.port fmt.Printf("http://%s\n", addr) go s.listenStdin() - return http.ListenAndServe(addr, s) + return http.ListenAndServe(addr, s) //nolint:gosec // local use only } func (s *Server) listenStdin() { diff --git a/internal/server/server_test.go b/internal/server/server_test.go index 791e3cb..1c70569 100644 --- a/internal/server/server_test.go +++ b/internal/server/server_test.go @@ -32,21 +32,6 @@ func TestHandleRequest(t *testing.T) { Run(t) }) - t.Run("request with fib param", func(t *testing.T) { - const ( - fib = 35 - expmin = 30 * time.Millisecond - expmax = 80 * time.Millisecond - ) - - r := httptest.NewRequest("", fmt.Sprintf("/?fib=%d", fib), nil) - - testx.HTTPHandler(s).WithRequest(r). - Response(checkStatusCode(200)). - Duration(check.Duration.InRange(expmin, expmax)). - Run(t) - }) - t.Run("request without params", func(t *testing.T) { const expmax = 3 * time.Millisecond From 83b3953bbb133794734de71bd0e87bc16244d104 Mon Sep 17 00:00:00 2001 From: Gregory Albouy Date: Tue, 11 Oct 2022 00:28:56 +0200 Subject: [PATCH 4/4] fix: ignore stdin in CI context For some reason it results in an infinite EOF when run in a container --- .github/workflows/demo-benchttp-action.yml | 2 +- internal/server/server.go | 16 +++++++++++----- main.go | 7 +++++-- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/.github/workflows/demo-benchttp-action.yml b/.github/workflows/demo-benchttp-action.yml index 4c3a6e0..6e386ee 100644 --- a/.github/workflows/demo-benchttp-action.yml +++ b/.github/workflows/demo-benchttp-action.yml @@ -14,7 +14,7 @@ jobs: go-version: 1.17 - name: Start server in the background - run: go run main.go -port=9999 & + run: go run main.go -port=9999 -ignoreStdin & - name: Run Benchttp CLI uses: benchttp/action@v1 diff --git a/internal/server/server.go b/internal/server/server.go index 2fc723a..6931b39 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -9,13 +9,17 @@ import ( ) type Server struct { - port string + port string + ignoreStdin bool requestCount int64 } -func New(port string) *Server { - return &Server{port: port} +func New(port string, ignoreStdin bool) *Server { + return &Server{ + port: port, + ignoreStdin: ignoreStdin, + } } func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -30,14 +34,16 @@ func (s *Server) ListenAndServe() error { } func (s *Server) listenStdin() { - reader := bufio.NewReader(os.Stdin) + if s.ignoreStdin { + return + } + reader := bufio.NewReader(os.Stdin) for { line, _, err := reader.ReadLine() if err != nil { fmt.Println(err) // non critical } - if string(line) == "debug" { fmt.Printf("Total requests: %d\n", s.requestCount) } diff --git a/main.go b/main.go index 33f65f4..c620da7 100644 --- a/main.go +++ b/main.go @@ -9,10 +9,13 @@ import ( const defaultPort = "9999" -var port = flag.String("port", defaultPort, "listening port") +var ( + port = flag.String("port", defaultPort, "listening port") + ignoreStdin = flag.Bool("ignoreStdin", false, "do not listen stdin while serving") +) func main() { flag.Parse() - s := server.New(*port) + s := server.New(*port, *ignoreStdin) log.Fatal(s.ListenAndServe()) }