Best Python code snippet using autotest_python
_service.py
Source:_service.py
1# Copyright 2015, Google Inc.2# All rights reserved.3#4# Redistribution and use in source and binary forms, with or without5# modification, are permitted provided that the following conditions are6# met:7#8# * Redistributions of source code must retain the above copyright9# notice, this list of conditions and the following disclaimer.10# * Redistributions in binary form must reproduce the above11# copyright notice, this list of conditions and the following disclaimer12# in the documentation and/or other materials provided with the13# distribution.14# * Neither the name of Google Inc. nor the names of its15# contributors may be used to endorse or promote products derived from16# this software without specific prior written permission.17#18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29"""Behaviors for servicing RPCs."""30from grpc.framework.crust import _control31from grpc.framework.foundation import abandonment32from grpc.framework.interfaces.base import utilities33from grpc.framework.interfaces.face import face34class _ServicerContext(face.ServicerContext):35 def __init__(self, rendezvous):36 self._rendezvous = rendezvous37 def is_active(self):38 return self._rendezvous.is_active()39 def time_remaining(self):40 return self._rendezvous.time_remaining()41 def add_abortion_callback(self, abortion_callback):42 return self._rendezvous.add_abortion_callback(abortion_callback)43 def cancel(self):44 self._rendezvous.cancel()45 def protocol_context(self):46 return self._rendezvous.protocol_context()47 def invocation_metadata(self):48 return self._rendezvous.initial_metadata()49 def initial_metadata(self, initial_metadata):50 self._rendezvous.set_initial_metadata(initial_metadata)51 def terminal_metadata(self, terminal_metadata):52 self._rendezvous.set_terminal_metadata(terminal_metadata)53 def code(self, code):54 self._rendezvous.set_code(code)55 def details(self, details):56 self._rendezvous.set_details(details)57def _adaptation(pool, in_pool):58 def adaptation(operator, operation_context):59 rendezvous = _control.Rendezvous(operator, operation_context)60 subscription = utilities.full_subscription(61 rendezvous, _control.protocol_receiver(rendezvous))62 outcome = operation_context.add_termination_callback(rendezvous.set_outcome)63 if outcome is None:64 pool.submit(_control.pool_wrap(in_pool, operation_context), rendezvous)65 return subscription66 else:67 raise abandonment.Abandoned()68 return adaptation69def adapt_inline_unary_unary(method, pool):70 def in_pool(rendezvous):71 request = next(rendezvous)72 response = method(request, _ServicerContext(rendezvous))73 rendezvous.consume_and_terminate(response)74 return _adaptation(pool, in_pool)75def adapt_inline_unary_stream(method, pool):76 def in_pool(rendezvous):77 request = next(rendezvous)78 response_iterator = method(request, _ServicerContext(rendezvous))79 for response in response_iterator:80 rendezvous.consume(response)81 rendezvous.terminate()82 return _adaptation(pool, in_pool)83def adapt_inline_stream_unary(method, pool):84 def in_pool(rendezvous):85 response = method(rendezvous, _ServicerContext(rendezvous))86 rendezvous.consume_and_terminate(response)87 return _adaptation(pool, in_pool)88def adapt_inline_stream_stream(method, pool):89 def in_pool(rendezvous):90 response_iterator = method(rendezvous, _ServicerContext(rendezvous))91 for response in response_iterator:92 rendezvous.consume(response)93 rendezvous.terminate()94 return _adaptation(pool, in_pool)95def adapt_event_unary_unary(method, pool):96 def in_pool(rendezvous):97 request = next(rendezvous)98 method(99 request, rendezvous.consume_and_terminate, _ServicerContext(rendezvous))100 return _adaptation(pool, in_pool)101def adapt_event_unary_stream(method, pool):102 def in_pool(rendezvous):103 request = next(rendezvous)104 method(request, rendezvous, _ServicerContext(rendezvous))105 return _adaptation(pool, in_pool)106def adapt_event_stream_unary(method, pool):107 def in_pool(rendezvous):108 request_consumer = method(109 rendezvous.consume_and_terminate, _ServicerContext(rendezvous))110 for request in rendezvous:111 request_consumer.consume(request)112 request_consumer.terminate()113 return _adaptation(pool, in_pool)114def adapt_event_stream_stream(method, pool):115 def in_pool(rendezvous):116 request_consumer = method(rendezvous, _ServicerContext(rendezvous))117 for request in rendezvous:118 request_consumer.consume(request)119 request_consumer.terminate()120 return _adaptation(pool, in_pool)121def adapt_multi_method(multi_method, pool):122 def adaptation(group, method, operator, operation_context):123 rendezvous = _control.Rendezvous(operator, operation_context)124 subscription = utilities.full_subscription(125 rendezvous, _control.protocol_receiver(rendezvous))126 outcome = operation_context.add_termination_callback(rendezvous.set_outcome)127 if outcome is None:128 def in_pool():129 request_consumer = multi_method.service(130 group, method, rendezvous, _ServicerContext(rendezvous))131 for request in rendezvous:132 request_consumer.consume(request)133 request_consumer.terminate()134 pool.submit(_control.pool_wrap(in_pool, operation_context), rendezvous)135 return subscription136 else:137 raise abandonment.Abandoned()...
test_rendezvous.py
Source:test_rendezvous.py
1from pymemcache.client.rendezvous import RendezvousHash2import pytest3@pytest.mark.unit()4def test_init_no_options():5 rendezvous = RendezvousHash()6 assert 0 == len(rendezvous.nodes)7 assert 1361238019 == rendezvous.hash_function('6666')8@pytest.mark.unit()9def test_init():10 nodes = ['0', '1', '2']11 rendezvous = RendezvousHash(nodes=nodes)12 assert 3 == len(rendezvous.nodes)13 assert 1361238019 == rendezvous.hash_function('6666')14@pytest.mark.unit()15def test_seed():16 rendezvous = RendezvousHash(seed=10)17 assert 2981722772 == rendezvous.hash_function('6666')18@pytest.mark.unit()19def test_add_node():20 rendezvous = RendezvousHash()21 rendezvous.add_node('1')22 assert 1 == len(rendezvous.nodes)23 rendezvous.add_node('1')24 assert 1 == len(rendezvous.nodes)25 rendezvous.add_node('2')26 assert 2 == len(rendezvous.nodes)27 rendezvous.add_node('1')28 assert 2 == len(rendezvous.nodes)29@pytest.mark.unit()30def test_remove_node():31 nodes = ['0', '1', '2']32 rendezvous = RendezvousHash(nodes=nodes)33 rendezvous.remove_node('2')34 assert 2 == len(rendezvous.nodes)35 with pytest.raises(ValueError):36 rendezvous.remove_node('2')37 assert 2 == len(rendezvous.nodes)38 rendezvous.remove_node('1')39 assert 1 == len(rendezvous.nodes)40 rendezvous.remove_node('0')41 assert 0 == len(rendezvous.nodes)42@pytest.mark.unit()43def test_get_node():44 nodes = ['0', '1', '2']45 rendezvous = RendezvousHash(nodes=nodes)46 assert '0' == rendezvous.get_node('ok')47 assert '1' == rendezvous.get_node('mykey')48 assert '2' == rendezvous.get_node('wat')49@pytest.mark.unit()50def test_get_node_after_removal():51 nodes = ['0', '1', '2']52 rendezvous = RendezvousHash(nodes=nodes)53 rendezvous.remove_node('1')54 assert '0' == rendezvous.get_node('ok')55 assert '0' == rendezvous.get_node('mykey')56 assert '2' == rendezvous.get_node('wat')57@pytest.mark.unit()58def test_get_node_after_addition():59 nodes = ['0', '1', '2']60 rendezvous = RendezvousHash(nodes=nodes)61 assert '0' == rendezvous.get_node('ok')62 assert '1' == rendezvous.get_node('mykey')63 assert '2' == rendezvous.get_node('wat')64 assert '2' == rendezvous.get_node('lol')65 rendezvous.add_node('3')66 assert '0' == rendezvous.get_node('ok')67 assert '1' == rendezvous.get_node('mykey')68 assert '2' == rendezvous.get_node('wat')69 assert '3' == rendezvous.get_node('lol')70@pytest.mark.unit()71def test_grow():72 rendezvous = RendezvousHash()73 placements = {}74 for i in range(10):75 rendezvous.add_node(str(i))76 placements[str(i)] = []77 for i in range(1000):78 node = rendezvous.get_node(str(i))79 placements[node].append(i)80 new_placements = {}81 for i in range(20):82 rendezvous.add_node(str(i))83 new_placements[str(i)] = []84 for i in range(1000):85 node = rendezvous.get_node(str(i))86 new_placements[node].append(i)87 keys = [k for sublist in placements.values() for k in sublist]88 new_keys = [k for sublist in new_placements.values() for k in sublist]89 assert sorted(keys) == sorted(new_keys)90 added = 091 removed = 092 for node, assignments in new_placements.items():93 after = set(assignments)94 before = set(placements.get(node, []))95 removed += len(before.difference(after))96 added += len(after.difference(before))97 assert added == removed98 assert 1062 == (added + removed)99@pytest.mark.unit()100def test_shrink():101 rendezvous = RendezvousHash()102 placements = {}103 for i in range(10):104 rendezvous.add_node(str(i))105 placements[str(i)] = []106 for i in range(1000):107 node = rendezvous.get_node(str(i))108 placements[node].append(i)109 rendezvous.remove_node('9')110 new_placements = {}111 for i in range(9):112 new_placements[str(i)] = []113 for i in range(1000):114 node = rendezvous.get_node(str(i))115 new_placements[node].append(i)116 keys = [k for sublist in placements.values() for k in sublist]117 new_keys = [k for sublist in new_placements.values() for k in sublist]118 assert sorted(keys) == sorted(new_keys)119 added = 0120 removed = 0121 for node, assignments in placements.items():122 after = set(assignments)123 before = set(new_placements.get(node, []))124 removed += len(before.difference(after))125 added += len(after.difference(before))126 assert added == removed127 assert 202 == (added + removed)128def collide(key, seed):129 return 1337130@pytest.mark.unit()131def test_rendezvous_collision():132 nodes = ['c', 'b', 'a']133 rendezvous = RendezvousHash(nodes, hash_function=collide)134 for i in range(1000):135 assert 'c' == rendezvous.get_node(i)136@pytest.mark.unit()137def test_rendezvous_names():138 nodes = [1, 2, 3, 'a', 'b', 'lol.wat.com']139 rendezvous = RendezvousHash(nodes, hash_function=collide)140 for i in range(10):141 assert 'lol.wat.com' == rendezvous.get_node(i)142 nodes = [1, 'a', '0']143 rendezvous = RendezvousHash(nodes, hash_function=collide)144 for i in range(10):...
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!!