Best Python code snippet using hypothesis
_handler.py
Source:_handler.py  
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 at6#7#     http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# 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 and13# limitations under the License.14import abc15import threading16import grpc17from grpc_testing import _common18_CLIENT_INACTIVE = object()19class Handler(_common.ServerRpcHandler):20    @abc.abstractmethod21    def initial_metadata(self):22        raise NotImplementedError()23    @abc.abstractmethod24    def add_request(self, request):25        raise NotImplementedError()26    @abc.abstractmethod27    def take_response(self):28        raise NotImplementedError()29    @abc.abstractmethod30    def requests_closed(self):31        raise NotImplementedError()32    @abc.abstractmethod33    def cancel(self):34        raise NotImplementedError()35    @abc.abstractmethod36    def unary_response_termination(self):37        raise NotImplementedError()38    @abc.abstractmethod39    def stream_response_termination(self):40        raise NotImplementedError()41class _Handler(Handler):42    def __init__(self, requests_closed):43        self._condition = threading.Condition()44        self._requests = []45        self._requests_closed = requests_closed46        self._initial_metadata = None47        self._responses = []48        self._trailing_metadata = None49        self._code = None50        self._details = None51        self._unary_response = None52        self._expiration_future = None53        self._termination_callbacks = []54    def send_initial_metadata(self, initial_metadata):55        with self._condition:56            self._initial_metadata = initial_metadata57            self._condition.notify_all()58    def take_request(self):59        with self._condition:60            while True:61                if self._code is None:62                    if self._requests:63                        request = self._requests.pop(0)64                        self._condition.notify_all()65                        return _common.ServerRpcRead(request, False, False)66                    elif self._requests_closed:67                        return _common.REQUESTS_CLOSED68                    else:69                        self._condition.wait()70                else:71                    return _common.TERMINATED72    def is_active(self):73        with self._condition:74            return self._code is None75    def add_response(self, response):76        with self._condition:77            self._responses.append(response)78            self._condition.notify_all()79    def send_termination(self, trailing_metadata, code, details):80        with self._condition:81            self._trailing_metadata = trailing_metadata82            self._code = code83            self._details = details84            if self._expiration_future is not None:85                self._expiration_future.cancel()86            self._condition.notify_all()87    def add_termination_callback(self, termination_callback):88        with self._condition:89            if self._code is None:90                self._termination_callbacks.append(termination_callback)91                return True92            else:93                return False94    def initial_metadata(self):95        with self._condition:96            while True:97                if self._initial_metadata is None:98                    if self._code is None:99                        self._condition.wait()100                    else:101                        raise ValueError(102                            'No initial metadata despite status code!')103                else:104                    return self._initial_metadata105    def add_request(self, request):106        with self._condition:107            self._requests.append(request)108            self._condition.notify_all()109    def take_response(self):110        with self._condition:111            while True:112                if self._responses:113                    response = self._responses.pop(0)114                    self._condition.notify_all()115                    return response116                elif self._code is None:117                    self._condition.wait()118                else:119                    raise ValueError('No more responses!')120    def requests_closed(self):121        with self._condition:122            self._requests_closed = True123            self._condition.notify_all()124    def cancel(self):125        with self._condition:126            if self._code is None:127                self._code = _CLIENT_INACTIVE128                termination_callbacks = self._termination_callbacks129                self._termination_callbacks = None130                if self._expiration_future is not None:131                    self._expiration_future.cancel()132                self._condition.notify_all()133        for termination_callback in termination_callbacks:134            termination_callback()135    def unary_response_termination(self):136        with self._condition:137            while True:138                if self._code is _CLIENT_INACTIVE:139                    raise ValueError('Huh? Cancelled but wanting status?')140                elif self._code is None:141                    self._condition.wait()142                else:143                    if self._unary_response is None:144                        if self._responses:145                            self._unary_response = self._responses.pop(0)146                    return (147                        self._unary_response,148                        self._trailing_metadata,149                        self._code,150                        self._details,151                    )152    def stream_response_termination(self):153        with self._condition:154            while True:155                if self._code is _CLIENT_INACTIVE:156                    raise ValueError('Huh? Cancelled but wanting status?')157                elif self._code is None:158                    self._condition.wait()159                else:160                    return self._trailing_metadata, self._code, self._details,161    def expire(self):162        with self._condition:163            if self._code is None:164                if self._initial_metadata is None:165                    self._initial_metadata = _common.FUSSED_EMPTY_METADATA166                self._trailing_metadata = _common.FUSSED_EMPTY_METADATA167                self._code = grpc.StatusCode.DEADLINE_EXCEEDED168                self._details = 'Took too much time!'169                termination_callbacks = self._termination_callbacks170                self._termination_callbacks = None171                self._condition.notify_all()172        for termination_callback in termination_callbacks:173            termination_callback()174    def set_expiration_future(self, expiration_future):175        with self._condition:176            self._expiration_future = expiration_future177def handler_without_deadline(requests_closed):178    return _Handler(requests_closed)179def handler_with_deadline(requests_closed, time, deadline):180    handler = _Handler(requests_closed)181    expiration_future = time.call_at(handler.expire, deadline)182    handler.set_expiration_future(expiration_future)..._rpc_state.py
Source:_rpc_state.py  
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 at6#7#     http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# 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 and13# limitations under the License.14import threading15import grpc16from grpc_testing import _common17class State(_common.ChannelRpcHandler):18    def __init__(self, invocation_metadata, requests, requests_closed):19        self._condition = threading.Condition()20        self._invocation_metadata = invocation_metadata21        self._requests = requests22        self._requests_closed = requests_closed23        self._initial_metadata = None24        self._responses = []25        self._trailing_metadata = None26        self._code = None27        self._details = None28    def initial_metadata(self):29        with self._condition:30            while True:31                if self._initial_metadata is None:32                    if self._code is None:33                        self._condition.wait()34                    else:35                        return _common.FUSSED_EMPTY_METADATA36                else:37                    return self._initial_metadata38    def add_request(self, request):39        with self._condition:40            if self._code is None and not self._requests_closed:41                self._requests.append(request)42                self._condition.notify_all()43                return True44            else:45                return False46    def close_requests(self):47        with self._condition:48            if self._code is None and not self._requests_closed:49                self._requests_closed = True50                self._condition.notify_all()51    def take_response(self):52        with self._condition:53            while True:54                if self._code is grpc.StatusCode.OK:55                    if self._responses:56                        response = self._responses.pop(0)57                        return _common.ChannelRpcRead(response, None, None,58                                                      None)59                    else:60                        return _common.ChannelRpcRead(61                            None, self._trailing_metadata, grpc.StatusCode.OK,62                            self._details)63                elif self._code is None:64                    if self._responses:65                        response = self._responses.pop(0)66                        return _common.ChannelRpcRead(response, None, None,67                                                      None)68                    else:69                        self._condition.wait()70                else:71                    return _common.ChannelRpcRead(None, self._trailing_metadata,72                                                  self._code, self._details)73    def termination(self):74        with self._condition:75            while True:76                if self._code is None:77                    self._condition.wait()78                else:79                    return self._trailing_metadata, self._code, self._details80    def cancel(self, code, details):81        with self._condition:82            if self._code is None:83                if self._initial_metadata is None:84                    self._initial_metadata = _common.FUSSED_EMPTY_METADATA85                self._trailing_metadata = _common.FUSSED_EMPTY_METADATA86                self._code = code87                self._details = details88                self._condition.notify_all()89                return True90            else:91                return False92    def take_invocation_metadata(self):93        with self._condition:94            if self._invocation_metadata is None:95                raise ValueError('Expected invocation metadata!')96            else:97                invocation_metadata = self._invocation_metadata98                self._invocation_metadata = None99                return invocation_metadata100    def take_invocation_metadata_and_request(self):101        with self._condition:102            if self._invocation_metadata is None:103                raise ValueError('Expected invocation metadata!')104            elif not self._requests:105                raise ValueError('Expected at least one request!')106            else:107                invocation_metadata = self._invocation_metadata108                self._invocation_metadata = None109                return invocation_metadata, self._requests.pop(0)110    def send_initial_metadata(self, initial_metadata):111        with self._condition:112            self._initial_metadata = _common.fuss_with_metadata(113                initial_metadata)114            self._condition.notify_all()115    def take_request(self):116        with self._condition:117            while True:118                if self._requests:119                    return self._requests.pop(0)120                else:121                    self._condition.wait()122    def requests_closed(self):123        with self._condition:124            while True:125                if self._requests_closed:126                    return127                else:128                    self._condition.wait()129    def send_response(self, response):130        with self._condition:131            if self._code is None:132                self._responses.append(response)133                self._condition.notify_all()134    def terminate_with_response(self, response, trailing_metadata, code,135                                details):136        with self._condition:137            if self._initial_metadata is None:138                self._initial_metadata = _common.FUSSED_EMPTY_METADATA139            self._responses.append(response)140            self._trailing_metadata = _common.fuss_with_metadata(141                trailing_metadata)142            self._code = code143            self._details = details144            self._condition.notify_all()145    def terminate(self, trailing_metadata, code, details):146        with self._condition:147            if self._initial_metadata is None:148                self._initial_metadata = _common.FUSSED_EMPTY_METADATA149            self._trailing_metadata = _common.fuss_with_metadata(150                trailing_metadata)151            self._code = code152            self._details = details153            self._condition.notify_all()154    def cancelled(self):155        with self._condition:156            while True:157                if self._code is grpc.StatusCode.CANCELLED:158                    return159                elif self._code is None:160                    self._condition.wait()161                else:162                    raise ValueError('Status code unexpectedly {}!'.format(163                        self._code))164    def is_active(self):165        raise NotImplementedError()166    def time_remaining(self):167        raise NotImplementedError()168    def add_callback(self, callback):...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
