Best Python code snippet using Airtest
test_rate_limits.py
Source:test_rate_limits.py  
1import asyncio2import pytest3from chia.protocols.protocol_message_types import ProtocolMessageTypes4from chia.server.outbound_message import make_msg5from chia.server.rate_limits import RateLimiter, NON_TX_FREQ6from tests.setup_nodes import test_constants7@pytest.fixture(scope="module")8def event_loop():9    loop = asyncio.get_event_loop()10    yield loop11constants = test_constants12class TestRateLimits:13    @pytest.mark.asyncio14    async def test_too_many_messages(self):15        # Too many messages16        r = RateLimiter(incoming=True)17        new_tx_message = make_msg(ProtocolMessageTypes.new_transaction, bytes([1] * 40))18        for i in range(3000):19            assert r.process_msg_and_check(new_tx_message)20        saw_disconnect = False21        for i in range(3000):22            response = r.process_msg_and_check(new_tx_message)23            if not response:24                saw_disconnect = True25        assert saw_disconnect26        # Non-tx message27        r = RateLimiter(incoming=True)28        new_peak_message = make_msg(ProtocolMessageTypes.new_peak, bytes([1] * 40))29        for i in range(20):30            assert r.process_msg_and_check(new_peak_message)31        saw_disconnect = False32        for i in range(200):33            response = r.process_msg_and_check(new_peak_message)34            if not response:35                saw_disconnect = True36        assert saw_disconnect37    @pytest.mark.asyncio38    async def test_large_message(self):39        # Large tx40        small_tx_message = make_msg(ProtocolMessageTypes.respond_transaction, bytes([1] * 500 * 1024))41        large_tx_message = make_msg(ProtocolMessageTypes.new_transaction, bytes([1] * 3 * 1024 * 1024))42        r = RateLimiter(incoming=True)43        assert r.process_msg_and_check(small_tx_message)44        assert r.process_msg_and_check(small_tx_message)45        assert not r.process_msg_and_check(large_tx_message)46        small_vdf_message = make_msg(ProtocolMessageTypes.respond_signage_point, bytes([1] * 5 * 1024))47        large_vdf_message = make_msg(ProtocolMessageTypes.respond_signage_point, bytes([1] * 600 * 1024))48        r = RateLimiter(incoming=True)49        assert r.process_msg_and_check(small_vdf_message)50        assert r.process_msg_and_check(small_vdf_message)51        assert not r.process_msg_and_check(large_vdf_message)52    @pytest.mark.asyncio53    async def test_too_much_data(self):54        # Too much data55        r = RateLimiter(incoming=True)56        tx_message = make_msg(ProtocolMessageTypes.respond_transaction, bytes([1] * 500 * 1024))57        for i in range(10):58            assert r.process_msg_and_check(tx_message)59        saw_disconnect = False60        for i in range(300):61            response = r.process_msg_and_check(tx_message)62            if not response:63                saw_disconnect = True64        assert saw_disconnect65        r = RateLimiter(incoming=True)66        block_message = make_msg(ProtocolMessageTypes.respond_block, bytes([1] * 1024 * 1024))67        for i in range(10):68            assert r.process_msg_and_check(block_message)69        saw_disconnect = False70        for i in range(40):71            response = r.process_msg_and_check(block_message)72            if not response:73                saw_disconnect = True74        assert saw_disconnect75    @pytest.mark.asyncio76    async def test_non_tx_aggregate_limits(self):77        # Frequency limits78        r = RateLimiter(incoming=True)79        message_1 = make_msg(ProtocolMessageTypes.request_additions, bytes([1] * 5 * 1024))80        message_2 = make_msg(ProtocolMessageTypes.request_removals, bytes([1] * 1024))81        message_3 = make_msg(ProtocolMessageTypes.respond_additions, bytes([1] * 1024))82        for i in range(450):83            assert r.process_msg_and_check(message_1)84        for i in range(450):85            assert r.process_msg_and_check(message_2)86        saw_disconnect = False87        for i in range(450):88            response = r.process_msg_and_check(message_3)89            if not response:90                saw_disconnect = True91        assert saw_disconnect92        # Size limits93        r = RateLimiter(incoming=True)94        message_4 = make_msg(ProtocolMessageTypes.respond_proof_of_weight, bytes([1] * 49 * 1024 * 1024))95        message_5 = make_msg(ProtocolMessageTypes.respond_blocks, bytes([1] * 49 * 1024 * 1024))96        for i in range(2):97            assert r.process_msg_and_check(message_4)98        saw_disconnect = False99        for i in range(2):100            response = r.process_msg_and_check(message_5)101            if not response:102                saw_disconnect = True103        assert saw_disconnect104    @pytest.mark.asyncio105    async def test_periodic_reset(self):106        r = RateLimiter(True, 5)107        tx_message = make_msg(ProtocolMessageTypes.respond_transaction, bytes([1] * 500 * 1024))108        for i in range(10):109            assert r.process_msg_and_check(tx_message)110        saw_disconnect = False111        for i in range(300):112            response = r.process_msg_and_check(tx_message)113            if not response:114                saw_disconnect = True115        assert saw_disconnect116        assert not r.process_msg_and_check(tx_message)117        await asyncio.sleep(6)118        assert r.process_msg_and_check(tx_message)119        # Counts reset also120        r = RateLimiter(True, 5)121        new_tx_message = make_msg(ProtocolMessageTypes.new_transaction, bytes([1] * 40))122        for i in range(3000):123            assert r.process_msg_and_check(new_tx_message)124        saw_disconnect = False125        for i in range(3000):126            response = r.process_msg_and_check(new_tx_message)127            if not response:128                saw_disconnect = True129        assert saw_disconnect130        assert not r.process_msg_and_check(new_tx_message)131        await asyncio.sleep(6)132        assert r.process_msg_and_check(new_tx_message)133    @pytest.mark.asyncio134    async def test_percentage_limits(self):135        r = RateLimiter(True, 60, 40)136        new_peak_message = make_msg(ProtocolMessageTypes.new_peak, bytes([1] * 40))137        for i in range(50):138            assert r.process_msg_and_check(new_peak_message)139        saw_disconnect = False140        for i in range(50):141            response = r.process_msg_and_check(new_peak_message)142            if not response:143                saw_disconnect = True144        assert saw_disconnect145        r = RateLimiter(True, 60, 40)146        block_message = make_msg(ProtocolMessageTypes.respond_block, bytes([1] * 1024 * 1024))147        for i in range(5):148            assert r.process_msg_and_check(block_message)149        saw_disconnect = False150        for i in range(5):151            response = r.process_msg_and_check(block_message)152            if not response:153                saw_disconnect = True154        assert saw_disconnect155        # Aggregate percentage limit count156        r = RateLimiter(True, 60, 40)157        message_1 = make_msg(ProtocolMessageTypes.request_additions, bytes([1] * 5 * 1024))158        message_2 = make_msg(ProtocolMessageTypes.request_removals, bytes([1] * 1024))159        message_3 = make_msg(ProtocolMessageTypes.respond_additions, bytes([1] * 1024))160        for i in range(180):161            assert r.process_msg_and_check(message_1)162        for i in range(180):163            assert r.process_msg_and_check(message_2)164        saw_disconnect = False165        for i in range(100):166            response = r.process_msg_and_check(message_3)167            if not response:168                saw_disconnect = True169        assert saw_disconnect170        # Aggregate percentage limit max total size171        r = RateLimiter(True, 60, 40)172        message_4 = make_msg(ProtocolMessageTypes.respond_proof_of_weight, bytes([1] * 18 * 1024 * 1024))173        message_5 = make_msg(ProtocolMessageTypes.respond_blocks, bytes([1] * 24 * 1024 * 1024))174        for i in range(2):175            assert r.process_msg_and_check(message_4)176        saw_disconnect = False177        for i in range(2):178            response = r.process_msg_and_check(message_5)179            if not response:180                saw_disconnect = True181        assert saw_disconnect182    @pytest.mark.asyncio183    async def test_too_many_outgoing_messages(self):184        # Too many messages185        r = RateLimiter(incoming=False)186        new_peers_message = make_msg(ProtocolMessageTypes.respond_peers, bytes([1]))187        passed = 0188        blocked = 0189        for i in range(NON_TX_FREQ):190            if r.process_msg_and_check(new_peers_message):191                passed += 1192            else:193                blocked += 1194        assert passed == 10195        assert blocked == NON_TX_FREQ - passed196        # ensure that *another* message type is not blocked because of this197        new_signatures_message = make_msg(ProtocolMessageTypes.respond_signatures, bytes([1]))198        assert r.process_msg_and_check(new_signatures_message)199    @pytest.mark.asyncio200    async def test_too_many_incoming_messages(self):201        # Too many messages202        r = RateLimiter(incoming=True)203        new_peers_message = make_msg(ProtocolMessageTypes.respond_peers, bytes([1]))204        passed = 0205        blocked = 0206        for i in range(NON_TX_FREQ):207            if r.process_msg_and_check(new_peers_message):208                passed += 1209            else:210                blocked += 1211        assert passed == 10212        assert blocked == NON_TX_FREQ - passed213        # ensure that other message types *are* blocked because of this214        new_signatures_message = make_msg(ProtocolMessageTypes.respond_signatures, bytes([1]))...__main__.py
Source:__main__.py  
...19        load_plugins(plugin_name.replace(".py", ""))20prirint("Enjoy! Do visit @YurikoXSpam")21if len(argv) not in (1, 3, 4):22    try:23        Yur.disconnect()24    except Exception as e:25        passnt("YurikoXSpam Bot Spam Successfully deployed -!")26p27    try:28        Yur2.disconnect()29    except Exception as e:30        pass31    try:32        Yur3.disconnect()33    except Exception as e:34        pass35    try:36        Yur4.disconnect()37    except Exception as e:38        pass39    try:40        Yur5.disconnect()41    except Exception as e:42        pass43    try:44        Yur6.disconnect()45    except Exception as e:46        pass47    try:48        Yur7.disconnect()49    except Exception as e:50        pass51    try:52        Yur8.disconnect()53    except Exception as e:54        pass55    try:56        Yur9.disconnect()57    except Exception as e:58        pass59    try:60        Yur10.disconnect()61    except Exception as e:62        pass63    try:64        Yur11.disconnect()65    except Exception as e:66        pass67    try:68        Yur12.disconnect()69    except Exception as e:70        pass71    try:72        Yur13.disconnect()73    except Exception as e:74        pass75    try:76        Yur14.disconnect()77    except Exception as e:78        pass79    try:80        Yur15.disconnect()81    except Exception as e:82        pass83    try:84        Yur16.disconnect()85    except Exception as e:86        pass87    try:88        Yur17.disconnect()89    except Exception as e:90        pass91    try:92        Yur18.disconnect()93    except Exception as e:94        pass95    try:96        Yur19.disconnect()97    except Exception as e:98        pass99    try:100        Yur20.disconnect()101    except Exception as e:102        pass103    try:104        Yur21.disconnect()105    except Exception as e:106        pass107    try:108        Yur22.disconnect()109    except Exception as e:110        pass111    try:112        Yur23.disconnect()113    except Exception as e:114        pass115    try:116        Yur24.disconnect()117    except Exception as e:118        pass119    try:120        Yur25.disconnect()121    except Exception as e:122        pass123    try:124        Yur26.disconnect()125    except Exception as e:126        pass127    try:128        Yur27.disconnect()129    except Exception as e:130        pass131    try:132        Yur28.disconnect()133    except Exception as e:134        pass135    try:136        Yur29.disconnect()137    except Exception as e:138        pass139    try:140        Yur30.disconnect()141    except Exception as e:142        pass143    try:144        Yur31.disconnect()145    except Exception as e:146        pass147    try:148        Yur32.disconnect()149    except Exception as e:150        pass151    try:152        Yur33.disconnect()153    except Exception as e:154        pass155    try:156        Yur34.disconnect()157    except Exception as e:158        pass159    try:160        Yur35.disconnect()161    except Exception as e:162        pass163    try:164        Yur36.disconnect()165    except Exception as e:166        pass167    try:168        Yur37.disconnect()169    except Exception as e:170        pass171    try:172        Yur38.disconnect()173    except Exception as e:174        pass175    try:176        Yur39.disconnect()177    except Exception as e:178        pass179    try:180        Yur40.disconnect()181    except Exception as e:182        pass183else:184    try:185        Yur.run_until_disconnected()186    except Exception as e:187        pass188    try:189        Yur2.run_until_disconnected()190    except Exception as e:191        pass192    try:193        Yur3.run_until_disconnected()194    except Exception as e:...Publisher.py
Source:Publisher.py  
...38        self._recv_disconnect = True39        self._cond.notifyAll()40        self._cond.release()41    42    def get_disconnect(self):43        return self._recv_disconnect44    def reset(self):45        self._recv_disconnect = False46    def disconnect_push_supplier(self):47        self.logger.debug("Publisher.Reciever handle disconnect_push_supplier")48        self._cond.acquire()49        self._recv_disconnect = True50        self._cond.notifyAll()51        self._cond.release()52    53    def wait_for_disconnect( self, wait_time=-1.0, retries=-1 ):54        try:55            self._cond.acquire()56            tries = retries57            while self._recv_disconnect == False:58                self.logger.debug("Publisher.Reciever .... waiting for disconnect")59                if wait_time > -1.0:60                    self._cond.wait( wait_time/1000.0 )61                    if tries == -1:62                        break63                    tries -= 164                    if tries < 1:65                        break;66                else:67                    self._cond.wait()68        finally:69            self._cond.release()70        71class DefaultReceiver(Receiver):72    def __init__(self,parent):73        self.parent = parent74        Receiver.__init__(self)75class Publisher:76    def __init__(self, channel ):77        self.channel = channel78        self.proxy = None79        self.logger = logging.getLogger("ossie.events.Publisher")80        self.disconnectReceiver = DefaultReceiver(self)81            82        self.connect()83    def __del__(self):84        self.logger.debug("Publisher  DTOR START")85        if self.disconnectReceiver and self.disconnectReceiver.get_disconnect() == False:86            self.logger.debug("Publisher::DTOR  DISCONNECT")87            self.disconnect()88        89        self.logger.debug("Publisher::DTOR  DEACTIVATE")90        self.disconnectReceiver=None91        self.proxy=None92        self.channel=None93        self.logger.debug("Publisher  DTOR END")94    def terminate(self):95        self.logger.debug("Publisher::terminate START")96        if self.disconnectReceiver and self.disconnectReceiver.get_disconnect() == False:97            self.logger.debug("Publisher::terminate  DISCONNECT")98            self.disconnect()99        100        self.logger.debug("Publisher::terminate  DEACTIVATE")101        self.disconnectReceiver=None102        self.proxy=None103        self.channel=None104        self.logger.debug("Publisher::terminate END")105        if self.proxy:106            for x in range(10):107                try:108                    self.proxy.disconnect_push_consumer()109                    break110                except CORBA.COMM_FAILURE:111                    pass112                time.sleep(.01)113        self.proxy = None114        self.supplier = None115        self.channel = None116    def push(self,data ):117        retval=0118        edata=data119        if not isinstance(data, CORBA.Any):120            edata = any.to_any(data)121                            122        try:123            if self.proxy != None:124                self.proxy.push(edata)125        except:126            #traceback.print_exc()127            retval=-1128        129        return retval130    def disconnect(self, retries=10, retry_wait=.01):131        retval=0132        if self.channel == None:133            return retval134        if self.proxy:135            retval=-1136            for x in range(retries):137                try:138                    self.proxy.disconnect_push_consumer()139                    retval=0140                    break141                except CORBA.COMM_FAILURE:142                    self.logger.error("Publisher ::disconnect, Caught COMM_FAILURE, Retrying.")143                    pass144                time.sleep(retry_wait)145            if self.disconnectReceiver:146                self.logger.debug("Publisher ::disconnect, Waiting for disconnect.......")147                self.disconnectReceiver.wait_for_disconnect( .01, 3)148                self.logger.debug("Publisher ::disconnect, received disconnect.......")149        return retval150    def connect(self, retries=10, retry_wait=.01):151        retval=-1152        153        if self.channel == None:154            return retval155    156        if self.proxy == None:157            self.logger.debug("Getting supplier object")158            for x in range(retries):159                try: 160                    self.logger.debug("Getting supplier object" + str(self.channel) )161                    supplier_admin = self.channel.for_suppliers()...Provider.js
Source:Provider.js  
...132            });133            134            describe("not forced", function() {135                beforeEach(function() {136                    provider.disconnect();137                });138                139                it("should not call doDisconnect", function() {140                    expect(provider.doDisconnect).not.toHaveBeenCalled();141                });142            143                it("should not fire disconnect event", function() {144                    expect(disconnectSpy).not.toHaveBeenCalled();145                });146            147                it("should decrement subscribers", function() {148                    expect(provider.subscribers).toBe(1);149                });150            });151            152            describe("forced", function() {153                beforeEach(function() {154                    provider.disconnect(true);155                });156                157                it("should call doDisconnect", function() {158                    expect(provider.doDisconnect).toHaveBeenCalled();159                });160                161                it("should fire disconnect event", function() {162                    expect(disconnectSpy).toHaveBeenCalled();163                });164                165                it("should reset subscribers to 0", function() {166                    expect(provider.subscribers).toBe(0);167                });168            });169        });170        171        describe("when subscribers == 1", function() {172            beforeEach(function() {173                provider.subscribers = 1;174                provider.disconnect();175            });176            177            it("should call doDisconnect", function() {178                expect(provider.doDisconnect).toHaveBeenCalled();179            });180            181            it("should fire disconnect event", function() {182                expect(disconnectSpy).toHaveBeenCalled();183            });184            185            it("should decrement subscribers", function() {186                expect(provider.subscribers).toBe(0);187            });188        });189        190        describe("when subscribers == 0", function() {191            beforeEach(function() {192                provider.disconnect();193            });194            195            it("should not call doDisconnect", function() {196                expect(provider.doDisconnect).not.toHaveBeenCalled();197            });198            199            it("should not fire disconnect event", function() {200                expect(disconnectSpy).not.toHaveBeenCalled();201            });202            203            it("should not decrement subscribers", function() {204                expect(provider.subscribers).toBe(0);205            });206        });
...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!!
