Skip to content

Commit

Permalink
fix: build-related fixes (#1)
Browse files Browse the repository at this point in the history
* chore: use localhost ip instead of name

* chore: fixed lint/test
  • Loading branch information
madhuravius authored Jun 3, 2023
1 parent 670c5a2 commit 3d593d0
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 22 deletions.
8 changes: 4 additions & 4 deletions internal/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func isPortActive(host string, port int) bool {
func (r *RunConfig) portsFoundCount() int {
count := 0
for _, status := range r.hostPortsReady {
if status == true {
if status {
count += 1
}
}
Expand All @@ -48,7 +48,7 @@ func (r *RunConfig) portsFoundCount() int {

func (r *RunConfig) allPortsFound() bool {
for _, status := range r.hostPortsReady {
if status != true {
if !status {
return false
}
}
Expand All @@ -58,7 +58,7 @@ func (r *RunConfig) allPortsFound() bool {
func (r *RunConfig) checkPorts() {
wg := sync.WaitGroup{}
for hostPort, status := range r.hostPortsReady {
if status == true {
if status {
// already checked to be there, continue
continue
}
Expand All @@ -69,7 +69,7 @@ func (r *RunConfig) checkPorts() {
defer wg.Done()
if isPortActive(hostPort.host, hostPort.port) {
r.hostPortsReady[hostPort] = true
r.printDebugStatement(fmt.Sprintf("Found %s:%d to be available.\n", hostPort.host, hostPort.port))
r.printDebugStatement(fmt.Sprintf("Found %s:%d to be available.", hostPort.host, hostPort.port))
}
}(&wg, hostPort, r)
}
Expand Down
12 changes: 6 additions & 6 deletions internal/ports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,32 @@ const (
)

func TestExtractHostAndPortFromStringSuccess(t *testing.T) {
host, port, err := extractHostAndPortFromString("localhost:1234")
host, port, err := extractHostAndPortFromString("127.0.0.1:1234")
assert.Nil(t, err)
assert.Equal(t, host, "localhost")
assert.Equal(t, host, "127.0.0.1")
assert.Equal(t, port, 1234)
}

func TestExtractHostAndPortFromStringFailureBadInput(t *testing.T) {
host, port, err := extractHostAndPortFromString("localhost")
host, port, err := extractHostAndPortFromString("127.0.0.1")
assert.Equal(t, err, ErrorHostPortBadFormat)
assert.Equal(t, host, "")
assert.Equal(t, port, 0)
}

func TestExtractHostAndPortFromStringFailureBadInt(t *testing.T) {
host, port, err := extractHostAndPortFromString("localhost:lol")
host, port, err := extractHostAndPortFromString("127.0.0.1:lol")
assert.NotNil(t, err)
assert.Equal(t, host, "")
assert.Equal(t, port, 0)
}

func TestIsPortActiveFalse(t *testing.T) {
// potentially flaky test, expects nothing to be running on this port
assert.False(t, isPortActive("localhost", TestPort))
assert.False(t, isPortActive("127.0.0.1", TestPort))
}

func TestIsPortActiveTrue(t *testing.T) {
defer startWebServer(TestPort).Close()
assert.True(t, isPortActive("localhost", TestPort))
assert.True(t, isPortActive("127.0.0.1", TestPort))
}
5 changes: 3 additions & 2 deletions internal/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func NewRunConfig(debug bool, portsToCheck cli.StringSlice, started time.Time, t
}
}
return &RunConfig{
debug: debug,
hostPortsReady: portsOpen,
started: started,
timeout: time.Duration(timeout) * time.Second,
Expand All @@ -48,7 +49,7 @@ func RunLoop(ctx *cli.Context) error {
return err
}

r.printDebugStatement(fmt.Sprintf("Starting ready checks (for up to %d seconds).\n", timeout))
r.printDebugStatement(fmt.Sprintf("Starting ready checks (for up to %d seconds).", timeout))
go func(r *RunConfig) {
for {
if r.allPortsFound() {
Expand All @@ -63,7 +64,7 @@ func RunLoop(ctx *cli.Context) error {
go func(r *RunConfig) {
for {
time.Sleep(DebugLogInterval)
r.printDebugStatement(fmt.Sprintf("Still running ready checks (%d/%d)...\n", r.portsFoundCount(), len(r.hostPortsReady)))
r.printDebugStatement(fmt.Sprintf("Still running ready checks (%d/%d)...", r.portsFoundCount(), len(r.hostPortsReady)))
}
}(r)

Expand Down
6 changes: 3 additions & 3 deletions internal/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestRunLoopSuccess(t *testing.T) {
err := app.Run([]string{
"--debugfalse",
"--timeout=1",
fmt.Sprintf("--host-ports=localhost:%d", TestPort),
fmt.Sprintf("--host-ports=127.0.0.1:%d", TestPort),
"run",
})
assert.Nil(t, err)
Expand All @@ -24,7 +24,7 @@ func TestRunLoopPartialSuccess(t *testing.T) {
err := app.Run([]string{
"--debugfalse",
"--timeout=1",
fmt.Sprintf("--host-ports=localhost:%d,localhost:%d", TestPort, TestPortFailure),
fmt.Sprintf("--host-ports=127.0.0.1:%d,127.0.0.1:%d", TestPort, TestPortFailure),
"run",
})
assert.NotNil(t, err)
Expand All @@ -36,7 +36,7 @@ func TestRunLoopFailure(t *testing.T) {
err := app.Run([]string{
"--debugfalse",
"--timeout=1",
fmt.Sprintf("--host-ports=localhost:%d", TestPortFailure),
fmt.Sprintf("--host-ports=127.0.0.1:%d", TestPortFailure),
"run",
})
assert.NotNil(t, err)
Expand Down
8 changes: 1 addition & 7 deletions internal/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,15 @@ package internal

import (
"fmt"
"github.com/urfave/cli/v2"
"net"
"os"
)

func startWebServer(port int) net.Listener {
l, err := net.Listen("tcp", fmt.Sprintf("localhost: %d", port))
l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
}
return l
}

func setCommonContextTestValues(ctx *cli.Context) {
ctx.Set(TimeoutKey, "1")
ctx.Set(DebugKey, "false")
}

0 comments on commit 3d593d0

Please sign in to comment.