title: How to mock Grpc Clients inside GRPC server implementation
body: <p>I have a GRPC server that is talking to many other GRPC servers.
  So, my server is client for many other GRPC servers
  I would like to test my server implementation but I'm stuck on how to mock the GRPC clients.</p>
  
  <p>I have a method from my GRPC server like this:</p>
  
  <pre class="lang-golang prettyprint-override"><code>func (s UserBackendServer) UpdateUser(ctx context.Context, req *api.UpdateUserRequest) (*api.User, error) {
      conn, err := grpc.DialContext(ctx, s.userSvcAddr,
          grpc.WithInsecure(),
          grpc.WithStatsHandler(&amp;ocgrpc.ClientHandler{}))
      if err != nil {
          return nil, fmt.Errorf("could not connect shipping service: %+v", err)
      }
      defer conn.Close()
  
      user, err := api.NewUserServiceClient(conn).
          UpdateUser(ctx, &amp;api.UpdateUserRequest{
          User:                 nil,
          UserId:               "",
      })
      return user, nil
  }
  </code></pre>
  
  <p>In this type of implementation, I cannot mock the UserServiceClient because I create one inside the method :(</p>
  
  <p>I have thought about two ways to solve this issue</p>
  
  <ol>
  <li>Have the client reference inside the <code>UserBackendServer</code> struct</li>
  </ol>
  
  <pre class="lang-golang prettyprint-override"><code>type UserBackendServer struct {
      projectID string
      userSvcClient api.UserServiceClient
  }
  
  func (s UserBackendServer) UpdateUser(ctx context.Context, req *api.UpdateUserRequest) (*api.User, error) {
      conn, err := grpc.DialContext(ctx, s.userSvcAddr,
          grpc.WithInsecure(),
          grpc.WithStatsHandler(&amp;ocgrpc.ClientHandler{}))
      if err != nil {
          return nil, fmt.Errorf("could not connect shipping service: %+v", err)
      }
      defer conn.Close()
  
      user, err := s.userSvcClient.UpdateUser(ctx, &amp;api.UpdateUserRequest{
          User:                 nil,
          UserId:               "",
      })
      return user, nil
  }
  </code></pre>
  
  <p>With this implementation, I have the side effect of having a continuously active connection to the clients (I have like 10 clients to communicate with). Is it fine in production? Having always-connected clients can it lead to a memory leak or too much concurrents connections?</p>
  
  <ol start="2">
  <li>Using methods that get the client as an argument</li>
  </ol>
  
  <pre class="lang-golang prettyprint-override"><code>func (s UserBackendServer) UpdateUser(ctx context.Context, req *api.UpdateUserRequest) (*api.User, error) {
      conn, err := grpc.DialContext(ctx, s.userSvcAddr,
          grpc.WithInsecure(),
          grpc.WithStatsHandler(&amp;ocgrpc.ClientHandler{}))
      if err != nil {
          return nil, fmt.Errorf("could not connect shipping service: %+v", err)
      }
      defer conn.Close()
  
      client := api.NewUserServiceClient(conn)
      updateUser(ctx, req.UserId, req.User, client)
  
  
      user, err := s.userSvcClient.UpdateUser(ctx, &amp;api.UpdateUserRequest{
          User:                 nil,
          UserId:               "",
      })
      return user, nil
  }
  
  func updateUser(ctx context.Context, userId string, user *api.User, client api.UserServiceClient) (*api.User, error){
      userUpdated, err := client.UpdateUser(ctx, &amp;api.UpdateUserRequest{
          User:                 nil,
          UserId:               "",
      })
  
      return userUpdated, err
  }
  </code></pre>
  
  <p>This seems a little bit cumbersome to write all these little functions and I can't make a unit test of the server function.</p>
  
  <p>The strategy number one is it feasible? do you have experience about limitations or how to retry a failed connections if the connections are maintained in the server struct</p>