vitess.io/vitess@v0.16.2/go/vt/throttler/grpcthrottlerclient/grpcthrottlerclient_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 grpcthrottlerclient
    18  
    19  import (
    20  	"fmt"
    21  	"net"
    22  	"testing"
    23  
    24  	"google.golang.org/grpc"
    25  
    26  	"vitess.io/vitess/go/vt/throttler"
    27  	"vitess.io/vitess/go/vt/throttler/grpcthrottlerserver"
    28  	"vitess.io/vitess/go/vt/throttler/throttlerclienttest"
    29  )
    30  
    31  // TestThrottlerServer tests the gRPC implementation using a throttler client
    32  // and server.
    33  func TestThrottlerServer(t *testing.T) {
    34  	// Use the global manager which is a singleton.
    35  	port := startGRPCServer(t, throttler.GlobalManager)
    36  
    37  	// Create a ThrottlerClient gRPC client to talk to the throttler.
    38  	client, err := factory(fmt.Sprintf("localhost:%v", port))
    39  	if err != nil {
    40  		t.Fatalf("Cannot create client: %v", err)
    41  	}
    42  	defer client.Close()
    43  
    44  	throttlerclienttest.TestSuite(t, client)
    45  }
    46  
    47  // TestThrottlerServerPanics tests the panic handling of the gRPC throttler
    48  // server implementation.
    49  func TestThrottlerServerPanics(t *testing.T) {
    50  	// For testing the panic handling, use a fake Manager instead.
    51  	port := startGRPCServer(t, &throttlerclienttest.FakeManager{})
    52  
    53  	// Create a ThrottlerClient gRPC client to talk to the throttler.
    54  	client, err := factory(fmt.Sprintf("localhost:%v", port))
    55  	if err != nil {
    56  		t.Fatalf("Cannot create client: %v", err)
    57  	}
    58  	defer client.Close()
    59  
    60  	throttlerclienttest.TestSuitePanics(t, client)
    61  }
    62  
    63  func startGRPCServer(t *testing.T, m throttler.Manager) int {
    64  	// Listen on a random port.
    65  	listener, err := net.Listen("tcp", "127.0.0.1:0")
    66  	if err != nil {
    67  		t.Fatalf("Cannot listen: %v", err)
    68  	}
    69  
    70  	s := grpc.NewServer()
    71  	grpcthrottlerserver.RegisterServer(s, m)
    72  	// Call Serve() after our service has been registered. Otherwise, the test
    73  	// will fail with the error "grpc: Server.RegisterService after Server.Serve".
    74  	go s.Serve(listener)
    75  	return listener.Addr().(*net.TCPAddr).Port
    76  }