syb
syb

Reputation: 62

How to test stream response form a server?

I am working on a client for a project I am writing in go and using grpc for the server. I wrote the code for the client with Cobra CLI. The purpose of the client is to replace the old cli interface of the project and also serve as an api client for the future.

I already wrote 2 commands that speak with the server and wrote tests for them. They are pretty basic because they send a request, get a response and close the connection. At this moment, I am working on a command for somthing to stream response from the server, i already wrote a working client but it only works with the real server and i want to write test for it so it will test if i think of adding more things. I will provide some of the things I am working on. Feel free to view the client in git hub, but i still working on it, https://github.com/ShohamBit/TraceeClient

this is the protobuff for, the server:

message StreamEventsRequest {
    repeated string policies = 1;
    google.protobuf.FieldMask mask = 2;
}

message StreamEventsResponse {
    Event event = 1;
}

service TraceeService {
    rpc StreamEvents(StreamEventsRequest) returns (stream StreamEventsResponse);
}

this is the code of the streamEvents command:

package cmd

import (
    "github.com/ShohamBit/TraceeClient/client"
    pb "github.com/aquasecurity/tracee/api/v1beta1"
    "github.com/spf13/cobra"
)

var (
    streamEventsCmd = &cobra.Command{
        Use:   "streamEvents",
        Short: "Stream events from tracee",
        Long:  "Stream the events that tracee trace to the client",
        Run: func(cmd *cobra.Command, args []string) {
            streamEvents(cmd, args)
        },
    }
)

func streamEvents(cmd *cobra.Command, args []string) {
    // create service client
    client, err := client.NewServiceClient(serverInfo)
    if err != nil {
        cmd.PrintErrln("Error creating client: ", err)
    }
    defer client.CloseConnection()
    // stream events
    req := &pb.StreamEventsRequest{Policies: args}
    stream, err := client.StreamEvents(cmd.Context(), req)
    if err != nil {
        cmd.PrintErrln("Error calling StreamEvents: ", err)
    }
    // Receive and process streamed responses
    for {
        res, err := stream.Recv()
        if err != nil {
            cmd.PrintErrln("Error receiving streamed event: ", err)
            break
        }
        cmd.Println(res.Event)
    }

}

I’ve tried to write a test for this and also mock server, I don’t really have something for it right now. I would like to get help on it, and know if someone knows how to write a mock stream for grpc

I have tried to loop on events, and just strugle with the code and got it barely to work but it didn’t do what i expected

Upvotes: 2

Views: 60

Answers (1)

Caique
Caique

Reputation: 188

There is definitely more than two ways to test grpc, but the documentation recommends either mocking or stubbing.

With these options, you can either implement the necessary interface with a test type or provide a way to pass in connection information so that you can call your test with a local one

Upvotes: 2

Related Questions