Open
Description
Is your feature request related to a problem? Please describe.
We recently stumbled over the issue of testing specific scenarios with the gRPC Channel object.
The issue was simplified (without the project overhead).
Example issue:
public class CustomGrpcService
{
public CustomGrpcService()
{
this.GrpcChannel = GrpcChannel.ForAddress("testUri");
}
public GrpcChannel GrpcChannel { get; set; }
public void DoSomething()
{
if (this.GrpcChannel.State == ConnectivityState.Idle)
{
// Do Smth
}
else if (this.GrpcChannel.State == ConnectivityState.Ready)
{
// Do Smth
}
else if (this.GrpcChannel.State == ConnectivityState.Connecting)
{
// Do Smth
}
else if (this.GrpcChannel.State == ConnectivityState.TransientFailure)
{
// Do Smth
}
else if (this.GrpcChannel.State == ConnectivityState.Shutdown)
{
// Do Smth
}
}
}
Example tests:
[Fact]
public void TestIdleBehaiour()
{
CustomGrpcService customGrpcService = new CustomGrpcService();
customGrpcService.DoSomething();
// Assert result in some way
}
[Fact]
public void TestReadyBehaiour()
{
CustomGrpcService customGrpcService = new CustomGrpcService();
customGrpcService.DoSomething();
// Assert result in some way
}
....
As of now, we have no possibility to change the behavior of the channel. Also, we would like to test this kind of functionality without acquiring a real OS port.
Mocking is unfortunately not an option, due to the class GrpcChannel
being sealed.
Describe the solution you'd like
I would like a possibility to abstract the GrpcChannel object in some way.
Define interface to decouple functionallity from implementation
public interface IGrpcChannel: IDisposable
{
public ConnectivityState State { get; }
public Task ConnectAsync(CancellationToken cancellationToken = default);
....
}
Open up sealed class
Remove the sealed keyword from the 'GrpcChannel' to allow a mock implementation.
Additional context
I believe that some applications could really benefit from this functionality. If it fits into the current project architecture, I would be glad to contribute.
Best
Fabian