pgweb/pkg/connection/port_test.go

76 lines
1.3 KiB
Go
Raw Normal View History

package connection
import (
"fmt"
"net"
"os"
2015-07-15 05:38:15 +03:00
"runtime"
"testing"
"github.com/stretchr/testify/assert"
)
2018-12-04 21:42:37 +03:00
func TestIsPortAvailable(t *testing.T) {
2015-07-15 05:38:15 +03:00
if runtime.GOOS == "windows" {
t.Skip("FIXME")
}
2018-12-04 21:42:37 +03:00
assert.Equal(t, true, IsPortAvailable(30000))
serv, err := net.Listen("tcp", "127.0.0.1:30000")
if err != nil {
2016-01-22 21:12:00 +03:00
fmt.Fprintln(os.Stderr, "Unable to start test tcp listener:", err)
t.Fail()
return
}
defer serv.Close()
go func() {
for {
conn, err := serv.Accept()
if err == nil {
conn.Close()
}
serv.Close()
}
}()
2018-12-04 21:42:37 +03:00
assert.Equal(t, false, IsPortAvailable(30000))
assert.Equal(t, true, IsPortAvailable(30001))
}
2018-12-04 21:42:37 +03:00
func TestFindAvailablePort(t *testing.T) {
2015-07-15 05:38:15 +03:00
if runtime.GOOS == "windows" {
t.Skip("FIXME")
}
2018-12-04 21:42:37 +03:00
port, err := FindAvailablePort(30000, 1)
assert.Equal(t, nil, err)
assert.Equal(t, 30000, port)
serv, err := net.Listen("tcp", "127.0.0.1:30000")
if err != nil {
2016-01-22 21:12:00 +03:00
fmt.Fprintln(os.Stderr, "Unable to start test tcp listener:", err)
t.Fail()
return
}
defer serv.Close()
go func() {
for {
conn, err := serv.Accept()
if err == nil {
conn.Close()
}
}
}()
2018-12-04 21:42:37 +03:00
port, err = FindAvailablePort(30000, 0)
assert.EqualError(t, err, "No available port")
assert.Equal(t, -1, port)
2018-12-04 21:42:37 +03:00
port, err = FindAvailablePort(30000, 1)
assert.Equal(t, nil, err)
assert.Equal(t, 30001, port)
}