feat: extract helper

This commit is contained in:
Kevin Franklin Kim 2023-09-11 17:11:09 +02:00
parent 7fded0fa58
commit 8da81ee42e
No known key found for this signature in database
3 changed files with 26 additions and 16 deletions

View File

@ -1,6 +1,9 @@
package service_test
import (
"io"
"net"
"net/http"
"syscall"
"time"
)
@ -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)
}

View File

@ -1,10 +1,7 @@
package service_test
import (
"io"
"net"
"net/http"
"time"
"github.com/foomo/keel"
"github.com/foomo/keel/service"
@ -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()
}()

View File

@ -2,8 +2,6 @@ package service_test
import (
"context"
"fmt"
"io"
"net/http"
"os"
@ -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()
}()