Skip to content

Commit

Permalink
prevent log access races in tests
Browse files Browse the repository at this point in the history
suppress false postive linter warns
  • Loading branch information
umputun committed May 22, 2024
1 parent 88882e7 commit 40724fd
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions app/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"os"
"strconv"
"sync"
"syscall"
"testing"
"time"
Expand All @@ -20,7 +21,16 @@ import (
"github.com/umputun/reproxy/lib"
)

var setupLoggerOnce sync.Once

func setupLogger() {
setupLoggerOnce.Do(func() {
log.Setup(log.Debug, log.CallerFile, log.CallerFunc, log.Msec, log.LevelBraces)
})
}

func Test_Main(t *testing.T) {
setupLogger()

port := 40000 + int(rand.Int31n(10000))
os.Args = []string{"test", "--static.enabled",
Expand All @@ -45,7 +55,6 @@ func Test_Main(t *testing.T) {
close(finished)
}()

// defer cleanup because require check below can fail
defer func() {
close(done)
<-finished
Expand Down Expand Up @@ -96,6 +105,8 @@ func Test_Main(t *testing.T) {
}

func Test_MainWithSSL(t *testing.T) {
setupLogger()

port := 40000 + int(rand.Int31n(10000))
os.Args = []string{"test", "--static.enabled",
"--static.rule=*,/svc1, https://httpbin.org/get,https://feedmaster.umputun.com/ping",
Expand All @@ -118,7 +129,6 @@ func Test_MainWithSSL(t *testing.T) {
close(finished)
}()

// defer cleanup because require check below can fail
defer func() {
close(done)
<-finished
Expand All @@ -128,7 +138,6 @@ func Test_MainWithSSL(t *testing.T) {
time.Sleep(time.Second)

client := http.Client{
// allow self-signed certificate
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Expand Down Expand Up @@ -156,6 +165,8 @@ func Test_MainWithSSL(t *testing.T) {
}

func Test_MainWithPlugin(t *testing.T) {
setupLogger()

proxyPort := rand.Intn(10000) + 40000
conductorPort := rand.Intn(10000) + 40000
os.Args = []string{"test", "--static.enabled",
Expand All @@ -181,7 +192,6 @@ func Test_MainWithPlugin(t *testing.T) {
close(finished)
}()

// defer cleanup because require check below can fail
defer func() {
close(done)
<-finished
Expand Down Expand Up @@ -222,6 +232,7 @@ func Test_MainWithPlugin(t *testing.T) {
}

func Test_listenAddress(t *testing.T) {
setupLogger()

tbl := []struct {
addr string
Expand Down Expand Up @@ -254,6 +265,8 @@ func Test_listenAddress(t *testing.T) {
}

func Test_redirHTTPPort(t *testing.T) {
setupLogger()

tbl := []struct {
port int
env string
Expand Down Expand Up @@ -281,6 +294,7 @@ func Test_redirHTTPPort(t *testing.T) {
}

func Test_sizeParse(t *testing.T) {
setupLogger()

tbl := []struct {
inp string
Expand Down Expand Up @@ -315,7 +329,6 @@ func Test_sizeParse(t *testing.T) {
}

func waitForHTTPServerStart(port int) {
// wait for up to 10 seconds for server to start before returning it
client := http.Client{Timeout: time.Second}
for i := 0; i < 100; i++ {
time.Sleep(time.Millisecond * 100)
Expand All @@ -328,8 +341,7 @@ func waitForHTTPServerStart(port int) {

type TestPlugin struct{}

// nolint
func (h *TestPlugin) HeaderThing(req *lib.Request, res *lib.Response) (err error) {
func (h *TestPlugin) HeaderThing(req *lib.Request, res *lib.Response) error { //nolint:unparam // doesn't fail in tests
log.Printf("req: %+v", req)
res.HeadersIn = http.Header{}
res.HeadersIn.Add("inh", "val")
Expand All @@ -339,8 +351,7 @@ func (h *TestPlugin) HeaderThing(req *lib.Request, res *lib.Response) (err error
return nil
}

// nolint
func (h *TestPlugin) ErrorThing(req lib.Request, res *lib.Response) (err error) {
func (h *TestPlugin) ErrorThing(req lib.Request, res *lib.Response) error { //nolint:unparam // doesn't fail in tests
log.Printf("req: %+v", req)
if req.URL == "/fail" {
res.StatusCode = 500
Expand All @@ -353,6 +364,7 @@ func (h *TestPlugin) ErrorThing(req lib.Request, res *lib.Response) (err error)
}

func Test_splitAtCommas(t *testing.T) {
setupLogger()

tbl := []struct {
inp string
Expand All @@ -377,6 +389,8 @@ func Test_splitAtCommas(t *testing.T) {
}

func Test_makeBasicAuth(t *testing.T) {
setupLogger()

pf := `test:$2y$05$zMxDmK65SjcH2vJQNopVSO/nE8ngVLx65RoETyHpez7yTS/8CLEiW
test2:$2y$05$TLQqHh6VT4JxysdKGPOlJeSkkMsv.Ku/G45i7ssIm80XuouCrES12
bad bad`
Expand Down

0 comments on commit 40724fd

Please sign in to comment.