vitess.io/vitess@v0.16.2/go/vt/binlog/grpcbinlogplayer/player_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 grpcbinlogplayer
    18  
    19  import (
    20  	"net"
    21  	"testing"
    22  
    23  	"google.golang.org/grpc"
    24  
    25  	"vitess.io/vitess/go/vt/binlog/binlogplayertest"
    26  	"vitess.io/vitess/go/vt/binlog/grpcbinlogstreamer"
    27  
    28  	binlogservicepb "vitess.io/vitess/go/vt/proto/binlogservice"
    29  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    30  )
    31  
    32  // the test here creates a fake server implementation, a fake client
    33  // implementation, and runs the test suite against the setup.
    34  func TestGRPCBinlogStreamer(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 := listener.Addr().(*net.TCPAddr).Port
    42  
    43  	// Create a gRPC server and listen on the port
    44  	server := grpc.NewServer()
    45  	fakeUpdateStream := binlogplayertest.NewFakeBinlogStreamer(t)
    46  	binlogservicepb.RegisterUpdateStreamServer(server, grpcbinlogstreamer.New(fakeUpdateStream))
    47  	go server.Serve(listener)
    48  
    49  	// Create a GRPC client to talk to the fake tablet
    50  	c := &client{}
    51  
    52  	// and send it to the test suite
    53  	binlogplayertest.Run(t, c, &topodatapb.Tablet{
    54  		Hostname: host,
    55  		PortMap: map[string]int32{
    56  			"grpc": int32(port),
    57  		},
    58  	}, fakeUpdateStream)
    59  }