vendredi 22 avril 2016

Python patch object method

I want to simulate ping timeout of a websocket in a test.

First I tried to close TCP socket quietly but it's not possible due FIN is sent anyway. iptables is not an option.

I use tornado library to mock a websocket client.

I read how to replace method in already instantiated object. It works in a simple demo, but tornado seems to ignore my manipulations.

What it could be?

import types

ws.websocket_connect(url, callback=openCallback,
            on_message_callback=messageCallback)

def openCallback(future):
    ws = future.result()
    inst = ws.protocol

    def stub(self, x):
        print "STUB"
    inst.write_ping = types.MethodType(stub, inst, inst.__class__)

There is no any error, but stub is not called and client still replies for server ping requests.





Aucun commentaire:

Enregistrer un commentaire