Skip to content

Remove testutil.WaitForPort, users can use testify.Eventually #2926

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Remove `configparser.DecodeTypeAndName`, use `config.IDFromString` (#2869)
- Remove `config.NewViper`, users should use `config.NewParser` (#2917)
- Remove `testutil.WaitFor`, use `testify.Eventually` helper if needed (#2920)
- Remove testutil.WaitForPort, users can use testify.Eventually (#2926)

## 💡 Enhancements 💡

Expand Down
9 changes: 8 additions & 1 deletion receiver/jaegerreceiver/jaeger_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,14 @@ func TestJaegerHTTP(t *testing.T) {
assert.NoError(t, jr.Start(context.Background(), componenttest.NewNopHost()), "Start failed")

// allow http server to start
assert.NoError(t, testutil.WaitForPort(t, port), "WaitForPort failed")
assert.Eventually(t, func() bool {
conn, err := net.Dial("tcp", fmt.Sprintf("localhost:%d", port))
if err == nil && conn != nil {
conn.Close()
return true
}
return false
}, 10*time.Second, 5*time.Millisecond, "failed to wait for the port to be open")

resp, err := http.Get(fmt.Sprintf("http://localhost:%d/sampling?service=test", port))
assert.NoError(t, err, "should not have failed to make request")
Expand Down
8 changes: 6 additions & 2 deletions testbed/testbed/test_case.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,12 @@ func (tc *TestCase) StartAgent(args ...string) {
// if the endpoint is not-empty, i.e. the sender does use network (some senders
// like text log writers don't).
tc.WaitFor(func() bool {
_, err := net.Dial(tc.LoadGenerator.sender.GetEndpoint().Network(), tc.LoadGenerator.sender.GetEndpoint().String())
return err == nil
conn, err := net.Dial(tc.LoadGenerator.sender.GetEndpoint().Network(), tc.LoadGenerator.sender.GetEndpoint().String())
if err == nil && conn != nil {
conn.Close()
return true
}
return false
})
}
}
Expand Down
31 changes: 0 additions & 31 deletions testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package testutil
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
Expand All @@ -27,7 +26,6 @@ import (
"strconv"
"strings"
"testing"
"time"

"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -143,35 +141,6 @@ func createExclusionsList(exclusionsText string, t *testing.T) []portpair {
return exclusions
}

// WaitForPort repeatedly attempts to open a local port until it either succeeds or 5 seconds pass
// It is useful if you need to asynchronously start a service and wait for it to start
func WaitForPort(t *testing.T, port uint16) error {
t.Helper()

totalDuration := 5 * time.Second
wait := 100 * time.Millisecond
address := fmt.Sprintf("localhost:%d", port)

ticker := time.NewTicker(wait)
defer ticker.Stop()

timeout := time.After(totalDuration)

for {
select {
case <-ticker.C:
conn, err := net.Dial("tcp", address)
if err == nil && conn != nil {
conn.Close()
return nil
}

case <-timeout:
return fmt.Errorf("failed to wait for port %d", port)
}
}
}

// LimitedWriter is an io.Writer that will return an EOF error after MaxLen has
// been reached. If MaxLen is 0, Writes will always succeed.
type LimitedWriter struct {
Expand Down
17 changes: 0 additions & 17 deletions testutil/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package testutil

import (
"fmt"
"net"
"strconv"
"testing"
Expand All @@ -34,22 +33,6 @@ func TestGetAvailablePort(t *testing.T) {
testEndpointAvailable(t, "localhost:"+portStr)
}

func TestWaitForPort(t *testing.T) {
port := GetAvailablePort(t)
err := WaitForPort(t, port)
require.Error(t, err)

port = GetAvailablePort(t)
l, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", port))
require.NoError(t, err)

err = WaitForPort(t, port)
require.NoError(t, err)

err = l.Close()
require.NoError(t, err)
}

func testEndpointAvailable(t *testing.T, endpoint string) {
// Endpoint should be free.
ln0, err := net.Listen("tcp", endpoint)
Expand Down