-
Notifications
You must be signed in to change notification settings - Fork 623
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
773 additions
and
8 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
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,57 @@ | ||
# Copyright The OpenTelemetry 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. | ||
|
||
from .protobuf.test_server_pb2 import Request | ||
|
||
CLIENT_ID = 1 | ||
|
||
|
||
def simple_method(stub, error=False): | ||
request = Request( | ||
client_id=CLIENT_ID, request_data="error" if error else "data" | ||
) | ||
stub.SimpleMethod(request) | ||
|
||
|
||
def client_streaming_method(stub, error=False): | ||
# create a generator | ||
def request_messages(): | ||
for _ in range(5): | ||
request = Request( | ||
client_id=CLIENT_ID, request_data="error" if error else "data" | ||
) | ||
yield request | ||
|
||
stub.ClientStreamingMethod(request_messages()) | ||
|
||
|
||
def server_streaming_method(stub, error=False): | ||
request = Request( | ||
client_id=CLIENT_ID, request_data="error" if error else "data" | ||
) | ||
response_iterator = stub.ServerStreamingMethod(request) | ||
list(response_iterator) | ||
|
||
|
||
def bidirectional_streaming_method(stub, error=False): | ||
def request_messages(): | ||
for _ in range(5): | ||
request = Request( | ||
client_id=CLIENT_ID, request_data="error" if error else "data" | ||
) | ||
yield request | ||
|
||
response_iterator = stub.BidirectionalStreamingMethod(request_messages()) | ||
|
||
list(response_iterator) |
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,87 @@ | ||
# Copyright The OpenTelemetry 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. | ||
|
||
from concurrent import futures | ||
|
||
import grpc | ||
|
||
from .protobuf import test_server_pb2, test_server_pb2_grpc | ||
|
||
SERVER_ID = 1 | ||
|
||
|
||
class TestServer(test_server_pb2_grpc.GRPCTestServerServicer): | ||
def SimpleMethod(self, request, context): | ||
if request.request_data == "error": | ||
context.set_code(grpc.StatusCode.INVALID_ARGUMENT) | ||
return test_server_pb2.Response() | ||
response = test_server_pb2.Response( | ||
server_id=SERVER_ID, response_data="data" | ||
) | ||
return response | ||
|
||
def ClientStreamingMethod(self, request_iterator, context): | ||
data = list(request_iterator) | ||
if data[0].request_data == "error": | ||
context.set_code(grpc.StatusCode.INVALID_ARGUMENT) | ||
return test_server_pb2.Response() | ||
response = test_server_pb2.Response( | ||
server_id=SERVER_ID, response_data="data" | ||
) | ||
return response | ||
|
||
def ServerStreamingMethod(self, request, context): | ||
if request.request_data == "error": | ||
|
||
context.abort( | ||
code=grpc.StatusCode.INVALID_ARGUMENT, | ||
details="server stream error", | ||
) | ||
return test_server_pb2.Response() | ||
|
||
# create a generator | ||
def response_messages(): | ||
for _ in range(5): | ||
response = test_server_pb2.Response( | ||
server_id=SERVER_ID, response_data="data" | ||
) | ||
yield response | ||
|
||
return response_messages() | ||
|
||
def BidirectionalStreamingMethod(self, request_iterator, context): | ||
data = list(request_iterator) | ||
if data[0].request_data == "error": | ||
context.abort( | ||
code=grpc.StatusCode.INVALID_ARGUMENT, | ||
details="bidirectional error", | ||
) | ||
return | ||
|
||
for _ in range(5): | ||
yield test_server_pb2.Response( | ||
server_id=SERVER_ID, response_data="data" | ||
) | ||
|
||
|
||
def create_test_server(port): | ||
server = grpc.server(futures.ThreadPoolExecutor(max_workers=1)) | ||
|
||
test_server_pb2_grpc.add_GRPCTestServerServicer_to_server( | ||
TestServer(), server | ||
) | ||
|
||
server.add_insecure_port("localhost:{}".format(port)) | ||
|
||
return server |
34 changes: 34 additions & 0 deletions
34
ext/opentelemetry-ext-grpc/tests/protobuf/test_server.proto
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,34 @@ | ||
// Copyright 2019 gRPC 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. | ||
syntax = "proto3"; | ||
|
||
message Request { | ||
int64 client_id = 1; | ||
string request_data = 2; | ||
} | ||
|
||
message Response { | ||
int64 server_id = 1; | ||
string response_data = 2; | ||
} | ||
|
||
service GRPCTestServer { | ||
rpc SimpleMethod (Request) returns (Response); | ||
|
||
rpc ClientStreamingMethod (stream Request) returns (Response); | ||
|
||
rpc ServerStreamingMethod (Request) returns (stream Response); | ||
|
||
rpc BidirectionalStreamingMethod (stream Request) returns (stream Response); | ||
} |
Oops, something went wrong.