...

Source file src/cloud.google.com/go/internal/testutil/server_test.go

Documentation: cloud.google.com/go/internal/testutil

     1  // Copyright 2016 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package testutil
    16  
    17  import (
    18  	"testing"
    19  
    20  	grpc "google.golang.org/grpc"
    21  	"google.golang.org/grpc/codes"
    22  	"google.golang.org/grpc/status"
    23  )
    24  
    25  func TestNewServer(t *testing.T) {
    26  	srv, err := NewServer()
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	defer srv.Close()
    31  	srv.Start()
    32  	conn, err := grpc.Dial(srv.Addr, grpc.WithInsecure())
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	defer conn.Close()
    37  }
    38  
    39  func TestPageBounds(t *testing.T) {
    40  	const length = 10
    41  	for _, test := range []struct {
    42  		size     int
    43  		tok      string
    44  		wantFrom int
    45  		wantTo   int
    46  		wantTok  string
    47  	}{
    48  		{5, "",
    49  			0, 5, "5"},
    50  		{11, "",
    51  			0, 10, ""},
    52  		{5, "2",
    53  			2, 7, "7"},
    54  		{5, "8",
    55  			8, 10, ""},
    56  		{11, "8",
    57  			8, 10, ""},
    58  		{1, "11",
    59  			10, 10, ""},
    60  	} {
    61  		gotFrom, gotTo, gotTok, err := PageBounds(test.size, test.tok, length)
    62  		if err != nil {
    63  			t.Fatal(err)
    64  		}
    65  		if got, want := gotFrom, test.wantFrom; got != want {
    66  			t.Errorf("%+v: from: got %d, want %d", test, got, want)
    67  		}
    68  		if got, want := gotTo, test.wantTo; got != want {
    69  			t.Errorf("%+v: to: got %d, want %d", test, got, want)
    70  		}
    71  		if got, want := gotTok, test.wantTok; got != want {
    72  			t.Errorf("%+v: got %q, want %q", test, got, want)
    73  		}
    74  	}
    75  
    76  	_, _, _, err := PageBounds(4, "xyz", 5)
    77  	if status.Code(err) != codes.InvalidArgument {
    78  		t.Errorf("want invalid argument, got <%v>", err)
    79  	}
    80  }
    81  

View as plain text