From 1c324a8918405030143c14fb4c86815be2321385 Mon Sep 17 00:00:00 2001 From: iamrajiv Date: Tue, 15 Jul 2025 13:39:48 +0530 Subject: [PATCH 1/7] Replaced all grpc.DialContext calls with dialContextBlocking --- pkg/control/v1/client/dial.go | 28 ++++++++++++++++++++++++++- pkg/control/v1/client/dial_windows.go | 28 ++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/pkg/control/v1/client/dial.go b/pkg/control/v1/client/dial.go index 31db92bd713..4ce3037ae83 100644 --- a/pkg/control/v1/client/dial.go +++ b/pkg/control/v1/client/dial.go @@ -14,11 +14,12 @@ import ( "github.com/elastic/elastic-agent/pkg/control" "google.golang.org/grpc" + "google.golang.org/grpc/connectivity" "google.golang.org/grpc/credentials/insecure" ) func dialContext(ctx context.Context) (*grpc.ClientConn, error) { - return grpc.DialContext( //nolint:staticcheck // Only the deprecated version allows this call to be blocking + return dialContextBlocking( ctx, strings.TrimPrefix(control.Address(), "unix://"), grpc.WithTransportCredentials(insecure.NewCredentials()), @@ -26,6 +27,31 @@ func dialContext(ctx context.Context) (*grpc.ClientConn, error) { ) } +// dialContextBlocking creates a blocking connection equivalent to the deprecated grpc.DialContext +func dialContextBlocking(ctx context.Context, target string, opts ...grpc.DialOption) (*grpc.ClientConn, error) { + // Create the connection using the new API + conn, err := grpc.NewClient(target, opts...) + if err != nil { + return nil, err + } + + // Keep waiting until we're connected or context is cancelled + for { + state := conn.GetState() + if state == connectivity.Ready { + return conn, nil + } + if state == connectivity.TransientFailure || state == connectivity.Shutdown { + conn.Close() + return nil, ctx.Err() + } + if !conn.WaitForStateChange(ctx, state) { + conn.Close() + return nil, ctx.Err() + } + } +} + func dialer(ctx context.Context, addr string) (net.Conn, error) { var d net.Dialer return d.DialContext(ctx, "unix", addr) diff --git a/pkg/control/v1/client/dial_windows.go b/pkg/control/v1/client/dial_windows.go index 6cb76ac8637..cacd1a6da21 100644 --- a/pkg/control/v1/client/dial_windows.go +++ b/pkg/control/v1/client/dial_windows.go @@ -13,13 +13,14 @@ import ( "github.com/elastic/elastic-agent/pkg/control" "google.golang.org/grpc" + "google.golang.org/grpc/connectivity" "google.golang.org/grpc/credentials/insecure" "github.com/elastic/elastic-agent-libs/api/npipe" ) func dialContext(ctx context.Context) (*grpc.ClientConn, error) { - return grpc.DialContext( //nolint:staticcheck // Only the deprecated version allows this call to be blocking + return dialContextBlocking( ctx, control.Address(), grpc.WithTransportCredentials(insecure.NewCredentials()), @@ -27,6 +28,31 @@ func dialContext(ctx context.Context) (*grpc.ClientConn, error) { ) } +// dialContextBlocking creates a blocking connection equivalent to the deprecated grpc.DialContext +func dialContextBlocking(ctx context.Context, target string, opts ...grpc.DialOption) (*grpc.ClientConn, error) { + // Create the connection using the new API + conn, err := grpc.NewClient(target, opts...) + if err != nil { + return nil, err + } + + // Keep waiting until we're connected or context is cancelled + for { + state := conn.GetState() + if state == connectivity.Ready { + return conn, nil + } + if state == connectivity.TransientFailure || state == connectivity.Shutdown { + conn.Close() + return nil, ctx.Err() + } + if !conn.WaitForStateChange(ctx, state) { + conn.Close() + return nil, ctx.Err() + } + } +} + func dialer(ctx context.Context, addr string) (net.Conn, error) { return npipe.DialContext(npipe.TransformString(addr))(ctx, "", "") } From 314ba1d77a3b97c58eca2bdbf0c90d7972029113 Mon Sep 17 00:00:00 2001 From: iamrajiv Date: Tue, 15 Jul 2025 13:39:54 +0530 Subject: [PATCH 2/7] Replaced all grpc.DialContext calls with dialContextBlocking --- pkg/control/v2/client/dial.go | 28 ++++++++++++++++++++++++++- pkg/control/v2/client/dial_windows.go | 28 ++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/pkg/control/v2/client/dial.go b/pkg/control/v2/client/dial.go index 7647a4c9cee..05bb9834c48 100644 --- a/pkg/control/v2/client/dial.go +++ b/pkg/control/v2/client/dial.go @@ -12,6 +12,7 @@ import ( "strings" "google.golang.org/grpc" + "google.golang.org/grpc/connectivity" "google.golang.org/grpc/credentials/insecure" ) @@ -21,7 +22,32 @@ func dialContext(ctx context.Context, address string, maxMsgSize int, opts ...gr grpc.WithContextDialer(dialer), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize)), ) - return grpc.DialContext(ctx, address, opts...) //nolint:staticcheck // Only the deprecated version allows this call to be blocking + return dialContextBlocking(ctx, address, opts...) +} + +// dialContextBlocking creates a blocking connection equivalent to the deprecated grpc.DialContext +func dialContextBlocking(ctx context.Context, target string, opts ...grpc.DialOption) (*grpc.ClientConn, error) { + // Create the connection using the new API + conn, err := grpc.NewClient(target, opts...) + if err != nil { + return nil, err + } + + // Keep waiting until we're connected or context is cancelled + for { + state := conn.GetState() + if state == connectivity.Ready { + return conn, nil + } + if state == connectivity.TransientFailure || state == connectivity.Shutdown { + conn.Close() + return nil, ctx.Err() + } + if !conn.WaitForStateChange(ctx, state) { + conn.Close() + return nil, ctx.Err() + } + } } func dialer(ctx context.Context, addr string) (net.Conn, error) { diff --git a/pkg/control/v2/client/dial_windows.go b/pkg/control/v2/client/dial_windows.go index 0ab339e1fdc..af1eab0174f 100644 --- a/pkg/control/v2/client/dial_windows.go +++ b/pkg/control/v2/client/dial_windows.go @@ -12,6 +12,7 @@ import ( "strings" "google.golang.org/grpc" + "google.golang.org/grpc/connectivity" "google.golang.org/grpc/credentials/insecure" "github.com/elastic/elastic-agent-libs/api/npipe" @@ -23,7 +24,32 @@ func dialContext(ctx context.Context, address string, maxMsgSize int, opts ...gr grpc.WithContextDialer(dialer), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize)), ) - return grpc.DialContext(ctx, address, opts...) //nolint:staticcheck // Only the deprecated version allows this call to be blocking + return dialContextBlocking(ctx, address, opts...) +} + +// dialContextBlocking creates a blocking connection equivalent to the deprecated grpc.DialContext +func dialContextBlocking(ctx context.Context, target string, opts ...grpc.DialOption) (*grpc.ClientConn, error) { + // Create the connection using the new API + conn, err := grpc.NewClient(target, opts...) + if err != nil { + return nil, err + } + + // Keep waiting until we're connected or context is cancelled + for { + state := conn.GetState() + if state == connectivity.Ready { + return conn, nil + } + if state == connectivity.TransientFailure || state == connectivity.Shutdown { + conn.Close() + return nil, ctx.Err() + } + if !conn.WaitForStateChange(ctx, state) { + conn.Close() + return nil, ctx.Err() + } + } } func dialer(ctx context.Context, addr string) (net.Conn, error) { From 00e7ad6ee5b1c792d24afb02d3e85bef50175e85 Mon Sep 17 00:00:00 2001 From: iamrajiv Date: Tue, 15 Jul 2025 13:44:46 +0530 Subject: [PATCH 3/7] Replaced all grpc.DialContext calls with dialContextBlocking --- ...-deprecated-grpc-connection-functions.yaml | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 changelog/fragments/1752567204-stop-using-deprecated-grpc-connection-functions.yaml diff --git a/changelog/fragments/1752567204-stop-using-deprecated-grpc-connection-functions.yaml b/changelog/fragments/1752567204-stop-using-deprecated-grpc-connection-functions.yaml new file mode 100644 index 00000000000..b22250ac2dc --- /dev/null +++ b/changelog/fragments/1752567204-stop-using-deprecated-grpc-connection-functions.yaml @@ -0,0 +1,35 @@ +# Kind can be one of: +# - breaking-change: a change to previously-documented behavior +# - deprecation: functionality that is being removed in a later release +# - bug-fix: fixes a problem in a previous version +# - enhancement: extends functionality but does not break or fix existing behavior +# - feature: new functionality +# - known-issue: problems that we are aware of in a given version +# - security: impacts on the security of a product or a user's deployment. +# - upgrade: important information for someone upgrading from a prior version +# - other: does not fit into any of the other categories +kind: enhancement + +# Change summary; a 80ish characters long description of the change. +summary: Stop using deprecated grpc connection establishment functions + +# Long description; in case the summary is not enough to describe the change +# this field accommodate a description without length limits. +# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment. +description: | + Migrated from deprecated grpc.DialContext to grpc.NewClient while maintaining + blocking connection behavior. Created custom wrapper functions to preserve + the same functionality without using deprecated APIs. + +# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc. +component: "elastic-agent" + +# PR URL; optional; the PR number that added the changeset. +# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added. +# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number. +# Please provide it if you are adding a fragment for a different PR. +#pr: https://github.com/elastic/elastic-agent/pull/XXXX + +# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of). +# If not present is automatically filled by the tooling with the issue linked to the PR number. +issue: https://github.com/elastic/elastic-agent/issues/5453 \ No newline at end of file From 3ee19a05c47de26045199bfc8602343ca93316fd Mon Sep 17 00:00:00 2001 From: iamrajiv Date: Thu, 24 Jul 2025 00:50:03 +0530 Subject: [PATCH 4/7] Implement DialContextBlocking function and update client dial methods to use it --- pkg/control/dial.go | 37 +++++++++ pkg/control/dial_test.go | 112 ++++++++++++++++++++++++++ pkg/control/v1/client/dial.go | 28 +------ pkg/control/v1/client/dial_windows.go | 28 +------ pkg/control/v2/client/dial.go | 30 +------ pkg/control/v2/client/dial_windows.go | 30 +------ 6 files changed, 157 insertions(+), 108 deletions(-) create mode 100644 pkg/control/dial.go create mode 100644 pkg/control/dial_test.go diff --git a/pkg/control/dial.go b/pkg/control/dial.go new file mode 100644 index 00000000000..dc774e7aa2d --- /dev/null +++ b/pkg/control/dial.go @@ -0,0 +1,37 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License 2.0; +// you may not use this file except in compliance with the Elastic License 2.0. + +package control + +import ( + "context" + + "google.golang.org/grpc" + "google.golang.org/grpc/connectivity" +) + +// DialContextBlocking creates a blocking connection equivalent to the deprecated grpc.DialContext. +// It uses the new grpc.NewClient API but waits for the connection to be established before returning, +// maintaining the same blocking behavior as the deprecated function. +func DialContextBlocking(ctx context.Context, target string, opts ...grpc.DialOption) (*grpc.ClientConn, error) { + conn, err := grpc.NewClient(target, opts...) + if err != nil { + return nil, err + } + + for { + state := conn.GetState() + if state == connectivity.Ready { + return conn, nil + } + if state == connectivity.TransientFailure || state == connectivity.Shutdown { + conn.Close() + return nil, ctx.Err() + } + if !conn.WaitForStateChange(ctx, state) { + conn.Close() + return nil, ctx.Err() + } + } +} diff --git a/pkg/control/dial_test.go b/pkg/control/dial_test.go new file mode 100644 index 00000000000..3deb7211046 --- /dev/null +++ b/pkg/control/dial_test.go @@ -0,0 +1,112 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License 2.0; +// you may not use this file except in compliance with the Elastic License 2.0. + +package control + +import ( + "context" + "net" + "testing" + "time" + + "google.golang.org/grpc" + "google.golang.org/grpc/connectivity" + "google.golang.org/grpc/credentials/insecure" + "google.golang.org/grpc/test/bufconn" +) + +func TestDialContextBlocking_Success(t *testing.T) { + // Create a buffer connection for testing + buffer := bufconn.Listen(1024 * 1024) + defer buffer.Close() + + // Start a mock gRPC server + server := grpc.NewServer() + go func() { + if err := server.Serve(buffer); err != nil { + t.Logf("Server serve error: %v", err) + } + }() + defer server.Stop() + + // Test successful connection + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + conn, err := DialContextBlocking(ctx, "bufconn", + grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) { + return buffer.Dial() + }), + grpc.WithTransportCredentials(insecure.NewCredentials()), + ) + + if err != nil { + t.Fatalf("DialContextBlocking failed: %v", err) + } + defer conn.Close() + + // Verify connection is ready + state := conn.GetState() + if state != connectivity.Ready { + t.Errorf("Expected connection state to be Ready, got %v", state) + } +} + +func TestDialContextBlocking_ContextCancellation(t *testing.T) { + // Create a context that cancels immediately + ctx, cancel := context.WithCancel(context.Background()) + cancel() // Cancel immediately + + // Try to establish connection with cancelled context + conn, err := DialContextBlocking(ctx, "unreachable:12345", + grpc.WithTransportCredentials(insecure.NewCredentials()), + ) + + // Should return an error due to context cancellation + if err == nil { + conn.Close() + t.Fatal("Expected DialContextBlocking to fail with cancelled context") + } + + if err != context.Canceled { + t.Errorf("Expected context.Canceled error, got %v", err) + } +} + +func TestDialContextBlocking_InvalidTarget(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) + defer cancel() + + // Try to connect to an invalid target + conn, err := DialContextBlocking(ctx, "invalid-scheme://invalid-target", + grpc.WithTransportCredentials(insecure.NewCredentials()), + ) + + // Should return an error + if err == nil { + conn.Close() + t.Fatal("Expected DialContextBlocking to fail with invalid target") + } +} + +func TestDialContextBlocking_Timeout(t *testing.T) { + // Create a very short timeout + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) + defer cancel() + + // Try to connect to a non-existent service + conn, err := DialContextBlocking(ctx, "127.0.0.1:1", + grpc.WithTransportCredentials(insecure.NewCredentials()), + ) + + // Should return an error due to timeout + if err == nil { + conn.Close() + t.Fatal("Expected DialContextBlocking to fail with timeout") + } + + if err != context.DeadlineExceeded { + t.Errorf("Expected context.DeadlineExceeded error, got %v", err) + } +} \ No newline at end of file diff --git a/pkg/control/v1/client/dial.go b/pkg/control/v1/client/dial.go index 4ce3037ae83..c0e20897dbf 100644 --- a/pkg/control/v1/client/dial.go +++ b/pkg/control/v1/client/dial.go @@ -14,12 +14,11 @@ import ( "github.com/elastic/elastic-agent/pkg/control" "google.golang.org/grpc" - "google.golang.org/grpc/connectivity" "google.golang.org/grpc/credentials/insecure" ) func dialContext(ctx context.Context) (*grpc.ClientConn, error) { - return dialContextBlocking( + return control.DialContextBlocking( ctx, strings.TrimPrefix(control.Address(), "unix://"), grpc.WithTransportCredentials(insecure.NewCredentials()), @@ -27,31 +26,6 @@ func dialContext(ctx context.Context) (*grpc.ClientConn, error) { ) } -// dialContextBlocking creates a blocking connection equivalent to the deprecated grpc.DialContext -func dialContextBlocking(ctx context.Context, target string, opts ...grpc.DialOption) (*grpc.ClientConn, error) { - // Create the connection using the new API - conn, err := grpc.NewClient(target, opts...) - if err != nil { - return nil, err - } - - // Keep waiting until we're connected or context is cancelled - for { - state := conn.GetState() - if state == connectivity.Ready { - return conn, nil - } - if state == connectivity.TransientFailure || state == connectivity.Shutdown { - conn.Close() - return nil, ctx.Err() - } - if !conn.WaitForStateChange(ctx, state) { - conn.Close() - return nil, ctx.Err() - } - } -} - func dialer(ctx context.Context, addr string) (net.Conn, error) { var d net.Dialer return d.DialContext(ctx, "unix", addr) diff --git a/pkg/control/v1/client/dial_windows.go b/pkg/control/v1/client/dial_windows.go index cacd1a6da21..cb1de9f59de 100644 --- a/pkg/control/v1/client/dial_windows.go +++ b/pkg/control/v1/client/dial_windows.go @@ -13,14 +13,13 @@ import ( "github.com/elastic/elastic-agent/pkg/control" "google.golang.org/grpc" - "google.golang.org/grpc/connectivity" "google.golang.org/grpc/credentials/insecure" "github.com/elastic/elastic-agent-libs/api/npipe" ) func dialContext(ctx context.Context) (*grpc.ClientConn, error) { - return dialContextBlocking( + return control.DialContextBlocking( ctx, control.Address(), grpc.WithTransportCredentials(insecure.NewCredentials()), @@ -28,31 +27,6 @@ func dialContext(ctx context.Context) (*grpc.ClientConn, error) { ) } -// dialContextBlocking creates a blocking connection equivalent to the deprecated grpc.DialContext -func dialContextBlocking(ctx context.Context, target string, opts ...grpc.DialOption) (*grpc.ClientConn, error) { - // Create the connection using the new API - conn, err := grpc.NewClient(target, opts...) - if err != nil { - return nil, err - } - - // Keep waiting until we're connected or context is cancelled - for { - state := conn.GetState() - if state == connectivity.Ready { - return conn, nil - } - if state == connectivity.TransientFailure || state == connectivity.Shutdown { - conn.Close() - return nil, ctx.Err() - } - if !conn.WaitForStateChange(ctx, state) { - conn.Close() - return nil, ctx.Err() - } - } -} - func dialer(ctx context.Context, addr string) (net.Conn, error) { return npipe.DialContext(npipe.TransformString(addr))(ctx, "", "") } diff --git a/pkg/control/v2/client/dial.go b/pkg/control/v2/client/dial.go index 05bb9834c48..d06cd88f793 100644 --- a/pkg/control/v2/client/dial.go +++ b/pkg/control/v2/client/dial.go @@ -11,8 +11,9 @@ import ( "net" "strings" + "github.com/elastic/elastic-agent/pkg/control" + "google.golang.org/grpc" - "google.golang.org/grpc/connectivity" "google.golang.org/grpc/credentials/insecure" ) @@ -22,32 +23,7 @@ func dialContext(ctx context.Context, address string, maxMsgSize int, opts ...gr grpc.WithContextDialer(dialer), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize)), ) - return dialContextBlocking(ctx, address, opts...) -} - -// dialContextBlocking creates a blocking connection equivalent to the deprecated grpc.DialContext -func dialContextBlocking(ctx context.Context, target string, opts ...grpc.DialOption) (*grpc.ClientConn, error) { - // Create the connection using the new API - conn, err := grpc.NewClient(target, opts...) - if err != nil { - return nil, err - } - - // Keep waiting until we're connected or context is cancelled - for { - state := conn.GetState() - if state == connectivity.Ready { - return conn, nil - } - if state == connectivity.TransientFailure || state == connectivity.Shutdown { - conn.Close() - return nil, ctx.Err() - } - if !conn.WaitForStateChange(ctx, state) { - conn.Close() - return nil, ctx.Err() - } - } + return control.DialContextBlocking(ctx, address, opts...) } func dialer(ctx context.Context, addr string) (net.Conn, error) { diff --git a/pkg/control/v2/client/dial_windows.go b/pkg/control/v2/client/dial_windows.go index af1eab0174f..e0312e81d33 100644 --- a/pkg/control/v2/client/dial_windows.go +++ b/pkg/control/v2/client/dial_windows.go @@ -11,8 +11,9 @@ import ( "net" "strings" + "github.com/elastic/elastic-agent/pkg/control" + "google.golang.org/grpc" - "google.golang.org/grpc/connectivity" "google.golang.org/grpc/credentials/insecure" "github.com/elastic/elastic-agent-libs/api/npipe" @@ -24,32 +25,7 @@ func dialContext(ctx context.Context, address string, maxMsgSize int, opts ...gr grpc.WithContextDialer(dialer), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize)), ) - return dialContextBlocking(ctx, address, opts...) -} - -// dialContextBlocking creates a blocking connection equivalent to the deprecated grpc.DialContext -func dialContextBlocking(ctx context.Context, target string, opts ...grpc.DialOption) (*grpc.ClientConn, error) { - // Create the connection using the new API - conn, err := grpc.NewClient(target, opts...) - if err != nil { - return nil, err - } - - // Keep waiting until we're connected or context is cancelled - for { - state := conn.GetState() - if state == connectivity.Ready { - return conn, nil - } - if state == connectivity.TransientFailure || state == connectivity.Shutdown { - conn.Close() - return nil, ctx.Err() - } - if !conn.WaitForStateChange(ctx, state) { - conn.Close() - return nil, ctx.Err() - } - } + return control.DialContextBlocking(ctx, address, opts...) } func dialer(ctx context.Context, addr string) (net.Conn, error) { From 3980ad8afb5610278bdf8fca23ed39880af70de2 Mon Sep 17 00:00:00 2001 From: Rajiv Singh Date: Fri, 25 Jul 2025 23:35:23 +0530 Subject: [PATCH 5/7] Update dial_test.go --- pkg/control/dial_test.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/control/dial_test.go b/pkg/control/dial_test.go index 3deb7211046..34920f73a70 100644 --- a/pkg/control/dial_test.go +++ b/pkg/control/dial_test.go @@ -11,6 +11,8 @@ import ( "time" "google.golang.org/grpc" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "google.golang.org/grpc/connectivity" "google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/test/bufconn" @@ -42,14 +44,14 @@ func TestDialContextBlocking_Success(t *testing.T) { ) if err != nil { - t.Fatalf("DialContextBlocking failed: %v", err) + require.NoError(t, err, "DialContextBlocking failed") } defer conn.Close() // Verify connection is ready state := conn.GetState() if state != connectivity.Ready { - t.Errorf("Expected connection state to be Ready, got %v", state) + assert.Equal(t, connectivity.Ready, state, "Expected connection state to be Ready") } } @@ -109,4 +111,4 @@ func TestDialContextBlocking_Timeout(t *testing.T) { if err != context.DeadlineExceeded { t.Errorf("Expected context.DeadlineExceeded error, got %v", err) } -} \ No newline at end of file +} From e3cb208247fd04f43a051ef57a7bc1860c31e1d5 Mon Sep 17 00:00:00 2001 From: iamrajiv Date: Sat, 26 Jul 2025 00:05:19 +0530 Subject: [PATCH 6/7] add test --- pkg/control/dial_test.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/pkg/control/dial_test.go b/pkg/control/dial_test.go index 34920f73a70..c50d6dd299a 100644 --- a/pkg/control/dial_test.go +++ b/pkg/control/dial_test.go @@ -43,16 +43,12 @@ func TestDialContextBlocking_Success(t *testing.T) { grpc.WithTransportCredentials(insecure.NewCredentials()), ) - if err != nil { - require.NoError(t, err, "DialContextBlocking failed") - } + require.NoError(t, err, "DialContextBlocking failed") defer conn.Close() // Verify connection is ready state := conn.GetState() - if state != connectivity.Ready { - assert.Equal(t, connectivity.Ready, state, "Expected connection state to be Ready") - } + assert.Equal(t, connectivity.Ready, state, "Expected connection state to be Ready") } func TestDialContextBlocking_ContextCancellation(t *testing.T) { From cad2552d7f0cefad564fea775cbfe6b5dcfc3482 Mon Sep 17 00:00:00 2001 From: iamrajiv Date: Tue, 29 Jul 2025 01:02:29 +0530 Subject: [PATCH 7/7] fix test --- pkg/control/dial_test.go | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/pkg/control/dial_test.go b/pkg/control/dial_test.go index c50d6dd299a..356b2548241 100644 --- a/pkg/control/dial_test.go +++ b/pkg/control/dial_test.go @@ -62,14 +62,12 @@ func TestDialContextBlocking_ContextCancellation(t *testing.T) { ) // Should return an error due to context cancellation - if err == nil { + require.Error(t, err, "Expected DialContextBlocking to fail with cancelled context") + if conn != nil { conn.Close() - t.Fatal("Expected DialContextBlocking to fail with cancelled context") } - if err != context.Canceled { - t.Errorf("Expected context.Canceled error, got %v", err) - } + assert.Equal(t, context.Canceled, err, "Expected context.Canceled error") } func TestDialContextBlocking_InvalidTarget(t *testing.T) { @@ -82,9 +80,9 @@ func TestDialContextBlocking_InvalidTarget(t *testing.T) { ) // Should return an error - if err == nil { + require.Error(t, err, "Expected DialContextBlocking to fail with invalid target") + if conn != nil { conn.Close() - t.Fatal("Expected DialContextBlocking to fail with invalid target") } } @@ -99,12 +97,10 @@ func TestDialContextBlocking_Timeout(t *testing.T) { ) // Should return an error due to timeout - if err == nil { + require.Error(t, err, "Expected DialContextBlocking to fail with timeout") + if conn != nil { conn.Close() - t.Fatal("Expected DialContextBlocking to fail with timeout") } - if err != context.DeadlineExceeded { - t.Errorf("Expected context.DeadlineExceeded error, got %v", err) - } + assert.Equal(t, context.DeadlineExceeded, err, "Expected context.DeadlineExceeded error") }