// the test here creates a fake server implementation, a fake client // implementation, and runs the test suite against the setup. func TestVtctlServer(t *testing.T) { ts := vtctlclienttest.CreateTopoServer(t) // Listen on a random port listener, err := net.Listen("tcp", ":0") if err != nil { t.Fatalf("Cannot listen: %v", err) } // Create a Go Rpc server and listen on the port server := rpcplus.NewServer() server.Register(gorpcvtctlserver.NewVtctlServer(ts)) // Create the HTTP server, serve the server from it handler := http.NewServeMux() bsonrpc.ServeCustomRPC(handler, server) httpServer := http.Server{ Handler: handler, } go httpServer.Serve(listener) // Create a VtctlClient Go Rpc client to talk to the fake server client, err := goRPCVtctlClientFactory(listener.Addr().String(), 30*time.Second) if err != nil { t.Fatalf("Cannot create client: %v", err) } defer client.Close() vtctlclienttest.TestSuite(t, ts, client) }
// NewVtctlPipe creates a new VtctlPipe based on the given topo server. func NewVtctlPipe(t *testing.T, ts topo.Server) *VtctlPipe { // Listen on a random port listener, err := net.Listen("tcp", ":0") if err != nil { t.Fatalf("Cannot listen: %v", err) } // Create a Go Rpc server and listen on the port server := rpcplus.NewServer() server.Register(gorpcvtctlserver.NewVtctlServer(ts)) // Create the HTTP server, serve the server from it handler := http.NewServeMux() bsonrpc.ServeCustomRPC(handler, server, false) httpServer := http.Server{ Handler: handler, } go httpServer.Serve(listener) // Create a VtctlClient Go Rpc client to talk to the fake server client, err := vtctlclient.New(listener.Addr().String(), 30*time.Second) if err != nil { t.Fatalf("Cannot create client: %v", err) } return &VtctlPipe{ listener: listener, client: client, t: t, } }