vitess.io/vitess@v0.16.2/go/vt/vttablet/grpctmserver/server_test.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package grpctmserver_test
    18  
    19  import (
    20  	"net"
    21  	"testing"
    22  
    23  	"google.golang.org/grpc"
    24  
    25  	"vitess.io/vitess/go/vt/vttablet/grpctmclient"
    26  	"vitess.io/vitess/go/vt/vttablet/grpctmserver"
    27  	"vitess.io/vitess/go/vt/vttablet/tmrpctest"
    28  
    29  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    30  )
    31  
    32  // TestGRPCTMServer creates a fake server implementation, a fake client
    33  // implementation, and runs the test suite against the setup.
    34  func TestGRPCTMServer(t *testing.T) {
    35  	// Listen on a random port
    36  	listener, err := net.Listen("tcp", "127.0.0.1:0")
    37  	if err != nil {
    38  		t.Fatalf("Cannot listen: %v", err)
    39  	}
    40  	host := listener.Addr().(*net.TCPAddr).IP.String()
    41  	port := int32(listener.Addr().(*net.TCPAddr).Port)
    42  
    43  	// Create a gRPC server and listen on the port.
    44  	s := grpc.NewServer()
    45  	fakeTM := tmrpctest.NewFakeRPCTM(t)
    46  	grpctmserver.RegisterForTest(s, fakeTM)
    47  	go s.Serve(listener)
    48  
    49  	// Create a gRPC client to talk to the fake tablet.
    50  	client := grpctmclient.NewClient()
    51  	tablet := &topodatapb.Tablet{
    52  		Alias: &topodatapb.TabletAlias{
    53  			Cell: "test",
    54  			Uid:  123,
    55  		},
    56  		Hostname: host,
    57  		PortMap: map[string]int32{
    58  			"grpc": port,
    59  		},
    60  	}
    61  
    62  	// and run the test suite
    63  	tmrpctest.Run(t, client, tablet, fakeTM)
    64  }