grpc / grpc/grpc-dotnet

Testing for a valid endpoint on a duplex connection

Open
#1,770 3 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
C#
Stars
4.5k
Forks
836
Avg merge
6d 3h
Merged PRs (30d)
7

Description

I need to test that an endpoint is valid for a duplex stream connection without sending a message. I have figured out a way to do it, but it doesn't work for every scenario.

`

var saCerts = CertificateValidation.CreateTrustedRootCollection( certificateAthorities );

var options = new GrpcChannelOptions();

if( url.StartsWith( "https" ) )
{
var handler = new SocketsHttpHandler();
handler.SslOptions = new SslClientAuthenticationOptions
{
RemoteCertificateValidationCallback = CertificateValidation.CreateRemoteCertificateValidationCallback( saCerts ),
ClientCertificates = new X509CertificateCollection()
};

handler.SslOptions.ClientCertificates.Add( new X509Certificate2( /*Certificate*/ ) );

options.HttpHandler = handler;
}

channel = GrpcChannel.ForAddress( url, options );

//This is how I found to test a connection
channel.ConnectAsync();

var task = channel.WaitForStateChangedAsync( ConnectivityState.Connecting );
task.Wait(5000);

if( channel.State != ConnectivityState.Ready )
throw new Exception( $"Could not connect to URL: {url}." );

//End connection test

var client = new MyMessage.MyMessageClient( channel );

call = client.Stream( null, null, cancellationSource.Token );
`

Using ConnectAsync works when there is no endpoint at all. It also works (through the task timeout) when there is something at the endpoint, but not one I can connect to.

However, in the instance the url is correct, but the server port is secure and the used url is not secure (or vice versa), channel.ConnectAsync() works, and the channel state becomes ConnectivityState.Ready, despite the mismatch in security.

The only way I could figure out how to test if the endpoint is completely valid is to send a message. However, for reasons outside of my control, this is not going to work for me.

So my questions are: Is ConnectAsync the right way to test? And how do you check if the endpoint is completely valid without sending a message?

Any help is appreciated.

Contributor guide

Open the contributing guide

Research direction

Start with the shown GrpcChannel.ConnectAsync and WaitForStateChangedAsync sequence, then compare it with the MyMessageClient.Stream call. Reproduce the secure and insecure URL/port mismatch described and determine whether ConnectAsync is expected to validate that condition without sending a message; document the supported behavior and resolution.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, backend-api-design
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.