| 1 |
# Copyright 2017 gRPC 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 |
import collections |
| 16 |
import threading |
| 17 |
|
| 18 |
from grpc_testing import _common |
| 19 |
from grpc_testing._channel import _rpc_state |
| 20 |
|
| 21 |
|
| 22 |
class State(_common.ChannelHandler): |
| 23 |
|
| 24 |
def __init__(self): |
| 25 |
self._condition = threading.Condition() |
| 26 |
self._rpc_states = collections.defaultdict(list) |
| 27 |
|
| 28 |
def invoke_rpc(self, method_full_rpc_name, invocation_metadata, requests, |
| 29 |
requests_closed, timeout): |
| 30 |
rpc_state = _rpc_state.State(invocation_metadata, requests, |
| 31 |
requests_closed) |
| 32 |
with self._condition: |
| 33 |
self._rpc_states[method_full_rpc_name].append(rpc_state) |
| 34 |
self._condition.notify_all() |
| 35 |
return rpc_state |
| 36 |
|
| 37 |
def take_rpc_state(self, method_descriptor): |
| 38 |
method_full_rpc_name = '/{}/{}'.format( |
| 39 |
method_descriptor.containing_service.full_name, |
| 40 |
method_descriptor.name) |
| 41 |
with self._condition: |
| 42 |
while True: |
| 43 |
method_rpc_states = self._rpc_states[method_full_rpc_name] |
| 44 |
if method_rpc_states: |
| 45 |
return method_rpc_states.pop(0) |
| 46 |
else: |
| 47 |
self._condition.wait() |