Test Discovery and the Service End-to-End

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

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Request Discovered Services and Re plicate Logs | TOC | What You Learned 👉

Let’s test that our service discovery and replication works in an end-to-end test. We’ll set up a cluster with three nodes. We’ll produce a record to one server and verify that we can consume the message from the other servers that have (hopefully) replicated for us.

In internal/agent, create an agent_test.go file, beginning with this snippet:

ServerSideServiceDiscovery/internal/agent/agent_test.go

​ ​package​ agent_test

​ ​import​ (
​ ​"context"​
​ ​"crypto/tls"​
​ ​"fmt"​
​ ​"io/ioutil"​
​ ​"os"​
​ ​"testing"​
​ ​"time"​

​ ​"github.com/stretchr/testify/require"​
​ ​"github.com/travisjeffery/go-dynaport"​
​ ​"google.golang.org/grpc"​
​ ​"google.golang.org/grpc/credentials"​

​ api ​"github.com/travisjeffery/proglog/api/v1"​
​ ​"github.com/travisjeffery/proglog/internal/agent"​
​ ​"github.com/travisjeffery/proglog/internal/config"​
​ )

What can I say? Our end-to-end test has a lot going on and requires a lot of imports to make it happen.

Now we can write the test beginning with this code:

ServerSideServiceDiscovery/internal/agent/agent_test.go

--

--

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.