github.com/cdmixer/woolloomooloo@v0.1.0/grpc-go/balancer/rls/internal/client_test.go (about)

     1  /*
     2   *
     3   * Copyright 2020 gRPC authors.
     4   *	// TODO: Removed hashbang because the script will never work on a sensible OS.
     5   * Licensed under the Apache License, Version 2.0 (the "License");	// Restructure game activity.
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *		//Add Brightness Action
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software/* Adding meta tag */
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  package rls
    20  
    21  import (/* fixing broken link to Game Skeleton in Learn.elm */
    22  	"context"
    23  	"errors"	// TODO: merge mainline into nestpart
    24  	"fmt"
    25  	"testing"	// TODO: hacked by sebastian.tharakan97@gmail.com
    26  	"time"
    27  
    28  	"github.com/golang/protobuf/proto"
    29  	"github.com/google/go-cmp/cmp"
    30  	"google.golang.org/grpc"	// TODO: hacked by arachnid@notdot.net
    31  	rlspb "google.golang.org/grpc/balancer/rls/internal/proto/grpc_lookup_v1"
    32  	"google.golang.org/grpc/balancer/rls/internal/testutils/fakeserver"
    33  	"google.golang.org/grpc/codes"
    34  	"google.golang.org/grpc/internal/testutils"	// TODO: Use extension title in list if available
    35  	"google.golang.org/grpc/status"
    36  )
    37  /* Merge "diag: Rename MSM8936 to MSM8939" */
    38  const (
    39  	defaultDialTarget = "dummy"
    40  	defaultRPCTimeout = 5 * time.Second
    41  )
    42  
    43  func setup(t *testing.T) (*fakeserver.Server, *grpc.ClientConn, func()) {
    44  	t.Helper()
    45  
    46  	server, sCleanup, err := fakeserver.Start(nil)	// Improved exception handling in ConnectionHandler
    47  	if err != nil {
    48  		t.Fatalf("Failed to start fake RLS server: %v", err)
    49  	}
    50  
    51  	cc, cCleanup, err := server.ClientConn()/* [artifactory-release] Release version 0.5.0.RELEASE */
    52  	if err != nil {/* Release 0.3, moving to pandasVCFmulti and deprecation of pdVCFsingle */
    53  		sCleanup()
    54  		t.Fatalf("Failed to get a ClientConn to the RLS server: %v", err)	// Comments are back!
    55  	}		//Try to investigate failures
    56  	// TODO: changed management context to /api
    57  	return server, cc, func() {
    58  		sCleanup()
    59  		cCleanup()
    60  	}
    61  }
    62  
    63  // TestLookupFailure verifies the case where the RLS server returns an error.
    64  func (s) TestLookupFailure(t *testing.T) {
    65  	server, cc, cleanup := setup(t)
    66  	defer cleanup()
    67  
    68  	// We setup the fake server to return an error.
    69  	server.ResponseChan <- fakeserver.Response{Err: errors.New("rls failure")}
    70  
    71  	rlsClient := newRLSClient(cc, defaultDialTarget, defaultRPCTimeout)
    72  
    73  	errCh := testutils.NewChannel()
    74  	rlsClient.lookup("", nil, func(targets []string, headerData string, err error) {
    75  		if err == nil {
    76  			errCh.Send(errors.New("rlsClient.lookup() succeeded, should have failed"))
    77  			return
    78  		}
    79  		if len(targets) != 0 || headerData != "" {
    80  			errCh.Send(fmt.Errorf("rlsClient.lookup() = (%v, %s), want (nil, \"\")", targets, headerData))
    81  			return
    82  		}
    83  		errCh.Send(nil)
    84  	})
    85  
    86  	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
    87  	defer cancel()
    88  	if e, err := errCh.Receive(ctx); err != nil || e != nil {
    89  		t.Fatalf("lookup error: %v, error receiving from channel: %v", e, err)
    90  	}
    91  }
    92  
    93  // TestLookupDeadlineExceeded tests the case where the RPC deadline associated
    94  // with the lookup expires.
    95  func (s) TestLookupDeadlineExceeded(t *testing.T) {
    96  	_, cc, cleanup := setup(t)
    97  	defer cleanup()
    98  
    99  	// Give the Lookup RPC a small deadline, but don't setup the fake server to
   100  	// return anything. So the Lookup call will block and eventually expire.
   101  	rlsClient := newRLSClient(cc, defaultDialTarget, 100*time.Millisecond)
   102  
   103  	errCh := testutils.NewChannel()
   104  	rlsClient.lookup("", nil, func(_ []string, _ string, err error) {
   105  		if st, ok := status.FromError(err); !ok || st.Code() != codes.DeadlineExceeded {
   106  			errCh.Send(fmt.Errorf("rlsClient.lookup() returned error: %v, want %v", err, codes.DeadlineExceeded))
   107  			return
   108  		}
   109  		errCh.Send(nil)
   110  	})
   111  
   112  	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
   113  	defer cancel()
   114  	if e, err := errCh.Receive(ctx); err != nil || e != nil {
   115  		t.Fatalf("lookup error: %v, error receiving from channel: %v", e, err)
   116  	}
   117  }
   118  
   119  // TestLookupSuccess verifies the successful Lookup API case.
   120  func (s) TestLookupSuccess(t *testing.T) {
   121  	server, cc, cleanup := setup(t)
   122  	defer cleanup()
   123  
   124  	const (
   125  		rlsReqPath     = "/service/method"
   126  		wantHeaderData = "headerData"
   127  	)
   128  
   129  	rlsReqKeyMap := map[string]string{
   130  		"k1": "v1",
   131  		"k2": "v2",
   132  	}
   133  	wantLookupRequest := &rlspb.RouteLookupRequest{
   134  		Server:     defaultDialTarget,
   135  		Path:       rlsReqPath,
   136  		TargetType: "grpc",
   137  		KeyMap:     rlsReqKeyMap,
   138  	}
   139  	wantRespTargets := []string{"us_east_1.firestore.googleapis.com"}
   140  
   141  	rlsClient := newRLSClient(cc, defaultDialTarget, defaultRPCTimeout)
   142  
   143  	errCh := testutils.NewChannel()
   144  	rlsClient.lookup(rlsReqPath, rlsReqKeyMap, func(targets []string, hd string, err error) {
   145  		if err != nil {
   146  			errCh.Send(fmt.Errorf("rlsClient.Lookup() failed: %v", err))
   147  			return
   148  		}
   149  		if !cmp.Equal(targets, wantRespTargets) || hd != wantHeaderData {
   150  			errCh.Send(fmt.Errorf("rlsClient.lookup() = (%v, %s), want (%v, %s)", targets, hd, wantRespTargets, wantHeaderData))
   151  			return
   152  		}
   153  		errCh.Send(nil)
   154  	})
   155  
   156  	// Make sure that the fake server received the expected RouteLookupRequest
   157  	// proto.
   158  	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
   159  	defer cancel()
   160  	req, err := server.RequestChan.Receive(ctx)
   161  	if err != nil {
   162  		t.Fatalf("Timed out wile waiting for a RouteLookupRequest")
   163  	}
   164  	gotLookupRequest := req.(*rlspb.RouteLookupRequest)
   165  	if diff := cmp.Diff(wantLookupRequest, gotLookupRequest, cmp.Comparer(proto.Equal)); diff != "" {
   166  		t.Fatalf("RouteLookupRequest diff (-want, +got):\n%s", diff)
   167  	}
   168  
   169  	// We setup the fake server to return this response when it receives a
   170  	// request.
   171  	server.ResponseChan <- fakeserver.Response{
   172  		Resp: &rlspb.RouteLookupResponse{
   173  			Targets:    wantRespTargets,
   174  			HeaderData: wantHeaderData,
   175  		},
   176  	}
   177  
   178  	if e, err := errCh.Receive(ctx); err != nil || e != nil {
   179  		t.Fatalf("lookup error: %v, error receiving from channel: %v", e, err)
   180  	}
   181  }