Test a gRPC Server and Client

Distributed Services with Go — by Travis Jeffery (36 / 84)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Register Your Server | TOC | What You Learned 👉

Now that we’ve finished our gRPC server, we need some tests to check that our client and server work like we expect. We’ve already tested the details of our log’s library implementation in the library, so the tests we’re writing here are at a higher level and focus on ensuring that everything’s hooked up properly between the gRPC and library bits and that our gRPC client and server can communicate.

In the grpc directory, create a server_test.go file, and add the following code that will set up your test:

ServeRequestsWithgRPC/internal/server/server_test.go

​ ​package​ server

​ ​import​ (
​ ​"context"​
​ ​"io/ioutil"​
​ ​"net"​
​ ​"testing"​

​ ​"github.com/stretchr/testify/require"​
​ api ​"github.com/travisjeffery/proglog/api/v1"​
​ ​"github.com/travisjeffery/proglog/internal/log"​
​ ​"google.golang.org/grpc"​
​ )

​ ​func​ TestServer(t *testing.T) {
​ ​for​ scenario, fn := ​range​ ​map​[​string​]​func​(
​ t *testing.T,
​ client api.LogClient,
​ config *Config,
​ ){
​ ​"produce/consume a message to/from the log succeeeds"​:
​ testProduceConsume,
​ ​"produce/consume stream succeeds"​:
​…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.