...

Source file src/go.opencensus.io/internal/testpb/impl.go

Documentation: go.opencensus.io/internal/testpb

     1  // Copyright 2018, OpenCensus Authors
     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 testpb
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"io"
    21  	"net"
    22  	"testing"
    23  	"time"
    24  
    25  	"go.opencensus.io/plugin/ocgrpc"
    26  	"go.opencensus.io/trace"
    27  	"google.golang.org/grpc"
    28  )
    29  
    30  type testServer struct{}
    31  
    32  var _ FooServer = (*testServer)(nil)
    33  
    34  func (s *testServer) Single(ctx context.Context, in *FooRequest) (*FooResponse, error) {
    35  	if in.SleepNanos > 0 {
    36  		_, span := trace.StartSpan(ctx, "testpb.Single.Sleep")
    37  		span.AddAttributes(trace.Int64Attribute("sleep_nanos", in.SleepNanos))
    38  		time.Sleep(time.Duration(in.SleepNanos))
    39  		span.End()
    40  	}
    41  	if in.Fail {
    42  		return nil, fmt.Errorf("request failed")
    43  	}
    44  	return &FooResponse{}, nil
    45  }
    46  
    47  func (s *testServer) Multiple(stream Foo_MultipleServer) error {
    48  	for {
    49  		in, err := stream.Recv()
    50  		if err == io.EOF {
    51  			return nil
    52  		}
    53  		if err != nil {
    54  			return err
    55  		}
    56  		if in.Fail {
    57  			return fmt.Errorf("request failed")
    58  		}
    59  		if err := stream.Send(&FooResponse{}); err != nil {
    60  			return err
    61  		}
    62  	}
    63  }
    64  
    65  // NewTestClient returns a new TestClient.
    66  func NewTestClient(l *testing.T) (client FooClient, cleanup func()) {
    67  	// initialize server
    68  	listener, err := net.Listen("tcp", "localhost:0")
    69  	if err != nil {
    70  		l.Fatal(err)
    71  	}
    72  	server := grpc.NewServer(grpc.StatsHandler(&ocgrpc.ServerHandler{}))
    73  	RegisterFooServer(server, &testServer{})
    74  	go server.Serve(listener)
    75  
    76  	// Initialize client.
    77  	clientConn, err := grpc.Dial(
    78  		listener.Addr().String(),
    79  		grpc.WithInsecure(),
    80  		grpc.WithStatsHandler(&ocgrpc.ClientHandler{}),
    81  		grpc.WithBlock())
    82  
    83  	if err != nil {
    84  		l.Fatal(err)
    85  	}
    86  	client = NewFooClient(clientConn)
    87  
    88  	cleanup = func() {
    89  		server.GracefulStop()
    90  		clientConn.Close()
    91  	}
    92  
    93  	return client, cleanup
    94  }
    95  

View as plain text