Skip to content

Commit

Permalink
fix: better handling of concurrent http servers (#57)
Browse files Browse the repository at this point in the history
Issue #, if available: runfinch/finch#322,
specifically regarding this comment
runfinch/finch#327 (comment)

*Description of changes:*
- instantiate a new `NewServeMux` every time the test is run
- use the `SpecContext` context instead of the default background
context

*Testing done:*
- tested by using go.mod `replace ()` and running tests locally



- [x] I've reviewed the guidance in CONTRIBUTING.md


#### License Acceptance

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 license.

Signed-off-by: Justin Alvarez <[email protected]>
  • Loading branch information
pendo324 authored Apr 6, 2023
1 parent e6f31a3 commit 0ae6182
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions tests/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package tests

import (
"context"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -332,15 +331,16 @@ func Run(o *RunOption) {
gomega.Expect(mapping).Should(gomega.ContainSubstring("test-host"))
})

ginkgo.It("should add a custom host-to-IP mapping with --add-host flag with special IP", func() {
ginkgo.It("should add a custom host-to-IP mapping with --add-host flag with special IP", func(ctx ginkgo.SpecContext) {
response := "This is the expected response for --add-host special IP test."
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, response) //nolint:errcheck,gosec // Function call in server handler for testing only.
})
hostPort := fnet.GetFreePort()
s := http.Server{Addr: fmt.Sprintf(":%d", hostPort), Handler: nil, ReadTimeout: 30 * time.Second}
s := http.Server{Addr: fmt.Sprintf(":%d", hostPort), Handler: mux, ReadTimeout: 30 * time.Second}
go s.ListenAndServe() //nolint:errcheck // Asynchronously starting server for testing only.
ginkgo.DeferCleanup(s.Shutdown, context.Background())
ginkgo.DeferCleanup(s.Shutdown, ctx)
command.Run(o.BaseOpt, "run", "-d", "--name", testContainerName, "--add-host", "test-host:host-gateway",
amazonLinux2Image, "sleep", "infinity")
mapping := command.StdoutStr(o.BaseOpt, "exec", testContainerName, "cat", "/etc/hosts")
Expand Down

0 comments on commit 0ae6182

Please sign in to comment.