Skip to content

Commit

Permalink
feat: extract helper
Browse files Browse the repository at this point in the history
  • Loading branch information
franklinkim committed Sep 11, 2023
1 parent 7fded0f commit 8da81ee
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
22 changes: 22 additions & 0 deletions service/helper_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package service_test

import (
"io"
"net"
"net/http"
"syscall"
"time"
)
Expand All @@ -18,3 +21,22 @@ func shutdown() {
panic(err)
}
}

func waitFor(addr string) {
if _, err := net.DialTimeout("tcp", addr, 10*time.Second); err != nil {
panic(err.Error())
}
}

func httpGet(url string) string {
resp, err := http.Get(url) //nolint:all
if err != nil {
panic(err.Error())
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
panic(err.Error())
}
return string(b)
}
12 changes: 2 additions & 10 deletions service/http_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package service_test

import (
"io"
"net"
"net/http"
"time"

"github.com/foomo/keel"
"github.com/foomo/keel/service"
Expand All @@ -26,13 +23,8 @@ func ExampleNewHTTP() {
)

go func() {
if _, err := net.DialTimeout("tcp", "localhost:8080", 10*time.Second); err != nil {
panic(err.Error())
}
resp, _ := http.Get("http://localhost:8080") //nolint:noctx
defer resp.Body.Close() //nolint:govet
b, _ := io.ReadAll(resp.Body)
l.Info(string(b))
waitFor("localhost:8080")
l.Info(httpGet("http://localhost:8080"))
shutdown()
}()

Expand Down
8 changes: 2 additions & 6 deletions service/httpreadme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package service_test

import (
"context"
"fmt"
"io"
"net/http"
"os"

Expand Down Expand Up @@ -68,10 +66,8 @@ func ExampleNewHTTPReadme() {
}))

go func() {
resp, _ := http.Get("http://localhost:9001/readme") //nolint:noctx
defer resp.Body.Close() //nolint:govet
b, _ := io.ReadAll(resp.Body)
fmt.Print(string(b))
waitFor("localhost:9001")
l.Info(httpGet("http://localhost:9001/readme"))
shutdown()
}()

Expand Down

0 comments on commit 8da81ee

Please sign in to comment.