Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock and Dependency Redundancy #2497

Open
Fabian918 opened this issue Jul 31, 2024 · 0 comments
Open

Mock and Dependency Redundancy #2497

Fabian918 opened this issue Jul 31, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@Fabian918
Copy link

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

@Fabian918 Fabian918 added the enhancement New feature or request label Jul 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant