This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for gRPC span duration an Stackdriver gRPC exporting (#596)
- Loading branch information
Ramon Nogueira
authored
Mar 19, 2018
1 parent
b2a78ff
commit e3a7a61
Showing
8 changed files
with
376 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright 2018, OpenCensus Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package testpb | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net" | ||
"testing" | ||
"time" | ||
|
||
"go.opencensus.io/plugin/ocgrpc" | ||
"go.opencensus.io/trace" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
type testServer struct{} | ||
|
||
var _ FooServer = (*testServer)(nil) | ||
|
||
func (s *testServer) Single(ctx context.Context, in *FooRequest) (*FooResponse, error) { | ||
if in.SleepNanos > 0 { | ||
_, span := trace.StartSpan(ctx, "testpb.Single.Sleep") | ||
span.AddAttributes(trace.Int64Attribute("sleep_nanos", in.SleepNanos)) | ||
time.Sleep(time.Duration(in.SleepNanos)) | ||
span.End() | ||
} | ||
if in.Fail { | ||
return nil, fmt.Errorf("request failed") | ||
} | ||
return &FooResponse{}, nil | ||
} | ||
|
||
func (s *testServer) Multiple(stream Foo_MultipleServer) error { | ||
for { | ||
in, err := stream.Recv() | ||
if err == io.EOF { | ||
return nil | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
if in.Fail { | ||
return fmt.Errorf("request failed") | ||
} | ||
if err := stream.Send(&FooResponse{}); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
func NewTestClient(l *testing.T) (client FooClient, cleanup func()) { | ||
// initialize server | ||
listener, err := net.Listen("tcp", "localhost:0") | ||
if err != nil { | ||
l.Fatal(err) | ||
} | ||
server := grpc.NewServer(grpc.StatsHandler(&ocgrpc.ServerHandler{})) | ||
RegisterFooServer(server, &testServer{}) | ||
go server.Serve(listener) | ||
|
||
// Initialize client. | ||
clientConn, err := grpc.Dial( | ||
listener.Addr().String(), | ||
grpc.WithInsecure(), | ||
grpc.WithStatsHandler(&ocgrpc.ClientHandler{}), | ||
grpc.WithBlock()) | ||
|
||
if err != nil { | ||
l.Fatal(err) | ||
} | ||
client = NewFooClient(clientConn) | ||
|
||
cleanup = func() { | ||
server.GracefulStop() | ||
clientConn.Close() | ||
} | ||
|
||
return client, cleanup | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ package testpb; | |
|
||
message FooRequest { | ||
bool fail = 1; | ||
int64 sleep_nanos = 2; | ||
} | ||
|
||
message FooResponse { | ||
|
Oops, something went wrong.