Skip to content

Commit beebe39

Browse files
authored
Remove testutil.WaitForPort, users can use testify.Eventually (#2926)
Signed-off-by: Bogdan Drutu <[email protected]>
1 parent 91cd120 commit beebe39

File tree

5 files changed

+15
-51
lines changed

5 files changed

+15
-51
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- Remove `configparser.DecodeTypeAndName`, use `config.IDFromString` (#2869)
1313
- Remove `config.NewViper`, users should use `config.NewParser` (#2917)
1414
- Remove `testutil.WaitFor`, use `testify.Eventually` helper if needed (#2920)
15+
- Remove testutil.WaitForPort, users can use testify.Eventually (#2926)
1516

1617
## 💡 Enhancements 💡
1718

receiver/jaegerreceiver/jaeger_agent_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,14 @@ func TestJaegerHTTP(t *testing.T) {
157157
assert.NoError(t, jr.Start(context.Background(), componenttest.NewNopHost()), "Start failed")
158158

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

162169
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/sampling?service=test", port))
163170
assert.NoError(t, err, "should not have failed to make request")

testbed/testbed/test_case.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,12 @@ func (tc *TestCase) StartAgent(args ...string) {
195195
// if the endpoint is not-empty, i.e. the sender does use network (some senders
196196
// like text log writers don't).
197197
tc.WaitFor(func() bool {
198-
_, err := net.Dial(tc.LoadGenerator.sender.GetEndpoint().Network(), tc.LoadGenerator.sender.GetEndpoint().String())
199-
return err == nil
198+
conn, err := net.Dial(tc.LoadGenerator.sender.GetEndpoint().Network(), tc.LoadGenerator.sender.GetEndpoint().String())
199+
if err == nil && conn != nil {
200+
conn.Close()
201+
return true
202+
}
203+
return false
200204
})
201205
}
202206
}

testutil/testutil.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package testutil
1717
import (
1818
"bytes"
1919
"encoding/json"
20-
"fmt"
2120
"io"
2221
"io/ioutil"
2322
"net"
@@ -27,7 +26,6 @@ import (
2726
"strconv"
2827
"strings"
2928
"testing"
30-
"time"
3129

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

146-
// WaitForPort repeatedly attempts to open a local port until it either succeeds or 5 seconds pass
147-
// It is useful if you need to asynchronously start a service and wait for it to start
148-
func WaitForPort(t *testing.T, port uint16) error {
149-
t.Helper()
150-
151-
totalDuration := 5 * time.Second
152-
wait := 100 * time.Millisecond
153-
address := fmt.Sprintf("localhost:%d", port)
154-
155-
ticker := time.NewTicker(wait)
156-
defer ticker.Stop()
157-
158-
timeout := time.After(totalDuration)
159-
160-
for {
161-
select {
162-
case <-ticker.C:
163-
conn, err := net.Dial("tcp", address)
164-
if err == nil && conn != nil {
165-
conn.Close()
166-
return nil
167-
}
168-
169-
case <-timeout:
170-
return fmt.Errorf("failed to wait for port %d", port)
171-
}
172-
}
173-
}
174-
175144
// LimitedWriter is an io.Writer that will return an EOF error after MaxLen has
176145
// been reached. If MaxLen is 0, Writes will always succeed.
177146
type LimitedWriter struct {

testutil/testutil_test.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package testutil
1616

1717
import (
18-
"fmt"
1918
"net"
2019
"strconv"
2120
"testing"
@@ -34,22 +33,6 @@ func TestGetAvailablePort(t *testing.T) {
3433
testEndpointAvailable(t, "localhost:"+portStr)
3534
}
3635

37-
func TestWaitForPort(t *testing.T) {
38-
port := GetAvailablePort(t)
39-
err := WaitForPort(t, port)
40-
require.Error(t, err)
41-
42-
port = GetAvailablePort(t)
43-
l, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", port))
44-
require.NoError(t, err)
45-
46-
err = WaitForPort(t, port)
47-
require.NoError(t, err)
48-
49-
err = l.Close()
50-
require.NoError(t, err)
51-
}
52-
5336
func testEndpointAvailable(t *testing.T, endpoint string) {
5437
// Endpoint should be free.
5538
ln0, err := net.Listen("tcp", endpoint)

0 commit comments

Comments
 (0)