vitess.io/vitess@v0.16.2/go/vt/servenv/grpc_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 servenv
    18  
    19  import (
    20  	"testing"
    21  
    22  	"context"
    23  
    24  	"google.golang.org/grpc"
    25  )
    26  
    27  func TestEmpty(t *testing.T) {
    28  	interceptors := &serverInterceptorBuilder{}
    29  	if len(interceptors.Build()) > 0 {
    30  		t.Fatalf("expected empty builder to report as empty")
    31  	}
    32  }
    33  
    34  func TestSingleInterceptor(t *testing.T) {
    35  	interceptors := &serverInterceptorBuilder{}
    36  	fake := &FakeInterceptor{}
    37  
    38  	interceptors.Add(fake.StreamServerInterceptor, fake.UnaryServerInterceptor)
    39  
    40  	if len(interceptors.streamInterceptors) != 1 {
    41  		t.Fatalf("expected 1 server options to be available")
    42  	}
    43  	if len(interceptors.unaryInterceptors) != 1 {
    44  		t.Fatalf("expected 1 server options to be available")
    45  	}
    46  }
    47  
    48  func TestDoubleInterceptor(t *testing.T) {
    49  	interceptors := &serverInterceptorBuilder{}
    50  	fake1 := &FakeInterceptor{name: "ettan"}
    51  	fake2 := &FakeInterceptor{name: "tvaon"}
    52  
    53  	interceptors.Add(fake1.StreamServerInterceptor, fake1.UnaryServerInterceptor)
    54  	interceptors.Add(fake2.StreamServerInterceptor, fake2.UnaryServerInterceptor)
    55  
    56  	if len(interceptors.streamInterceptors) != 2 {
    57  		t.Fatalf("expected 1 server options to be available")
    58  	}
    59  	if len(interceptors.unaryInterceptors) != 2 {
    60  		t.Fatalf("expected 1 server options to be available")
    61  	}
    62  }
    63  
    64  type FakeInterceptor struct {
    65  	name       string
    66  	streamSeen any
    67  	unarySeen  any
    68  }
    69  
    70  func (fake *FakeInterceptor) StreamServerInterceptor(value any, stream grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
    71  	fake.streamSeen = value
    72  	return handler(value, stream)
    73  }
    74  
    75  func (fake *FakeInterceptor) UnaryServerInterceptor(ctx context.Context, value any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
    76  	fake.unarySeen = value
    77  	return handler(ctx, value)
    78  }