github.com/0xKiwi/rules_go@v0.24.3/tests/integration/googleapis/color_service_test.go (about)

     1  // Copyright 2018 The Bazel Authors. All rights reserved.
     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 color_service_test
    16  
    17  import (
    18  	"context"
    19  	"net"
    20  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/bazelbuild/rules_go/tests/integration/googleapis/color_service"
    24  	cspb "github.com/bazelbuild/rules_go/tests/integration/googleapis/color_service_proto"
    25  	"google.golang.org/genproto/googleapis/type/color"
    26  	"google.golang.org/grpc"
    27  )
    28  
    29  func TestColorService(t *testing.T) {
    30  	lis, err := net.Listen("tcp", "127.0.0.1:")
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	grpcServer := grpc.NewServer()
    35  	cspb.RegisterColorServiceServer(grpcServer, color_service.New())
    36  	go grpcServer.Serve(lis)
    37  	defer grpcServer.Stop()
    38  
    39  	conn, err := grpc.Dial(lis.Addr().String(), grpc.WithInsecure())
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	defer conn.Close()
    44  	client := cspb.NewColorServiceClient(conn)
    45  
    46  	_, err = client.SetColor(context.Background(), &cspb.SetColorRequest{
    47  		Name:  "red",
    48  		Color: &color.Color{Red: 1.0},
    49  	})
    50  	if err != nil {
    51  		t.Errorf("SetColor: %v", err)
    52  	}
    53  	resp, err := client.GetColor(context.Background(), &cspb.GetColorRequest{
    54  		Name: "red",
    55  	})
    56  	if err != nil {
    57  		t.Errorf("GetColor: %v", err)
    58  	}
    59  	want := &color.Color{Red: 1.0}
    60  	if !reflect.DeepEqual(resp.Color, want) {
    61  		t.Errorf("got %#v; want %#v", resp.Color, want)
    62  	}
    63  }