You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2026-06-03 18:35:08 +02:00
chore: fix noctx issues (#8008)
***Description*** Fixes [noctx](https://golangci-lint.run/docs/linters/configuration/#noctx) issues and enables the linter Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com> Co-authored-by: Damien Mathieu <42@dmathieu.com>
This commit is contained in:
@@ -17,6 +17,7 @@ linters:
|
||||
- ineffassign
|
||||
- misspell
|
||||
- modernize
|
||||
- noctx
|
||||
- perfsprint
|
||||
- revive
|
||||
- staticcheck
|
||||
|
||||
@@ -39,7 +39,7 @@ func (*testGRPCServer) StreamingBidirectionalCall(testpb.TestService_StreamingBi
|
||||
}
|
||||
|
||||
func startTestGRPCServer(t *testing.T, tracer ot.Tracer) (*grpc.Server, net.Addr) {
|
||||
lis, _ := net.Listen("tcp", ":0")
|
||||
lis, _ := (&net.ListenConfig{}).Listen(t.Context(), "tcp", ":0")
|
||||
server := grpc.NewServer(
|
||||
grpc.UnaryInterceptor(otgrpc.OpenTracingServerInterceptor(tracer)),
|
||||
)
|
||||
|
||||
@@ -411,7 +411,7 @@ var _ collogpb.LogsServiceServer = (*grpcCollector)(nil)
|
||||
// If errCh is not nil, the collector will respond to Export calls with errors
|
||||
// sent on that channel. This means that if errCh is not nil Export calls will
|
||||
// block until an error is received.
|
||||
func newGRPCCollector(endpoint string, resultCh <-chan exportResult) (*grpcCollector, error) {
|
||||
func newGRPCCollector(ctx context.Context, endpoint string, resultCh <-chan exportResult) (*grpcCollector, error) {
|
||||
if endpoint == "" {
|
||||
endpoint = "localhost:0"
|
||||
}
|
||||
@@ -422,7 +422,7 @@ func newGRPCCollector(endpoint string, resultCh <-chan exportResult) (*grpcColle
|
||||
}
|
||||
|
||||
var err error
|
||||
c.listener, err = net.Listen("tcp", endpoint)
|
||||
c.listener, err = (&net.ListenConfig{}).Listen(ctx, "tcp", endpoint)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -464,7 +464,7 @@ func (c *grpcCollector) Collect() *storage {
|
||||
|
||||
func clientFactory(t *testing.T, rCh <-chan exportResult) (*client, *grpcCollector) {
|
||||
t.Helper()
|
||||
coll, err := newGRPCCollector("", rCh)
|
||||
coll, err := newGRPCCollector(t.Context(), "", rCh)
|
||||
require.NoError(t, err)
|
||||
|
||||
addr := coll.listener.Addr().String()
|
||||
@@ -564,7 +564,7 @@ func TestClient(t *testing.T) {
|
||||
|
||||
func TestConfig(t *testing.T) {
|
||||
factoryFunc := func(rCh <-chan exportResult, o ...Option) (log.Exporter, *grpcCollector) {
|
||||
coll, err := newGRPCCollector("", rCh)
|
||||
coll, err := newGRPCCollector(t.Context(), "", rCh)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := t.Context()
|
||||
@@ -1184,7 +1184,7 @@ func BenchmarkExporterExportLogs(b *testing.B) {
|
||||
const logRecordsCount = 100
|
||||
|
||||
run := func(b *testing.B) {
|
||||
coll, err := newGRPCCollector("", nil)
|
||||
coll, err := newGRPCCollector(b.Context(), "", nil)
|
||||
require.NoError(b, err)
|
||||
b.Cleanup(func() {
|
||||
coll.srv.Stop()
|
||||
|
||||
@@ -54,7 +54,7 @@ func nextExporterID() int64 {
|
||||
}
|
||||
|
||||
// newHTTPClient creates a new HTTP log client.
|
||||
func newHTTPClient(cfg config) (*client, error) {
|
||||
func newHTTPClient(ctx context.Context, cfg config) (*client, error) {
|
||||
if cfg.insecure.Value && cfg.tlsCfg.Value != nil {
|
||||
return nil, errInsecureEndpointWithTLS
|
||||
}
|
||||
@@ -88,7 +88,7 @@ func newHTTPClient(cfg config) (*client, error) {
|
||||
u.Scheme = "http"
|
||||
}
|
||||
// Body is set when this is cloned during upload.
|
||||
req, err := http.NewRequest(http.MethodPost, u.String(), http.NoBody)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), http.NoBody)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -263,7 +263,7 @@ func newHTTPCollector(
|
||||
opt(c)
|
||||
}
|
||||
|
||||
c.listener, err = net.Listen("tcp", u.Host)
|
||||
c.listener, err = (&net.ListenConfig{}).Listen(context.Background(), "tcp", u.Host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -492,7 +492,7 @@ func TestClient(t *testing.T) {
|
||||
addr := coll.Addr().String()
|
||||
opts := []Option{WithEndpoint(addr), WithInsecure()}
|
||||
cfg := newConfig(opts)
|
||||
client, err := newHTTPClient(cfg)
|
||||
client, err := newHTTPClient(t.Context(), cfg)
|
||||
require.NoError(t, err)
|
||||
return client, coll
|
||||
}
|
||||
@@ -573,7 +573,7 @@ func TestClientWithHTTPCollectorRespondingPlainText(t *testing.T) {
|
||||
addr := coll.Addr().String()
|
||||
opts := []Option{WithEndpoint(addr), WithInsecure()}
|
||||
cfg := newConfig(opts)
|
||||
client, err := newHTTPClient(cfg)
|
||||
client, err := newHTTPClient(t.Context(), cfg)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NoError(t, client.uploadLogs(ctx, make([]*lpb.ResourceLogs, 1)))
|
||||
@@ -850,7 +850,7 @@ func newFactory(t testing.TB) func(rCh <-chan exportResult) (*client, *httpColle
|
||||
addr := coll.Addr().String()
|
||||
opts := []Option{WithEndpoint(addr), WithInsecure()}
|
||||
cfg := newConfig(opts)
|
||||
client, err := newHTTPClient(cfg)
|
||||
client, err := newHTTPClient(t.Context(), cfg)
|
||||
require.NoError(t, err)
|
||||
return client, coll, addr
|
||||
}
|
||||
|
||||
@@ -26,9 +26,9 @@ var _ log.Exporter = (*Exporter)(nil)
|
||||
//
|
||||
// It is recommended to use it with a [BatchProcessor]
|
||||
// or other processor exporting records asynchronously.
|
||||
func New(_ context.Context, options ...Option) (*Exporter, error) {
|
||||
func New(ctx context.Context, options ...Option) (*Exporter, error) {
|
||||
cfg := newConfig(options)
|
||||
c, err := newHTTPClient(cfg)
|
||||
c, err := newHTTPClient(ctx, cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ func newClient(cfg oconf.Config) (*client, error) {
|
||||
u.Scheme = "http"
|
||||
}
|
||||
// Body is set when this is cloned during upload.
|
||||
req, err := http.NewRequest(http.MethodPost, u.String(), http.NoBody)
|
||||
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, u.String(), http.NoBody)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -205,7 +205,7 @@ func TestNewCollectorOnBadConnection(t *testing.T) {
|
||||
t.Skipf("Skipping this long running test")
|
||||
}
|
||||
|
||||
ln, err := net.Listen("tcp", "localhost:0")
|
||||
ln, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "localhost:0")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to grab an available port: %v", err)
|
||||
}
|
||||
|
||||
@@ -162,7 +162,7 @@ func runMockCollectorAtEndpoint(tb testing.TB, endpoint string) *mockCollector {
|
||||
|
||||
func runMockCollectorWithConfig(tb testing.TB, mockConfig *mockConfig) *mockCollector {
|
||||
tb.Helper()
|
||||
ln, err := net.Listen("tcp", mockConfig.endpoint)
|
||||
ln, err := (&net.ListenConfig{}).Listen(tb.Context(), "tcp", mockConfig.endpoint)
|
||||
require.NoError(tb, err, "net.Listen")
|
||||
|
||||
srv := grpc.NewServer()
|
||||
|
||||
@@ -259,7 +259,7 @@ func (d *client) UploadTraces(ctx context.Context, protoSpans []*tracepb.Resourc
|
||||
|
||||
func (d *client) newRequest(body []byte) (request, error) {
|
||||
u := url.URL{Scheme: d.getScheme(), Host: d.cfg.Endpoint, Path: d.cfg.URLPath}
|
||||
r, err := http.NewRequest(http.MethodPost, u.String(), http.NoBody)
|
||||
r, err := http.NewRequestWithContext(context.Background(), http.MethodPost, u.String(), http.NoBody)
|
||||
if err != nil {
|
||||
return request{Request: r}, err
|
||||
}
|
||||
|
||||
@@ -215,7 +215,7 @@ func (c *mockCollectorConfig) fillInDefaults() {
|
||||
|
||||
func runMockCollector(tb testing.TB, cfg mockCollectorConfig) *mockCollector {
|
||||
cfg.fillInDefaults()
|
||||
ln, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", cfg.Port))
|
||||
ln, err := (&net.ListenConfig{}).Listen(tb.Context(), "tcp", fmt.Sprintf("localhost:%d", cfg.Port))
|
||||
require.NoError(tb, err)
|
||||
_, portStr, err := net.SplitHostPort(ln.Addr().String())
|
||||
require.NoError(tb, err)
|
||||
|
||||
@@ -41,7 +41,7 @@ type producerFunc func(context.Context) ([]metricdata.ScopeMetrics, error)
|
||||
func (f producerFunc) Produce(ctx context.Context) ([]metricdata.ScopeMetrics, error) { return f(ctx) }
|
||||
|
||||
// Helper: scrape with ContinueOnError and return body + status.
|
||||
func scrapeWithContinueOnError(reg *prometheus.Registry) (int, string) {
|
||||
func scrapeWithContinueOnError(ctx context.Context, reg *prometheus.Registry) (int, string) {
|
||||
h := promhttp.HandlerFor(
|
||||
reg,
|
||||
promhttp.HandlerOpts{
|
||||
@@ -50,7 +50,7 @@ func scrapeWithContinueOnError(reg *prometheus.Registry) (int, string) {
|
||||
)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/metrics", http.NoBody)
|
||||
req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/metrics", http.NoBody)
|
||||
h.ServeHTTP(rr, req)
|
||||
|
||||
return rr.Code, rr.Body.String()
|
||||
@@ -1113,7 +1113,7 @@ func TestDuplicateMetrics(t *testing.T) {
|
||||
// 2) Also assert what users will see if they opt into ContinueOnError.
|
||||
// Compare the HTTP body to an expected file that contains only the valid series
|
||||
// (e.g., "target_info" and any non-conflicting families).
|
||||
status, body := scrapeWithContinueOnError(registry)
|
||||
status, body := scrapeWithContinueOnError(t.Context(), registry)
|
||||
require.Equal(t, http.StatusOK, status)
|
||||
|
||||
matched := false
|
||||
|
||||
@@ -98,7 +98,7 @@ func startMockZipkinCollector(t *testing.T) *mockZipkinCollector {
|
||||
t: t,
|
||||
closing: false,
|
||||
}
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
listener, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0")
|
||||
require.NoError(t, err)
|
||||
collector.url = fmt.Sprintf("http://%s", listener.Addr().String())
|
||||
server := &http.Server{
|
||||
|
||||
@@ -127,7 +127,7 @@ func TestExtractValidBaggage(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
mapCarr := propagation.MapCarrier{}
|
||||
mapCarr["baggage"] = tt.header
|
||||
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
|
||||
req, _ := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://example.com", http.NoBody)
|
||||
req.Header.Set("baggage", tt.header)
|
||||
|
||||
// test with http header carrier (which implements ValuesGetter)
|
||||
@@ -313,7 +313,7 @@ func TestExtractValidMultipleBaggageHeaders(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
|
||||
req, _ := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://example.com", http.NoBody)
|
||||
req.Header["Baggage"] = tt.headers
|
||||
|
||||
ctx := t.Context()
|
||||
@@ -378,7 +378,7 @@ func TestExtractInvalidDistributedContextFromHTTPReq(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
|
||||
req, _ := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://example.com", http.NoBody)
|
||||
req.Header.Set("baggage", tt.header)
|
||||
|
||||
expected := tt.has.Baggage(t)
|
||||
@@ -431,7 +431,7 @@ func TestInjectBaggageToHTTPReq(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
|
||||
req, _ := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://example.com", http.NoBody)
|
||||
ctx := baggage.ContextWithBaggage(t.Context(), tt.mems.Baggage(t))
|
||||
propagator.Inject(ctx, propagation.HeaderCarrier(req.Header))
|
||||
|
||||
@@ -478,7 +478,7 @@ func TestBaggageInjectExtractRoundtrip(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
b := tt.mems.Baggage(t)
|
||||
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
|
||||
req, _ := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://example.com", http.NoBody)
|
||||
ctx := baggage.ContextWithBaggage(t.Context(), b)
|
||||
propagator.Inject(ctx, propagation.HeaderCarrier(req.Header))
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ func BenchmarkExtract(b *testing.B) {
|
||||
|
||||
func extractSubBenchmarks(b *testing.B, fn func(*testing.B, *http.Request)) {
|
||||
b.Run("Sampled", func(b *testing.B) {
|
||||
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
|
||||
req, _ := http.NewRequestWithContext(b.Context(), http.MethodGet, "http://example.com", http.NoBody)
|
||||
req.Header.Set("traceparent", "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01")
|
||||
b.ReportAllocs()
|
||||
|
||||
@@ -64,14 +64,14 @@ func extractSubBenchmarks(b *testing.B, fn func(*testing.B, *http.Request)) {
|
||||
})
|
||||
|
||||
b.Run("BogusVersion", func(b *testing.B) {
|
||||
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
|
||||
req, _ := http.NewRequestWithContext(b.Context(), http.MethodGet, "http://example.com", http.NoBody)
|
||||
req.Header.Set("traceparent", "qw-00000000000000000000000000000000-0000000000000000-01")
|
||||
b.ReportAllocs()
|
||||
fn(b, req)
|
||||
})
|
||||
|
||||
b.Run("FutureAdditionalData", func(b *testing.B) {
|
||||
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
|
||||
req, _ := http.NewRequestWithContext(b.Context(), http.MethodGet, "http://example.com", http.NoBody)
|
||||
req.Header.Set("traceparent", "02-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-09-XYZxsf09")
|
||||
b.ReportAllocs()
|
||||
fn(b, req)
|
||||
|
||||
@@ -141,7 +141,10 @@ func TestHTTPServerRequest(t *testing.T) {
|
||||
srvPort, err := strconv.ParseInt(srvURL.Port(), 10, 32)
|
||||
require.NoError(t, err)
|
||||
|
||||
resp, err := srv.Client().Get(srv.URL)
|
||||
r, err := http.NewRequestWithContext(t.Context(), http.MethodGet, srv.URL, http.NoBody)
|
||||
require.NoError(t, err)
|
||||
|
||||
resp, err := srv.Client().Do(r)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, resp.Body.Close())
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"strconv"
|
||||
"testing"
|
||||
@@ -74,12 +75,12 @@ func TestNetServerNilAddr(t *testing.T) {
|
||||
assert.ElementsMatch(t, expected, got)
|
||||
}
|
||||
|
||||
func newTCPListener() (net.Listener, error) {
|
||||
return net.Listen("tcp4", "127.0.0.1:0")
|
||||
func newTCPListener(ctx context.Context) (net.Listener, error) {
|
||||
return (&net.ListenConfig{}).Listen(ctx, "tcp4", "127.0.0.1:0")
|
||||
}
|
||||
|
||||
func TestNetServerTCP(t *testing.T) {
|
||||
ln, err := newTCPListener()
|
||||
ln, err := newTCPListener(t.Context())
|
||||
require.NoError(t, err)
|
||||
defer func() { require.NoError(t, ln.Close()) }()
|
||||
|
||||
@@ -145,13 +146,13 @@ func TestNetClientNilAddr(t *testing.T) {
|
||||
assert.ElementsMatch(t, expected, got)
|
||||
}
|
||||
|
||||
func newTCPConn() (net.Conn, net.Listener, error) {
|
||||
ln, err := newTCPListener()
|
||||
func newTCPConn(ctx context.Context) (net.Conn, net.Listener, error) {
|
||||
ln, err := newTCPListener(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
conn, err := net.Dial("tcp4", ln.Addr().String())
|
||||
conn, err := (&net.Dialer{}).DialContext(ctx, "tcp4", ln.Addr().String())
|
||||
if err != nil {
|
||||
_ = ln.Close()
|
||||
return nil, nil, err
|
||||
@@ -161,7 +162,7 @@ func newTCPConn() (net.Conn, net.Listener, error) {
|
||||
}
|
||||
|
||||
func TestNetClientTCP(t *testing.T) {
|
||||
conn, ln, err := newTCPConn()
|
||||
conn, ln, err := newTCPConn(t.Context())
|
||||
require.NoError(t, err)
|
||||
defer func() { require.NoError(t, ln.Close()) }()
|
||||
defer func() { require.NoError(t, conn.Close()) }()
|
||||
@@ -196,7 +197,7 @@ type remoteOnlyConn struct{ net.Conn }
|
||||
func (remoteOnlyConn) LocalAddr() net.Addr { return nil }
|
||||
|
||||
func TestNetClientTCPNilLocal(t *testing.T) {
|
||||
conn, ln, err := newTCPConn()
|
||||
conn, ln, err := newTCPConn(t.Context())
|
||||
require.NoError(t, err)
|
||||
defer func() { require.NoError(t, ln.Close()) }()
|
||||
defer func() { require.NoError(t, conn.Close()) }()
|
||||
|
||||
@@ -141,7 +141,10 @@ func TestHTTPServerRequest(t *testing.T) {
|
||||
srvPort, err := strconv.ParseInt(srvURL.Port(), 10, 32)
|
||||
require.NoError(t, err)
|
||||
|
||||
resp, err := srv.Client().Get(srv.URL)
|
||||
r, err := http.NewRequestWithContext(t.Context(), http.MethodGet, srv.URL, http.NoBody)
|
||||
require.NoError(t, err)
|
||||
|
||||
resp, err := srv.Client().Do(r)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, resp.Body.Close())
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"strconv"
|
||||
"testing"
|
||||
@@ -74,12 +75,12 @@ func TestNetServerNilAddr(t *testing.T) {
|
||||
assert.ElementsMatch(t, expected, got)
|
||||
}
|
||||
|
||||
func newTCPListener() (net.Listener, error) {
|
||||
return net.Listen("tcp4", "127.0.0.1:0")
|
||||
func newTCPListener(ctx context.Context) (net.Listener, error) {
|
||||
return (&net.ListenConfig{}).Listen(ctx, "tcp4", "127.0.0.1:0")
|
||||
}
|
||||
|
||||
func TestNetServerTCP(t *testing.T) {
|
||||
ln, err := newTCPListener()
|
||||
ln, err := newTCPListener(t.Context())
|
||||
require.NoError(t, err)
|
||||
defer func() { require.NoError(t, ln.Close()) }()
|
||||
|
||||
@@ -145,13 +146,13 @@ func TestNetClientNilAddr(t *testing.T) {
|
||||
assert.ElementsMatch(t, expected, got)
|
||||
}
|
||||
|
||||
func newTCPConn() (net.Conn, net.Listener, error) {
|
||||
ln, err := newTCPListener()
|
||||
func newTCPConn(ctx context.Context) (net.Conn, net.Listener, error) {
|
||||
ln, err := newTCPListener(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
conn, err := net.Dial("tcp4", ln.Addr().String())
|
||||
conn, err := (&net.Dialer{}).DialContext(ctx, "tcp4", ln.Addr().String())
|
||||
if err != nil {
|
||||
_ = ln.Close()
|
||||
return nil, nil, err
|
||||
@@ -161,7 +162,7 @@ func newTCPConn() (net.Conn, net.Listener, error) {
|
||||
}
|
||||
|
||||
func TestNetClientTCP(t *testing.T) {
|
||||
conn, ln, err := newTCPConn()
|
||||
conn, ln, err := newTCPConn(t.Context())
|
||||
require.NoError(t, err)
|
||||
defer func() { require.NoError(t, ln.Close()) }()
|
||||
defer func() { require.NoError(t, conn.Close()) }()
|
||||
@@ -196,7 +197,7 @@ type remoteOnlyConn struct{ net.Conn }
|
||||
func (remoteOnlyConn) LocalAddr() net.Addr { return nil }
|
||||
|
||||
func TestNetClientTCPNilLocal(t *testing.T) {
|
||||
conn, ln, err := newTCPConn()
|
||||
conn, ln, err := newTCPConn(t.Context())
|
||||
require.NoError(t, err)
|
||||
defer func() { require.NoError(t, ln.Close()) }()
|
||||
defer func() { require.NoError(t, conn.Close()) }()
|
||||
|
||||
@@ -142,7 +142,10 @@ func TestHTTPServerRequest(t *testing.T) {
|
||||
srvPort, err := strconv.ParseInt(srvURL.Port(), 10, 32)
|
||||
require.NoError(t, err)
|
||||
|
||||
resp, err := srv.Client().Get(srv.URL)
|
||||
r, err := http.NewRequestWithContext(t.Context(), http.MethodGet, srv.URL, http.NoBody)
|
||||
require.NoError(t, err)
|
||||
|
||||
resp, err := srv.Client().Do(r)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, resp.Body.Close())
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"strconv"
|
||||
"testing"
|
||||
@@ -74,12 +75,12 @@ func TestNetServerNilAddr(t *testing.T) {
|
||||
assert.ElementsMatch(t, expected, got)
|
||||
}
|
||||
|
||||
func newTCPListener() (net.Listener, error) {
|
||||
return net.Listen("tcp4", "127.0.0.1:0")
|
||||
func newTCPListener(ctx context.Context) (net.Listener, error) {
|
||||
return (&net.ListenConfig{}).Listen(ctx, "tcp4", "127.0.0.1:0")
|
||||
}
|
||||
|
||||
func TestNetServerTCP(t *testing.T) {
|
||||
ln, err := newTCPListener()
|
||||
ln, err := newTCPListener(t.Context())
|
||||
require.NoError(t, err)
|
||||
defer func() { require.NoError(t, ln.Close()) }()
|
||||
|
||||
@@ -145,13 +146,13 @@ func TestNetClientNilAddr(t *testing.T) {
|
||||
assert.ElementsMatch(t, expected, got)
|
||||
}
|
||||
|
||||
func newTCPConn() (net.Conn, net.Listener, error) {
|
||||
ln, err := newTCPListener()
|
||||
func newTCPConn(ctx context.Context) (net.Conn, net.Listener, error) {
|
||||
ln, err := newTCPListener(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
conn, err := net.Dial("tcp4", ln.Addr().String())
|
||||
conn, err := (&net.Dialer{}).DialContext(ctx, "tcp4", ln.Addr().String())
|
||||
if err != nil {
|
||||
_ = ln.Close()
|
||||
return nil, nil, err
|
||||
@@ -161,7 +162,7 @@ func newTCPConn() (net.Conn, net.Listener, error) {
|
||||
}
|
||||
|
||||
func TestNetClientTCP(t *testing.T) {
|
||||
conn, ln, err := newTCPConn()
|
||||
conn, ln, err := newTCPConn(t.Context())
|
||||
require.NoError(t, err)
|
||||
defer func() { require.NoError(t, ln.Close()) }()
|
||||
defer func() { require.NoError(t, conn.Close()) }()
|
||||
@@ -196,7 +197,7 @@ type remoteOnlyConn struct{ net.Conn }
|
||||
func (remoteOnlyConn) LocalAddr() net.Addr { return nil }
|
||||
|
||||
func TestNetClientTCPNilLocal(t *testing.T) {
|
||||
conn, ln, err := newTCPConn()
|
||||
conn, ln, err := newTCPConn(t.Context())
|
||||
require.NoError(t, err)
|
||||
defer func() { require.NoError(t, ln.Close()) }()
|
||||
defer func() { require.NoError(t, conn.Close()) }()
|
||||
|
||||
Reference in New Issue
Block a user