2020-02-15 19:39:40 +00:00
|
|
|
package testtool
|
|
|
|
|
|
|
|
import (
|
2020-02-15 19:56:46 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2020-02-15 19:39:40 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetFreePort(t *testing.T) {
|
|
|
|
p, err := GetFreePort()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWaitForPort(t *testing.T) {
|
2020-02-15 19:56:46 +00:00
|
|
|
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
u, err := url.Parse(s.URL)
|
2020-03-12 21:12:46 +00:00
|
|
|
require.NoError(t, err)
|
2020-02-15 19:56:46 +00:00
|
|
|
_, port, err := net.SplitHostPort(u.Host)
|
2020-03-12 21:12:46 +00:00
|
|
|
require.NoError(t, err)
|
2020-02-15 19:56:46 +00:00
|
|
|
|
2020-02-18 21:22:00 +00:00
|
|
|
require.NoError(t, WaitForPort(t, time.Second, port))
|
2020-02-15 19:56:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWaitForPort_timeout(t *testing.T) {
|
2020-02-15 19:39:40 +00:00
|
|
|
p, err := GetFreePort()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-02-18 21:26:48 +00:00
|
|
|
err = WaitForPort(t, time.Second, p)
|
|
|
|
require.Error(t, err)
|
|
|
|
t.Log(err.Error())
|
2020-02-15 19:39:40 +00:00
|
|
|
}
|